1 Star 0 Fork 1

weishujie / Learn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
PureShp2Img.py 948 Bytes
一键复制 编辑 原始数据 按行查看 历史
geospatialpython 提交于 2015-06-18 12:37 . Consolidated repositories
"""
PureShp2Img - Convert a shapefile polygon to a PNG image using pure python
Requires Rui Carmo's PNGCanvas module from http://the.taoofmac.com
"""
import shapefile
import pngcanvas
# Read in a shapefile and write png image
r = shapefile.Reader("mississippi")
xdist = r.bbox[2] - r.bbox[0]
ydist = r.bbox[3] - r.bbox[1]
iwidth = 400
iheight = 600
xratio = iwidth/xdist
yratio = iheight/ydist
pixels = []
#
# Only using the first shape record
for x,y in r.shapes()[0].points:
px = int(iwidth - ((r.bbox[2] - x) * xratio))
py = int((r.bbox[3] - y) * yratio)
pixels.append([px,py])
c = pngcanvas.PNGCanvas(iwidth,iheight)
c.polyline(pixels)
f = file("mississippi.png","wb")
f.write(c.dump())
f.close()
#
# Create a world file
wld = file("mississippi.pgw", "w")
wld.write("%s\n" % (xdist/iwidth))
wld.write("0.0\n")
wld.write("0.0\n")
wld.write("-%s\n" % (ydist/iheight))
wld.write("%s\n" % r.bbox[0])
wld.write("%s\n" % r.bbox[3])
wld.close
1
https://gitee.com/wei_shujie/Learn.git
git@gitee.com:wei_shujie/Learn.git
wei_shujie
Learn
Learn
master

搜索帮助