Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Test other infoFormat when getFeatureInfo fails #2112

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/getfeatureinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -134,12 +143,24 @@ 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();
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];
const url = layer.getSource().getFeatureInfoUrl(coordinate, resolution, projection, {
INFO_FORMAT: format,
FEATURE_COUNT: '20'
});
// 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;
}
}
let json = {};
try {
json = JSON.parse(text);
Expand Down
Loading