Issues with ring polygons

194 views
Skip to first unread message

JonCorker

unread,
Mar 12, 2013, 12:01:40 PM3/12/13
to geospati...@googlegroups.com
I've run into an issue using pyshp where ring polygons are being written with an outline 'cobwebbing' from the edges of the polygon to the inner ring (I've attached an image to show what I mean). This is having some peculiar effects where the cobwebs overlap polygon edges, effectively inverting the polygon in places. I can make polygons available for download if required.

Most of my issues so far have been resolved by OGR2OGR-ing every shape file, but I'm not sure whether this is an issue with the library or my code. Example python code below, no errors are returned on run.

def example_function(output_file, input_files):
    w = shapefile.Writer(shapefile.POLYGON)
    w.field('keyval')                     
    w.field('date_created')
    for entries in input_files :  
        geometries = shapefile.Reader(entries['path']).shapes()
        for geometry in geometries :
            w.poly([geometry.points])
            w.record(entries['keyval'], entries['date_created'])
    w.save(output_file)
    return True

Essentially I'm just looking to copy geometry from a series of shapefiles into a single shapefile, whilst changing the attribute data. Has anyone else encountered this problem? Or is it a problem with my code that I'm missing?

donut_cobwebs.PNG

Joel Lawhead

unread,
Mar 12, 2013, 1:01:27 PM3/12/13
to geospati...@googlegroups.com
Jon,

I'll take a look at this. The library is always suspect :-)

But the good news is it could be a simple point order issue. 

- Joel 

--
Joel Lawhead, PMP
NVision Solutions Inc.
--
You received this message because you are subscribed to the Google Groups "Geospatial Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geospatialpyth...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
<donut_cobwebs.PNG>

JonCorker

unread,
Mar 13, 2013, 8:41:28 AM3/13/13
to geospati...@googlegroups.com
Hi Joel,

Thanks for the quick reply! I can provide some of the affected shape files if they'd help.

-Jon

Trevor Caskey

unread,
Jun 7, 2013, 5:41:07 PM6/7/13
to geospati...@googlegroups.com
Im having the exact same issue.  Have either of you found a solution?  I was given some bad shapefiles with the XY coords flipped, so my script is pretty much the same as your, just im swapping the coords as I copy from one shapefile to another.

any help would be much appreciated.

Trevor

Joel Lawhead

unread,
Jun 7, 2013, 11:35:02 PM6/7/13
to geospati...@googlegroups.com
Guys - Here's what's probably happening. Shapefiles have a concept called "parts".  A part is a subgroup of points within shape record. Polygon rings are defined as parts with points stored in a counter-clockwise direction.  However the shapefile format stores all points in a record together.  Calling shape.points in pyshp returns all if the points. The parts are a separate list which contain the starting index of each point in the shape.points list.  When you call the pyshp poly() method you can specify parts using nested lists: 
w.poly([[[x1,y1],[x2,y2],[x3,y3],[x4,y4]],[[x8,y8],[x7,y7],[x6,y6],[x5,y5]]]). 

If copying from a pyshp reader you could just manually read the shape.parts attribute and set it in the writer. 

There are other ways too depending on how you are reading data. 

- Joel

--
You received this message because you are subscribed to the Google Groups "Geospatial Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geospatialpyth...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
Joel Lawhead, PMP
E-mail: jlaw...@geospatialpython.com
Web: GeospatialPython.com
Twitter: @SpatialPython

Trevor Caskey

unread,
Jun 19, 2013, 1:19:04 PM6/19/13
to geospati...@googlegroups.com
No luck yet on this.  my attempt just draws polygons inside the outer polygon instead of making an inner ring / hole.  attached are my two code files and test shapefile

you just need to put the test.shp in a folder named 'input' and have an empty 'output' folder

let me know if you figure this out.

Thanks!

Trevor

flipCoords2.py
flipXY.py
test.dbf
test.prj
test.qpj
test.shp
test.shx

Joel Lawhead

unread,
Jun 20, 2013, 9:04:45 AM6/20/13
to geospati...@googlegroups.com
Trevor,

Thank you for the samples.  I'm working on this now.  Question - test.shp is a polygon (shapefile.POLYGON).  Is there a particular reason you switch it to Multipatch in the flip() function?  That's not the source of the problem - just curious.

- Joel




Trevor

--
You received this message because you are subscribed to the Google Groups "Geospatial Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geospatialpyth...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Joel Lawhead

unread,
Jun 20, 2013, 12:01:31 PM6/20/13
to geospati...@googlegroups.com, tcas...@gmail.com
Trevor,

See the attached zip file.  I made a flipCoords3.py copy and pointed flipXY.py to it which provide a working example.  However even though I made a bunch of modifications, your code ended up being correct and was just missing a single line which you could add or just use my example. It looks like you had almost solved the problem earlier.  

As you know, based on your code comments, outer ring shapefile points run clockwise and inner rings run counter clockwise.  That's how the software knows to make a hole in the outer polygon.  The flip operation you did apparently also flipped the order of the ring points in both cases.  So all you needed to do in your makeParts() function is reverse the order of each part so the flipped version works:

part.reverse()
myparts.append(part)

I added a couple of functions you might find handy for testing that tell you if a polygon (i.e. a ring) is clockwise or not.

It's important to realize when parsing shapefiles that the spec allows the outer rings and inner rings to be added to the shapefile in any order.  You don't have to add the outer rings first. Just something to be aware of.

One more thing - you were using the multipatch shape type for the flipped polygon which wasn't necessary.  It think that would have caused headaches unless you manually defined the part types which have numeric codes for inner and outer rings as well as surfaces.  If you alter your code be sure to  change the Writer shape type to "inSF.shapeType" so it works with all of the polygon type shapefiles.

- Joel


On Wed, Jun 19, 2013 at 12:19 PM, Trevor Caskey <tcas...@gmail.com> wrote:


Trevor

--
You received this message because you are subscribed to the Google Groups "Geospatial Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geospatialpyth...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
reissueswithringpolygons.zip

Trevor Caskey

unread,
Jun 20, 2013, 12:26:57 PM6/20/13
to geospati...@googlegroups.com, tcas...@gmail.com
Works Perfect!

I knew I was getting close.  Thanks for getting me the rest of the way there.

Trevor
Reply all
Reply to author
Forward
0 new messages