From 41a83225533ea71ccadef18c37fee5ab87575925 Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Tue, 16 Jan 2024 15:47:15 +0100
Subject: [PATCH] Allow for more than one WMS server

---
 VIPSWeb/local_settings_sample.py       | 5 +++--
 spatial/static/spatial/js/gridmap.js   | 8 ++++----
 spatial/templates/spatial/gridmap.html | 2 +-
 spatial/views.py                       | 2 +-
 4 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/VIPSWeb/local_settings_sample.py b/VIPSWeb/local_settings_sample.py
index fee12ee5..466a2c16 100755
--- a/VIPSWeb/local_settings_sample.py
+++ b/VIPSWeb/local_settings_sample.py
@@ -143,8 +143,9 @@ SITE_OWNER_URL = "http://www.example.com/"
 # Grid models settings
 WMS_SERVER_URL = "https://gridweb.vips.nibio.no/cgi-bin/"
 AVAILABLE_GRID_MODELS = [
-    {"model_id":"SEPTREFHUM", "model_name": {"nb":"Septoria referansefuktmodell", "en":"Septoria reference humidity model"}},
-    {"model_id":"PSILARTEMP", "model_name": {"nb":"Gulrotflue svermetidspunktmodell", "en":"Carrot rust fly (Psila rosae) temperature model"}}
+   {"model_id":"SEPTREFHUM", "model_name": {"nb":"Septoria referansefuktmodell", "en":"Septoria reference humidity model"}, "url": "%sSEPTREFHUM" % WMS_SERVER_URL},
+    {"model_id":"PSILARTEMP", "model_name": {"nb":"Gulrotflue svermetidspunktmodell", "en":"Carrot rust fly (Psila rosae) temperature model"}, "url": "%sPSILARTEMP" % WMS_SERVER_URL},
+    {"model_id":"NAERSTADMO", "model_name": {"nb":"Nærstads modell", "en":"Nærstad's potato blight model"}, "url": "%sNAERSTADMO" % WMS_SERVER_URL}
     ]
 
 # OpenLayers Map (map on front page) initial configuration
diff --git a/spatial/static/spatial/js/gridmap.js b/spatial/static/spatial/js/gridmap.js
index 21feeaaa..f339ee59 100644
--- a/spatial/static/spatial/js/gridmap.js
+++ b/spatial/static/spatial/js/gridmap.js
@@ -27,7 +27,7 @@ proj4.defs(
 
 let layers = [];
 
-let mapserverURL=settings.WMS_SERVER_URL
+//let mapserverURL=settings.WMS_SERVER_URL
 let modelId = undefined;
 let today = getSystemTime().format("YYYY-MM-DD");
 let todayLayerIndex = undefined;
@@ -165,7 +165,7 @@ function getLayersForCurrentTimestamp()
  * @param {*} inputModelId model Id, e.g. PSILARTEMP
  * @param {*} mapAttribution background map source information (displayed in the map)
  */
-async function initGridMap(inputModelId, mapAttribution) {
+async function initGridMap(inputModelId, wmsURL, mapAttribution) {
     modelId = inputModelId;
     var backgroundLayer = new ol.layer.Tile({
 		source: new ol.source.OSM({
@@ -180,7 +180,7 @@ async function initGridMap(inputModelId, mapAttribution) {
     // Get layers from WMS service
     let parser = new ol.format.WMSCapabilities();
     //console.info(mapserverURL + modelId + "?service=WMS&version=1.3.0&request=GetCapabilities");
-    let response = await fetch(mapserverURL + modelId + "?service=WMS&version=1.3.0&request=GetCapabilities&language=" + settings.currentLanguage);
+    let response = await fetch(wmsURL + "?service=WMS&version=1.3.0&request=GetCapabilities&language=" + settings.currentLanguage);
     //console.info(response.status); // Response is 200 even if a model doesn't exist on the mapserver
     let txt = await response.text();
     
@@ -256,7 +256,7 @@ async function initGridMap(inputModelId, mapAttribution) {
             layers.push(
                 new ol.layer.Image({
                     source: new ol.source.ImageWMS({
-                        url: mapserverURL + modelId,
+                        url: wmsURL,
                         params: { "LAYERS": modelId + "." + currentLayerParam + "." + dateStr, "TRANSPARENT": "TRUE" },
                         serverType: "mapserver",
                         ratio: 1,
diff --git a/spatial/templates/spatial/gridmap.html b/spatial/templates/spatial/gridmap.html
index 851502e2..c929bcfd 100644
--- a/spatial/templates/spatial/gridmap.html
+++ b/spatial/templates/spatial/gridmap.html
@@ -25,7 +25,7 @@
 		var longitude = {{settings.MAP_CENTER_LONGITUDE|unlocalize}};
 		var latitude = {{settings.MAP_CENTER_LATITUDE|unlocalize}};
 		var zoomLevel = {{settings.MAP_ZOOMLEVEL}};
-		initGridMap("{{model_id}}","{{settings.MAP_ATTRIBUTION|safe}}");
+		initGridMap("{{model_id}}","{{url}}", "{{settings.MAP_ATTRIBUTION|safe}}");
 	});
 </script>
 {% endblock %}
diff --git a/spatial/views.py b/spatial/views.py
index eea91a82..6f043a34 100644
--- a/spatial/views.py
+++ b/spatial/views.py
@@ -35,7 +35,7 @@ def gridmap(request, model_id=None):
     try:
         for model in settings.AVAILABLE_GRID_MODELS:
             if model_id == model["model_id"]:
-                context = {"model_id": model_id}
+                context = {"model_id": model_id, "url": model["url"]}
                 return render(request, 'spatial/gridmap.html', context)
     except AttributeError:
         pass
-- 
GitLab