-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcustom.py
128 lines (103 loc) · 2.96 KB
/
custom.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import re
from StringIO import StringIO
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.pyplot as plt
from PIL import Image
import rasterio
from openterrain import MAX_ZOOM, render_hillshade, Tile
POSITRON_RAMP = {
"red": [(0.0, 0.0, 0.3),
(1.0, 1.0, 1.0)],
"green": [(0.0, 0.0, 0.3),
(1.0, 1.0, 1.0)],
"blue": [(0.0, 0.0, 0.3),
(1.0, 1.0, 1.0)],
"alpha": [(0.0, 0.7, 0.7),
(195 / 255.0, 0.0, 0.0),
(1.0, 1.0, 1.0)]
}
POSITRON = LinearSegmentedColormap("positron", POSITRON_RAMP)
DARKMATTER_RAMP = {
"red": [(0.0, 60 / 255.0, 60 / 255.0),
(1.0, 220 / 255.0, 220 / 255.0)],
"green": [(0.0, 75 / 255.0, 75 / 255.0),
(1.0, 1.0, 1.0)],
"blue": [(0.0, 80 / 255.0, 80 / 255.0),
(1.0, 100 / 255.0, 100 / 255.0)],
"alpha": [(0.0, 0.4, 0.4),
(180 / 255.0, 0.0, 0.0),
(1.0, 0.2, 0.2)]
}
DARKMATTER = LinearSegmentedColormap("darkmatter", DARKMATTER_RAMP)
def main():
meta = {}
data = render_hillshade(Tile(653, 1581, 12), src_meta=meta, resample=True)
meta.update(
driver="GTiff",
dtype=rasterio.uint8,
compress="deflate",
predictor=1,
nodata=None,
tiled=True,
sparse_ok=True,
blockxsize=256,
blockysize=256,
)
with rasterio.open("hillshade_combined_resampled.tif", "w", **meta) as tmp:
tmp.write(data, 1)
plt.imsave(
"darkmatter_combined_resampled.png",
data,
cmap=DARKMATTER,
vmin=0,
vmax=255,
format="png",
)
plt.imsave(
"positron_combined_resampled.png",
data,
cmap=POSITRON,
vmin=0,
vmax=255,
format="png",
)
def handle(event):
zoom = int(event["params"]["path"]["z"])
x = int(event["params"]["path"]["x"])
filename, format = event["params"]["path"]["y"].split(".")
parts = filename.split("@")
y = int(parts[0])
if len(parts) > 1:
scale = int(re.sub(r"[^\d]", "", parts[1]))
else:
scale = 1
tile = Tile(x, y, zoom)
if format != "png":
raise Exception("Invalid format")
if not 0 <= tile.z <= MAX_ZOOM:
raise Exception("Invalid zoom")
if not 0 < scale <= 2:
raise Exception("Invalid scale")
if not 0 <= tile.x < 2**tile.z:
raise Exception("Invalid coordinates")
if not 0 <= tile.y < 2**tile.z:
raise Exception("Invalid coordinates")
hs = render_hillshade(tile, resample=True)
out = StringIO()
plt.imsave(
out,
hs,
cmap=POSITRON,
# cmap=plt.get_cmap("Accent"), # use a default coloramp
vmin=0,
vmax=255,
format=format,
)
if scale == 1:
im = Image.open(out)
im.thumbnail((256, 256), Image.ANTIALIAS)
out = StringIO()
im.save(out, format)
return out.getvalue()
if __name__ == "__main__":
main()