[1073] Calculate intersected polygons that do not share the same boundaries
Key concept:
- The intersection part is the
Polygon
orPolyLine
. - If they share the same boundary, it will be the line.
To use the geopandas.sjoin()
function to calculate intersected polygons between two GeoDataFrames, excluding those that only share the same boundaries, you can follow these steps:
-
Import the necessary libraries:
-
Load your GeoDataFrames:
-
Ensure both GeoDataFrames use the same Coordinate Reference System (CRS):
-
Perform the spatial join:
-
Filter out polygons that only share boundaries:
-
Check the result:
Here's a breakdown of what each step does:
-
Imports the necessary libraries.
-
Loads your polygon GeoDataFrames.
-
Ensures both GeoDataFrames use the same Coordinate Reference System (CRS).
-
Performs the spatial join using
sjoin()
with thehow="inner"
andop="intersects"
parameters to get intersected polygons. -
Defines a function
is_not_shared_boundary
to check if the intersection is not just a shared boundary, and then filters the joined GeoDataFrame using this function. -
Prints the filtered result.
This will give you a GeoDataFrame of intersected polygons, excluding those that only share the same boundaries.
Give it a try and let me know if you need further assistance!
Let's break down the function is_not_shared_boundary
step by step:
Function Purpose
The purpose of this function is to check whether two polygons intersect in a way that is more than just sharing a boundary. In other words, it ensures that the intersection area is not just a line (the boundary) but includes some interior area as well.
Function Components
-
Function Definition:
This defines a function named
is_not_shared_boundary
that takes two arguments:row
(a row from a GeoDataFrame) andgdf2
(another GeoDataFrame). -
Calculate Intersection:
This line calculates the intersection between the geometry of the current row from the first GeoDataFrame (
row['geometry']
) and the corresponding geometry from the second GeoDataFrame (gdf2
) identified by the indexindex_right
. The result is stored in the variableintersection
. -
Check If Intersection Is More Than Just the Boundary:
This line checks if the intersection is equal to its boundary. If the intersection is equal to its boundary, it means the polygons only share a boundary (no interior overlap). The function returns
False
in this case. If the intersection is not just the boundary, the function returnsTrue
.
Summary
The function is_not_shared_boundary
effectively filters out intersecting polygons that only share boundaries. It ensures that only intersections with some overlapping area are considered, excluding those that are merely touching along their edges.
Does this help you understand the function better?