bounding box support
[gpl/argeo-suite.git] / js / src / geo / OpenLayersMapPart.js
index 177ce33862919d750769ae9029e187d1467df6db..71b9a17b0574dd1539e1eb00c4cc5fa7823598c6 100644 (file)
@@ -6,7 +6,7 @@ import Map from 'ol/Map.js';
 import View from 'ol/View.js';
 import OSM from 'ol/source/OSM.js';
 import TileLayer from 'ol/layer/Tile.js';
-import { fromLonLat, getPointResolution } from 'ol/proj.js';
+import { fromLonLat, getPointResolution, transformExtent } from 'ol/proj.js';
 import VectorSource from 'ol/source/Vector.js';
 import Feature from 'ol/Feature.js';
 import { Point } from 'ol/geom.js';
@@ -21,11 +21,16 @@ import * as SLDReader from '@nieuwlandgeo/sldreader';
 
 import MapPart from './MapPart.js';
 
+import { bbox } from 'ol/loadingstrategy';
+
 /** OpenLayers implementation of MapPart. */
 export default class OpenLayersMapPart extends MapPart {
        /** The OpenLayers Map. */
        #map;
 
+       /** Styled layer descriptor */
+       #sld;
+
        /** Externally added callback functions. */
        callbacks = {};
 
@@ -37,18 +42,20 @@ export default class OpenLayersMapPart extends MapPart {
                                //                              new TileLayer({
                                //                                      source: new SentinelCloudless(),
                                //                              }),
-//                                                             new TileLayer({
-//                                                                     source: new OSM(),
-//                                                                     opacity: 0.4,
-//                                                                     transition: 0,
-//                                                             }),
+                               //                                                              new TileLayer({
+                               //                                                                      source: new OSM(),
+                               //                                                                      opacity: 0.4,
+                               //                                                                      transition: 0,
+                               //                                                              }),
                        ],
-//                     view: new View({
-//                             center: [0, 0],
-//                             zoom: 2,
-//                     }),
+                       //                                              view: new View({
+                       //                                                      projection: 'EPSG:4326',
+                       //                                                      center: [0, 0],
+                       //                                                      zoom: 2,
+                       //                                              }),
                        target: this.getMapName(),
                });
+               //this.#map.getView().set('projection', 'EPSG:4326', true);
        }
 
        /* GEOGRAPHICAL METHODS */
@@ -108,6 +115,19 @@ export default class OpenLayersMapPart extends MapPart {
                return this.#map;
        }
 
+       getLayerByName(name) {
+               let layers = this.#map.getLayers();
+               for (let i = 0; i < layers.getLength(); i++) {
+                       let layer = layers.item(i);
+                       let n = layer.get('name');
+                       if (n !== undefined) {
+                               if (name === n)
+                                       return layer;
+                       }
+               }
+               return undefined;
+       }
+
        /* CALLBACKS */
        enableFeatureSingleClick() {
                // we cannot use 'this' in the function provided to OpenLayers
@@ -210,6 +230,40 @@ export default class OpenLayersMapPart extends MapPart {
        //
        // SLD STYLING
        //
+
+       setSld(xml) {
+               this.#sld = SLDReader.Reader(xml);
+       }
+
+       /** Get a FeatureTypeStyle (https://nieuwlandgeo.github.io/SLDReader/api.html#FeatureTypeStyle).  */
+       getFeatureTypeStyle(styledLayerName, styleName) {
+               const sldLayer = SLDReader.getLayer(this.#sld, styledLayerName);
+               const style = styleName === undefined ? SLDReader.getStyle(sldLayer) : SLDReader.getStyle(sldLayer, styleName);
+               // OpenLayers can only use one definition
+               const featureTypeStyle = style.featuretypestyles[0];
+               return featureTypeStyle;
+       }
+
+       applyStyle(layerName, styledLayerName, styleName) {
+               const layer = this.getLayerByName(layerName);
+               const featureTypeStyle = this.getFeatureTypeStyle(styledLayerName, styleName);
+               const viewProjection = this.#map.getView().getProjection();
+               const olStyleFunction = SLDReader.createOlStyleFunction(featureTypeStyle, {
+                       // Use the convertResolution option to calculate a more accurate resolution.
+                       convertResolution: viewResolution => {
+                               const viewCenter = this.#map.getView().getCenter();
+                               return getPointResolution(viewProjection, viewResolution, viewCenter);
+                       },
+                       // If you use point icons with an ExternalGraphic, you have to use imageLoadCallback
+                       // to update the vector layer when an image finishes loading.
+                       // If you do not do this, the image will only be visible after next layer pan/zoom.
+                       imageLoadedCallback: () => {
+                               layer.changed();
+                       },
+               });
+               layer.setStyle(olStyleFunction);
+       }
+
        #applySLD(vectorLayer, text) {
                const sldObject = SLDReader.Reader(text);
                const sldLayer = SLDReader.getLayer(sldObject);
@@ -232,4 +286,44 @@ export default class OpenLayersMapPart extends MapPart {
                });
                vectorLayer.setStyle(olStyleFunction);
        }
-}
\ No newline at end of file
+
+       //
+       // BBOX
+       //
+       applyBboxStrategy(layerName) {
+               const layer = this.getLayerByName(layerName);
+               const vectorSource = layer.getSource();
+               const baseUrl = vectorSource.getUrl();
+               if (typeof baseUrl === 'function')
+                       throw new Error('A strategy was already applied to layer ' + layerName);
+
+               const loadFunction = function(extent, resolution, projection, success, failure) {
+
+                       const proj = projection.getCode();
+                       var bbox = transformExtent(extent, proj, 'EPSG:4326');
+
+                       const url = baseUrl + '&' +
+                               'bbox=' + bbox.join(',') ;
+//                             'bbox=' + extent.join(',') + ',' + proj;
+                       const xhr = new XMLHttpRequest();
+                       xhr.open('GET', url);
+                       const onError = function() {
+                               vectorSource.removeLoadedExtent(extent);
+                               failure();
+                       }
+                       xhr.onerror = onError;
+                       xhr.onload = function() {
+                               if (xhr.status == 200) {
+                                       const features = vectorSource.getFormat().readFeatures(xhr.responseText);
+                                       vectorSource.addFeatures(features);
+                                       success(features);
+                               } else {
+                                       onError();
+                               }
+                       }
+                       xhr.send();
+               }
+
+               vectorSource.setLoader(loadFunction);
+       }
+}