geopandas : ambiguity of indexing contains() method

232 Views Asked by At

I've got a simple GeoDataFrames of Points, like this one :

        name        geometry
0       Location1   POINT (9.71852 50.56186)
1       Location2   POINT (9.72912 50.55847)

On the other side, I've got a more complex GeoDataFrame, which is composed of polygons. My goal is to update my GDF of points with some infos of the other GDF if a point is in the area of a polygon.

So I wanted to use contains() method to check which polygons contains a point. I've tested with a simple boolean mask :

mask = polygons.contains(points.loc[0,'geometry'])
selection = polygons.loc[mask]

Obviously, selection returns a polygon that matched with the first row of GDF points. I wanted to consider all the rows of the GDF with something like :

mask = polygons.contains(points['geometry'])

This mask didn't work, I guess it's an indexing issue, but I don't figure out how to fix it...

1

There are 1 best solutions below

1
On BEST ANSWER

This is actually expected behaviour now. GeoSeries.contains is row-wise operation, which means that if you do polygons.contains(point), it will first align both GeoSeries and then check if polygon on line 0 contains point on line 0 etc.

If you want to update Points with data from Polygons, you should use geopandas.sjoin - https://geopandas.readthedocs.io/en/latest/docs/user_guide/mergingdata.html#spatial-joins

In your case, that will probably look similar to this:

points_with_data = geopandas.sjoin(points, polygons, how='left')