-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of code duplication happening here, I don't think you need to change anything in
Line 632 in 4fcc3d3
def _find_ids_on_shapely_geometry(self, gdf, how, shapely_input): |
Now that we're not relying on these intersects
and within
methods, it would be cleaner and more general to get rid of the if statements and open up the predicate
param: https://geopandas.org/en/stable/docs/reference/api/geopandas.sindex.SpatialIndex.valid_query_predicates.html so the user can choose from the geopandas list of operations and our code would simplify to:
def _find_ids_on_shapely_geometry(self, gdf, predicate, shapely_input):
geojson_input_gdf = ...
return list(gpd.sjoin(gdf, geojson_input_gdf, how='inner', predicate=predicate)['id'])
|
||
def _find_ids_on_shapely_geometry(self, gdf, how, shapely_input): | ||
if how == 'intersect': | ||
return list(gdf[gdf.intersects(shapely_input)]['id']) |
There was a problem hiding this comment.
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']) | ||
elif how == 'within': | ||
return list(gdf[gdf.within(shapely_input)]['id']) |
There was a problem hiding this comment.
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?
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']) |
There was a problem hiding this comment.
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
Addresses issue #117 :
Using the function links_on_spatial_condition with a geojson input results in an error due to a memory error:
The geojson contained a polygon which covered multiple cities. Possible to work around it by using
gpd.sjoin(network_gdf,urban_gdf,how='inner', op='intersects')
. It might be worth changing the function links_on_spatial_condition to usegpd.sjoin
instead ofgdf.intersects
.