X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=js%2Fsrc%2Fgeo%2FOpenLayersMapPart.js;h=d033e303df5583b96850d2f714b116bde8c2857e;hb=e9978679e86dcd297270432e4ed953b782f1e7c6;hp=a1fc2b852f1e9a298f9bf07a2615664f31cc3f6d;hpb=9cfc7842603c9c09d686ab9972099ed0a7c22a6e;p=gpl%2Fargeo-suite.git diff --git a/js/src/geo/OpenLayersMapPart.js b/js/src/geo/OpenLayersMapPart.js index a1fc2b8..d033e30 100644 --- a/js/src/geo/OpenLayersMapPart.js +++ b/js/src/geo/OpenLayersMapPart.js @@ -2,38 +2,65 @@ * @module OpenLayersMapPart */ -import Map from 'ol/Map.js'; -import View from 'ol/View.js'; import { fromLonLat, getPointResolution } from 'ol/proj.js'; -import VectorSource from 'ol/source/Vector.js'; -import Feature from 'ol/Feature.js'; -import { Point } from 'ol/geom.js'; -import VectorLayer from 'ol/layer/Vector.js'; -import GeoJSON from 'ol/format/GeoJSON.js'; -import GPX from 'ol/format/GPX.js'; + +import TileLayer from 'ol/layer/Tile.js'; + +import OSM from 'ol/source/OSM.js'; +import { isEmpty } from 'ol/extent'; + import Select from 'ol/interaction/Select.js'; import Overlay from 'ol/Overlay.js'; -import { Style, Icon } from 'ol/style.js'; + +import Map from 'ol/Map.js'; + +import { OverviewMap, ScaleLine, defaults as defaultControls } from 'ol/control.js'; +import { easeOut } from 'ol/easing'; import * as SLDReader from '@nieuwlandgeo/sldreader'; import MapPart from './MapPart.js'; +import { transformToOlLonLatExtent } from './OpenLayersUtils.js'; /** OpenLayers implementation of MapPart. */ export default class OpenLayersMapPart extends MapPart { /** The OpenLayers Map. */ #map; + /** The overview map */ + #overviewMap; + /** Styled layer descriptor */ #sld; + /** The select interaction */ + select; + /** Externally added callback functions. */ callbacks = {}; /** Constructor taking the mapName as an argument. */ constructor(mapName) { super(mapName); + this.#overviewMap = new OverviewMap({ + layers: [ + new TileLayer({ + source: new OSM(), + }), + ], + }); + this.select = new Select(); this.#map = new Map({ + controls: defaultControls({ + attribution: false, + rotate: false, + }).extend([this.#overviewMap, new ScaleLine({ + bar: false, + steps: 2, + text: false, + minWidth: 150, + maxWidth: 200, + })]), layers: [ ], // view: new View({ @@ -43,62 +70,22 @@ export default class OpenLayersMapPart extends MapPart { // }), target: this.getMapName(), }); + this.#map.addInteraction(this.select); //this.#map.getView().set('projection', 'EPSG:4326', true); } /* GEOGRAPHICAL METHODS */ - setZoom(zoom) { - this.#map.getView().setZoom(zoom); - } - - setCenter(lng, lat) { - this.#map.getView().setCenter(fromLonLat([lng, lat])); - } - - addPoint(lng, lat, style) { - let vectorSource = new VectorSource({ - features: [new Feature({ - geometry: new Point(fromLonLat([lng, lat])) - })] - }); - this.#map.addLayer(new VectorLayer({ - source: vectorSource, - style: style, - })); - } - - addUrlLayer(url, format, style, sld) { - let featureFormat; - if (format === 'GEOJSON') { - featureFormat = new GeoJSON(); - } - else if (format === 'GPX') { - featureFormat = new GPX(); - } else { - throw new Error("Unsupported format " + format); - } - const vectorSource = new VectorSource({ - url: url, - format: featureFormat, - }); - const vectorLayer = new VectorLayer({ - source: vectorSource, - }); - if (sld) { - this.#applySLD(vectorLayer, style); - } else if (style !== null) { - vectorLayer.setStyle(style); - } - this.#map.addLayer(vectorLayer); + setCenter(lat, lon) { + this.#map.getView().setCenter(fromLonLat([lon, lat])); } - addLayer(js) { - const func = new Function(js); - const layer = (func)(); - this.#map.addLayer(layer); + fit(extent, options) { + var transformed = transformToOlLonLatExtent(extent, this.#map.getView().getProjection()); + this.#map.getView().fit(transformed, options); } + /** Accessors */ getMap() { return this.#map; } @@ -128,7 +115,7 @@ export default class OpenLayersMapPart extends MapPart { return true; }); if (feature !== null) - mapPart.callbacks['onFeatureSingleClick'](feature.get('path')); + mapPart.callbacks['onFeatureSingleClick'](feature.get('cr:path')); }); } @@ -140,7 +127,7 @@ export default class OpenLayersMapPart extends MapPart { select.on('select', function(e) { if (e.selected.length > 0) { let feature = e.selected[0]; - mapPart.callbacks['onFeatureSelected'](feature.get('path')); + mapPart.callbacks['onFeatureSelected'](feature.get('cr:path')); } }); } @@ -184,7 +171,7 @@ export default class OpenLayersMapPart extends MapPart { return; } const coordinate = e.coordinate; - const path = selected.get('path'); + const path = selected.get('cr:path'); if (path === null) return true; const res = mapPart.callbacks['onFeaturePopup'](path); @@ -197,6 +184,44 @@ export default class OpenLayersMapPart extends MapPart { }); } + selectFeatures(layerName, featureIds) { + // we cannot use 'this' in the function provided to OpenLayers + let mapPart = this; + this.select.getFeatures().clear(); + const layer = this.getLayerByName(layerName); + const source = layer.getSource(); + for (const featureId of featureIds) { + let feature = source.getFeatureById(featureId); + if (feature === null) { + source.on('featuresloadend', function(e) { + feature = source.getFeatureById(featureId); + if (feature !== null) + mapPart.select.getFeatures().push(feature); + }); + } else { + this.select.getFeatures().push(feature); + } + } + } + + fitToLayer(layerName) { + // we cannot use 'this' in the function provided to OpenLayers + let mapPart = this; + const layer = this.getLayerByName(layerName); + const source = layer.getSource(); + const extent = source.getExtent(); + const options = { + duration: 1000, + padding: [20, 20, 20, 20], + easing: easeOut, + }; + if (!isEmpty(extent)) + this.#map.getView().fit(source.getExtent(), options); + source.on('featuresloadend', function(e) { + mapPart.getMap().getView().fit(source.getExtent(), options); + }); + } + // // HTML // @@ -204,6 +229,7 @@ export default class OpenLayersMapPart extends MapPart { return 'map'; } + // // STATIC FOR EXTENSION // @@ -274,5 +300,4 @@ export default class OpenLayersMapPart extends MapPart { }); vectorLayer.setStyle(olStyleFunction); } - }