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

Conversation

ana-kop
Copy link
Contributor

@ana-kop ana-kop commented Jul 6, 2022

Addresses issue #117 :

Using the function links_on_spatial_condition with a geojson input results in an error due to a memory error:

Segmentation fault (core dumped)

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 use gpd.sjoin instead of gdf.intersects.

@KasiaKoz KasiaKoz linked an issue Aug 3, 2022 that may be closed by this pull request
@ana-kop ana-kop requested a review from KasiaKoz August 24, 2022 10:00
Copy link
Collaborator

@KasiaKoz KasiaKoz left a 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

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'])
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'])
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?

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'])
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

links_on_spatial_condition failing for large spatial area
2 participants