Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Clickable objects

Russ Garrette5248fe1c883
diff --git a/README b/README.md
similarity index 100%
rename from README
rename to README.md
diff --git a/package-lock.json b/package-lock.json
index 248a7f7..4df08e1 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -5285,8 +5285,7 @@
"lower-case": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/lower-case/-/lower-case-1.1.4.tgz",
- "integrity": "sha1-miyr0bno4K6ZOkv31YdcOcQujqw=",
- "dev": true
+ "integrity": "sha1-miyr0bno4K6ZOkv31YdcOcQujqw="
},
"lru-cache": {
"version": "5.1.1",
@@ -5722,7 +5721,6 @@
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/no-case/-/no-case-2.3.2.tgz",
"integrity": "sha512-rmTZ9kz+f3rCvK2TD1Ue/oZlns7OGoIWP4fc3llxxRXlOkHKoWPPWJOfFYpITabSow43QJbRIoHQXtt10VldyQ==",
- "dev": true,
"requires": {
"lower-case": "^1.1.1"
}
@@ -7718,6 +7716,15 @@
"resolved": "https://registry.npmjs.org/tinyqueue/-/tinyqueue-1.2.3.tgz",
"integrity": "sha512-Qz9RgWuO9l8lT+Y9xvbzhPT2efIUIFd69N7eF7tJ9lnQl0iLj1M7peK7IoUGZL9DJHw9XftqLreccfxcQgYLxA=="
},
+ "title-case": {
+ "version": "2.1.1",
+ "resolved": "https://registry.npmjs.org/title-case/-/title-case-2.1.1.tgz",
+ "integrity": "sha1-PhJyFtpY0rxb7PE3q5Ha46fNj6o=",
+ "requires": {
+ "no-case": "^2.2.0",
+ "upper-case": "^1.0.3"
+ }
+ },
"tmp": {
"version": "0.0.33",
"resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz",
@@ -7989,8 +7996,7 @@
"upper-case": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/upper-case/-/upper-case-1.1.3.tgz",
- "integrity": "sha1-9rRQHC7EzdJrp4vnIilh3ndiFZg=",
- "dev": true
+ "integrity": "sha1-9rRQHC7EzdJrp4vnIilh3ndiFZg="
},
"uri-js": {
"version": "4.2.2",
diff --git a/package.json b/package.json
index 24b61df..8545685 100644
--- a/package.json
+++ b/package.json
@@ -37,6 +37,7 @@
},
"dependencies": {
"mapbox-gl": "^0.50.0",
- "redom": "^3.17.1"
+ "redom": "^3.17.1",
+ "title-case": "^2.1.1"
}
}
diff --git a/src/index.js b/src/index.js
index 7c65f20..33362a5 100644
--- a/src/index.js
+++ b/src/index.js
@@ -6,6 +6,7 @@ import { mount } from 'redom';
import EditButton from './editbutton.js';
import InfoBox from './infobox.js';
+import InfoPopup from './infopopup.js';
import style_base from './style_base.js';
import style_oim_power from './style_oim_power.js';
@@ -60,7 +61,9 @@ function init() {
center: [12, 26]
});
map.addControl(new mapboxgl.NavigationControl(), 'top-right');
- map.addControl(new EditButton(), 'bottom-right');
+ map.addControl(new EditButton(), 'bottom-right');
+
+ (new InfoPopup(oim_layers.map(layer => layer['id']), 9)).add(map);
}
if (document.readyState != 'loading') {
diff --git a/src/infopopup.css b/src/infopopup.css
new file mode 100644
index 0000000..6708ccd
--- /dev/null
+++ b/src/infopopup.css
@@ -0,0 +1,3 @@
+table.item_info th {
+ text-align: left;
+}
diff --git a/src/infopopup.js b/src/infopopup.js
new file mode 100644
index 0000000..1f1c72f
--- /dev/null
+++ b/src/infopopup.js
@@ -0,0 +1,104 @@
+import './infopopup.css';
+
+import mapboxgl from 'mapbox-gl';
+import titleCase from 'title-case';
+import {el, text, mount, unmount, setChildren, setStyle} from 'redom';
+
+class InfoPopup {
+ constructor(layers, min_zoom) {
+ this.layers = layers;
+ this.min_zoom = min_zoom;
+ this.popup_obj = null;
+ }
+
+ add(map) {
+ this._map = map;
+
+ for (let layer of this.layers) {
+ map.on('click', layer, e => {
+ if (this._map.getZoom() > this.min_zoom) {
+ this.popup(e);
+ }
+ });
+
+ map.on('mouseenter', layer, () => {
+ if (this._map.getZoom() > this.min_zoom) {
+ map.getCanvas().style.cursor = 'pointer';
+ }
+ });
+ map.on('mouseleave', layer, () => {
+ if (this._map.getZoom() > this.min_zoom) {
+ map.getCanvas().style.cursor = '';
+ }
+ });
+ }
+ }
+
+ osmLink(id) {
+ if (id > 0) {
+ return `https://openstreetmap.org/way/${id}`;
+ } else {
+ return `https://openstreetmap.org/relation/${-id}`;
+ }
+ }
+
+ renderKey(key, value) {
+ if (key == 'name') {
+ return null;
+ }
+ if (!value) {
+ return null;
+ }
+
+ if (key == 'voltage') {
+ value += ' kV';
+ }
+
+ if (key == 'output') {
+ value += ' MW';
+ }
+
+ if (key == 'gid') {
+ key = 'OSM ID';
+ value = el('a', value, {
+ href: this.osmLink(value),
+ target: '_blank',
+ });
+ } else {
+ key = titleCase(key);
+ }
+
+ return el('tr', el('th', key), el('td', value));
+ }
+
+ popupHtml(feature) {
+ let attrs_table = el('table', {'class': 'item_info'});
+ setChildren(
+ attrs_table,
+ Object.keys(feature.properties).map(key =>
+ this.renderKey(key, feature.properties[key]),
+ ),
+ );
+
+ let title_text = '';
+ if (feature.properties['name']) {
+ title_text = feature.properties['name'];
+ } else {
+ title_text = feature.layer['id'];
+ }
+ let content = el('div', el('h3', title_text), attrs_table);
+ return content;
+ }
+
+ popup(e) {
+ if (this.popup_obj && this.popup_obj.isOpen()) {
+ this.popup_obj.remove();
+ }
+ this.popup_obj = new mapboxgl.Popup()
+ .setLngLat(e.lngLat)
+ .setDOMContent(this.popupHtml(e.features[0]))
+ .addTo(this._map);
+ }
+}
+
+export {InfoPopup as default};
diff --git a/src/style_oim_power.js b/src/style_oim_power.js
index b8836c3..dc580cf 100644
--- a/src/style_oim_power.js
+++ b/src/style_oim_power.js
@@ -360,7 +360,7 @@ const layers = [
0,
14, 10
],
- 'text-offset': [0, 1],
+ 'text-offset': [0, 1.5],
'text-max-angle': 10
}
},