3

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?

1
  • I use the trapezoid rule to calculate the area of each ring, and negative area (assuming the first is positive) indicates a hole. And, yes a hole can have a "hole", but it's actually an outer ring that happens to be inside an inner ring (and this ring can have an inner ring, etc). Commented May 8, 2014 at 3:22

1 Answer 1

8

If you have a Polygon you can get the number of rings using

geometry.GetGeometryCount() 

The first ring is the outer ring, the following rings are inner rings (aka holes).

Here a small script:

print "Next polygon:" nbrRings = geometry.GetGeometryCount() for i in range(nbrRings): print geometry.GetGeometryRef(i) 

The output for the following polygons is:

Next polygon: LINEARRING (0.916058394160584 0.333333333333333,1.193430656934307 0.253041362530414,0.947688564476886 0.038929440389294,0.925790754257908 -0.423357664233577,0.409975669099757 -0.381995133819951,0.305352798053528 0.126520681265207,0.916058394160584 0.333333333333333) LINEARRING (0.577858880778589 0.085158150851582,0.451338199513382 0.004866180048662,0.616788321167884 -0.27007299270073,0.757907542579075 -0.065693430656934,0.577858880778589 0.085158150851582) LINEARRING (0.774939172749392 0.211678832116788,0.738442822384428 0.160583941605839,0.762773722627737 0.121654501216545,0.791970802919708 0.116788321167883,0.830900243309002 0.131386861313869,0.774939172749392 0.211678832116788) Next polygon: LINEARRING (-0.784671532846715 0.669099756690998,-0.495133819951338 0.778588807785888,-0.495133819951338 0.778588807785888,-0.412408759124087 0.708029197080292,-0.156934306569343 0.537712895377129,-0.487834549878345 0.347931873479319,-0.424574209245742 0.114355231143552,-0.706812652068126 0.077858880778589,-1.09610705596107 0.464720194647202,-0.784671532846715 0.669099756690998) 

Two polygons, one with inner rings

2
  • So does it mean that a polygon will never have an 'hole inside hole`? Commented May 8, 2014 at 6:13
  • A geometry with a 'hole inside a hole' must be a MultiPolygon, this is not possible for a valid Polygon. Commented May 8, 2014 at 6:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.