Writing Multiple polygons to shapefile

116 views
Skip to first unread message

wjba...@gmail.com

unread,
Dec 9, 2013, 10:40:06 PM12/9/13
to geospati...@googlegroups.com
Joel,
I can create a shapefile with one (1) polygon and associated data using this:

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')


Now I want to append more polygons to the same shape file. Nothing I have tried seems to be working.

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')
#
# add second polygon (not working)
w.poly(parts2)
w.record('Second','Polygon')
w.save('c:\\Python27\\polygon')



Questions.I have...
1.
Do the w.field statements need to be repeated?
2.
Does the w.save statement close the shapefile?
3.
What separates the records?


Thanks
Bill

Joel Lawhead

unread,
Dec 9, 2013, 10:52:59 PM12/9/13
to geospati...@googlegroups.com
Bill,

As soon as you call the "save()" method the shapefile is finalized to disk.  If you were to remove the first save call in your example, your code would create two polygons. 

To edit a shapefile after you have saved it you need to create a Reader object, read in the saved shapefile, transfer it to a Writer object, call the poly and record methods for the second polygon, and save again. 

Please see this blog post where I demonstrate exactly what you are trying to do using two polygons:

You may have noticed there is also an Editor class in the library.  That class is supposed to perform the above operation in a single object to save you some lines of code but people keep finding bugs so it's a work in progress.  The method above is extremely reliable so it's worth the extra steps. 

Let me know if how that goes for you or if you have any other questions. 

Thanks,
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,
Dec 11, 2013, 10:50:07 AM12/11/13
to geospati...@googlegroups.com
Joel,

I got my Autocad To Shape (ats.py) program working. I start in Autocad using Autolisp/OpenDCL (ats.lsp). I use a dialog box to select multiple polylines and enter the field information. ATS.lsp (autolisp program) then exports a text file that can be read by Python.

The Python program then reads the text file, translates/rotates the State Plane points using Corpscon6.dll.
Then it imports your shapefile.py that creates the shapefile.

ats.py also creates a prj file.

ArcGIS Explorer will add the files with no problems (ArcGis Coordinate system has to match what is specified in the prj file).

Thanks for the help and the shapefile.py
Bill

Joel Lawhead

unread,
Dec 11, 2013, 11:33:17 AM12/11/13
to geospati...@googlegroups.com
Wow - that's neat!  There is nothing I love more than process automation.  

How does Python access the dll? Is it ctypes or the win32 package?

- 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.

wjba...@gmail.com

unread,
Dec 11, 2013, 1:27:19 PM12/11/13
to geospati...@googlegroups.com
I am using ctypes for the calls to Corpscon6.dll. I was not aware of the win32 package.

This is my first python program ( that does anything).

Getting these programs to interface with each other is a chore. I struggled for days until I found that Python file.readline() adds a '\n' that was in my variable I was sending to shapefile.py.

Joel Lawhead

unread,
Dec 11, 2013, 2:13:08 PM12/11/13
to geospati...@googlegroups.com
Ctypes is the "right" way most 95% of the time. I was just curious. Not bad for your first script!

On Wednesday, December 11, 2013, wrote:
--
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