-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
384 lines (377 loc) · 13.6 KB
/
script.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
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
const mapboxgl = require('mapbox-gl')
const area = require('@turf/area').default
const turf = require('@turf/helpers')
const token = process.env.TOKEN
class MapboxMap {
constructor (HTMLelement) {
this.HTMLContainer = document.querySelector(HTMLelement)
this.loaded = false
this.map = null
this.markerTitle = ''
this.couches = new Array()
this.layerColours = {
building: {
'fill-color': '#f08',
'fill-opacity': 0.4
},
city: {
'fill-outline-color': '#889be8',
'fill-opacity': 0.35,
},
sheet: {
'fill-color': '#f6ff9e',
'fill-opacity': 0.1
},
known: {
'fill-color': '#f6ff9e',
'fill-opacity': 0.2
},
area: {
'fill-color': '#5b602b',
'fill-opacity': 0.3
},
section: {
'fill-color': '#797a73',
'fill-opacity': 0.15
}
}
}
load() {
if (!this.loaded) {
this.loaded = true
mapboxgl.accessToken = token
this.map = new mapboxgl.Map({
container: this.HTMLContainer,
center: [2.5377, 46.718],
zoom: 4,
style: 'mapbox://styles/mgcarre/cj9suhpr72dkz2socqotoy2p8'
});
this.map.addControl(new mapboxgl.NavigationControl());
this.map.addControl(new mapboxgl.FullscreenControl());
this.addButton()
this.map.on('mousemove', (event) => this.evented(event))
this.map.on('touchmove', (event) => this.evented(event))
this.map.on('contextmenu', (event) => {
let ici = this.map.queryRenderedFeatures(event.point)
let target = document.querySelector('#surface-section')
let sortie = new Array()
ici.forEach(calque => {
if (calque.layer.id.indexOf('cadastre-calque') > -1) {
let filtered = calque.layer.id.substr('cadastre-calque-'.length)
switch (filtered) {
case 'area':
sortie.push(`<h4>Parcelle :</h4>
Préfixe : ${calque.properties.prefixe}
Section : ${calque.properties.section}
Numéro : ${calque.properties.numero}
Surface : ${calque.properties.contenance}`)
break;
case 'known':
sortie.push(`<h4>Lieu-dit :</h4>${calque.properties.nom}`)
break
case 'city':
sortie.push(`<h4>Commune :</h4> ${calque.properties.nom}`)
break
case 'sheet':
sortie.push(`</h4>Feuille :</h4>
Préfixe : ${calque.properties.prefixe}
Section : ${calque.properties.section}
Numéro : ${calque.properties.numero}
Qualité : ${calque.properties.qualite}
Echelle : ${calque.properties.echelle}
Créée le : ${calque.properties.created}
Dernière mise à jour : ${calque.properties.updated}`)
break
case 'section':
sortie.push(`</h4>Section :</h4>
Préfixe : ${calque.properties.prefixe}
Code : ${calque.properties.code}
Créée le : ${calque.properties.created}
Dernière mise à jour : ${calque.properties.updated}`)
break
default:
sortie.push(`Les informations sont déjà affichées à l'écran.`)
break;
}
}
})
console.log(sortie)
new mapboxgl.Popup()
.setLngLat(event.lngLat)
.setHTML(sortie.join('<br>'))
.addTo(this.map)
})
}
}
addButton() {
let div = document.createElement('div')
div.style.top = 0
div.style.right = 0
div.style.overflow = 'auto'
div.style.position = 'absolute'
let btn = document.createElement('button')
btn.classList.add('btn', 'btn-error')
btn.innerText = "Efface carte"
btn.addEventListener('click', () => this.removePolygons())
div.appendChild(btn)
document.body.appendChild(div)
}
evented(event) {
let ici = this.map.queryRenderedFeatures(event.point)
let batiment = ici.find(elem => elem.layer.id == "cadastre-calque-building")
let parcelle = ici.find(elem => elem.layer.id == "cadastre-calque-area")
let target = document.querySelector('#surface-section')
target.innerHTML = ''
let sortie = new Array()
if (batiment || parcelle) {
if (batiment) {
let polygon = turf.polygon(batiment.geometry.coordinates)
let aire = area(polygon)
console.log(aire)
sortie.push(`Bât : ${turf.round(aire, 2)}m<sup>2</sup> au sol.`)
}
if (parcelle) {
let polygon = turf.polygon(parcelle.geometry.coordinates)
let aire = area(polygon)
console.log(aire)
let surface = aire > 10000 ? { unit: "ha", value: (aire / 10000), sup: '' } : { unit: 'm', value: aire, sup: 2 }
sortie.push(`Parc. : ${turf.round(surface.value * 0.99765, 3)}${surface.unit}<sup>${surface.sup}</sup> au sol.`)
}
sortie.forEach(elem => {
let p = document.createElement('p')
p.classList.add('section-content')
p.innerHTML = elem
target.appendChild(p)
})
} else {
target.innerHTML = '<p class="surface-content">Survolez un batiment ou une parcelle...</p>'
}
}
setMarkerTitle(title) {
this.markerTitle = title
}
removePolygons() {
this.couches.forEach(couche => {
this.map.removeLayer(couche.layer)
this.map.removeSource(couche.source)
})
this.couches = new Array()
console.info('Polygones supprimes')
}
addPolygons(feature, layer) {
this.removePolygons()
let source = 'cadastre-source-' + layer
let calque = 'cadastre-calque-' + layer
this.map.addSource(source, {
type: "geojson",
data: feature,
clusterMaxZoom: 14,
clusterRadius: 50
})
this.map.addLayer({
id: calque,
type: "fill",
source: source,
paint: this.layerColours[layer]
})
this.couches.push({ source: source, layer: calque })
}
removeMarkers() {
document.querySelectorAll('.mapboxgl-marker').forEach(element => element.remove())
}
toggleVisibility() {
this.HTMLContainer.classList.toggle('_nodisplay')
this.map.resize()
}
drawMarker(geom) {
let marker = new mapboxgl.Marker()
if (this.markerTitle !== '') {
let popup = new mapboxgl.Popup()
.setText(this.markerTitle)
marker.setPopup(popup)
}
marker.setLngLat(geom.coordinates).addTo(this.map)
setTimeout(() => {
this.map.flyTo({ center: geom.coordinates, zoom: 12 })
}, 2000);
}
}
class Cadastre {
constructor (idVille, options = {}) {
this.id = idVille
this.options = Object.assign({}, {
building: true,
city: false,
sheet: false,
known: false,
area: false,
section: false
}, options)
this.types = {
building: 0,
city: 1,
sheet: 2,
known: 3,
area: 4,
section: 5
}
this.loaded = {
building: null,
city: null,
sheet: null,
known: null,
area: null,
section: null
}
this.promises = new Array()
}
getCount() {
let options = Object.keys(this.options)
return options.reduce(elem => this.options[elem] === true, 0)
}
async getData(type) {
let res = await fetch(`/geom/${this.id}/${this.types[type]}`)
if (res.status === 200) {
return await res.json()
} else {
return null
}
}
}
const Cmap = new MapboxMap('#map')
Cmap.load()
Vue.component('todo-item', {
props: ['todo'],
template: '<span class="chip">{{ todo.label }}</span>'
})
const resultats = new Vue({
el: '#resultats',
data: {
myList: []
},
methods: {
select: function (id) {
if (id === 0) {
return
}
let obj = (this.myList.filter(elem => elem.id === id))[0]
let cadastre = new Cadastre(id, app.options)
if (app.getOptions().length === 0) {
alert('Pensez à sélectionner des données à afficher puis cliquer à nouveau sur la ville dans la liste "Résultats"')
} else {
app.getOptions().forEach(element => {
cadastre.getData(element).then(res => {
Cmap.addPolygons(res, element)
})
})
}
Cmap.setMarkerTitle(obj.label)
Cmap.drawMarker(obj.coords)
this.clear()
},
clear: function () {
this.myList = []
}
}
})
const app = new Vue({
el: '#app',
data: {
value: '',
error: {
active: false,
message: 'Une erreur est survenue, veuillez réessayer.'
},
isLoading: false,
options: {
building: true,
city: true,
sheet: false,
area: false,
known: false,
section: false
}
},
methods: {
sub: function () {
this.isLoading = true
let restrict = ['lyon', 'marseille', 'paris']
if (restrict.indexOf(this.value.toLowerCase()) > -1) {
this.error.active = true
this.error.message = `Les villes suivantes ne permettent pas la recherche inversée : ${restrict.join(', ')}. Privilégiez la recherche par adresse postale ou par code postal.`
this.isLoading = false
throw new Error(this.error.message)
}
if (this.value !== '' && this.value.length !== 0) {
recherche(this.value).then(res => {
resultats.clear()
this.error.active = false
for (let feature of res.features) {
let props = feature.properties
resultats.myList.push({
id: props.id,
label: `${props.label} (${props.postcode})`,
coords: feature.geometry,
hidden: false
})
}
this.isLoading = false
}).catch(err => {
console.error(err)
this.error.active = true
this.isLoading = false
})
} else {
this.error.active = true
this.isLoading = false
}
},
getOptions: function () {
let options = Object.keys(this.options)
return options.filter(elem => this.options[elem] === true)
}
}
})
const baseUrl = 'https://api-adresse.data.gouv.fr/'
/**
* URL de recherche texte
*
* @param {string} lookup
* @returns {string} - Url
*/
const searchAdresse = (lookup) => `${baseUrl}search/?q=${lookup}&limit=20`
/**
* URL de recherche par coordonées
*
* @param {any} {lat, lon}
* @returns {string} - Url
*/
const searchCoords = ({ lat, lng }) => `${baseUrl}reverse/?lon=${lng}&lat=${lat}`
const recherche = (q) => {
if (typeof q === 'string') {
return fetch(searchAdresse(q)).then(res => res.json())
} else {
return fetch(searchCoords(q)).then(res => res.json())
}
}
document.querySelector('#geolocateMe').addEventListener('click', (e) => {
e.preventDefault()
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((pos) => {
console.log(pos)
fetch(searchCoords({ lat: pos.coords.latitude, lng: pos.coords.longitude })).then(rep => rep.json()).then(res => {
resultats.myList = new Array()
for (let feature of res.features) {
let props = feature.properties
resultats.myList.push({
id: props.id,
label: `${props.city} (${props.postcode})`,
coords: feature.geometry
})
}
})
}, (err) => {
console.error(err)
})
}
})