alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[1073] Calculate intersected polygons that do not share the same boundaries

Key concept:

  • The intersection part is the Polygon or PolyLine.
  • 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:

  1. Import the necessary libraries:

    import geopandas as gpd
    from shapely.geometry import Polygon
  2. Load your GeoDataFrames:

    gdf1 = gpd.read_file('path_to_first_shapefile.shp')
    gdf2 = gpd.read_file('path_to_second_shapefile.shp')
  3. Ensure both GeoDataFrames use the same Coordinate Reference System (CRS):

    gdf1 = gdf1.to_crs(gdf2.crs)
  4. Perform the spatial join:

    sjoin_gdf = gpd.sjoin(gdf1, gdf2, how="inner", op="intersects")
  5. Filter out polygons that only share boundaries:

    def is_not_shared_boundary(row, gdf2):
    intersection = row['geometry'].intersection(gdf2.loc[row['index_right'], 'geometry'])
    return not intersection.equals(intersection.boundary)
    filtered_gdf = sjoin_gdf[sjoin_gdf.apply(is_not_shared_boundary, axis=1, gdf2=gdf2)]
  6. Check the result:

    print(filtered_gdf)

Here's a breakdown of what each step does:

  1. Imports the necessary libraries.

  2. Loads your polygon GeoDataFrames.

  3. Ensures both GeoDataFrames use the same Coordinate Reference System (CRS).

  4. Performs the spatial join using sjoin() with the how="inner" and op="intersects" parameters to get intersected polygons.

  5. 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.

  6. 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

  1. Function Definition:

    def is_not_shared_boundary(row, gdf2):

    This defines a function named is_not_shared_boundary that takes two arguments: row (a row from a GeoDataFrame) and gdf2 (another GeoDataFrame).

  2. Calculate Intersection:

    intersection = row['geometry'].intersection(gdf2.loc[row['index_right'], 'geometry'])

    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 index index_right. The result is stored in the variable intersection.

  3. Check If Intersection Is More Than Just the Boundary:

    return not intersection.equals(intersection.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 returns True.

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?

posted on   McDelfino  阅读(12)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示