-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchData.js
More file actions
120 lines (98 loc) · 2.88 KB
/
fetchData.js
File metadata and controls
120 lines (98 loc) · 2.88 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
import { writeFile, copyFile, mkdir } from "node:fs/promises";
import { join } from "node:path";
const MAX_RETRIES = 3;
const USER_AGENT = process.env.USER_AGENT;
if (!USER_AGENT) {
console.error("Error: USER_AGENT environment variable is not set.");
process.exit(1);
}
/**
* Fetches stair data from the Overpass API, maps it to
* GeoJSON and saves it to public/stairs.geojson.
*/
async function fetchData(retryNumber = 0) {
const overpassUrl = "https://overpass-api.de/api/interpreter";
const query = `
[out:json][timeout:300];
area["ISO3166-1:alpha2"="NO"]->.a;
(
way(area.a)["highway"="steps"];
);
out body;
>;
out skel qt;
`;
console.log("Querying Overpass API...");
const start = Date.now();
const response = await fetch(overpassUrl, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": USER_AGENT,
},
body: `data=${encodeURIComponent(query)}`,
});
if (!response.ok) {
const isRetryableError = response.status === 429 || response.status >= 500;
if (isRetryableError && retryNumber < MAX_RETRIES) {
const retry = retryNumber + 1;
const sleepTimeSeconds = 2 + Math.pow(2, retry); // 4, 6, 10 seconds
console.log(
`Overpass API request failed with status ${response.status}. Retrying in ${sleepTimeSeconds} seconds... (retry ${retry}/${MAX_RETRIES})`,
);
await new Promise((resolve) =>
setTimeout(resolve, sleepTimeSeconds * 1000),
);
return fetchData(retry);
}
throw new Error(`HTTP error! Status: ${response.status}`);
}
console.log(
"Got successful response from Overpass API after",
(Date.now() - start) / 1000,
"seconds",
);
const data = await response.json();
const nodes = data.elements
.filter((element) => element.type === "node")
.reduce((acc, node) => {
acc[node.id] = node;
return acc;
}, {});
const features = data.elements
.filter((element) => element.type === "way")
.map((element) => ({
id: element.id,
type: "Feature",
geometry: {
type: "LineString",
coordinates: element.nodes.map((nodeId) => [
nodes[nodeId].lon,
nodes[nodeId].lat,
]),
},
properties: {
id: element.nodes[0],
name: element.tags.name,
step_count: element.tags.step_count
? Number(element.tags.step_count)
: undefined,
},
}));
console.log("Found", features.length, "features");
const featureCollection = {
type: "FeatureCollection",
features,
};
console.log("Writing to stairs.geojson...");
await writeFile(
join("public", "stairs.geojson"),
JSON.stringify(featureCollection),
);
}
async function main() {
await fetchData();
const lib = join("public", "lib");
await mkdir(lib, { recursive: true });
}
main();