Python's "zipfile" module allows you to write files straight from buffer objects including python's StringIO or cStringIO modules. For web applications where you will return the zipped shapefile as part of an http response, you can write the zip file itself to a file-like object without writing it to disk. In this post, the example writes the zip file to disk.
In Python, file-like objects provide a powerful way to re-route complex data structures from the disk to other targets such as a database, memory data structures, or serialized objects. In most other programming languages file-like objects are called "streams" and work in similar fashion. So this post also demonstrates writing shapefiles to file-like objects using a zip file as a target.
Normally when you save a shapefile you call the writer.save method which writes three files to disk. To use file-like objects you call separate save methods for each file: writer.saveShp, writer.saveShx, and writer.saveDbf.
import zipfile import StringIO import shapefile # Set up buffers for saving shp = StringIO.StringIO() shx = StringIO.StringIO() dbf = StringIO.StringIO() # Make a point shapefile w = shapefile.Writer(shapefile.POINT) w.point(90.3, 30) w.point(92, 40) w.point(-122.4, 30) w.point(-90, 35.1) w.field('FIRST_FLD') w.field('SECOND_FLD','C','40') w.record('First','Point') w.record('Second','Point') w.record('Third','Point') w.record('Fourth','Point') # Save shapefile components to buffers w.saveShp(shp) w.saveShx(shx) w.saveDbf(dbf) # Save shapefile buffers to zip file # Note: zlib must be available for # ZIP_DEFLATED to compress. Otherwise # just use ZIP_STORED. z = zipfile.ZipFile("myshape.zip", "w", zipfile.ZIP_DEFLATED) z.writestr("myshape.shp", shp.getvalue()) z.writestr("myshape.shx", shx.getvalue()) z.writestr("myshape.dbf", dbf.getvalue()) z.close()
If you've been using PyShp for awhile make sure you have the latest version. The file-like object save feature was uploaded to the PyShp subversion repository on Aug. 20, 2011 at revision 30.
You can download PyShp here.
You download the sample script above here.