Use different background map based on location.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 5 Jan 2021 21:53:17 +0000 (22:53 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 5 Jan 2021 21:53:17 +0000 (22:53 +0100)
wrmap.js

index e457e29..8b08cb4 100644 (file)
--- a/wrmap.js
+++ b/wrmap.js
@@ -260,23 +260,44 @@ function init_wrmap(i, jq_map) {
 
        // background layer
        // ----------------
+       function get_austria_feature() {
+               // the following polygon is too coarse to be useful but still nice for debugging
+               let austria_wkt = 'Polygon((16.97 48.12, 16.90 47.71, 16.34 47.71, 16.53 47.49, 16.20 46.85, 16.01 46.68, 15.13 46.65, 14.63 46.43, 13.80 46.50, 12.37 46.76, 12.15 47.11, 11.16 46.94, 11.04 46.75, 10.44 46.89, 9.93 46.92, 9.47 47.10, 9.63 47.34, 9.59 47.52, 9.89 47.58, 10.40 47.30, 10.54 47.56, 11.42 47.52, 12.14 47.70, 12.62 47.67, 12.93 47.46, 13.02 47.63, 12.88 48.28, 13.24 48.41, 13.59 48.87, 14.33 48.55, 14.90 48.96, 15.25 49.03, 16.02 48.73, 16.49 48.78, 16.96 48.59, 16.87 48.47, 16.97 48.12))';
+               let format = new ol.format.WKT();
+               let feature = format.readFeature(austria_wkt, {
+                       dataProjection: EPSG4326,
+                       featureProjection: EPSG3857,
+               });
+               return feature;
+       }
 
        // basemap.at layer
-       let capabilitiesUrl = 'https://www.basemap.at/wmts/1.0.0/WMTSCapabilities.xml';
-       fetch(capabilitiesUrl).then(function(response) {
-               return response.text();
-       }).then(function(text) {
-               let result = new ol.format.WMTSCapabilities().read(text);
-               let options = ol.source.WMTS.optionsFromCapabilities(result, {
-                       layer: 'geolandbasemap',
-                       matrixSet: 'google3857',
-                       style: 'normal',
+       let austria_feature = get_austria_feature();
+       function is_in_austria(feature) {
+               return austria_feature.getGeometry().intersectsCoordinate(feature.getGeometry().getFirstCoordinate());
+       }
+       if (features_all.every(is_in_austria)) {
+               let capabilitiesUrl = 'https://www.basemap.at/wmts/1.0.0/WMTSCapabilities.xml';
+               fetch(capabilitiesUrl).then(function(response) {
+                       return response.text();
+               }).then(function(text) {
+                       let result = new ol.format.WMTSCapabilities().read(text);
+                       let options = ol.source.WMTS.optionsFromCapabilities(result, {
+                               layer: 'geolandbasemap',
+                               matrixSet: 'google3857',
+                               style: 'normal',
+                       });
+                       let layer_map = new ol.layer.Tile({
+                               source: new ol.source.WMTS(options),
+                       });
+                       map.getLayers().insertAt(0, layer_map);
                });
+       } else {
                let layer_map = new ol.layer.Tile({
-                       source: new ol.source.WMTS(options),
+                       source: new ol.source.OSM()
                });
                map.getLayers().insertAt(0, layer_map);
-       });
+       }
 
        // // Alternatives:
        // // * OpenTopoMap (see https://opentopomap.org/about)
@@ -284,7 +305,7 @@ function init_wrmap(i, jq_map) {
        // let layer_map = new ol.layer.Tile({
        //     source: new ol.source.OSM()
        // });
-       // map.getLayers().insertAt(0, layer_map);      
+       // map.getLayers().insertAt(0, layer_map);
 }