|
| 1 | +<template> |
| 2 | + <div class="person-data-map" ref="mapElement" /> |
| 3 | +</template> |
| 4 | + |
| 5 | +<script setup lang="ts"> |
| 6 | + import type { TrialPlus } from '@/plugins/types/client' |
| 7 | + import { coreStore } from '@/stores/app' |
| 8 | + import L, { type Polygon, type Map, type TileLayer } from 'leaflet' |
| 9 | + import 'leaflet/dist/leaflet.css' |
| 10 | + import iconRetinaUrl from 'leaflet/dist/images/marker-icon-2x.png' |
| 11 | + import iconUrl from 'leaflet/dist/images/marker-icon.png' |
| 12 | + import shadowUrl from 'leaflet/dist/images/marker-shadow.png' |
| 13 | + import { categoricalColors } from '@/plugins/color' |
| 14 | + import type { PersonLocationData } from '@/pages/visualization/trial-statistics.vue' |
| 15 | + import { mapAreaTypeMap } from '@/plugins/constants' |
| 16 | +
|
| 17 | + // Set the leaflet marker icon |
| 18 | + // @ts-ignore |
| 19 | + delete L.Icon.Default.prototype._getIconUrl |
| 20 | + L.Icon.Default.mergeOptions({ |
| 21 | + iconRetinaUrl: iconRetinaUrl, |
| 22 | + iconUrl: iconUrl, |
| 23 | + shadowUrl: shadowUrl, |
| 24 | + }) |
| 25 | +
|
| 26 | + const compProps = defineProps<{ |
| 27 | + trial: TrialPlus |
| 28 | + personData: { [index: string]: PersonLocationData } |
| 29 | + overall: PersonLocationData |
| 30 | + areaType: string |
| 31 | + }>() |
| 32 | +
|
| 33 | + const store = coreStore() |
| 34 | +
|
| 35 | + const mapElement = useTemplateRef('mapElement') |
| 36 | + let themeLayer: TileLayer |
| 37 | + let map: Map |
| 38 | + let polygons: Polygon[] = [] |
| 39 | +
|
| 40 | + watch(() => store.storeIsDarkMode, async () => updateThemeLayer()) |
| 41 | +
|
| 42 | + function initMap () { |
| 43 | + if (!mapElement.value) { |
| 44 | + return |
| 45 | + } |
| 46 | +
|
| 47 | + map = L.map(mapElement.value) |
| 48 | + map.setView([22.5937, 2.1094], 3) |
| 49 | +
|
| 50 | + themeLayer = L.tileLayer(`//services.arcgisonline.com/arcgis/rest/services/Canvas/${store.storeIsDarkMode ? 'World_Dark_Gray_Base' : 'World_Light_Gray_Base'}/MapServer/tile/{z}/{y}/{x}`, { |
| 51 | + id: store.storeIsDarkMode ? 'Esri Dark Gray Base' : 'Esri Light Gray Base', |
| 52 | + attribution: 'Esri, HERE, Garmin, FAO, NOAA, USGS, © OpenStreetMap contributors, and the GIS User Community', |
| 53 | + maxZoom: 21, |
| 54 | + maxNativeZoom: 15, |
| 55 | + }) |
| 56 | +
|
| 57 | + const openstreetmap = L.tileLayer('//tile.openstreetmap.org/{z}/{x}/{y}.png', { |
| 58 | + id: 'OpenStreetMap', |
| 59 | + attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', |
| 60 | + maxZoom: 21, |
| 61 | + maxNativeZoom: 19, |
| 62 | + }) |
| 63 | +
|
| 64 | + // Add an additional satellite layer |
| 65 | + const satellite = L.tileLayer('//server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', { |
| 66 | + id: 'Esri WorldImagery', |
| 67 | + attribution: 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community', |
| 68 | + maxZoom: 21, |
| 69 | + maxNativeZoom: 19, |
| 70 | + }) |
| 71 | +
|
| 72 | + switch (store.storeMapLayer) { |
| 73 | + case 'theme': { |
| 74 | + map.addLayer(themeLayer) |
| 75 | + break |
| 76 | + } |
| 77 | + case 'satellite': { |
| 78 | + map.addLayer(satellite) |
| 79 | + break |
| 80 | + } |
| 81 | + default: { |
| 82 | + map.addLayer(openstreetmap) |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | +
|
| 87 | + const baseMaps = { |
| 88 | + 'Theme-based': themeLayer, |
| 89 | + OpenStreetMap: openstreetmap, |
| 90 | + 'Esri WorldImagery': satellite, |
| 91 | + } |
| 92 | +
|
| 93 | + map.on('baselayerchange', e => { |
| 94 | + switch (e.name) { |
| 95 | + case 'Theme-based': { |
| 96 | + store.setMapLayer('theme') |
| 97 | + break |
| 98 | + } |
| 99 | + case 'OpenStreetMap': { |
| 100 | + store.setMapLayer('osm') |
| 101 | + break |
| 102 | + } |
| 103 | + case 'Esri WorldImagery': { |
| 104 | + store.setMapLayer('satellite') |
| 105 | + break |
| 106 | + } |
| 107 | + } |
| 108 | + }) |
| 109 | +
|
| 110 | + L.control.layers(baseMaps).addTo(map) |
| 111 | +
|
| 112 | + // Disable zoom until focus gained, disable when blur |
| 113 | + map.scrollWheelZoom.disable() |
| 114 | + map.on('focus', () => map.scrollWheelZoom.enable()) |
| 115 | + map.on('blur', () => map.scrollWheelZoom.disable()) |
| 116 | +
|
| 117 | + nextTick(() => updateData()) |
| 118 | + } |
| 119 | +
|
| 120 | + async function updateData () { |
| 121 | + if (polygons) { |
| 122 | + polygons.forEach(p => map.removeLayer(p)) |
| 123 | + polygons = [] |
| 124 | + } |
| 125 | +
|
| 126 | + const bounds = new L.LatLngBounds([]) |
| 127 | +
|
| 128 | + if (compProps.overall && compProps.overall.bounds.length > 0) { |
| 129 | + const polygon = L.polygon(compProps.overall.bounds.map(ll => [ll.lat || 0, ll.lng || 0]), undefined) |
| 130 | +
|
| 131 | + // @ts-ignore |
| 132 | + polygon.getLatLngs().forEach(point => bounds.extend(point)) |
| 133 | +
|
| 134 | + const areaType = mapAreaTypeMap[compProps.areaType] |
| 135 | + const area = areaType?.convert(compProps.overall.area) || 0 |
| 136 | + const displayValue = `Overall: ${area.toLocaleString()}` |
| 137 | + polygon.bindTooltip(`${displayValue} ${areaType?.unit}`, { |
| 138 | + permanent: true, |
| 139 | + direction: 'center', |
| 140 | + sticky: false, |
| 141 | + }) |
| 142 | +
|
| 143 | + polygon.addTo(map) |
| 144 | + polygons.push(polygon) |
| 145 | + } |
| 146 | +
|
| 147 | + if (compProps.personData) { |
| 148 | + Object.keys(compProps.personData).forEach(p => { |
| 149 | + if (compProps.personData[p] && compProps.personData[p].bounds.length > 0) { |
| 150 | + const personIndex = compProps.trial.people.findIndex(pp => pp.id === p) |
| 151 | + const personObject = compProps.trial.people[personIndex] |
| 152 | +
|
| 153 | + const polygon = L.polygon(compProps.personData[p].bounds.map(ll => [ll.lat || 0, ll.lng || 0]), personObject ? { color: categoricalColors.D3schemeCategory10[personIndex % categoricalColors.D3schemeCategory10.length] } : undefined) |
| 154 | +
|
| 155 | + // @ts-ignore |
| 156 | + polygon.getLatLngs().forEach(point => bounds.extend(point)) |
| 157 | +
|
| 158 | + const areaType = mapAreaTypeMap[compProps.areaType] |
| 159 | + const area = areaType?.convert(compProps.personData[p].area) || 0 |
| 160 | + const displayValue = `${personObject ? personObject.name : 'Overall'}: ${area.toLocaleString()}` |
| 161 | + polygon.bindTooltip(`${displayValue} ${areaType?.unit}`, { |
| 162 | + permanent: false, |
| 163 | + direction: 'top', |
| 164 | + sticky: true, |
| 165 | + }) |
| 166 | +
|
| 167 | + polygon.addTo(map) |
| 168 | + polygons.push(polygon) |
| 169 | + } |
| 170 | + }) |
| 171 | + } |
| 172 | +
|
| 173 | + if (bounds.isValid()) { |
| 174 | + const size = map.getSize() |
| 175 | + map.fitBounds(bounds, { padding: [size.x / 4, size.y / 4] }) |
| 176 | + } |
| 177 | + } |
| 178 | +
|
| 179 | + function updateThemeLayer () { |
| 180 | + if (themeLayer) { |
| 181 | + themeLayer.setUrl(`//services.arcgisonline.com/arcgis/rest/services/Canvas/${store.storeIsDarkMode ? 'World_Dark_Gray_Base' : 'World_Light_Gray_Base'}/MapServer/tile/{z}/{y}/{x}`) |
| 182 | + } |
| 183 | + } |
| 184 | +
|
| 185 | + function invalidateSize () { |
| 186 | + nextTick(() => map?.invalidateSize()) |
| 187 | + } |
| 188 | +
|
| 189 | + defineExpose({ |
| 190 | + invalidateSize, |
| 191 | + }) |
| 192 | +
|
| 193 | + onMounted(() => initMap()) |
| 194 | +
|
| 195 | + watch(() => compProps.overall, async () => updateData()) |
| 196 | + watch(() => compProps.personData, async () => updateData()) |
| 197 | + watch(() => compProps.areaType, async () => updateData()) |
| 198 | +</script> |
| 199 | + |
| 200 | +<style scoped> |
| 201 | +.person-data-map { |
| 202 | + height: 50vh; |
| 203 | +} |
| 204 | +</style> |
0 commit comments