Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #117: links_on_spatial_condition failing for large spatial area #126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions genet/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as I understand that's the only line you need to change, the rest of the code falls in place

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'])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

op is deprecated, predicate should be used instead: https://geopandas.org/en/stable/docs/reference/api/geopandas.sjoin.html

elif how == 'within':
return list(gdf[gdf.within(shapely_input)]['id'])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is at risk too and should be updated similarly to intersection?

else:
raise NotImplementedError('Only `intersect` and `contain` options for `how` param.')
Expand Down