-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tupfile-preview.lua
127 lines (108 loc) · 2.81 KB
/
Tupfile-preview.lua
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
tup.include("Tupfile-consts.lua")
output_dir = "data/"
regions = {
{"world", 0, 0, 0},
{"europe", 3, 49, 9.6},
{"africa", 2, 6, 19},
{"usa", 3, 40, -101},
{"japan", 5, 35, 130},
}
cities = {
{"world", 0, 0, 0},
{"berlin", 9, 52.44504, 13.40973},
{"nyc", 9, 40.76828, -73.88639},
{"tokyo", 8, 35.7295, 139.70422},
{"ljubljana", 10, 46.09049, 14.54004},
}
function gdal(cmd)
return 'docker run --rm -i' ..
' -w / ghcr.io/osgeo/gdal:alpine-normal-3.7.1 ' ..
cmd
end
function previews(input, table, places)
w, h = 1024, 1024
multisample = 1
mw, mh = w * multisample, h * multisample
for i = 1, #places do
local place = places[i]
local name = place[1]
local level = place[2]
local lat = place[3]
local lng = place[4]
local ar = h / w * 360 / 180
local p = 2^level
local cx, cy = lng, lat
local xs = 360 / p * 0.5
local xmin, xmax = cx-xs, cx+xs
local ys = 180 / p * 0.5
local ymin, ymax = cy-ys*ar, cy+ys*ar
output = output_dir .. fullbase .. "_" .. name .. ".tiff"
outputpng = output_dir .. fullbase .. "_" .. name .. ".png"
cmd = '^s^ ' ..
'cat ' .. input .. ' | ' ..
gdal(
'sh -c "' ..
'cp /dev/stdin input.gpkg &&' ..
'gdal_rasterize ' ..
' -q ' ..
' -init 255 ' ..
' -burn 90 ' ..
' -ot Byte ' ..
' -ts ' .. mw .. ' ' .. mh .. ' ' ..
' -te ' .. xmin .. ' ' .. ymin .. ' ' .. xmax .. ' ' .. ymax .. ' ' ..
' -l ' .. table .. ' ' ..
' input.gpkg ' ..
' /vsistdout/ | ' ..
'gdal_translate ' ..
' -q ' ..
' -of PNG ' ..
' -r lanczos ' ..
' -outsize ' .. w .. ' ' .. h .. ' ' ..
' /vsistdin/ ' ..
' /vsistdout/ ' ..
'"'
) ..
' > ' .. outputpng ..
' && optipng -quiet ' .. outputpng
tup.rule(input, cmd, outputpng)
end
end
files = tup.glob("data/*_makevalid.gpkg")
for i = 1, #files do
input = files[i]
filename = tup.file(input)
fullbase = tup.base(input)
input = output_dir .. filename
base = fullbase:sub(1, -11)
places = regions
if base:find("urban") then
places = cities
end
previews(input, table_from_base(base), places)
end
files = tup.glob("data/*_roundtrip_*.gpkg")
for i = 1, #files do
input = files[i]
filename = tup.file(input)
fullbase = tup.base(input)
input = output_dir .. filename
base = fullbase:sub(1, -22)
places = regions
if base:find("urban") then
places = cities
end
previews(input, table_from_base(base), places)
end
files = tup.glob("data/*_s?_wkb.gpkg")
for i = 1, #files do
input = files[i]
filename = tup.file(input)
fullbase = tup.base(input)
input = output_dir .. filename
base = fullbase:sub(1, -8)
places = regions
if base:find("urban") then
places = cities
end
previews(input, table_from_base(base), places)
end