From 9cfc7842603c9c09d686ab9972099ed0a7c22a6e Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Thu, 5 Oct 2023 07:07:16 +0200 Subject: [PATCH] BBOX vector source working --- js/src/geo/BboxVectorSource.js | 23 ++++++++ js/src/geo/OpenLayersMapPart.js | 53 +------------------ js/src/geo/export-package.js | 4 +- .../argeo/app/geo/ux/BboxVectorSource.java | 26 +++++++++ .../argeo/app/geo/ux/OpenLayersMapPart.java | 17 +++--- .../src/org/argeo/app/ol/VectorSource.java | 13 +---- 6 files changed, 64 insertions(+), 72 deletions(-) create mode 100644 js/src/geo/BboxVectorSource.js create mode 100644 org.argeo.app.geo/src/org/argeo/app/geo/ux/BboxVectorSource.java diff --git a/js/src/geo/BboxVectorSource.js b/js/src/geo/BboxVectorSource.js new file mode 100644 index 0000000..49606b1 --- /dev/null +++ b/js/src/geo/BboxVectorSource.js @@ -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 diff --git a/js/src/geo/OpenLayersMapPart.js b/js/src/geo/OpenLayersMapPart.js index 71b9a17..a1fc2b8 100644 --- a/js/src/geo/OpenLayersMapPart.js +++ b/js/src/geo/OpenLayersMapPart.js @@ -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); - } } diff --git a/js/src/geo/export-package.js b/js/src/geo/export-package.js index ca10dfc..cdf39c7 100644 --- a/js/src/geo/export-package.js +++ b/js/src/geo/export-package.js @@ -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 index 0000000..fa0656c --- /dev/null +++ b/org.argeo.app.geo/src/org/argeo/app/geo/ux/BboxVectorSource.java @@ -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); + } + +} diff --git a/org.argeo.app.geo/src/org/argeo/app/geo/ux/OpenLayersMapPart.java b/org.argeo.app.geo/src/org/argeo/app/geo/ux/OpenLayersMapPart.java index c66ed3a..6a9f362 100644 --- a/org.argeo.app.geo/src/org/argeo/app/geo/ux/OpenLayersMapPart.java +++ b/org.argeo.app.geo/src/org/argeo/app/geo/ux/OpenLayersMapPart.java @@ -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; + } + } diff --git a/org.argeo.app.geo/src/org/argeo/app/ol/VectorSource.java b/org.argeo.app.geo/src/org/argeo/app/ol/VectorSource.java index 122ef36..3b60d0b 100644 --- a/org.argeo.app.geo/src/org/argeo/app/ol/VectorSource.java +++ b/org.argeo.app.geo/src/org/argeo/app/ol/VectorSource.java @@ -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) { -- 2.30.2