-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
392 lines (379 loc) · 12.5 KB
/
index.html
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Swiss Post Letter Boxes</title>
<link rel="icon" href="favicon.png" />
<link
rel="stylesheet"
href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
crossorigin=""
/>
<script
src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""
></script>
<style>
html,
body {
padding: 0;
margin: 0;
height: 100%;
}
#map {
height: 100%;
width: 100%;
}
#map .leaflet-popup-content p {
margin: 12px 0;
}
p.warning {
background-color: #ffeeba;
border-radius: 5px;
padding: 0.5em;
}
.popup-poi-title,
.popup-osm-id {
font-size: inherit;
font-weight: bold;
text-align: center;
}
.popup-status {
text-align: center;
}
.popup-status span {
font-family: monospace;
user-select: all;
white-space: nowrap;
}
.popup-osm-id a {
color: inherit;
text-decoration: inherit;
}
.popup-osm-tags {
border-collapse: collapse;
}
.popup-osm-tags th,
.popup-osm-tags td {
font-family: monospace;
font-size: 90%;
font-weight: normal;
padding: 0 1ch 0 0;
vertical-align: top;
}
.popup-welcome {
text-align: center;
width: 400px;
}
.popup-welcome h2 {
font-size: 1.2em;
}
.popup-legend {
display: flex;
list-style-type: none;
padding: 0;
gap: 1ch;
align-items: center;
}
.popup-legend li {
display: contents;
}
.popup-legend span {
flex: 1;
text-align: left;
}
.popup-version {
font-size: 70%;
font-family: monospace;
}
</style>
</head>
<body>
<div id="map"></div>
<template id="popup">
<div>
<h2 class="popup-poi-title"></h2>
<p class="popup-status">…</p>
<h3 class="popup-osm-id"></h3>
<table class="popup-osm-tags"></table>
</div>
</template>
<template id="popup-welcome">
<div class="popup-welcome">
<h1><img src="logo.png" alt="Logo" /><br />Swiss Post Letter Boxes</h1>
<h2>Usage</h2>
<p>Zoom in on your desired region and click the refresh button.</p>
<p class="warning">
Please use this tool only to plan your survey tours and do not copy
data directly!
</p>
<ul class="popup-legend">
<li><img src="./marker-post.png" /><span>Swiss Post</span></li>
<li>
<img src="./marker-osmpost.png" /><span>Swiss Post & OSM</span>
</li>
<li><img src="./marker-osm.png" /><span>OSM</span></li>
</ul>
<h2>Code/Bug Reports</h2>
<p>
<a href="https://github.com/1-Byte/swisspost-osm"
>https://github.com/1-Byte/swisspost-osm</a
>
</p>
<p class="popup-version"></p>
</div>
</template>
<script>
const SWISSPOST_KEY = "";
const OVERPASS_URL = "https://overpass.osm.ch/api/interpreter";
const BUFFER = 30;
const CENTER = [47.0, 8.3];
let iconOptions = {
...L.Icon.Default.prototype.options,
iconRetinaUrl: null,
shadowUrl: "marker-shadow.png",
shadowRetinaUrl: null,
};
let postIcon = L.icon({ ...iconOptions, iconUrl: "marker-post.png" });
let osmIcon = L.icon({ ...iconOptions, iconUrl: "marker-osm.png" });
let osmPostIcon = L.icon({
...iconOptions,
iconUrl: "marker-osmpost.png",
});
let popupOptions = { minWidth: 200, maxWidth: 400 };
let popupTemplate = document.getElementById("popup").content;
let welcomeTemplate = document.getElementById("popup-welcome").content;
let map = L.map("map").setView(CENTER, 9);
const Button = L.Control.extend({
initialize: function (label, handler, options) {
this.label = label;
this.handler = handler;
L.setOptions(this, options);
},
onAdd: function (map) {
var div = L.DomUtil.create("div");
div.classList.add("leaflet-control");
div.classList.add("leaflet-bar");
var a = L.DomUtil.create("a");
a.role = "button";
a.title = this.label;
a.style.fontSize = "22px";
a.style.cursor = "pointer";
a.textContent = this.label;
a.onclick = () => {
this.handler(map);
return false;
};
div.appendChild(a);
return div;
},
onRemove: function (map) {},
});
async function queryOsm(map) {
let bbox = map.getBounds().toBBoxString();
let url = `${OVERPASS_URL}?data=[bbox][out:json];node[amenity=post_box];out;&bbox=${bbox}`;
let res = await fetch(url);
let json = await res.json();
let result = {};
for (let node of json.elements) {
result[node.id] = node;
}
return result;
}
async function queryPost(map) {
let center = map.getCenter();
let bounds = map.getBounds();
let distance = ~~bounds
.getSouthWest()
.distanceTo(bounds.getNorthEast());
let geofilter = [center.lat, center.lng, distance].join(",");
let res = await fetch(
`https://swisspost.opendatasoft.com/api/records/1.0/search/?dataset=zugangspunkte-post&q=&rows=50&facet=address_kantoncode&facet=address_zip&refine.poityp_de=Briefeinwurf&geofilter.distance=${geofilter}`,
{
headers: {
authorization: "Apikey " + SWISSPOST_KEY,
},
}
);
let json = await res.json();
let result = {};
for (let record of json.records) {
result[record.fields.id] = {
title: record.fields.description_en.replace(/^Letterbox /, ""),
geopoint: record.fields.geopoint,
};
}
return result;
}
function parsePartial(html) {
let doc = document.createElement("template");
doc.innerHTML = html;
let details = doc.content.querySelector(".button_to-details");
if (!details) return null;
for (let container of doc.content.querySelectorAll(".info-container")) {
if (container.querySelector("h6").textContent != "Leerungszeiten")
continue;
let data = {};
for (let dt of container.querySelectorAll("dt")) {
data[dt.textContent] = dt.nextElementSibling.textContent;
}
return { url: details.href, data };
}
return null;
}
function mapPartial(data) {
const MAPPINGS = {
"Montag – Freitag": "Mo-Fr",
"Montag – Samstag": "Mo-Sa",
Samstag: "Sa",
Sonntag: "Su,PH",
};
let result = [];
for (let key in data) {
if (key in MAPPINGS && /^[0-9]{1,2}\.[0-9]{2}$/.test(data[key])) {
let value = data[key].replace(".", ":");
result.push(`${MAPPINGS[key]} ${value}`);
} else {
return null;
}
}
return result.join("; ");
}
function merge(post, osm) {
let matches = {};
for (let id in post) {
let buffer = L.latLng(post[id].geopoint).toBounds(BUFFER);
matches[id] = [];
for (let node in osm) {
if (buffer.contains([osm[node].lat, osm[node].lon])) {
if (!("post" in osm[node])) osm[node].post = [];
osm[node].post.push(id);
matches[id].push(node);
}
}
}
for (let id in matches) {
let osmId =
matches[id].length == 1 && osm[matches[id][0]].post.length == 1
? matches[id][0]
: null;
let osmTags = osmId ? osm[osmId].tags : null;
if (osmId) delete osm[osmId];
L.marker(post[id].geopoint, {
title: `${post[id].title} ↔ ${osmId ? "#" + osmId : "-"}`,
icon: osmId ? osmPostIcon : postIcon,
poiId: id,
poiTitle: post[id].title,
osmId,
osmTags,
}).addTo(postMarkers);
}
for (let node in osm) {
L.marker([osm[node].lat, osm[node].lon], {
title: `- ↔ #${node}`,
icon: osmIcon,
osmId: node,
osmTags: osm[node].tags,
}).addTo(osmMarkers);
}
}
function bindPopup({ options }) {
let popup = popupTemplate.firstElementChild.cloneNode(true);
let poiTitle = popup.querySelector(".popup-poi-title");
options.status = popup.querySelector(".popup-status");
if (options.poiId) {
poiTitle.textContent = options.poiTitle;
} else {
poiTitle.parentNode.removeChild(poiTitle);
options.status.parentNode.removeChild(options.status);
}
let osmId = popup.querySelector(".popup-osm-id");
let osmTags = popup.querySelector(".popup-osm-tags");
if (options.osmId) {
let a = document.createElement("a");
a.target = "_blank";
a.href = "https://www.openstreetmap.org/node/" + options.osmId;
a.textContent = "#" + options.osmId;
osmId.appendChild(a);
for (let key in options.osmTags) {
let tr = document.createElement("tr");
let keyCell = document.createElement("th");
keyCell.textContent = key;
let valueCell = document.createElement("td");
valueCell.textContent = options.osmTags[key];
tr.appendChild(keyCell);
tr.appendChild(valueCell);
osmTags.appendChild(tr);
}
} else {
osmId.parentNode.removeChild(osmId);
osmTags.parentNode.removeChild(osmTags);
}
return popup;
}
L.tileLayer("https://tile.osm.ch/switzerland/{z}/{x}/{y}.png", {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>, <a href="https://swisspost.opendatasoft.com/">SwissPost</a>',
maxZoom: 18,
}).addTo(map);
let osmMarkers = L.featureGroup([])
.bindPopup(bindPopup, popupOptions)
.addTo(map);
let postMarkers = L.featureGroup([])
.bindPopup(bindPopup, popupOptions)
.on("popupopen", function (e) {
let { poiId, osmTags, status } = e.sourceTarget.options;
let url = `https://places.post.ch/Public/PoiDetail/ShortDetailPagePartial?poiId=${poiId}&lang=de`;
fetch(url)
.then((res) => {
return res.text();
})
.then((html) => {
let parsed = parsePartial(html);
if (!parsed) {
status.textContent = "?";
return;
}
let mapped = mapPartial(parsed.data);
if (mapped) {
let span = document.createElement("span");
span.textContent = mapped;
if (osmTags && osmTags.collection_times == mapped) {
span.style.color = "green";
} else if (osmTags) {
span.style.color = "red";
}
status.replaceChild(span, status.childNodes[0]);
} else {
let a = document.createElement("a");
a.target = "_blank";
a.href = parsed.url;
a.textContent = "Manual Verification";
status.replaceChild(a, status.childNodes[0]);
}
});
})
.addTo(map);
async function refresh(map) {
postMarkers.clearLayers();
osmMarkers.clearLayers();
let post = await queryPost(map);
let osm = await queryOsm(map);
merge(post, osm);
}
new Button("↻", refresh, { position: "topleft" }).addTo(map);
L.popup({
minWidth: 300,
maxWidth: 500,
})
.setLatLng([
(map.getBounds().getSouthWest().lat + CENTER[0]) / 2,
CENTER[1],
])
.setContent(welcomeTemplate.cloneNode(true))
.openOn(map);
</script>
</body>
</html>