Reputation: 1592
I have shapefile with 5 polygons that I have load with geopandas. I want to check if those polygons are overlap each other.
I want to identify all the polygons that are overlap. I read in shapely manual that is_valid tool can detect that:
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.
so I have tried it on my polygons:
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
as yuo can see, all the polygons get True even though 1,3,4 are overlap. Why is this happenning? as I understood from the manual it suppose to show also if polygons from the same layer?
my end goal: to have the overlapping polygon classifiaed as False / overlap
Upvotes: 0
Views: 5209
Reputation: 7814
Shapely's is_valid
works on a single polygon, and checks for overlaps between exterior and interior rings of that polygon. It does not check for overlaps with others. All your polygons are perfectly valid.
If you want to check if the polygon overlaps with any other, you should check for that using geopandas spatial indexing capability.
input_indices, result_indices = shapes.sindex.query_bulk(shapes.geometry, predicate='overlaps')
overlapping = numpy.unique(result_indices) # integer indeces of overlapping
If you want to get a boolean array out of it:
import numpy
results = numpy.ones(len(shapes), dtype=bool)
results[overlapping] = False
shapes['validation'] = results
Note that this requires geopandas 0.8+
Upvotes: 3