Shapely "is_valid" returns 真实到无效的重叠多边形

Shapely "is_valid" returns True to invalid overlap polygons

我有 5 个多边形的 shapefile,我用 geopandas 加载了这些多边形。我想检查这些多边形是否相互重叠。

我想识别所有重叠的多边形。我在漂亮的手册中读到 is_valid 工具可以检测到:

object.is_valid Returns True if a feature is “valid” in the sense of 1.

Note

The validity test is meaningful only for Polygons and MultiPolygons. True is always returned for other types of geometries.

A valid Polygon may not possess any overlapping exterior or interior rings. A valid MultiPolygon may not collect any overlapping polygons. Operations on invalid features may fail.

所以我已经在我的多边形上试过了:

imprt geopandas as gpd
import shapely as sh

shapes = gpd.read_file(r'test.shp')

shapes.head()

>>>
    id  geometry
0   1   POLYGON ((15.49457 7.61388, 15.71412 7.59753, ...
1   3   POLYGON ((15.60721 7.70417, 15.91074 7.71807, ...
2   2   POLYGON ((15.86208 7.28826, 16.07873 7.31838, ...
3   5   POLYGON ((15.34075 7.85246, 15.46587 7.85362, ...
4   4   POLYGON ((15.84123 7.48057, 16.03354 7.48405, ...

shapes['validation']=shapes['geometry'].is_valid

shapes.head()

>>>id   geometry                                           validation
0   1   POLYGON ((15.49457 7.61388, 15.71412 7.59753, ...   True
1   3   POLYGON ((15.60721 7.70417, 15.91074 7.71807, ...   True
2   2   POLYGON ((15.86208 7.28826, 16.07873 7.31838, ...   True
3   5   POLYGON ((15.34075 7.85246, 15.46587 7.85362, ...   True
4   4   POLYGON ((15.84123 7.48057, 16.03354 7.48405, ...   True

如您所见,即使 1,3,4 重叠,所有多边形都为 True。为什么会这样?正如我从手册中了解到的那样,如果来自同一层的多边形也应该显示?

我的最终目标:将重叠多边形分类为错误/重叠

Shapely 的 is_valid 适用于单个多边形,并检查该多边形的外环和内环之间的重叠。它不检查与其他人的重叠。您所有的多边形都完全有效。

如果您想检查多边形是否与其他多边形重叠,您应该使用 geopandas 空间索引功能进行检查。

input_indices, result_indices = shapes.sindex.query_bulk(shapes.geometry, predicate='overlaps')
overlapping = numpy.unique(result_indices)  # integer indeces of overlapping

如果你想从中得到一个布尔数组:

import numpy

results = numpy.ones(len(shapes), dtype=bool)
results[overlapping] = False

shapes['validation'] = results

请注意,这需要 geopandas 0.8+