-
Notifications
You must be signed in to change notification settings - Fork 30
/
explore.js
150 lines (134 loc) · 4.63 KB
/
explore.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
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
import { Socket } from 'phoenix'
import Leaflet from 'leaflet'
import { LeafletLayer } from 'deck.gl-leaflet'
import { ScatterplotLayer, GeoJsonLayer } from '@deck.gl/layers'
import { MapView } from '@deck.gl/core'
const socket = new Socket('/socket', { params: { token: window.userToken } })
socket.connect()
const channel = socket.channel('explore', {})
channel.join()
.receive('ok', resp => { console.log('Joined successfully', resp) })
.receive('error', resp => { console.log('Unable to join', resp) })
const Mapbox = {
url: 'https://api.mapbox.com/styles/v1/istopopoki/ckg98kpoc010h19qusi9kxcct/tiles/256/{z}/{x}/{y}?access_token={accessToken}',
accessToken: 'pk.eyJ1IjoiaXN0b3BvcG9raSIsImEiOiJjaW12eWw2ZHMwMGFxdzVtMWZ5NHcwOHJ4In0.VvZvyvK0UaxbFiAtak7aVw',
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors <a href="https://spdx.org/licenses/ODbL-1.0.html">ODbL</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 20
}
const metropolitanFranceBounds = [[51.1, -4.9], [41.2, 9.8]]
const map = Leaflet.map('map', { renderer: Leaflet.canvas() }).fitBounds(metropolitanFranceBounds)
Leaflet.tileLayer(Mapbox.url, {
accessToken: Mapbox.accessToken,
attribution: Mapbox.attribution,
maxZoom: Mapbox.maxZoom
}).addTo(map)
const visibility = { gtfsrt: true, bnlc: false }
function prepareLayer (layerId, layerData) {
return new ScatterplotLayer({
id: layerId,
data: layerData,
pickable: true,
opacity: 1,
stroked: false,
filled: true,
radiusMinPixels: 4,
radiusMaxPixels: 10,
lineWidthMinPixels: 1,
visible: visibility.gtfsrt,
getPosition: d => {
return [d.position.longitude, d.position.latitude]
},
getRadius: d => 1000,
getFillColor: d => [0, 150, 136, 150],
getLineColor: d => [0, 150, 136]
})
}
const deckGLLayer = new LeafletLayer({
views: [
new MapView({
repeat: true
})
],
layers: [],
getTooltip
})
map.addLayer(deckGLLayer)
function getTooltip ({ object, layer }) {
if (object) {
if (layer.id === 'bnlc-layer') {
return { html: `<strong>Aire de covoiturage</strong><br>${object.properties.nom_lieu}` }
} else {
return { html: `<strong>Position temps-réel</strong><br>transport_resource: ${object.transport.resource_id}<br>id: ${object.vehicle.id}` }
}
}
}
// internal dictionary were all layers are stored
const layers = { gtfsrt: {}, bnlc: undefined }
function getLayers (layers) {
const layersArray = Object.values(layers.gtfsrt)
layersArray.push(layers.bnlc)
return layersArray
}
channel.on('vehicle-positions', payload => {
if (payload.error) {
console.log(`Resource ${payload.resource_id} failed to load`)
} else {
layers.gtfsrt[payload.resource_id] = prepareLayer(payload.resource_id, payload.vehicle_positions)
deckGLLayer.setProps({ layers: getLayers(layers) })
}
})
// handle GTFS-RT toggle
const gtfsrtCheckbox = document.getElementById('gtfs-rt-check')
gtfsrtCheckbox.addEventListener('change', (event) => {
if (event.currentTarget.checked) {
visibility.gtfsrt = true
} else {
visibility.gtfsrt = false
for (const key in layers.gtfsrt) {
layers.gtfsrt[key] = prepareLayer(key, [])
}
deckGLLayer.setProps({ layers: getLayers(layers) })
}
})
let bnlcGeoJSON
const checkbox = document.getElementById('bnlc-check')
checkbox.addEventListener('change', (event) => {
if (event.currentTarget.checked) {
visibility.bnlc = true
if (bnlcGeoJSON) {
updateBNLCLayer(bnlcGeoJSON)
} else {
fetch('/api/geo-query?data=bnlc')
.then(data => data.json())
.then(geojson => {
updateBNLCLayer(geojson)
})
}
} else {
visibility.bnlc = false
updateBNLCLayer(bnlcGeoJSON)
}
})
function updateBNLCLayer (geojson) {
const geojsonLayer = createBNLCLayer(geojson)
layers.bnlc = geojsonLayer
deckGLLayer.setProps({ layers: getLayers(layers) })
}
function createBNLCLayer (geojson) {
return new GeoJsonLayer({
id: 'bnlc-layer',
data: geojson,
pickable: true,
stroked: false,
filled: true,
extruded: true,
pointType: 'circle',
getFillColor: [255, 174, 0, 100],
getPointRadius: 1000,
pointRadiusUnits: 'meters',
pointRadiusMinPixels: 2,
pointRadiusMaxPixels: 10,
visible: visibility.bnlc
})
}
export default socket