Problem writing a shapfile using shapefile.py

546 views
Skip to first unread message

wjba...@gmail.com

unread,
Oct 24, 2013, 4:24:59 PM10/24/13
to geospati...@googlegroups.com

Hello, first post here. I am learning about python and shapefiles. Running on a Windows 7 computer.


Following the pyshp example I tried to create a shapefile for a polygon and got an error after the w.save ...

>>> import shapefile
>>> w = shapefile.Writer(shapefile.POLYGON)

>>> test='[[[5774289,3820335][5774289,3820335][5774649,3820268][5774709,3819882][5774689,3819640][5774599,3819368][5774399,3819538][5774219,3819779][5774109,3819990][5774039,3820175]]]'

>>> w.poly(parts=test)
>>> w.field('FIRST_FLD','C','40')
>>> w.field('SECOND_FLD','C','40')
>>> w.record('First','Polygon')

>>> w.save('c:\\Python27\\polygon')

Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    w.save('c:\\Python27\\polygon')
  File "C:/Python27\shapefile.py", line 1028, in save
    self.saveShp(target)
  File "C:/Python27\shapefile.py", line 985, in saveShp
    self.__shapefileHeader(self.shp, headerType='shp')
  File "C:/Python27\shapefile.py", line 709, in __shapefileHeader
    raise ShapefileException("Failed to write shapefile bounding box. Floats required.")
ShapefileException: Failed to write shapefile bounding box. Floats required.



Not sure what to do. Suggestions appreciated.

Bill

Joel Lawhead

unread,
Oct 24, 2013, 4:48:58 PM10/24/13
to geospati...@googlegroups.com
Bill,

Easy fix.  Your polygon coordinates are a big string (because there are quotes around them).  The polygon coordinates are defined as a list containing three or more 2-item list with x,y coordinates.  That whole data structure is rolled up into a list of one or more "parts".  A part is a distinct polygon which may share a dbf attribute record with other polygons.  It looks like your coordinates list is also missing some commas between the coordinate pair lists.

It should look like this:

>>> test=[[[5774289,3820335],[5774289,3820335],[5774649,3820268],[5774709,3819882],[5774689,3819640],[5774599,3819368],[5774399,3819538],[5774219,3819779],[5774109,3819990],[5774039,3820175]]]

In Python a list can contain any other type of Python object including other lists.  So the shapefile data structure for a polygon (or line) is:  

point1 = [x1,y1]
point2 = [x2,y2]
point3 = [x3,y3]
coordinates = [point1, point2, point3]
parts = [coordinates]

Let me know if that doesn't work.

- 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

wjba...@gmail.com

unread,
Oct 24, 2013, 9:54:14 PM10/24/13
to geospati...@googlegroups.com
Joel,
That  worked. Polygon files (.dbf, .shp and .shx) were created. Thanks for getting me one step closer.


The list is constructed from a text file string ( created from an autocad polyline)
using this code:

Coords=[]
Loop through the input string...
     myPoint=[int(outx),int(outy)]
     Coords.append(myPoint)
parts=[Coords]

import shapefile
w = shapefile.Writer(shapefile.POLYGON)
w.poly(parts)
w.field('FIRST_FLD','C','40')
w.field('SECOND_FLD','C','40')
w.record('First','Polygon')
w.save('c:\\Python27\\polygon')


After adding a prj file I got it to load correctly into ArcGIS Explorer.

Now I need to learn to enter the attributes. Will probably have more questions.
Bill
Reply all
Reply to author
Forward
0 new messages