I am trying to read the geometry of some shapefiles, now I meet some problem when I try to identify the polygon with holes, since a polygon may(or not) have one hole(or multiple holes).
This is the code I am using:
def _get_features(coord,datalayer):
bbox=_get_bbox(coord)
datalayer.SetSpatialFilter(bbox)
layer_def=datalayer.GetLayerDefn()
geom_type=layer_def.GetGeomType()
for feature in datalayer:
geometry = feature.geometry().Clone()
if not geometry.Intersect(bbox):
continue
geometry = geometry.Intersection(bbox)
fixed_geom=_get_coordinate(geometry)
#maybe a polygon with hole
if isinstance(fixed_geom[0], list):
if geom_type is wktPolygon:
# now, how to get the rings? outer, inner, multiple inner?
def _get_coordinate(geometry,result=None):
if result is None:
result=[]
if geometry.GetPointCount()>0:
for i in range(0,geometry.GetPointCount()):
point=geometry.GetPoint(i)[:2]
result.extend(map(_fix_coordinate,point))
elif geometry.GetGeometryCount()>0:
for i in range(0,geometry.GetGeometryCount()):
geom=geometry.GetGeometryRef(i)
_get_coordinate(geom,result)
return result
Any suggestion?
BTW, is it possible a hole inside a hole of a polygon?
Best Answer
If you have a Polygon you can get the number of rings using
The first ring is the outer ring, the following rings are inner rings (aka holes).
Here a small script:
The output for the following polygons is: