-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
227 lines (211 loc) · 8.48 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
<html>
<head>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"/>
<link rel="stylesheet" href="//unpkg.com/[email protected]/dist/leaflet.css"/>
<script src="//unpkg.com/[email protected]/dist/leaflet.js"></script>
<script type="text/javascript" src="//unpkg.com/[email protected]/leaflet-providers.js"></script>
<script type="text/javascript" src="//unpkg.com/[email protected]/osm-read-pbf.js"></script>
<script type="text/javascript" src="//unpkg.com/[email protected]/osmtogeojson.js"></script>
<script type="text/javascript" src="//unpkg.com/[email protected]/leaflet-hash.js"></script>
<script src="//bundle.run/[email protected]"></script>
<style>
#mapid {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
}
.shouldZoom {
width: 100%;
text-align: center;
position: absolute;
top: 0;
left: 0;
z-index: 500;
background-color: #fdfdfd;
}
</style>
</head>
<body>
<div id="mapid"></div>
<div class="shouldZoom">You should zoom to see something, red rectangle, no transport, green there is transports to query</div>
<a href="https://github.com/wadouk/osm-public-transports" style="z-index: 10"><img
style="position: absolute; z-index: 1000; top: 0; left: 0; border: 0;"
src="https://s3.amazonaws.com/github/ribbons/forkme_left_gray_6d6d6d.png" alt="Fork me on GitHub"></a>
<script language="JavaScript" type="application/javascript">
if (!localStorage.dataHost) {
localStorage.dataHost = 'https://data-osm-public-transport.5apps.com'
}
document.addEventListener('DOMContentLoaded', function () {
const map = L.map('mapid', {
center: [27.293689, 5.449219],
zoomControl: false,
zoom: 3,
attribution: '' +
'<a href="http://leafletjs.com" target="_blank">Leaflet</a>' +
' © <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
accessToken: 'not-needed',
})
L.tileLayer.provider("Esri.WorldTopoMap").addTo(map)
new L.Hash(map)
const findColor = (p, defaultColor) => p.features
.map(f => f.properties.relations)
.filter(Boolean)
.reduce((a, b) => a.concat(b), [])
.map(f => f.reltags.colour)
.filter(Boolean)
.reduce((a, b) => a === defaultColor ? b : a, defaultColor)
function latlng(coord) {
return [coord[1], coord[0]]
}
const Button = L.Control.extend({
getContainer: function () {
const button = document.createElement('button')
button.innerHTML = this.options.label
button.addEventListener('click', this.options.click)
return button
},
onAdd: function (map) {
this._map = map
return this.getContainer()
}
})
function getCenterGeohash(l) {
return latlonGeohash.encode(l.lat % 360, l.lng % 360, 5)
}
function attr(k, v) {
const keys = {}
keys['id']= (v) => `<a href="https://openstreetmap.org/${v}" target="_blank">${v}</a>`
keys['relations'] = (v) => v.map(i => `<a href="https://openstreetmap.org/relation/${i['rel']}" target="_blank">${i.reltags['name']}</a>`).join('<br>')
return (keys[k] && keys[k](v)) || v
}
function dumpProps(o) {
return o ? Object.entries(o)
.map(([key, value]) => `<b>${key}</b>=${attr(key, value)}`) : []
}
function geojson(file) {
return () => {
let centerGeohash = getCenterGeohash(map.getCenter());
if (!hashes.has(centerGeohash)) {
return
}
map.eachLayer(layer => {
if (layer instanceof L.FeatureGroup) {
layer.remove()
}
})
fetch(`${localStorage.dataHost}/geohashes/${centerGeohash}.json`).then(function (response) {
return response.ok && response.json()
.then(relations => {
relations
.filter(r => new RegExp(`${file}`).test(r))
.map(r => r.replace(/(.+)\.osm/, (match, p1) => `${p1}.pbf`))
.map(relationMasterId => {
return fetch(`${localStorage.dataHost}/pbf/${relationMasterId}`)
.then(response => response.ok ? response.arrayBuffer() : Promise.reject())
.then(arrayBuffer => {
return new Promise((resolve, reject) => {
const data = []
const push = (type) => (node) => data.push({type, ...node})
const pushWay = (type) => (n) => {
const {nodeRefs, ...rest} = n
data.push({type, nodes: nodeRefs, ...rest})
}
pbfParser.parse({
buffer: arrayBuffer,
endDocument: () => resolve(data),
bounds: push('bounds'),
node: push('node'),
way: pushWay('way'),
relation: push('relation'),
error: (msg) => reject(msg)
});
}).then((content) => {
const blob = osmtogeojson({
"version": 0.6,
"generator": "Overpass API",
"osm3s": {
"timestamp_osm_base": "",
"copyright": ""
},
"elements": content
}, {flatProperties: false})
L.geoJSON(blob, {
style: _ => ({color: findColor(blob, '#31302f')}),
pointToLayer: feature => L.featureGroup([
L.circleMarker(L.latLng(latlng(feature.geometry.coordinates)), {
color: findColor(blob),
fillColor: '#e2e2e2',
fill: true,
fillOpacity: 1,
radius: 5
}).bindTooltip(feature.properties.tags.name),
]),
onEachFeature: (feature, layer) => {
const {id, properties} = feature
const {tags, relations} = properties
layer.bindPopup(dumpProps({id, ...tags, relations})
.join('<br>'))
},
filter: (f) => (f.type === 'node' && f.properties.tags.name || f.type !== 'node')
}).addTo(map)
})
})
})
})
})
}
}
new Button({label: 'subway', click: geojson('subway')}).addTo(map)
new Button({label: 'tram', click: geojson('tram')}).addTo(map)
new Button({label: 'bus', click: geojson('bus')}).addTo(map)
new Button({label: 'train', click: geojson('train')}).addTo(map)
L.control.zoom({position: 'topright'}).addTo(map)
let hashes = new Set()
fetch(`${localStorage.dataHost}/geohashes.csv`)
.then(response => response.ok && response.text() || Promise.reject())
.then(text => {
text.split('\n').map(line => {
hashes.add(line.split(':')[0])
})
})
.catch(() => console.error('failed to get geohashes.csv, override localStorage.dataHost'))
let rectangle
function highlight(bounds, color) {
if (bounds) {
if (!rectangle) {
rectangle = L.rectangle(bounds).addTo(map)
} else {
rectangle.setBounds(bounds)
}
}
rectangle.setStyle({
color : color,
fill: false,
})
}
map.on('moveend load', () => {
try {
let center = map.getCenter();
let geohash = getCenterGeohash(center);
let bounds = latlonGeohash.bounds(geohash);
let ll1 = L.latLng(bounds.ne.lat, bounds.ne.lon);
let ll2 = L.latLng(bounds.sw.lat, bounds.sw.lon);
let p1 = map.latLngToLayerPoint(ll2);
let p2 = map.latLngToLayerPoint(ll1);
const diag = Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2))
if (diag < 60) {
document.querySelector('.shouldZoom').style.display = 'block'
} else {
document.querySelector('.shouldZoom').style.display = 'none'
}
highlight(L.latLngBounds(ll1, ll2), hashes.has(geohash) ? '#8BFF6B' : '#FF8580')
} catch (e) {
console.error(e)
}
})
})
</script>
</body>
</html>