Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Add initial Osmose validation layer, some additional refactoring

Russ Garrett6b26c1d08c04
diff --git a/web/locales/en/translation.json b/web/locales/en/translation.json
index 26d600e..47f2b77 100644
--- a/web/locales/en/translation.json
+++ b/web/locales/en/translation.json
@@ -192,15 +192,20 @@
"webgl": "<p>Your browser may have performance or functionality issues with Open Infrastructure Map.</p><p><a href=\"http://webglreport.com\">WebGL</a> with hardware acceleration is required for this site to perform well.</p><p>If your browser supports WebGL, you may need to disable browser fingerprinting protection for this site.</p>"
},
"layers": {
+ "background": "Background",
+ "heatmaps": "Heatmaps",
+ "infrastructure": "Infrastructure",
+ "validation": "Validation",
"nighttime-lights": "Nighttime Lights",
"power": "Power",
- "heatmap": "Solar Generation",
+ "solar-generation": "Solar Generation",
"telecoms": "Telecoms",
"petroleum": "Oil & Natural Gas",
"water": "Water",
"labels": "Labels",
"title": "Layers",
- "other-pipelines": "Other Pipelines"
+ "other-pipelines": "Other Pipelines",
+ "osmose-power": "Osmose (Power)"
},
"construction": "under construction",
"disused": "disused"
diff --git a/web/src/click-router.ts b/web/src/click-router.ts
new file mode 100644
index 0000000..d451a3d
--- /dev/null
+++ b/web/src/click-router.ts
@@ -0,0 +1,81 @@
+import { LngLat, MapGeoJSONFeature, MapMouseEvent } from 'maplibre-gl'
+import { LayerSpecificationWithZIndex } from './style/types'
+
+export type ClickHandler = (feature: MapGeoJSONFeature, location: LngLat, event: MapMouseEvent) => void
+
+type LayerId = string
+
+/*
+ * Handles clicks on objects in a MapLibre map and routes them to exactly one registered handler.
+ * Also deals with setting the mouse cursor to a pointer when hovering over clickable features.
+ *
+ * Clicking is disabled at lower zooms to prevent accidental clicks.
+ */
+export class ClickRouter {
+ map: maplibregl.Map
+ layerPriorities: { [layerId: LayerId]: number | undefined } = {}
+ handlers: { [layerId: LayerId]: ClickHandler } = {}
+ minZoom: number = 0
+
+ constructor(map: maplibregl.Map, layers: LayerSpecificationWithZIndex[], minZoom: number = 6) {
+ this.minZoom = minZoom
+ this.map = map
+ for (const layer of layers) {
+ this.layerPriorities[layer.id] = layer.zorder
+ }
+
+ // Always reset the cursor on zoom
+ map.on('zoom', () => this.clearCursor())
+ }
+
+ registerHandler(layerIds: LayerId | LayerId[], handler: ClickHandler) {
+ if (!Array.isArray(layerIds)) {
+ layerIds = [layerIds]
+ }
+ for (const layerId of layerIds) {
+ this.handlers[layerId] = handler
+ }
+ }
+
+ private async handleClick(e: MapMouseEvent & { features?: MapGeoJSONFeature[] | undefined }) {
+ if (this.map.getZoom() < this.minZoom) return
+ if (e.features === undefined || e.features.length === 0) return
+
+ const data = []
+ for (const feature of e.features) {
+ const layerId = feature.layer.id
+ data.push({
+ priority: this.layerPriorities[layerId] || 0,
+ feature: feature,
+ handler: this.handlers[layerId]
+ })
+ }
+
+ data.sort((a, b) => b.priority - a.priority)
+
+ const handler = data[0].handler
+ this.clearCursor()
+ handler(data[0].feature, e.lngLat, e)
+ }
+
+ private setCursor() {
+ if (this.map.getZoom() < this.minZoom) return
+ this.map.getCanvas().style.cursor = 'pointer'
+ }
+
+ private clearCursor() {
+ if (this.map.getZoom() < this.minZoom) return
+ this.map.getCanvas().style.cursor = ''
+ }
+
+ private getLayers() {
+ return Object.keys(this.handlers).sort()
+ }
+
+ register() {
+ const layers = this.getLayers()
+ this.map.on('click', layers, (e) => this.handleClick(e))
+ this.map.on('mouseenter', layers, () => this.setCursor())
+ this.map.on('mouseleave', layers, () => this.clearCursor())
+ }
+}
diff --git a/web/src/infopopup.ts b/web/src/infopopup.ts
index 8ad9403..1c38f8e 100644
--- a/web/src/infopopup.ts
+++ b/web/src/infopopup.ts
@@ -1,12 +1,13 @@
import './infopopup.css'
import i18next, { t } from 'i18next'
-import maplibregl, { MapGeoJSONFeature, MapMouseEvent, Popup } from 'maplibre-gl'
+import maplibregl, { LngLat, MapGeoJSONFeature, Popup } from 'maplibre-gl'
import { titleCase } from 'title-case'
import { local_name_tags } from './l10n.ts'
import friendlyNames from './friendlynames.ts'
import friendlyIcons from './friendlyicons.ts'
import { el, mount, setChildren, RedomElement } from 'redom'
+import { ClickRouter } from './click-router.ts'
const hidden_keys = [
'osm_id',
@@ -205,32 +206,10 @@ class InfoPopup {
this.friendlyNames = friendlyNames()
}
- add(map: maplibregl.Map) {
+ add(map: maplibregl.Map, clickRouter: ClickRouter) {
this._map = map
- map.on('click', this.layers, (e) => {
- if (this._map.getZoom() > this.min_zoom) {
- this.popup(e)
- }
- })
-
- map.on('mouseenter', this.layers, () => {
- if (this._map.getZoom() > this.min_zoom) {
- map.getCanvas().style.cursor = 'pointer'
- }
- })
-
- map.on('mouseleave', this.layers, () => {
- if (this._map.getZoom() > this.min_zoom) {
- map.getCanvas().style.cursor = ''
- }
- })
-
- map.on('zoom', () => {
- if (this._map.getZoom() <= this.min_zoom) {
- this._map.getCanvas().style.cursor = ''
- }
- })
+ clickRouter.registerHandler(this.layers, (f, l) => this.popup(f, l))
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && this.popup_obj) {
@@ -415,23 +394,17 @@ class InfoPopup {
return content
}
- popup(e: MapMouseEvent & { features?: MapGeoJSONFeature[] | undefined }) {
+ popup(feature: MapGeoJSONFeature, location: LngLat) {
if (this.popup_obj && this.popup_obj.isOpen()) {
this.popup_obj.remove()
}
- this._map.getCanvas().style.cursor = ''
-
- if (e.features === undefined || e.features.length == 0) {
- return
- }
- const feature = e.features[0]
if (import.meta.env.DEV) {
- console.info('Clicked feature on layer', feature.layer.id, 'at', e.lngLat, '\n', feature.properties)
+ console.info('Clicked feature on layer', feature.layer.id, 'at', location, '\n', feature.properties)
}
this.popup_obj = new maplibregl.Popup()
- .setLngLat(e.lngLat)
+ .setLngLat(location)
.setDOMContent(this.popupHtml(feature))
.addTo(this._map)
.addClassName('oim-info')
diff --git a/web/src/openinframap.ts b/web/src/openinframap.ts
index c115281..3362b0d 100644
--- a/web/src/openinframap.ts
+++ b/web/src/openinframap.ts
@@ -2,7 +2,7 @@ import maplibregl from 'maplibre-gl'
import { t } from 'i18next'
import { mount } from 'redom'
-import { LayerSwitcher, URLHash, Layer } from '@russss/maplibregl-layer-switcher'
+import { LayerSwitcher, URLHash, Layer, LayerGroup } from '@russss/maplibregl-layer-switcher'
import EditButton from './edit-control.js'
import InfoPopup from './infopopup.js'
@@ -19,9 +19,12 @@ import style_oim_telecoms from './style/style_oim_telecoms.js'
import style_oim_petroleum from './style/style_oim_petroleum.js'
import style_oim_water from './style/style_oim_water.js'
import style_oim_other_pipelines from './style/style_oim_other_pipelines.js'
+import style_osmose from './style/style_osmose.js'
import { LayerSpecificationWithZIndex } from './style/types.js'
import { manifest } from 'virtual:render-svg'
+import { ValidationErrorPopup } from './validation-error-popup.js'
+import { ClickRouter } from './click-router.js'
export default class OpenInfraMap {
map?: maplibregl.Map
@@ -87,20 +90,29 @@ export default class OpenInfraMap {
const layer_switcher = new LayerSwitcher(
[
- new Layer('N', t('layers.nighttime-lights', 'Nighttime Lights'), 'black_marble', false),
- new Layer('P', t('layers.power', 'Power'), 'power_', true),
- new Layer('S', t('layers.heatmap', 'Solar Generation'), 'heatmap_', false),
- new Layer('T', t('layers.telecoms', 'Telecoms'), 'telecoms_', false),
- new Layer('O', t('layers.petroleum', 'Oil & Gas'), 'petroleum_', false),
- new Layer('I', t('layers.other-pipelines', 'Other Pipelines'), 'pipeline_', false),
- new Layer('W', t('layers.water', 'Water'), 'water_', false),
- new Layer('L', t('layers.labels', 'Labels'), 'place_', true)
+ new LayerGroup(t('layers.background'), [
+ new Layer('N', t('layers.nighttime-lights'), 'black_marble', false),
+ new Layer('L', t('layers.labels'), 'place_', true)
+ ]),
+ new LayerGroup(t('layers.heatmaps'), [
+ new Layer('S', t('layers.solar-generation'), 'heatmap_', false)
+ ]),
+ new LayerGroup(t('layers.infrastructure'), [
+ new Layer('P', t('layers.power'), 'power_', true),
+ new Layer('T', t('layers.telecoms'), 'telecoms_', false),
+ new Layer('O', t('layers.petroleum'), 'petroleum_', false),
+ new Layer('I', t('layers.other-pipelines'), 'pipeline_', false),
+ new Layer('W', t('layers.water'), 'water_', false)
+ ]),
+ new LayerGroup(t('layers.validation'), [
+ new Layer('E', t('layers.power'), 'osmose_errors_power', false)
+ ])
],
t('layers.title', 'Layers')
)
const url_hash = new URLHash(layer_switcher)
- map_style.layers = style_base.concat(oim_layers, style_labels())
+ map_style.layers = style_base.concat(oim_layers, ...style_osmose(), style_labels())
layer_switcher.setInitialVisibility(map_style)
@@ -115,6 +127,8 @@ export default class OpenInfraMap {
})
)
+ const clickRouter = new ClickRouter(map, map_style.layers)
+
const icon_ratio = Math.min(Math.round(window.devicePixelRatio), 2)
const icons = manifest[icon_ratio.toString()]
const loadedIcons = new Set<string>()
@@ -153,8 +167,10 @@ export default class OpenInfraMap {
new InfoPopup(
oim_layers.map((layer: { [x: string]: any }) => layer['id']),
6
- ).add(map)
+ ).add(map, clickRouter)
+ new ValidationErrorPopup(map, clickRouter)
+ clickRouter.register()
this.map = map
}
}
diff --git a/web/src/style/style.ts b/web/src/style/style.ts
index ab65589..8e5c7bd 100644
--- a/web/src/style/style.ts
+++ b/web/src/style/style.ts
@@ -73,6 +73,12 @@ const style: StyleSpecification = {
tiles: ['https://openinframap.org/map/other_pipeline/{z}/{x}/{y}.pbf'],
maxzoom: 17,
attribution: oim_attribution
+ },
+ osmose_errors_power: {
+ type: 'vector',
+ tiles: ['https://osmose.openstreetmap.fr/api/0.3/issues/{z}/{x}/{y}.mvt?tags=power'],
+ maxzoom: 17,
+ minzoom: 11
}
},
glyphs: '/fonts/{fontstack}/{range}.pbf',
diff --git a/web/src/style/style_osmose.ts b/web/src/style/style_osmose.ts
new file mode 100644
index 0000000..d7f46fa
--- /dev/null
+++ b/web/src/style/style_osmose.ts
@@ -0,0 +1,31 @@
+import { interpolate, zoom } from './stylehelpers.ts'
+import { LayerSpecificationWithZIndex } from './types.ts'
+
+export default function layers(): LayerSpecificationWithZIndex[] {
+ return [
+ {
+ id: 'osmose_errors_power',
+ zorder: 700,
+ minzoom: 11,
+ source: 'osmose_errors_power',
+ 'source-layer': 'issues',
+ type: 'circle',
+ paint: {
+ 'circle-radius': interpolate(zoom, [
+ [11, 2],
+ [17, 15]
+ ]),
+ 'circle-color': '#ff0000',
+ 'circle-opacity': interpolate(zoom, [
+ [11, 0],
+ [17, 0.6]
+ ]),
+ 'circle-stroke-color': '#dd0000',
+ 'circle-stroke-width': interpolate(zoom, [
+ [11, 0.5],
+ [17, 3]
+ ])
+ }
+ }
+ ]
+}
diff --git a/web/src/validation-error-popup.ts b/web/src/validation-error-popup.ts
new file mode 100644
index 0000000..036f5bc
--- /dev/null
+++ b/web/src/validation-error-popup.ts
@@ -0,0 +1,45 @@
+import i18next from 'i18next'
+import maplibregl, { MapGeoJSONFeature } from 'maplibre-gl'
+import { ClickRouter } from './click-router'
+
+export class ValidationErrorPopup {
+ map: maplibregl.Map
+ errors_layer = 'osmose_errors_power'
+ popup_obj?: maplibregl.Popup
+
+ constructor(map: maplibregl.Map, clickRouter: ClickRouter) {
+ this.map = map
+
+ clickRouter.registerHandler(this.errors_layer, (f, l) => this.popup(f, l))
+
+ document.addEventListener('keydown', (e) => {
+ if (e.key === 'Escape' && this.popup_obj) {
+ this.popup_obj.remove()
+ }
+ })
+ }
+
+ async fetchIssue(uuid: string) {
+ const response = await fetch(`https://osmose.openstreetmap.fr/api/0.3/issue/${uuid}`, {
+ headers: {
+ 'Accept-Language': `${i18next.language}, en`
+ }
+ })
+ return await response.json()
+ }
+
+ popup_content(issue: any, location: maplibregl.LngLat) {
+ const osmose_url = `https://osmose.openstreetmap.fr/en/map/#loc=15/${location.lat}/${location.lng}&issue_uuid=${issue.uuid}`
+ return `<h3>${issue.title.auto}</h3><p><a href="${osmose_url}" target="_blank">View on Osmose</a></p>`
+ }
+
+ async popup(feature: MapGeoJSONFeature, location: maplibregl.LngLat) {
+ const uuid = feature.properties.uuid
+ const issue = await this.fetchIssue(uuid)
+
+ this.popup_obj = new maplibregl.Popup()
+ .setLngLat(location)
+ .setHTML(this.popup_content(issue, location))
+ .addTo(this.map)
+ }
+}