[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?
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)