FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

- Added sample script "zipshape.py" to demonstrate saving a shapefile… · ucespff/geospatialpython@1aa568c · GitHub

Commit 1aa568c

Browse files
geospatialpython
committed
- Added sample script "zipshape.py" to demonstrate saving a shapefile to a file-like object using a zip file as the target.
1 parent 3405656 commit 1aa568c

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

‎zipshape.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff 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+

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL