| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3405656 commit 1aa568c
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,45 @@ | |||
| 1 | + """ | ||
| 2 | + Example of saving a shapefile to a file-like | ||
| 3 | + object using a zip file as the target. Uses | ||
| 4 | + the individual save methods for each type | ||
| 5 | + of file instead of the general pyshp | ||
| 6 | + save method. | ||
| 7 | + """ | ||
| 8 | + | ||
| 9 | + import zipfile | ||
| 10 | + import StringIO | ||
| 11 | + import shapefile | ||
| 12 | + | ||
| 13 | + # Set up buffers for saving | ||
| 14 | + shp = StringIO.StringIO() | ||
| 15 | + shx = StringIO.StringIO() | ||
| 16 | + dbf = StringIO.StringIO() | ||
| 17 | + | ||
| 18 | + # Make a point shapefile | ||
| 19 | + w = shapefile.Writer(shapefile.POINT) | ||
| 20 | + w.point(90.3, 30) | ||
| 21 | + w.point(92, 40) | ||
| 22 | + w.point(-122.4, 30) | ||
| 23 | + w.point(-90, 35.1) | ||
| 24 | + w.field('FIRST_FLD') | ||
| 25 | + w.field('SECOND_FLD','C','40') | ||
| 26 | + w.record('First','Point') | ||
| 27 | + w.record('Second','Point') | ||
| 28 | + w.record('Third','Point') | ||
| 29 | + w.record('Fourth','Point') | ||
| 30 | + | ||
| 31 | + # Save shapefile components to buffers | ||
| 32 | + w.saveShp(shp) | ||
| 33 | + w.saveShx(shx) | ||
| 34 | + w.saveDbf(dbf) | ||
| 35 | + | ||
| 36 | + # Save shapefile buffers to zip file | ||
| 37 | + # Note: zlib must be available for | ||
| 38 | + # ZIP_DEFLATED to compress. Otherwise | ||
| 39 | + # just use ZIP_STORED. | ||
| 40 | + z = zipfile.ZipFile("myshape.zip", "w", zipfile.ZIP_DEFLATED) | ||
| 41 | + z.writestr("myshape.shp", shp.getvalue()) | ||
| 42 | + z.writestr("myshape.shx", shx.getvalue()) | ||
| 43 | + z.writestr("myshape.dbf", dbf.getvalue()) | ||
| 44 | + z.close() | ||
| 45 | + | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments