Skip to content
Advertisement

geopandas known intersection returns False

I have two sets of multipolygons. I want to select one polygon from one dataset (by using the matplotlib click event which works) and then select all polygons from the second dataset that intersect with it. When I plot the datasets they clearly overlap (see below) but my intersection always returns False.

The datasets look like this:

enter image description here

I select one of the red polygons using a click event which I have simulated here:

Dataset1 = gpd.read_file('Datafile_1.gpkg')
Dataset1_sub = Dataset1[Dataset1['ID']==15000]

This returns:

(17795, 73)
DN  ...     ID
14999  19  ...  15000

[1 rows x 74 columns]

But when I try intersecting with my second dataset (black boxes) like this:

Dataset2 = gpd.read('Datafile_2.gpkg')

Dataset2_sub = Dataset2.intersects(Dataset1_sub)

this returns False for all records of Dataset 2, but shouldn’t at least one of them be True?:

0       False
1       False
2       False
3       False
4       False
...
480     False
481     False
482     False
483     False
5162    False
dtype: bool

I do get the warning:

UserWarning: The indices of the two GeoSeries are different.
warn("The indices of the two GeoSeries are different.")

but I don’t know if this is causing this. I’m effectively trying to intersect a df with one row with another df with lots of rows to end up with intersecting polygons only. Am I Missing something?

Advertisement

Answer

You do not select one polygon but one row of the GeoDataFrame. Therefore you are using an intersects on a Series, not a Polygon. As shown in the documentation, geopandas aligns those series and do row-wise 1:1 operation. You need to pass a shapely.geometry if you want to check all rows against a single geometry.

# this is shapely.geometry.Polygon
geom = Dataset1.loc[Dataset1['ID'] == 15000, 'geometry'].iloc[0]

Dataset2.intersects(geom)

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement