From bafc113a6bb7ae76af31edbe26dc43d53cc35c4c Mon Sep 17 00:00:00 2001 From: Anastasia Kopytina <65538698+ana-kop@users.noreply.github.com> Date: Wed, 6 Jul 2022 17:52:41 +0100 Subject: [PATCH] replace gdf.intersects with gpd.sjoin --- genet/core.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/genet/core.py b/genet/core.py index 7798fbf3..a6310b94 100644 --- a/genet/core.py +++ b/genet/core.py @@ -600,13 +600,20 @@ def retain_n_connected_subgraphs(self, n: int, mode: str): self.remove_mode_from_links(diff_links, mode) def _find_ids_on_geojson(self, gdf, how, geojson_input): - shapely_input = spatial.read_geojson_to_shapely(geojson_input) - return self._find_ids_on_shapely_geometry(gdf=gdf, how=how, shapely_input=shapely_input) + if how == 'intersect': + geojson_input_gdf = gpd.read_file(geojson_input) + return list(gpd.sjoin(gdf, geojson_input_gdf, how='inner', op='intersects')['id']) + elif how == 'within': + shapely_input = spatial.read_geojson_to_shapely(geojson_input) + return list(gdf[gdf.within(shapely_input)]['id']) + else: + raise NotImplementedError('Only `intersect` and `contain` options for `how` param.') def _find_ids_on_shapely_geometry(self, gdf, how, shapely_input): if how == 'intersect': - return list(gdf[gdf.intersects(shapely_input)]['id']) - if how == 'within': + geojson_input_gdf = gpd.GeoDataFrame(index=[0], crs=gdf.crs, geometry=[shapely_input]) + return list(gpd.sjoin(gdf, geojson_input_gdf, how='inner', op='intersects')['id']) + elif how == 'within': return list(gdf[gdf.within(shapely_input)]['id']) else: raise NotImplementedError('Only `intersect` and `contain` options for `how` param.')