-
Notifications
You must be signed in to change notification settings - Fork 2
/
overpass.js
125 lines (116 loc) · 2.65 KB
/
overpass.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
import queryOverpass from "@derhuerst/query-overpass";
// For queries, see https://wiki.openstreetmap.org/wiki/
// All icon names from https://www.mapbox.com/maki-icons/ are valid sym names
// The menu entries for POIs are automatically generated from this Map.
const mapping = new Map();
mapping.set("atm", {
query: "amenity~'atm|bank'",
sym: "bank-11",
});
mapping.set("bakery", {
query: "shop=bakery",
sym: "bakery-11",
});
mapping.set("bike_shop", {
query: "shop=bicycle",
sym: "bicycle-11",
});
mapping.set("cafe", {
query: "amenity=cafe",
sym: "cafe-11",
});
mapping.set("camping", {
query: "tourism=camp_site",
sym: "campsite-11",
});
mapping.set("drinking_water", {
query: "amenity=drinking_water",
sym: "drinking-water-11",
});
mapping.set("graveyard", {
query: "landuse=cemetery][amenity=grave_yard",
sym: "cemetery-11",
});
mapping.set("hospitals", {
query: "amenity=hospital",
sym: "hospital-11",
});
mapping.set("information", {
query: "information=office",
sym: "information-11",
});
mapping.set("pharmacy", {
query: "amenity=pharmacy",
sym: "pharmacy-11",
});
mapping.set("picnic", {
query: "tourism=picnic_site",
sym: "picnic-site-11",
});
mapping.set("restaurant", {
query: "amenity=restaurant",
sym: "restaurant-11",
});
mapping.set("shelter", {
query: "amenity=shelter",
sym: "shelter-11",
});
mapping.set("supermarket", {
query: "shop~'supermarket|convenience'",
sym: "grocery-11",
});
function queryPOI(bbox, tag) {
if (!mapping.has(tag)) {
throw new Error("Tag " + tag + " unknown");
}
return queryOverpass(query(bbox, tag), { fetchmode: "cors" })
.then((elements) => {
let pois = [];
elements.forEach((ele) =>
pois.push({
coords: [ele.lon, ele.lat],
props: {
name: ele.tags.name || tag[0].toUpperCase() + tag.substr(1),
symbol: mapping.get(tag).sym,
},
})
);
return pois;
})
.catch((e) => {
throw e;
});
}
function query(bbox, tag) {
const box = [
bbox.getSouth(),
bbox.getWest(),
bbox.getNorth(),
bbox.getEast(),
].join(",");
return `\
[out:json][timeout:25];
node[${mapping.get(tag).query}](${box});
out;`;
}
const overpass = {
loadPOIs(features, tag) {
const promises = [];
features.forEach((feature) => {
promises.push(queryPOI(feature.bbox, tag));
});
return Promise.all(promises)
.then((allElements) => {
return allElements.reduce((prev, cur) => {
return prev.concat(cur);
});
})
.catch((e) => {
throw e;
});
},
mapping() {
return mapping;
},
};
export default overpass;