-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
276 lines (215 loc) · 6.02 KB
/
main.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import http from "node:http";
import fs from "node:fs";
import process from "node:process";
import { $ } from "zx";
/** @type {import("zx").ProcessPromise} */
let p;
let busy = false;
const server = http.createServer(handleRequest);
/**
*
* @param {http.IncomingMessage} req
* @param {http.ServerResponse} res
* @returns
*/
async function handleRequest(req, res) {
if (
req.method === "POST" &&
req.headers["content-type"] !== "application/json"
) {
res.writeHead(406, { "Content-Type": "text/plain" });
res.end("Body is not application/json.");
return;
}
if (busy) {
console.log("[Busy]");
res.writeHead(503, { "Content-Type": "text/plain" });
res.end("Busy, try again later.");
return;
}
console.log("[Starting]");
let closed = false;
res.on("close", () => {
console.log("[Closed]");
closed = true;
p?.kill("SIGTERM").catch((err) =>
console.error("Error terminating process", err)
);
p = undefined;
});
const params = new URL(req.url, `http://${req.headers.host}`).searchParams;
const threshold = params.get("threshold") || "20000";
const pixelSize = params.get("pixel-size");
const minLen = params.get("min-len") || "50";
const simplifyTolerance = params.get("simplify-tolerance") || "1.5";
if (
[threshold, pixelSize ?? "1", minLen, simplifyTolerance].some((a) =>
isNaN(parseFloat(a))
)
) {
res.statusCode = 400;
return;
}
const toOsm = !!params.get("to-osm");
busy = true;
function writeHeader() {
if (res.headersSent) {
return;
}
res.writeHead(200, {
"Content-Type": toOsm ? "application/xml" : "application/geo+json",
// "Content-Disposition": 'attachment; filename="streams.geojson"',
});
}
try {
const ws = fs.createWriteStream("mask.geojson");
if (req.method === "POST") {
await new Promise((resolve, reject) => {
ws.on("open", () => {
req.pipe(ws);
});
ws.on("error", (err) => {
reject(err);
});
req.on("error", (err) => {
reject(err);
});
req.on("end", () => {
resolve();
});
});
} else if (req.method === "GET") {
const mask = params.get("mask");
if (!mask) {
res.writeHead(400, { "Content-Type": "text/plain" });
res.end("Missing mask parameter.");
return;
}
await new Promise((resolve, reject) => {
ws.write(mask, (err) => {
if (err) {
reject(err);
} else {
ws.close((err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
}
});
});
} else {
res.writeHead(405);
res.end();
return;
}
console.log("[Responding]");
const tid = setInterval(() => {
writeHeader();
res.write("\n");
}, 10000);
try {
await workHard(threshold, minLen, simplifyTolerance, pixelSize, toOsm);
} finally {
clearInterval(tid);
}
writeHeader();
const rs = fs.createReadStream(toOsm ? "result.osm" : "result.geojson");
rs.on("open", () => {
rs.pipe(res);
});
await new Promise((resolve, reject) => {
res.on("error", (err) => {
reject(err);
});
res.on("close", () => {
reject(new Error("unecpected close"));
});
rs.on("error", (err) => {
reject(err);
});
rs.on("end", () => {
resolve();
});
});
} catch (err) {
if (closed) {
console.log("Connection closed prematurely");
} else if (err.message === "area too big") {
if (!res.headersSent) {
res.writeHead(400, { "Content-Type": "text/plain" });
}
res.end("Area is too big.");
} else {
console.error(err);
if (!res.headersSent) {
res.writeHead(500);
}
res.end(err.message);
}
} finally {
busy = false;
console.log("[Done]");
}
}
server.listen(8080);
/**
* @param {ProcessPromise} pp
*/
async function run(pp) {
p = pp;
const res = await pp;
p = undefined;
return res;
}
async function workHard(
threshold,
minLen,
simplifyTolerance,
pixelSize,
toOsm
) {
const a = await run(
$`ogrinfo -q -dialect SQLite -sql "SELECT SUM(ST_Area(st_transform(geometry, 8353))) AS area FROM mask" mask.geojson`
);
const area = Number(/area \(Real\) = ([\d\.]*)/.exec(a.stdout)?.[1]);
if (!area || area > 200_000_000) {
throw new Error("area too big");
}
const demPath =
process.env.DEM_PATH ??
"/media/martin/OSM/___LIDAR_UGKK_DEM5_0_JTSK03_1cm.tif";
await run(
$`gdalwarp -overwrite -of GTiff -dstnodata -9999 -cutline mask.geojson -crop_to_cutline ${
pixelSize ? `-tr ${pixelSize} ${pixelSize}` : ""
} ${demPath} cropped.tif`
);
await run(
$`whitebox_tools --wd=. --run=FlowAccumulationFullWorkflow --dem=cropped.tif --out_dem=dem.tif --out_pntr=pointer.tif --out_accum=accum.tif`
);
await run(
$`whitebox_tools --wd=. --run=ExtractStreams --flow_accum=accum.tif --threshold=${threshold} --output=streams.tif`
);
await run(
$`whitebox_tools --wd=. --run=RemoveShortStreams --d8_pntr=pointer.tif --streams=streams.tif --output=long_streams.tif --min_length=${minLen}`
);
await run(
$`gdal_calc.py --overwrite --calc '(A==1)*1' -A long_streams.tif --outfile long_streams_clean.tif`
);
await run(
$`whitebox_tools --wd=. --run=RasterStreamsToVector --streams=long_streams_clean.tif --d8_pntr=pointer.tif --output=streams`
);
await run($`ogr2ogr -a_srs epsg:8353 streams8.shp streams.shp`);
await run($`grass --tmp-location EPSG:8353 --exec sh grass_batch_job.sh`);
await run(
$`ogr2ogr -simplify ${simplifyTolerance} -t_srs EPSG:4326 simplified.geojson smooth.gpkg`
);
await run(
$`jq '.features[].properties = {waterway: "stream", source: "ÚGKK SR DMR 5.0"}' simplified.geojson > result.geojson`
);
if (toOsm) {
await run($`geojsontoosm result.geojson > result.osm`);
}
}