Files
PRISM/frontend/src/components/ResultMap.vue
T
2026-11-26 15:43:56 +01:00

251 lines
6.7 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts">
import { setWorkerUrl } from 'maplibre-gl'
import workerUrl from 'maplibre-gl/dist/maplibre-gl-worker.mjs?url'
// MapLibre cherche son worker à côté de son propre module, ce que le regroupement de Vite
// casse : sans worker, aucune source raster (couches WMS) ne se charge.
setWorkerUrl(workerUrl)
</script>
<script setup lang="ts">
import { Map as MapLibreMap, Marker, NavigationControl, ScaleControl } from 'maplibre-gl'
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
import type { Assessment } from '@/api/types'
const props = defineProps<{ location: Assessment['location'] | null }>()
const WALONMAP_WMS = 'https://geoservices.wallonie.be/arcgis/services'
// Fond OpenStreetMap Belgique (serveur de la communauté OpenStreetMap Belgique). Vérifier ses
// conditions d'utilisation avant la mise en production (specs, risque 12).
const OSM_TILES = 'https://tile.openstreetmap.be/osmbe/{z}/{x}/{y}.png'
const BELGIUM: [number, number] = [4.47, 50.5]
function wms(service: string, layers: string): string {
const params = new URLSearchParams({
service: 'WMS',
request: 'GetMap',
version: '1.3.0',
layers,
styles: '',
format: 'image/png',
transparent: 'true',
crs: 'EPSG:3857',
width: '256',
height: '256',
})
return `${WALONMAP_WMS}/${service}/MapServer/WMSServer?${params}&bbox={bbox-epsg-3857}`
}
const overlays = [
// Couches 1, 2 et 3 : chacune ne couvre qu'une plage d'échelles (1:25 000 et moins, entre
// 1:25 000 et 1:5 000, au-delà de 1:5 000).
{ id: 'flood', url: wms('EAU/ALEA_INOND', '1,2,3'), color: '#00b0f0' },
{ id: 'noise', url: wms('INDUSTRIES_SERVICES/BRUIT_MROAD_2017', '1'), color: '#e85b25' },
] as const
type OverlayId = (typeof overlays)[number]['id']
const container = ref<HTMLDivElement>()
const layersPanel = ref<HTMLFieldSetElement>()
const visible = ref<Record<OverlayId, boolean>>({ flood: true, noise: false })
let map: MapLibreMap | null = null
let marker: Marker | null = null
onMounted(() => {
const instance = new MapLibreMap({
container: container.value!,
center: BELGIUM,
zoom: 7,
attributionControl: { compact: false },
style: {
version: 8,
sources: {
osm: {
type: 'raster',
tiles: [OSM_TILES],
tileSize: 256,
maxzoom: 18,
attribution: '© OpenStreetMap contributors',
},
...Object.fromEntries(
overlays.map((o) => [
o.id,
{ type: 'raster', tiles: [o.url], tileSize: 256, attribution: '© SPW – WalOnMap' },
]),
),
},
layers: [
{ id: 'osm', type: 'raster', source: 'osm' },
...overlays.map((o) => ({
id: o.id,
type: 'raster' as const,
source: o.id,
paint: { 'raster-opacity': 0.6 },
layout: { visibility: visible.value[o.id] ? ('visible' as const) : ('none' as const) },
})),
],
},
})
instance.addControl(new NavigationControl({ showCompass: false }), 'top-right')
instance.addControl(new ScaleControl({ unit: 'metric' }), 'bottom-left')
map = instance
showLocation()
})
onBeforeUnmount(() => {
map?.remove()
map = null
})
function showLocation() {
if (!map || !props.location) return
const lngLat: [number, number] = [props.location.lon, props.location.lat]
marker?.remove()
marker = new Marker({ color: '#e85b25' }).setLngLat(lngLat).addTo(map)
// La mise en page peut avoir changé depuis la création de la carte.
map.resize()
// Décale le centre sous le panneau des couches pour que le marqueur reste visible.
const top = (layersPanel.value?.offsetHeight ?? 0) + 12
map.jumpTo({ center: lngLat, zoom: 15, padding: { top, bottom: 0, left: 0, right: 0 } })
}
function toggle(id: OverlayId) {
visible.value[id] = !visible.value[id]
map?.setLayoutProperty(id, 'visibility', visible.value[id] ? 'visible' : 'none')
}
watch(() => props.location, showLocation, { flush: 'post' })
</script>
<template>
<div class="result-map prism-card" :class="{ expanded: !location }">
<div ref="container" class="map" role="region" :aria-label="$t('map.label')"></div>
<fieldset ref="layersPanel" class="map-panel layers">
<legend>{{ $t('map.layers') }}</legend>
<div class="form-check">
<input id="layer-osm" class="form-check-input" type="checkbox" checked disabled />
<label class="form-check-label" for="layer-osm">{{ $t('map.basemap') }}</label>
</div>
<div v-for="overlay in overlays" :key="overlay.id" class="form-check">
<input
:id="`layer-${overlay.id}`"
class="form-check-input"
type="checkbox"
:checked="visible[overlay.id]"
@change="toggle(overlay.id)"
/>
<label class="form-check-label" :for="`layer-${overlay.id}`">
{{ $t(`map.${overlay.id}`) }}
</label>
</div>
</fieldset>
<div class="map-panel legend">
<strong>{{ $t('map.legend') }}</strong>
<div>
<span class="swatch round" style="background: #e85b25"></span>{{ $t('map.patient') }}
</div>
<template v-for="overlay in overlays" :key="overlay.id">
<div v-if="visible[overlay.id]">
<span class="swatch" :style="{ background: overlay.color }"></span>
{{ $t(`map.${overlay.id}`) }}
</div>
</template>
</div>
</div>
</template>
<style scoped>
.result-map {
position: relative;
overflow: hidden;
height: 720px;
}
/* Avant la première analyse, la carte occupe toute la hauteur disponible. */
.result-map.expanded {
height: max(420px, calc(100vh - 330px));
}
@media (min-width: 992px) {
.result-map {
position: sticky;
top: 24px;
}
}
@media (max-width: 991.98px) {
.result-map,
.result-map.expanded {
height: 240px;
}
.legend {
display: none;
}
}
.map {
position: absolute;
inset: 0;
}
.map-panel {
position: absolute;
left: 12px;
z-index: 2;
background: var(--prism-surface);
border: 1px solid var(--prism-border);
border-radius: 8px;
padding: 10px 12px;
font-size: 0.6875rem;
box-shadow: 0 2px 8px rgb(9 41 51 / 10%);
}
.layers {
top: 12px;
}
.layers legend {
float: none;
font-size: 0.75rem;
font-weight: 700;
color: var(--prism-navy);
margin-bottom: 4px;
}
.form-check {
margin-bottom: 2px;
}
.form-check-input:checked {
background-color: var(--prism-orange);
border-color: var(--prism-orange);
}
.legend {
bottom: 40px;
}
.legend strong {
display: block;
font-size: 0.75rem;
color: var(--prism-navy);
margin-bottom: 4px;
}
.swatch {
display: inline-block;
width: 10px;
height: 10px;
margin-right: 8px;
border-radius: 2px;
vertical-align: middle;
}
.swatch.round {
border-radius: 50%;
}
</style>