Created
June 25, 2017 19:28
-
-
Save Bhacaz/59ca421e018d85b8565455dace818461 to your computer and use it in GitHub Desktop.
Generate image (PNG) preview of a Shapefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Big thanks and inspiration from : https://github.com/mfurlend/shp2png | |
Addapted to have: | |
- random fill color | |
- shapeType = Points to pentagone | |
''' | |
import shapefile, math | |
import Image, ImageDraw | |
import argparse | |
import numpy.random as np | |
def pointToPentagone(x, y, r): | |
dot = [] | |
for i in list(range(5)): | |
i = math.radians(i * 72 + 90) | |
dot.append([int(r * math.cos(i)) + x, int(r * math.sin(i)) + y]) | |
close = dot[0] | |
dot.append(close) | |
return dot | |
def getRandomColor(): | |
return str(np.random_integers(75, 240)) | |
def process(r): | |
iwidth = args['width'] | |
bbox = r.bbox | |
xdist = bbox[2] - bbox[0] | |
ydist = bbox[3] - bbox[1] | |
ratio=xdist/ydist | |
iheight = int(iwidth/ratio) | |
xratio = iwidth/xdist | |
yratio = iheight/ydist | |
img = Image.new("RGB", (iwidth, iheight), "white") | |
transparent_area = (0,0,iwidth,iheight) | |
mask=Image.new('L', (iwidth, iheight), color=255) | |
draw=ImageDraw.Draw(mask) | |
draw.rectangle(transparent_area, fill=0) | |
img.putalpha(mask) | |
draw = ImageDraw.Draw(img) | |
xys = [] | |
xy = [] | |
for shape in r.shapes(): | |
if shape.shapeType == 1: # shapeType == points | |
x,y = shape.points[0] | |
x = int(iwidth - ((bbox[2] - x) * xratio)) | |
y = int((bbox[3] - y) * yratio) | |
penta = pointToPentagone(x, y, 15) | |
for x,y in penta: | |
xy.append((x,y)) | |
draw.polygon(xy, outline=args['stroke'], fill=args['fill']) | |
else: | |
for x,y in shape.points: | |
xy.append((x,y)) | |
xys.append(xy) | |
xy = [] | |
for pts in xys: | |
pixels2 = [] | |
for x,y in pts: | |
px = int(iwidth - ((bbox[2] - x) * xratio)) | |
py = int((bbox[3] - y) * yratio) | |
pixels2.append((px,py)) | |
draw.polygon(pixels2, outline=args['stroke'], fill=args['fill']) | |
img.save(args['input']+".png") | |
parser = argparse.ArgumentParser(description="Produce .png image from shapefile. Optionally filter by attribute value(s).") | |
parser.add_argument('input' ,help='input filename (without .shp)') | |
parser.add_argument('-w', '--width', type=int, default=360) | |
parser.add_argument('--stroke', type=str, default='rgb(255, 255, 255)', help="polygon stroke color. defaults to \"rgb(255, 255, 255)\"") | |
args = vars(parser.parse_args()) | |
myshp = open(args['input']+".shp", "rb") | |
mydbf = open(args['input']+".dbf", "rb") | |
r = shapefile.Reader(shp=myshp, shx=None, dbf=mydbf) | |
args['fill'] = "rgb(" + getRandomColor() + ',' + getRandomColor() + ',' + getRandomColor() + ')' | |
process(r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment