Negative Area and polygon not closed.

155 views
Skip to first unread message

Billy Barrows

unread,
Feb 7, 2014, 10:49:13 PM2/7/14
to geospati...@googlegroups.com
Joel,

I created my vertex list input data from Autocad closed polylines. These vertex points are read by shapefile.py as polygons.

The people who are using the shapefiles I create tell me I have two problems.
1. The area of some my polygons in negative. I found the polygons having this problem were drawn in autocad in a counterclockwise direction.

I can check in autocad that the area is positive and reverse the vertex list if negative. Then create the input data.  Are there other options in shapefile.py?

2. The polygons in the created shapefiles are not closed.  The vertex list I input into Shapefile.py does not end at the start point. Is a option to close or should I include the start point at the end of the vertex list?

Thanks
Billy


Joel Lawhead

unread,
Feb 7, 2014, 11:17:24 PM2/7/14
to geospati...@googlegroups.com
Billy,

Both cases are differences between AutoCAD's definition of a polygon vs the shapefile format and in both cases your intuition is correct.

The library doesn't have much in the way of corrections because geometries are stored as lists and python list manipulation is so simple.

So in the case of closing the polygons, just duplicate the first point as the last point if they aren't identical already. 

In the case of the negative area, counter-clockwise points are used to create holes in polygons (doughnuts) according to the shapefile spec. 

There is a function in the library called "signed_area()" that takes a list of points and returns an integer. If that integer is >= 0 then the points are counter-clockwise and you need to reverse the list to get a positive area. That way you don't have to do the check in AutoCAD. 

Let me know how that works out for you.  Thanks for the feedback.

- 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

Christian Ledermann

unread,
Feb 10, 2014, 2:27:24 AM2/10/14
to geospati...@googlegroups.com
On Sat, Feb 8, 2014 at 7:17 AM, Joel Lawhead <jlaw...@geospatialpython.com> wrote:
Billy,

Both cases are differences between AutoCAD's definition of a polygon vs the shapefile format and in both cases your intuition is correct.

The library doesn't have much in the way of corrections because geometries are stored as lists and python list manipulation is so simple.

So in the case of closing the polygons, just duplicate the first point as the last point if they aren't identical already. 

linear rings of a polygon may or may not be closed, so apply what joel suggested.

 

In the case of the negative area, counter-clockwise points are used to create holes in polygons (doughnuts) according to the shapefile spec. 

There is a function in the library called "signed_area()" that takes a list of points and returns an integer. If that integer is >= 0 then the points are counter-clockwise and you need to reverse the list to get a positive area. That way you don't have to do the check in AutoCAD. 


when developing the __geo_interface__ I stumbled upon the same issues:
The first linear ring of a (multi)polygon may be stored in an arbitrary direction.
(that's why the code in __geo_interface__ looks a bit odd)
I don't know which software produced it and if it's a bug or a feature that it was stored 
that way, it is contrary to the definition, but it happens.


    def _set_orientation(self, clockwise=False):
        """ sets the orientation of the coordinates in
clockwise or counterclockwise (default) order"""
        area = signed_area(self.coords)
        if (area >= 0) and clockwise:
            self._geoms = self._geoms[::-1]
        elif (area < 0) and not clockwise:
            self._geoms = self._geoms[::-1]



--
Best Regards,

Christian Ledermann

Nairobi - Kenya
Mobile : +254 702978914

<*)))>{

If you save the living environment, the biodiversity that we have left,
you will also automatically save the physical environment, too. But If
you only save the physical environment, you will ultimately lose both.

1) Don’t drive species to extinction

2) Don’t destroy a habitat that species rely on.

3) Don’t change the climate in ways that will result in the above.

}<(((*>

Billy Barrows

unread,
Feb 11, 2014, 4:16:57 PM2/11/14
to geospati...@googlegroups.com

Billy Barrows

unread,
Feb 11, 2014, 4:23:30 PM2/11/14
to geospati...@googlegroups.com
Joel,
Got it fixed. Python makes it simple to add the first value to the end of a list:

# ver1.5a  add last point same as first
        InputList.append(InputList[0])

# ver1.5a  Reverse list if negative area
 NegativeArea=shapefile.signed_area(Coords)
        if NegativeArea >=0:
           Coords=Coords[::-1]

Thanks
Billy

Joel Lawhead

unread,
Feb 11, 2014, 4:25:56 PM2/11/14
to geospati...@googlegroups.com
Excellent!
--
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.
Reply all
Reply to author
Forward
0 new messages