how do geopandas sjoin predicate 'within' and 'intersects' differ
Asked Answered
S

2

6

I'm identifying if a point locates within a polygon. I have a dataframe contains the points and another dataframe contains the polygons, so I want to spatial join them like:

gpd.sjoin(df_points, df_polygons, how = 'left')

I know the default is predicate=intersects, but how do intersects and within differ? Which one should I use for my purpose?

Thanks

Sliver answered 23/7, 2021 at 20:53 Comment(2)
Geopandas uses shapely for such operations, you can see the documents here: shapely.readthedocs.io/en/stable/…Isidora
In the case of point within a polygon, there is no difference in the result. However, you may notice a difference in performance.Marquet
S
4

The PostGIS Tutorial has some very good graphics that explain the different Spatial Joins.

Intersects

Within

Selwyn answered 1/11, 2021 at 13:59 Comment(0)
A
2

In short, within requires that all of a geometry's points to be within the interior of the spatially joined geometry (and none on the exterior). Whereas intersects allows some of a geometry's points to be on the exterior of a spatially joined geometry so long at least one of its points touches or is within the second geometry.

Here are some more formal definitions:

  • intersects: An object is said to intersect other if its boundary and interior intersects in any way with those of the other.

  • within: An object is said to be within other if at least one of its points is located in the interior and no points are located in the exterior of the other. If either object is empty, this operation returns False. This is the inverse of contains() in the sense that the expression a.within(b) == b.contains(a) always evaluates to True.

  • contains: An object is said to contain other if at least one point of other lies in the interior and no points of other lie in the exterior of the object. (Therefore, any given polygon does not contain its own boundary – there is not any point that lies in the interior.) If either object is empty, this operation returns False. This is the inverse of within() in the sense that the expression a.contains(b) == b.within(a) always evaluates to True.

Anglim answered 30/10, 2023 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.