From 82c68b9cc309e514fcb69bddbd3ed65c8fbcd0be Mon Sep 17 00:00:00 2001 From: Carles Tena Date: Wed, 12 Feb 2020 13:18:49 +0100 Subject: [PATCH 1/2] Intersected clip with the unary union of the domain --- hermesv3_bu/clipping/clip.py | 7 ++++--- hermesv3_bu/clipping/custom_clip.py | 14 ++++++++++---- hermesv3_bu/clipping/default_clip.py | 2 +- hermesv3_bu/clipping/shapefile_clip.py | 8 +++++--- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/hermesv3_bu/clipping/clip.py b/hermesv3_bu/clipping/clip.py index 5b2ebb6..9addce1 100755 --- a/hermesv3_bu/clipping/clip.py +++ b/hermesv3_bu/clipping/clip.py @@ -34,10 +34,10 @@ def select_clip(comm, logger, auxiliary_path, clipping, grid): clip = DefaultClip(logger, auxiliary_path, grid) elif clipping[0] == os.path.sep: from hermesv3_bu.clipping.shapefile_clip import ShapefileClip - clip = ShapefileClip(logger, auxiliary_path, clipping) + clip = ShapefileClip(logger, auxiliary_path, clipping, grid) else: from hermesv3_bu.clipping.custom_clip import CustomClip - clip = CustomClip(logger, auxiliary_path, clipping) + clip = CustomClip(logger, auxiliary_path, clipping, grid) else: clip = None @@ -49,9 +49,10 @@ def select_clip(comm, logger, auxiliary_path, clipping, grid): class Clip(object): - def __init__(self, logger, auxiliary_path): + def __init__(self, logger, auxiliary_path, grid): spent_time = timeit.default_timer() self.logger = logger + self.grid = grid self.shapefile = None self.shapefile_path = os.path.join(auxiliary_path, 'clip', 'clip.shp') diff --git a/hermesv3_bu/clipping/custom_clip.py b/hermesv3_bu/clipping/custom_clip.py index f6f79b2..2026cfb 100755 --- a/hermesv3_bu/clipping/custom_clip.py +++ b/hermesv3_bu/clipping/custom_clip.py @@ -9,7 +9,7 @@ from hermesv3_bu.logger.log import Log class CustomClip(Clip): - def __init__(self, logger, auxiliary_path, points_str): + def __init__(self, logger, auxiliary_path, points_str, grid): """ Initialise the Custom Clip class @@ -24,7 +24,7 @@ class CustomClip(Clip): """ spent_time = timeit.default_timer() logger.write_log('Custom clip selected') - super(CustomClip, self).__init__(logger, auxiliary_path) + super(CustomClip, self).__init__(logger, auxiliary_path, grid) self.clip_type = 'Custom clip' self.shapefile = self.create_clip(points_str) self.logger.write_time_log('CustomClip', '__init__', timeit.default_timer() - spent_time) @@ -56,11 +56,17 @@ class CustomClip(Clip): if not ((lon_list[0] == lon_list[-1]) and (lat_list[0] == lat_list[-1])): lon_list.append(lon_list[0]) lat_list.append(lat_list[0]) - + geom = Polygon([[p.x, p.y] for p in [Point(xy) for xy in zip(lon_list, lat_list)]]) clip = gpd.GeoDataFrame( - geometry=[Polygon([[p.x, p.y] for p in [Point(xy) for xy in zip(lon_list, lat_list)]])], + geometry=[geom], crs={'init': 'epsg:4326'}) + border = gpd.GeoDataFrame(geometry=[self.grid.shapefile.unary_union], crs=self.grid.shapefile.crs) + geom = gpd.overlay(clip, border.to_crs(clip.crs), how='intersection').unary_union + clip = gpd.GeoDataFrame( + geometry=[geom], + crs={'init': 'epsg:4326'}) + clip.to_file(self.shapefile_path) else: clip = gpd.read_file(self.shapefile_path) diff --git a/hermesv3_bu/clipping/default_clip.py b/hermesv3_bu/clipping/default_clip.py index f05bda8..5cc1f99 100755 --- a/hermesv3_bu/clipping/default_clip.py +++ b/hermesv3_bu/clipping/default_clip.py @@ -24,7 +24,7 @@ class DefaultClip(Clip): """ spent_time = timeit.default_timer() logger.write_log('Default clip selected') - super(DefaultClip, self).__init__(logger, auxiliary_path) + super(DefaultClip, self).__init__(logger, auxiliary_path, grid) self.clip_type = 'Default clip' self.shapefile = self.create_clip(grid) self.logger.write_time_log('DefaultClip', '__init__', timeit.default_timer() - spent_time) diff --git a/hermesv3_bu/clipping/shapefile_clip.py b/hermesv3_bu/clipping/shapefile_clip.py index 88792ef..7d4eb46 100755 --- a/hermesv3_bu/clipping/shapefile_clip.py +++ b/hermesv3_bu/clipping/shapefile_clip.py @@ -10,7 +10,7 @@ from hermesv3_bu.tools.checker import error_exit class ShapefileClip(Clip): - def __init__(self, logger, auxiliary_path, clip_input_path): + def __init__(self, logger, auxiliary_path, clip_input_path, grid): """ Initialise the Shapefile Clip class @@ -25,7 +25,7 @@ class ShapefileClip(Clip): """ spent_time = timeit.default_timer() logger.write_log('Shapefile clip selected') - super(ShapefileClip, self).__init__(logger, auxiliary_path) + super(ShapefileClip, self).__init__(logger, auxiliary_path, grid) self.clip_type = 'Shapefile clip' self.shapefile = self.create_clip(clip_input_path) self.logger.write_time_log('ShapefileClip', '__init__', timeit.default_timer() - spent_time) @@ -46,7 +46,9 @@ class ShapefileClip(Clip): if not os.path.exists(os.path.dirname(self.shapefile_path)): os.makedirs(os.path.dirname(self.shapefile_path)) clip = gpd.read_file(clip_path) - clip = gpd.GeoDataFrame(geometry=[clip.unary_union], crs=clip.crs) + border = gpd.GeoDataFrame(geometry=[self.grid.shapefile.unary_union], crs=self.grid.shapefile.crs) + geom = gpd.overlay(clip, border.to_crs(clip.crs), how='intersection').unary_union + clip = gpd.GeoDataFrame(geometry=[geom], crs=clip.crs) clip.to_file(self.shapefile_path) else: error_exit(" Clip shapefile {0} not found.") -- GitLab From 11ddd1805ab50454dbc1e91670ee42272870df75 Mon Sep 17 00:00:00 2001 From: Carles Tena Date: Wed, 12 Feb 2020 13:33:34 +0100 Subject: [PATCH 2/2] PEP8 corrections --- hermesv3_bu/clipping/custom_clip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hermesv3_bu/clipping/custom_clip.py b/hermesv3_bu/clipping/custom_clip.py index 2026cfb..e780dd2 100755 --- a/hermesv3_bu/clipping/custom_clip.py +++ b/hermesv3_bu/clipping/custom_clip.py @@ -66,7 +66,7 @@ class CustomClip(Clip): clip = gpd.GeoDataFrame( geometry=[geom], crs={'init': 'epsg:4326'}) - + clip.to_file(self.shapefile_path) else: clip = gpd.read_file(self.shapefile_path) -- GitLab