BBOX vector source working
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 5 Oct 2023 05:07:16 +0000 (07:07 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 5 Oct 2023 05:07:16 +0000 (07:07 +0200)
js/src/geo/BboxVectorSource.js [new file with mode: 0644]
js/src/geo/OpenLayersMapPart.js
js/src/geo/export-package.js
org.argeo.app.geo/src/org/argeo/app/geo/ux/BboxVectorSource.java [new file with mode: 0644]
org.argeo.app.geo/src/org/argeo/app/geo/ux/OpenLayersMapPart.java
org.argeo.app.geo/src/org/argeo/app/ol/VectorSource.java

diff --git a/js/src/geo/BboxVectorSource.js b/js/src/geo/BboxVectorSource.js
new file mode 100644 (file)
index 0000000..49606b1
--- /dev/null
@@ -0,0 +1,23 @@
+
+import VectorSource from 'ol/source/Vector.js';
+import { bbox } from 'ol/loadingstrategy';
+import { transformExtent } from 'ol/proj.js';
+
+export default class BboxVectorSource extends VectorSource {
+       constructor(options) {
+               super(BboxVectorSource.processOptions(options));
+       }
+
+       static processOptions(options) {
+               options.strategy = bbox;
+               options.url = function(extent, resolution, projection) {
+                       const proj = projection.getCode();
+                       var bbox = transformExtent(extent, proj, 'EPSG:4326');
+
+                       const baseUrl = options.baseUrl;
+                       const url = baseUrl + '&bbox=' + bbox.join(',');
+                       return url;
+               }
+               return options;
+       }
+}
\ No newline at end of file
index 71b9a17b0574dd1539e1eb00c4cc5fa7823598c6..a1fc2b852f1e9a298f9bf07a2615664f31cc3f6d 100644 (file)
@@ -4,9 +4,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, transformExtent } from 'ol/proj.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';
@@ -21,8 +19,6 @@ 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. */
@@ -39,14 +35,6 @@ export default class OpenLayersMapPart extends MapPart {
                super(mapName);
                this.#map = new Map({
                        layers: [
-                               //                              new TileLayer({
-                               //                                      source: new SentinelCloudless(),
-                               //                              }),
-                               //                                                              new TileLayer({
-                               //                                                                      source: new OSM(),
-                               //                                                                      opacity: 0.4,
-                               //                                                                      transition: 0,
-                               //                                                              }),
                        ],
                        //                                              view: new View({
                        //                                                      projection: 'EPSG:4326',
@@ -287,43 +275,4 @@ export default class OpenLayersMapPart extends MapPart {
                vectorLayer.setStyle(olStyleFunction);
        }
 
-       //
-       // 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);
-       }
 }
index ca10dfc1caf10ca82726a825ab016c6cbd1548dc..cdf39c742b5e7a18cc3509c6629331ac0e32125c 100644 (file)
@@ -1,4 +1,5 @@
 import OpenLayersMapPart from './OpenLayersMapPart.js';
+import BboxVectorSource from './BboxVectorSource.js';
 import SentinelCloudless from './SentinelCloudless.js';
 
 import Map from 'ol/Map.js';
@@ -8,7 +9,6 @@ import TileLayer from 'ol/layer/Tile.js';
 import VectorSource from 'ol/source/Vector.js';
 import VectorLayer from 'ol/layer/Vector.js';
 import GeoJSON from 'ol/format/GeoJSON.js';
-import {bbox} from 'ol/loadingstrategy';
 
 // PSEUDO PACKAGE
 if (typeof globalThis.argeo === 'undefined')
@@ -26,6 +26,7 @@ if (typeof globalThis.argeo.tp.ol === 'undefined')
 
 // PUBLIC CLASSES
 globalThis.argeo.app.geo.OpenLayersMapPart = OpenLayersMapPart;
+globalThis.argeo.app.geo.BboxVectorSource = BboxVectorSource;
 globalThis.argeo.app.geo.SentinelCloudless = SentinelCloudless;
 
 globalThis.argeo.tp.ol.Map = Map;
@@ -35,7 +36,6 @@ globalThis.argeo.tp.ol.OSM = OSM;
 globalThis.argeo.tp.ol.VectorSource = VectorSource;
 globalThis.argeo.tp.ol.VectorLayer = VectorLayer;
 globalThis.argeo.tp.ol.GeoJSON = GeoJSON;
-globalThis.argeo.tp.ol.bbox = bbox;
 
 "use strict";
 
diff --git a/org.argeo.app.geo/src/org/argeo/app/geo/ux/BboxVectorSource.java b/org.argeo.app.geo/src/org/argeo/app/geo/ux/BboxVectorSource.java
new file mode 100644 (file)
index 0000000..fa0656c
--- /dev/null
@@ -0,0 +1,26 @@
+package org.argeo.app.geo.ux;
+
+import org.argeo.app.ol.FeatureFormat;
+import org.argeo.app.ol.VectorSource;
+
+public class BboxVectorSource extends VectorSource {
+
+       public BboxVectorSource(Object... args) {
+               super(args);
+       }
+
+       public BboxVectorSource(String baseUrl, FeatureFormat format) {
+               setFormat(format);
+               setBaseUrl(baseUrl);
+       }
+
+       @Override
+       public String getJsPackage() {
+               return AbstractGeoJsObject.JS_PACKAGE;
+       }
+
+       public void setBaseUrl(String baseUrl) {
+               doSetValue(null, "baseUrl", baseUrl);
+       }
+
+}
index c66ed3a03164760ec6aa61db61273b68d677e48a..6a9f3625b378587fe956cf4a1f10697d57bff6c9 100644 (file)
@@ -7,8 +7,12 @@ import org.argeo.app.ol.TileLayer;
 import org.argeo.app.ol.VectorLayer;
 import org.argeo.app.ux.js.JsClient;
 
+/**
+ * A wrapper around an OpenLayers map, adding specific features, such as SLD
+ * styling.
+ */
 public class OpenLayersMapPart extends AbstractGeoJsObject {
-       private String mapPartName;
+       private final String mapPartName;
 
        public OpenLayersMapPart(JsClient jsClient, String mapPartName) {
                super(mapPartName);
@@ -28,13 +32,9 @@ public class OpenLayersMapPart extends AbstractGeoJsObject {
                executeMethod(getMethodName(), layerName, styledLayerName);
        }
 
-       public void applyBboxStrategy(String layerName) {
-               executeMethod(getMethodName(), layerName);
-       }
-
        public Layer getLayer(String name) {
                // TODO deal with not found
-               String reference = "getLayerByName('" + name + "')";
+               String reference = getReference() + ".getLayerByName('" + name + "')";
                if (getJsClient().isInstanceOf(reference, AbstractOlObject.getJsClassName(VectorLayer.class))) {
                        return new VectorLayer(getJsClient(), reference);
                } else if (getJsClient().isInstanceOf(reference, AbstractOlObject.getJsClassName(TileLayer.class))) {
@@ -43,4 +43,9 @@ public class OpenLayersMapPart extends AbstractGeoJsObject {
                        return new Layer(getJsClient(), reference);
                }
        }
+
+       public String getMapPartName() {
+               return mapPartName;
+       }
+
 }
index 122ef36025181b35f7cdc27fdbf582137b55d57c..3b60d0b91fba203550c15444d1b79a479e8d5720 100644 (file)
@@ -1,7 +1,5 @@
 package org.argeo.app.ol;
 
-import org.argeo.app.ux.js.JsReference;
-
 public class VectorSource extends Source {
 
        public VectorSource(Object... args) {
@@ -9,17 +7,8 @@ public class VectorSource extends Source {
        }
 
        public VectorSource(String url, FeatureFormat format) {
-               this(url, format, false);
-       }
-
-       public VectorSource(String url, FeatureFormat format, boolean bboxStrategy) {
+               setUrl(url);
                setFormat(format);
-               if (bboxStrategy) {
-                       setUrl(url);
-                       getNewOptions().put("strategy", new JsReference(getJsPackage() + ".bbox"));
-               } else {
-                       setUrl(url);
-               }
        }
 
        public void setUrl(String url) {