2

I am trying to generate a shapefile with Joel Lawheads pyshape-library. This works fine so far, but I have one important question: Is there any mandatory or recommended order of the rings of a multipolygon-geometry?

for example this gives a valid shapefile:

import shapefile
w = shapefile.Writer(shapefile.POLYGON)
ring1 = [[0,50],[50,50],[50,0],[0,0],[0,50]] # Main-Polygon
ring2 = [[10,40],[10,10],[30,10],[30,40],[10,40]] #Hole in Main-Polygon
ring3 = [[70,20],[100,20],[100,0],[70,0],[70,20]] # Island of Main-Polygon
ring4 = [[35,40],[35,10],[40,10],[35,40]] # Second Hole in Main-Polygon
w.poly(parts=[ring1,ring2,ring3,ring4])
w.field('FIRST_FLD','C','40')
w.field('SECOND_FLD','C','40')
w.record('Poly','PolyTest')
w.save('TESTPOLY1')

And this gives also a valid shapefile:

import shapefile
w = shapefile.Writer(shapefile.POLYGON)
ring1 = [[0,50],[50,50],[50,0],[0,0],[0,50]] # Main-Polygon
ring2 = [[10,40],[10,10],[30,10],[30,40],[10,40]] #Hole in Main-Polygon
ring3 = [[70,20],[100,20],[100,0],[70,0],[70,20]] # Island of Main-Polygon
ring4 = [[35,40],[35,10],[40,10],[35,40]] # Second Hole in Main-Polygon
w.poly(parts=[ring1,ring3,ring2,ring4])
w.field('FIRST_FLD','C','40')
w.field('SECOND_FLD','C','40')
w.record('Poly','PolyTest')
w.save('TESTPOLY2')

Both examples work fine in qgis and deliver no error , when I check the geometry: enter image description here

therefore my question: Is there any mandatory or recommended order of the rings of a multipolygon-geometry?

5
  • 1
    Have you read esri.com/library/whitepapers/pdfs/shapefile.pdf?
    – user30184
    Sep 7 '16 at 17:22
  • @user30184: I did (but I am no technican) and page 8, which describes type 5 = polygon, says nothing about the order of the rings. rings which describe holes must be in counterclockwise order of POINTS is the only spec I could find
    – Kurt
    Sep 7 '16 at 17:33
  • 1
    The spec is not crystal clear for me either. The R folks have, however, interpreted that rings can come in any order. See the good comments in github.com/rgeo/rgeo-shapefile/blob/master/lib/rgeo/shapefile/…. The spec says only "The order of rings in the points array is not significant" but nothing about the order in the parts array. But I would say that it is safest to consider that the order can be anything.
    – user30184
    Sep 7 '16 at 18:32
  • 1
    While rings could come in any order, I expect you'll find that Esri always encodes them with an exterior, followed by all the interior rings within that exterior, followed by any additional exterior ring(s), with their interior rings after their parent. The WKT spec is a good example of how shapes should be encoded (though shapefiles have an opposite handedness).
    – Vince
    Sep 8 '16 at 1:06
  • What I would expect to find in WKT is that MULTIPOLYGON comprises a set of POLYGON objects. MULTIPOLYGON( (( p1 )), (( p2 )) ... ) The idea of mixing and matching { p1, p1hole1, p2, p1hole2 } seems like a bad idea, even if it might (seem to) work with some code. Another way of saying what Vince said.
    – fortboise
    Jul 14 '17 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.