From 058d0a40c7d6777b4fed4279c46929dc3840c8a0 Mon Sep 17 00:00:00 2001
From: zksx <52799997+zksx@users.noreply.github.com>
Date: Mon, 23 Oct 2023 23:30:25 -0700
Subject: [PATCH] got sld styling to work

---
 .../presentational/FootprintResults.jsx       | 26 ++++++++++++++++++-
 src/js/AstroMap.js                            | 13 +++-------
 2 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/src/components/presentational/FootprintResults.jsx b/src/components/presentational/FootprintResults.jsx
index 121935140..ca35d2d35 100644
--- a/src/components/presentational/FootprintResults.jsx
+++ b/src/components/presentational/FootprintResults.jsx
@@ -169,15 +169,39 @@ export default function FootprintResults(props) {
         }
       }
 
+      async function fetchSLD(sld_url) {
+        try {
+           const response = await fetch(sld_url)
+           if(!response.ok) {
+             throw new Error('SLD response not ok ' + response.statusText);
+             }
+            const SLD_DATA = await response.text();
+            //const parser = new DOMParser();
+            //const sld_file = parser.parseFromString(SLD_DATA, text/xml);
+           return SLD_DATA
+               } catch (error) {
+                  console.error('SLD unable to be fetched', error);
+               }
+     }
+
+
       (async () => {
         let collections = await FetchObjects(collectionUrls);
 
+
         // Add extra properties to each collection
         for(const key in collections){
           collections[key].id = key;
           collections[key].title = props.target.collections.find(collection => collection.id === key).title;
           collections[key].url = collectionUrls[key];
-          collections[key].styleSheets = styleSheetUrls[key];
+
+          let sldtext = null;
+
+          if (styleSheetUrls[key]) {
+            sldtext = await fetchSLD(styleSheetUrls[key]);
+            collections[key].styleSheets = sldtext;
+          }
+
         }
 
         // Updates collectionId if switching to a new set of collections (new target)
diff --git a/src/js/AstroMap.js b/src/js/AstroMap.js
index 566309c4c..ff596ddaf 100644
--- a/src/js/AstroMap.js
+++ b/src/js/AstroMap.js
@@ -302,7 +302,7 @@ export default L.Map.AstroMap = L.Map.extend({
 
         let title = featureCollections[i].title;
 
-        let sld_file =  featureCollections[i].styleSheets;
+        let sld_text = featureCollections[i].styleSheets;
         // set style if available
 
         let myStyle = null;
@@ -312,15 +312,8 @@ export default L.Map.AstroMap = L.Map.extend({
         // [old] and it shows up as the layer title when added to the separate Leaflet control
         if(featureCollections[i].features.length > 0) {
 
-
-
-          if (sld_file) {
-            let sldtext = fetch(sld_file);
-            let res = sldtext.res
-
-            console.log("good style wow");
-            this.SLDStyler = new L.SLDStyler(sldtext);
-
+          if (sld_text != null) {
+            this.SLDStyler = new L.SLDStyler(sld_text);
             myStyle = this.SLDStyler.getStyleFunction();
 
           }
-- 
GitLab