Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 747f45c

Browse files
Sample script from GeospatialPython.com blog post
0 parents  commit 747f45c

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

shp2img.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
shp2img.py - creates a png image and world file (.pgw) from a shapefile
3+
containing a single polygon.
4+
5+
date: 20101204
6+
7+
This script requires the Python Imaging Library.
8+
The sample shapefile used is available at:
9+
http://geospatialpython.googlecode.com/files/Mississippi.zip
10+
"""
11+
12+
import shapefile
13+
import Image, ImageDraw
14+
15+
# Read in a shapefile
16+
r = shapefile.Reader("mississippi")
17+
xdist = r.bbox[2] - r.bbox[0]
18+
ydist = r.bbox[3] - r.bbox[1]
19+
iwidth = 400
20+
iheight = 600
21+
xratio = iwidth/xdist
22+
yratio = iheight/ydist
23+
pixels = []
24+
for x,y in r.shapes()[0].points:
25+
px = int(iwidth - ((r.bbox[2] - x) * xratio))
26+
py = int((r.bbox[3] - y) * yratio)
27+
pixels.append((px,py))
28+
img = Image.new("RGB", (iwidth, iheight), "white")
29+
draw = ImageDraw.Draw(img)
30+
draw.polygon(pixels, outline="rgb(203, 196, 190)", fill="rgb(198, 204, 189)")
31+
img.save("mississippi.png")
32+
33+
# Create a world file
34+
wld = file("mississippi.pgw", "w")
35+
wld.write("%s\n" % (xdist/iwidth))
36+
wld.write("0.0\n")
37+
wld.write("0.0\n")
38+
wld.write("-%s\n" % (ydist/iheight))
39+
wld.write("%s\n" % r.bbox[0])
40+
wld.write("%s\n" % r.bbox[3])
41+
wld.close

0 commit comments

Comments
 (0)