-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
178 lines (157 loc) · 4.61 KB
/
Copy pathserver.js
File metadata and controls
178 lines (157 loc) · 4.61 KB
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
import { execFile, spawn } from "child_process";
import express from "express";
import fs from "fs/promises";
import fsSync from "fs";
import { promisify } from "node:util";
import os from "os";
import path from "path";
import turfArea from "@turf/area";
import cors from "cors";
const app = express();
app.use(cors());
// TODO Input polygon should be way smaller
app.use(express.json({ limit: "1mb" }));
const PORT = 3000;
const LOGFILE = "requests.log";
const MAX_AREA_M2 = 5 * 10e7;
const INPUT_GOL = "/Users/dabreegster/Downloads/london.gol";
const OSMIUM = "/opt/homebrew/bin/osmium";
const GOL = "/Users/dabreegster/Downloads/gol";
app.post("/", async (req, resp) => {
const start = Date.now();
const geojson = req.body;
const [areaM2, err] = validateRequest(geojson, resp);
if (err) {
return err;
}
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "osm-extractor-"));
console.log(`Working on a request in ${tmpDir}`);
const inputPath = path.join(tmpDir, "input_polygon.geojson");
const xmlOutPath = path.join(tmpDir, "tmp_output.xml");
const pbfOutPath = path.join(tmpDir, "output.pbf");
try {
// gol just takes the geometry, it gets confused by the whole feature
await fs.writeFile(inputPath, JSON.stringify(geojson.geometry));
await runCommandWithOutputFile(
GOL,
["query", INPUT_GOL, "-a", inputPath, "-f", "xml", "*"],
xmlOutPath,
);
// gol outputs XML, not PBF, and it includes very long ways and relations that cross the
// boundary. Use osmium to do a second pass.
await promisify(execFile)(OSMIUM, [
"extract",
"--polygon",
inputPath,
xmlOutPath,
"-o",
pbfOutPath,
]);
const pbfBuffer = await fs.readFile(pbfOutPath);
const totalMs = Date.now() - start;
geojson.properties.stats = {
time: new Date().toISOString(),
duration_ms: totalMs,
area_m2: areaM2,
};
try {
await fs.appendFile(LOGFILE, JSON.stringify(geojson));
} catch (err) {
console.error(`Problem writing to log: ${err}`);
}
console.log(`Request succeeded, taking ${totalMs} ms`);
resp.setHeader("Content-Type", "application/octet-stream");
resp.setHeader("Content-Length", String(pbfBuffer.length));
return resp.send(pbfBuffer);
} catch (err) {
geojson.properties.stats = {
time: new Date().toISOString(),
duration_ms: Date.now() - start,
area_m2: areaM2,
error: String(err),
};
try {
await fs.appendFile(LOGFILE, JSON.stringify(geojson));
} catch (err) {
console.error(`Problem writing to log: ${err}`);
}
console.error(`Request failed: ${err}`);
return resp
.status(500)
.json({ error: "Processing failed", detail: String(err) });
} finally {
try {
await fs.rm(tmpDir, { recursive: true, force: true });
} catch (err) {
console.error(`Problem cleaning up ${tmpDir}: ${err}`);
}
}
});
app.listen(PORT, () => {
console.log(`osm-extractor running on port ${PORT}`);
});
// Takes the request body. Returns [areaM2, returnable error response] as an Either.
function validateRequest(geojson, resp) {
if (
!geojson ||
geojson.type != "Feature" ||
!geojson.geometry ||
geojson.geometry.type != "Polygon" ||
!geojson.properties
) {
return [
0,
resp.status(400).json({
error:
"Request must be a GeoJSON Feature with a Polygon and a properties dictionary",
}),
];
}
let areaM2;
try {
areaM2 = turfArea(geojson);
} catch (err) {
return [
0,
resp
.status(400)
.json({ error: `Input doesn't look like a valid polygon: ${err}` }),
];
}
if (areaM2 > MAX_AREA_M2) {
return [
0,
resp.status(400).json({
error: `Polygon area ${Math.round(areaM2).toLocaleString()} m^2 exceeds limit of ${MAX_AREA_M2.toLocaleString()} m^2.`,
}),
];
}
return [areaM2, null];
}
// Run a command, redirecting STDOUT to a file
function runCommandWithOutputFile(cmd, args, outFile) {
return new Promise((resolve, reject) => {
const child = spawn(cmd, args);
const outStream = fsSync.createWriteStream(outFile);
child.stdout.pipe(outStream);
let stderr = "";
child.stderr.on("data", (chunk) => {
stderr += chunk.toString();
});
child.on("error", (err) => {
outStream.close();
reject(err);
});
child.on("close", (code) => {
outStream.close();
if (code == 0) {
return resolve();
}
reject(
new Error(
`'${cmd} ${args.join(" ")}' exited with code ${code}: ${stderr}`,
),
);
});
});
}