Search code examples
geospatialintersectiongeopandas

intersection of two geopandas GeoSeries gives warning "The indices of the two GeoSeries are different." and few matches


I am using geopandas for finding intersections between points and polygons. When I use the following:

intersection_mb = buffers_df.intersection(rest_VIC)

I get this output with a warning basically saying there are no intersections:

0         None
112780    None
112781    None
112782    None
112784    None
      ... 
201314    None
201323    None
201403    None
201404    None
201444    None
Length: 3960, dtype: geometry

Warning message:

 C:\Users\Name\Anaconda3\lib\site-packages\geopandas\base.py:31: UserWarning: The indices of the two GeoSeries are different.
 warn("The indices of the two GeoSeries are different.")

I looked for any suggestions and found that I could solve by setting crs for both geoseries for which the intersection is to be performed on, but it did not work.

rest_VIC = rest_VIC.set_crs(epsg=4326, allow_override=True)
buffers_df = buffers_df.set_crs(epsg=4326, allow_override=True)

Any suggestions will be helpful. Thanks.


Solution

  • geopandas.GeoSeries.intersection is an element-wise operation. From the intersection docs:

    The operation works on a 1-to-1 row-wise manner

    There is an optional align argument which determines whether the series should be first compared based after aligning based on the index (if True, the default) or if the intersection operation should be performed row-wise based on position.

    So the warning you're getting, and the resulting NaNs, are because you're performing an elementwise comparison on data with unmatched indices. The same issue would occur in pandas when trying to merge columns for DataFrames with un-aligned indices.

    If you're trying to find the mapping from points to polygons across all combinations of rows across the two dataframes, you're looking for a spatial join, which can be done with geopandas.sjoin:

    intersection_mb = geopandas.sjoin(
        buffers_df,
        rest_VIC,
        how='outer',
        predicate='intersects',
    )
    

    See the geopandas guide to merging data for more info.