From 95ca196d71e5cec0f93f06061e6cbcbf7880df05 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 13 Dec 2024 13:53:50 +0100 Subject: [PATCH 1/4] Update getfeatureinfo.js Try different infoFormats --- src/getfeatureinfo.js | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/getfeatureinfo.js b/src/getfeatureinfo.js index e55af40fb..e57d678b2 100644 --- a/src/getfeatureinfo.js +++ b/src/getfeatureinfo.js @@ -5,6 +5,15 @@ import infoTemplates from './featureinfotemplates'; import maputils from './maputils'; import SelectedItem from './models/SelectedItem'; +const isValidJSON = str => { + try { + JSON.parse(str); + return true; + } catch (e) { + return false; + } +}; + /** * Factory method to create a SelectedItem instance. Note that this method is exposed in api. * Does not add async content (related tables and attachments). If you need async content use @@ -134,12 +143,21 @@ async function getFeatureInfoUrl({ } if (layer.get('infoFormat') === 'application/geo+json' || layer.get('infoFormat') === 'application/geojson') { - const url = layer.getSource().getFeatureInfoUrl(coordinate, resolution, projection, { - INFO_FORMAT: layer.get('infoFormat'), - FEATURE_COUNT: '20' - }); - const res = await fetch(url, { method: 'GET' }); - const text = await res.text(); + let format = layer.get('infoFormat'); + let formatArr = [...new Set([format, 'application/geo+json', 'application/geojson', 'application/json'])]; + let text; + for (const f of formatArr) { + const url = layer.getSource().getFeatureInfoUrl(coordinate, resolution, projection, { + INFO_FORMAT: f, + FEATURE_COUNT: '20' + }); + const res = await fetch(url, { method: 'GET' }); + text = await res.text(); + if(isValidJSON(text)){ + layer.set('infoFormat', f); + break; + } + }; let json = {}; try { json = JSON.parse(text); From 8d9ee8ee4f24f3f9c3577cda80b142a93890e16c Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 13 Dec 2024 14:02:12 +0100 Subject: [PATCH 2/4] Update getfeatureinfo.js --- src/getfeatureinfo.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/getfeatureinfo.js b/src/getfeatureinfo.js index e57d678b2..8bebaa568 100644 --- a/src/getfeatureinfo.js +++ b/src/getfeatureinfo.js @@ -13,7 +13,7 @@ const isValidJSON = str => { return false; } }; - + /** * Factory method to create a SelectedItem instance. Note that this method is exposed in api. * Does not add async content (related tables and attachments). If you need async content use @@ -143,8 +143,8 @@ async function getFeatureInfoUrl({ } if (layer.get('infoFormat') === 'application/geo+json' || layer.get('infoFormat') === 'application/geojson') { - let format = layer.get('infoFormat'); - let formatArr = [...new Set([format, 'application/geo+json', 'application/geojson', 'application/json'])]; + const format = layer.get('infoFormat'); + const formatArr = [...new Set([format, 'application/geo+json', 'application/geojson', 'application/json'])]; let text; for (const f of formatArr) { const url = layer.getSource().getFeatureInfoUrl(coordinate, resolution, projection, { @@ -153,11 +153,11 @@ async function getFeatureInfoUrl({ }); const res = await fetch(url, { method: 'GET' }); text = await res.text(); - if(isValidJSON(text)){ + if (isValidJSON(text)) { layer.set('infoFormat', f); break; } - }; + } let json = {}; try { json = JSON.parse(text); From 834583bc8ad1324d437dde5dec2d2cf8ba3908f9 Mon Sep 17 00:00:00 2001 From: Jonas Date: Sat, 14 Dec 2024 09:15:04 +0100 Subject: [PATCH 3/4] Update getfeatureinfo.js --- src/getfeatureinfo.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/getfeatureinfo.js b/src/getfeatureinfo.js index 8bebaa568..ca6959c2f 100644 --- a/src/getfeatureinfo.js +++ b/src/getfeatureinfo.js @@ -146,15 +146,18 @@ async function getFeatureInfoUrl({ const format = layer.get('infoFormat'); const formatArr = [...new Set([format, 'application/geo+json', 'application/geojson', 'application/json'])]; let text; - for (const f of formatArr) { + for (let i = 0; i < formatArr.length; i += 1) { + const format = formatArr[i]; const url = layer.getSource().getFeatureInfoUrl(coordinate, resolution, projection, { - INFO_FORMAT: f, + INFO_FORMAT: format, FEATURE_COUNT: '20' }); - const res = await fetch(url, { method: 'GET' }); - text = await res.text(); - if (isValidJSON(text)) { - layer.set('infoFormat', f); + // eslint-disable-next-line no-await-in-loop + const result = await fetch(url, { method: 'GET' }).then((res) => res.text()) + .catch(error => console.error(error)); + if (isValidJSON(result)) { + text = result; + layer.set('infoFormat', format); break; } } From 15fc346c4fd05e35068695ffb822b336f7c8423b Mon Sep 17 00:00:00 2001 From: Jonas Date: Sat, 14 Dec 2024 09:17:42 +0100 Subject: [PATCH 4/4] Update getfeatureinfo.js --- src/getfeatureinfo.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/getfeatureinfo.js b/src/getfeatureinfo.js index ca6959c2f..13e10ede2 100644 --- a/src/getfeatureinfo.js +++ b/src/getfeatureinfo.js @@ -143,8 +143,8 @@ async function getFeatureInfoUrl({ } if (layer.get('infoFormat') === 'application/geo+json' || layer.get('infoFormat') === 'application/geojson') { - const format = layer.get('infoFormat'); - const formatArr = [...new Set([format, 'application/geo+json', 'application/geojson', 'application/json'])]; + const infoFormat = layer.get('infoFormat'); + const formatArr = [...new Set([infoFormat, 'application/geo+json', 'application/geojson', 'application/json'])]; let text; for (let i = 0; i < formatArr.length; i += 1) { const format = formatArr[i]; @@ -154,7 +154,7 @@ async function getFeatureInfoUrl({ }); // eslint-disable-next-line no-await-in-loop const result = await fetch(url, { method: 'GET' }).then((res) => res.text()) - .catch(error => console.error(error)); + .catch(error => console.error(error)); if (isValidJSON(result)) { text = result; layer.set('infoFormat', format);