-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateJPEG.py
executable file
·50 lines (46 loc) · 1.63 KB
/
createJPEG.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
import xml.etree.ElementTree as ET
from descartes import PolygonPatch
import matplotlib.pyplot as plt
import shapefile
import os
import sys
from PIL import Image
import base64
def get_cmap(n, name='hsv'):
'''Returns a function that maps each index in 0, 1, ..., n-1 to a distinct
RGB color; the keyword argument name must be a standard mpl colormap name.'''
return plt.cm.get_cmap(name, n)
def createJPEG():
shapefile_name = os.path.join(dirName, f)
metadata = os.path.join(dirName, f + '.xml')
tree = ET.parse(metadata)
root = tree.getroot()
thumbnail = root.find('Binary/Thumbnail/Data')
outputfile = shapefile_name[:-4] + '.jpg'
polys = shapefile.Reader(shapefile_name)
shapes = polys.shapes()
cmap = get_cmap(len(shapes))
fig = plt.figure()
fig.set_size_inches(30, fig.get_figheight(), forward=True)
#DPI = fig.get_dpi()
print (f)
ax = fig.add_axes((0,0,1,1))
for i,poly in enumerate(shapes):
poly = poly.__geo_interface__
color = cmap(i)
ax.add_patch(PolygonPatch(poly, fc=None, ec="black", alpha=1, zorder=2))
ax.axis('scaled')
ax.set_axis_off()
plt.savefig(outputfile, bbox_inches='tight', pad_inches=0)
with open(outputfile, "rb") as image:
b64string = base64.b64encode(image.read())
#thumbnail.clear()
thumbnail.text = str(b64string)[2:]
tree.write(metadata)
for dirName, subDirs, fileNames in os.walk('.'):
for f in fileNames:
if f.startswith('.'):
continue
if f.endswith('.shp'):
createJPEG()