如何使用 Python 在单个 GeoDataFrame 中提取重叠的几何图形?

问题描述 投票:0回答:0

我使用

geopandas.Geoseries.buffer
更新了包含点几何的大型 GeoDataFrame 的几何。我需要从这个缓冲的几何图形创建两个 GeoDataFrame:

  • 多边形重叠
  • 一个孤立的多边形

在 QGIS 中,我会使用带有

Select by location
几何谓词的
Overlap
工具。通过将缓冲层与其自身进行比较,我将获得重叠实体的选择,通过反转选择,我将获得非重叠实体。

在 Python 中,我尝试在几何图形中循环,并使用

geopandas.GeoSeries.overlaps
将每个几何图形与 DataFrame 进行比较。此方法有效,但由于循环需要花费大量时间。在 QGIS 中,完成这个过程需要 40 秒,在 Python 中需要 1:20 小时。

有没有办法像QGIS一样快?

这是我使用的代码示例:

# Points buffer
points = gpd.read_file("path_to_my_layer.gpkg")
buffer = (
    points.copy()
) 
buffer.geometry = points.geometry.buffer(3)

# Overlaps check
overlaps_ids = np.array([], dtype=np.int64)
for geom in buffer.geometry:  # <-- This takes a lot of time
    overlaps = buffer.geometry.overlaps(geom)  # check overlapping features
    where_overlaps = np.where(overlaps == True)
    overlaps_ids = np.concatenate((overlaps_ids, where_overlaps[0]))
overlaps_ids = np.unique(overlaps_ids)  # list overlapping IDs

# Overlapping geometries extraction
buffer_overlaps = buffer.iloc[overlaps_ids]

# Non overlapping geometries extraction
buffer_no_overlaps = buffer.drop(overlaps_ids)
python gis geopandas overlap
© www.soinside.com 2019 - 2024. All rights reserved.