Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Start of geocoding. (WIP)

Michael Rademaker61d3ca8b9115
diff --git a/manager/src/map/mapsettings.json b/manager/src/map/mapsettings.json
index 4ebe70b72..ee7f64a3e 100644
--- a/manager/src/map/mapsettings.json
+++ b/manager/src/map/mapsettings.json
@@ -5,7 +5,8 @@
"bounds": [ 4.42, 51.88, 4.55, 51.94 ],
"zoom": 14,
"maxZoom": 19,
- "boxZoom": false
+ "boxZoom": false,
+ "geoCodeUrl": "https://nominatim.openstreetmap.org"
}
},
"version": 8,
diff --git a/ui/app/console_loader/src/pages/page-mobile-geofences.ts b/ui/app/console_loader/src/pages/page-mobile-geofences.ts
index 04c9e7cf7..923a86854 100644
--- a/ui/app/console_loader/src/pages/page-mobile-geofences.ts
+++ b/ui/app/console_loader/src/pages/page-mobile-geofences.ts
@@ -2,7 +2,7 @@ import {css, html, PropertyValues} from "lit";
import {customElement, property} from "lit/decorators.js";
import {AppStateKeyed} from "@openremote/or-app/dist/app";
import {Page, PageProvider} from "@openremote/or-app/dist/types";
-import {NavigationControl} from "mapbox-gl";
+import {NavigationControl} from "maplibre-gl";
import {EnhancedStore} from "@reduxjs/toolkit";
import manager from "@openremote/core";
import {JsonRulesetDefinition} from "@openremote/model";
diff --git a/ui/component/or-map/@types/maplibre-gl-geocoder.d.ts b/ui/component/or-map/@types/maplibre-gl-geocoder.d.ts
new file mode 100644
index 000000000..88e278b8f
--- /dev/null
+++ b/ui/component/or-map/@types/maplibre-gl-geocoder.d.ts
@@ -0,0 +1,2 @@
+/* tslint:disable:class-name */
+declare module "@maplibre/maplibre-gl-geocoder";
diff --git a/ui/component/or-map/package.json b/ui/component/or-map/package.json
index 939c64384..f408c48e2 100644
--- a/ui/component/or-map/package.json
+++ b/ui/component/or-map/package.json
@@ -21,13 +21,13 @@
"@openremote/rest": "^1.0.3",
"@polymer/polymer": "^3.3.1",
"@types/mapbox": "^1.6.42",
- "@types/mapbox-gl": "^2.4.2",
"lit": "^2.0.2",
- "mapbox-gl": "^1.13.0",
- "mapbox.js": "^3.2.0"
+ "mapbox.js": "^3.2.0",
+ "maplibre-gl": ">=1.15.2",
+ "@maplibre/maplibre-gl-geocoder": "^1.2.0"
},
"devDependencies": {
- "@types/mapbox-gl": "^2.0.2",
+ "@types/geojson": "^7946.0.7",
"tslint": ">=5.11.0",
"typescript": ">=4.5.2",
"webpack": "^5.64.3",
diff --git a/ui/component/or-map/src/index.ts b/ui/component/or-map/src/index.ts
index 9a917400f..9804167d8 100644
--- a/ui/component/or-map/src/index.ts
+++ b/ui/component/or-map/src/index.ts
@@ -2,7 +2,7 @@ import manager, {EventCallback, MapType} from "@openremote/core";
import {FlattenedNodesObserver} from "@polymer/polymer/lib/utils/flattened-nodes-observer.js";
import {CSSResult, html, LitElement, PropertyValues} from "lit";
import {customElement, property, query} from "lit/decorators.js";
-import {Control, IControl, LngLat, LngLatBoundsLike, LngLatLike, Map as MapGL} from "mapbox-gl";
+import {Control, IControl, LngLat, LngLatBoundsLike, LngLatLike, Map as MapGL} from "maplibre-gl";
import {MapWidget} from "./mapwidget";
import {style} from "./style";
import {OrMapMarker, OrMapMarkerChangedEvent} from "./markers/or-map-marker";
@@ -21,7 +21,7 @@ import {i18next} from "@openremote/or-translate";
export {Util, LngLatLike};
export * from "./markers/or-map-marker";
export * from "./markers/or-map-marker-asset";
-export {Control, IControl} from "mapbox-gl";
+export {Control, IControl} from "maplibre-gl";
export * from "./or-map-asset-card";
export interface ViewSettings {
@@ -31,6 +31,7 @@ export interface ViewSettings {
"maxZoom": number;
"minZoom": number;
"boxZoom": boolean;
+ "geoCodeUrl": String;
}
export interface MapEventDetail {
@@ -83,16 +84,16 @@ export class CenterControl {
onAdd(map: MapGL): HTMLElement {
this.map = map;
const control = document.createElement("div");
- control.classList.add("mapboxgl-ctrl");
- control.classList.add("mapboxgl-ctrl-group");
+ control.classList.add("maplibregl-ctrl");
+ control.classList.add("maplibregl-ctrl-group");
const button = document.createElement("button");
- button.className = "mapboxgl-ctrl-geolocate";
+ button.className = "maplibregl-ctrl-geolocate";
button.addEventListener("click", (ev) => map.flyTo({
center: this.pos,
zoom: map.getZoom()
}));
const buttonIcon = document.createElement("span");
- buttonIcon.className = "mapboxgl-ctrl-icon";
+ buttonIcon.className = "maplibregl-ctrl-icon";
button.appendChild(buttonIcon);
control.appendChild(button);
this.elem = control;
@@ -144,8 +145,8 @@ export class CoordinatesControl {
onAdd(map: MapGL): HTMLElement {
this.map = map;
const control = document.createElement("div");
- control.classList.add("mapboxgl-ctrl");
- control.classList.add("mapboxgl-ctrl-group");
+ control.classList.add("maplibregl-ctrl");
+ control.classList.add("maplibregl-ctrl-group");
const input = new OrMwcInput();
input.type = InputType.TEXT;
@@ -251,7 +252,7 @@ export const geoJsonPointInputTemplateProvider: ValueInputProviderGenerator = (a
margin: 3px 0;
}
</style>
- <or-map id="geo-json-point-map" class="or-map" @or-map-clicked="${(ev: OrMapClickedEvent) => {if (ev.detail.doubleClick) {setPos(ev.detail.lngLat);}}}" .center="${center}" .controls="${[centerControl, [coordinatesControl, "top-left"]]}">
+ <or-map id="geo-json-point-map" class="or-map" @or-map-clicked="${(ev: OrMapClickedEvent) => {if (ev.detail.doubleClick) {setPos(ev.detail.lngLat);}}}" .center="${center}" .controls="${[centerControl, [coordinatesControl, "top-left"]]}" ?showGeoCodingControl="true">
<or-map-marker id="geo-json-point-marker" active .lng="${pos ? pos.lng : undefined}" .lat="${pos ? pos.lat : undefined}" .icon="${iconAndColor ? iconAndColor.icon : undefined}" .activeColor="${iconAndColor ? "#" + iconAndColor.color : undefined}" .colour="${iconAndColor ? "#" + iconAndColor.color : undefined}"></or-map-marker>
</or-map>
`;
@@ -365,6 +366,9 @@ export class OrMap extends LitElement {
@property({type: Number})
public zoom?: number;
+ @property({type: Boolean})
+ public showGeoCodingControl: boolean = true;
+
public controls?: (Control | IControl | [Control | IControl, ControlPosition?])[];
protected _initCallback?: EventCallback;
@@ -443,7 +447,7 @@ export class OrMap extends LitElement {
}
if (this._mapContainer && this._slotElement) {
- this._map = new MapWidget(this.type, this.shadowRoot!, this._mapContainer)
+ this._map = new MapWidget(this.type, this.showGeoCodingControl, this.shadowRoot!, this._mapContainer)
.setCenter(this.center)
.setZoom(this.zoom)
.setControls(this.controls);
diff --git a/ui/component/or-map/src/mapwidget.ts b/ui/component/or-map/src/mapwidget.ts
index 5affade03..aa93f87b1 100644
--- a/ui/component/or-map/src/mapwidget.ts
+++ b/ui/component/or-map/src/mapwidget.ts
@@ -3,14 +3,16 @@ import {LngLatLike, Map as MapGL, MapboxOptions as OptionsGL, Marker as MarkerGL
MapMouseEvent,
NavigationControl,
Control,
- IControl} from "mapbox-gl";
+ IControl} from "maplibre-gl";
+import MaplibreGeocoder from "@maplibre/maplibre-gl-geocoder";
+import '@maplibre/maplibre-gl-geocoder/dist/maplibre-gl-geocoder.css';
import {ControlPosition, OrMapClickedEvent, OrMapLoadedEvent, ViewSettings} from "./index";
import {
OrMapMarker
} from "./markers/or-map-marker";
import {getLatLngBounds, getLngLat} from "./util";
const mapboxJsStyles = require("mapbox.js/dist/mapbox.css");
-const mapboxGlStyles = require("mapbox-gl/dist/mapbox-gl.css");
+const maplibreGlStyles = require("maplibre-gl/dist/maplibre-gl.css");
// TODO: fix any type
const metersToPixelsAtMaxZoom = (meters:number, latitude:number) =>
@@ -29,13 +31,15 @@ export class MapWidget {
protected _viewSettings?: ViewSettings;
protected _center?: LngLatLike;
protected _zoom?: number;
+ protected _showGeoCodingControl: boolean = false;
protected _controls?: (Control | IControl | [Control | IControl, ControlPosition?])[];
protected _clickHandlers: Map<OrMapMarker, (ev: MouseEvent) => void> = new Map();
- constructor(type: MapType, styleParent: Node, mapContainer: HTMLElement) {
+ constructor(type: MapType, showGeoCodingControl: boolean, styleParent: Node, mapContainer: HTMLElement) {
this._type = type;
this._styleParent = styleParent;
this._mapContainer = mapContainer;
+ this._showGeoCodingControl = showGeoCodingControl;
}
public setCenter(center?: LngLatLike): this {
@@ -234,11 +238,11 @@ export class MapWidget {
} else {
// Add style to shadow root
const style = document.createElement("style");
- style.id = "mapboxGlStyle";
- style.textContent = mapboxGlStyles;
+ style.id = "maplibreGlStyle";
+ style.textContent = maplibreGlStyles;
this._styleParent.appendChild(style);
- const map: typeof import("mapbox-gl") = await import(/* webpackChunkName: "mapbox-gl" */ "mapbox-gl");
+ const map: typeof import("maplibre-gl") = await import(/* webpackChunkName: "maplibre-gl" */ "maplibre-gl");
const settings = await this.loadViewSettings();
const options: OptionsGL = {
@@ -295,6 +299,11 @@ export class MapWidget {
// Add zoom and rotation controls to the map
this._mapGl.addControl(new NavigationControl());
}
+
+ if (this._showGeoCodingControl && this._viewSettings && this._viewSettings.geoCodeUrl) {
+ var geocoder = new MaplibreGeocoder({forwardGeocode: this._forwardGeocode, reverseGeocode: this._reverseGeocode }, {marker: false});
+ this._mapGl!.addControl(geocoder);
+ }
}
this._mapContainer.dispatchEvent(new OrMapLoadedEvent());
@@ -508,4 +517,39 @@ export class MapWidget {
this._clickHandlers.delete(marker);
}
}
+
+ protected async _forwardGeocode(config: any) {
+ const features = [];
+ try {
+ let request = this._viewSettings!.geoCodeUrl + '/search?q=' + config.query + '&format=geojson&polygon_geojson=1&addressdetails=1';
+ const response = await fetch(request);
+ const geojson = await response.json();
+ for (let feature of geojson.features) {
+ let center = [feature.bbox[0] + (feature.bbox[2] - feature.bbox[0]) / 2, feature.bbox[1] + (feature.bbox[3] - feature.bbox[1]) / 2 ];
+ let point = {
+ type: 'Feature',
+ geometry: {
+ type: 'Point',
+ coordinates: center
+ },
+ place_name: feature.properties.display_name,
+ properties: feature.properties,
+ text: feature.properties.display_name,
+ place_type: ['place'],
+ center: center
+ };
+ features.push(point);
+ }
+ } catch (e) {
+ console.error(`Failed to forwardGeocode with error: ${e}`);
+ }
+
+ return {
+ features: features
+ };
+ }
+
+ protected async _reverseGeocode(config: any) {
+
+ }
}
diff --git a/ui/component/or-map/src/style.ts b/ui/component/or-map/src/style.ts
index daefaf01f..e14626df7 100644
--- a/ui/component/or-map/src/style.ts
+++ b/ui/component/or-map/src/style.ts
@@ -62,7 +62,7 @@ export const style = css`
bottom: 0;
}
- .leaflet-marker-icon, .mapboxgl-marker {
+ .leaflet-marker-icon, .maplibregl-marker, .mapboxgl-marker {
pointer-events: none !important;
}
diff --git a/ui/component/or-map/src/util.ts b/ui/component/or-map/src/util.ts
index b0d36d1f0..e4acda1c7 100644
--- a/ui/component/or-map/src/util.ts
+++ b/ui/component/or-map/src/util.ts
@@ -1,4 +1,4 @@
-import {LngLat, LngLatBounds, LngLatBoundsLike, LngLatLike} from "mapbox-gl";
+import {LngLat, LngLatBounds, LngLatBoundsLike, LngLatLike} from "maplibre-gl";
import {Asset, Attribute, GeoJSONPoint, ValueHolder, WellknownAttributes} from "@openremote/model";
export function getLngLat(lngLatLike?: LngLatLike | Asset | ValueHolder<any> | GeoJSONPoint): { lng: number, lat: number } | undefined {
diff --git a/ui/component/or-map/tsconfig.json b/ui/component/or-map/tsconfig.json
index 682436851..da514469c 100644
--- a/ui/component/or-map/tsconfig.json
+++ b/ui/component/or-map/tsconfig.json
@@ -6,7 +6,8 @@
"rootDir": "src"
},
"include": [
- "./src"
+ "./src",
+ "../or-map/@types"
],
"references": [
{ "path": "../core" },
diff --git a/ui/component/or-map/webpack.config.js b/ui/component/or-map/webpack.config.js
index 59ee22935..1161dade8 100644
--- a/ui/component/or-map/webpack.config.js
+++ b/ui/component/or-map/webpack.config.js
@@ -3,7 +3,7 @@ const util = require("@openremote/util");
bundles = {
"index": {
vendor: {
- "mapbox-gl": "mapboxgl",
+ "maplibre-gl": "maplibre",
"mapbox.js": "L.mapbox",
"moment": "moment"
},
diff --git a/ui/component/or-rules/src/json-viewer/modals/or-rule-radial-modal.ts b/ui/component/or-rules/src/json-viewer/modals/or-rule-radial-modal.ts
index 8af3f8475..97b881913 100644
--- a/ui/component/or-rules/src/json-viewer/modals/or-rule-radial-modal.ts
+++ b/ui/component/or-rules/src/json-viewer/modals/or-rule-radial-modal.ts
@@ -18,7 +18,7 @@ import {translate} from "@openremote/or-translate";
import {DialogAction, OrMwcDialog, OrMwcDialogOpenedEvent} from "@openremote/or-mwc-components/or-mwc-dialog";
import {OrMap, OrMapClickedEvent} from '@openremote/or-map';
import '@openremote/or-map/dist/markers/or-map-marker';
-import {LngLatLike} from "mapbox-gl";
+import {LngLatLike} from "maplibre-gl";
@customElement("or-rule-radial-modal")
export class OrRuleRadialModal extends translate(i18next)(LitElement) {