Skip to content

Commit 982c874

Browse files
authored
Merge pull request #1006 from terrestris/fix-get-results
Fix duplicated results issue in useCoordinate
2 parents 742f406 + a09b766 commit 982c874

1 file changed

Lines changed: 87 additions & 102 deletions

File tree

src/Hooks/useCoordinateInfo/useCoordinateInfo.ts

Lines changed: 87 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -263,107 +263,104 @@ export const useCoordinateInfo = ({
263263
};
264264

265265
const getResultsFromImageLayers = useCallback(async (
266-
coordinate: OlCoordinate,
267-
useWms: boolean
266+
layerId: string,
267+
coordinate: OlCoordinate
268268
): Promise<FeatureLayerResult[]> => {
269269
if (_isNil(map) || _isNil(viewResolution) || _isNil(viewProjection)) {
270270
return [];
271271
}
272272

273273
const results: FeatureLayerResult[] = [];
274-
const layerUids = useWms ? wmsMapLayerUids : wmtsMapLayerUids;
275274

276-
for (const layerId of layerUids) {
277-
try {
278-
const abortController = abortControllers.current.get(layerId);
275+
try {
276+
const abortController = abortControllers.current.get(layerId);
279277

280-
if (! abortController) {
281-
continue;
282-
}
278+
if (! abortController) {
279+
return [];
280+
}
283281

284-
const layer = map.getAllLayers().find(l => getUid(l) === layerId) as WmsLayer | WmtsLayer | undefined;
285-
if (!layer) {
286-
continue;
287-
}
282+
const layer = map.getAllLayers().find(l => getUid(l) === layerId) as WmsLayer | WmtsLayer | undefined;
283+
if (!layer) {
284+
return [];
285+
}
288286

289-
const layerSource = layer.getSource();
290-
if (!layerSource) {
291-
continue;
292-
}
287+
const layerSource = layer.getSource();
288+
if (!layerSource) {
289+
return [];
290+
}
293291

294-
const infoFormat = await getInfoFormat(layer);
295-
let featureInfoUrl;
296-
297-
if (isWmsLayer(layer) && !(layerSource instanceof OlSourceWmts)) {
298-
featureInfoUrl = layerSource.getFeatureInfoUrl(
299-
coordinate,
300-
viewResolution,
301-
viewProjection,
302-
{
303-
INFO_FORMAT: infoFormat,
304-
FEATURE_COUNT: featureCount
305-
}
306-
);
307-
} else {
308-
const wmtsLayerSource = layer.getSource() as OlSourceWmts;
309-
const tileGrid = wmtsLayerSource.getTileGrid() as OlTileGridWMTS;
310-
if (_isNil(tileGrid)) {
311-
continue;
292+
const infoFormat = await getInfoFormat(layer);
293+
let featureInfoUrl;
294+
295+
if (isWmsLayer(layer) && !(layerSource instanceof OlSourceWmts)) {
296+
featureInfoUrl = layerSource.getFeatureInfoUrl(
297+
coordinate,
298+
viewResolution,
299+
viewProjection,
300+
{
301+
INFO_FORMAT: infoFormat,
302+
FEATURE_COUNT: featureCount
312303
}
313-
314-
const featureInfoTemplates = layer.get('featureInfoTemplates') as Record<string, string>;
315-
316-
featureInfoUrl = determineWmtsFeatureInfoUrl(
317-
featureInfoTemplates[infoFormat],
318-
tileGrid,
319-
coordinate,
320-
viewResolution,
321-
wmtsLayerSource.getMatrixSet()
322-
);
304+
);
305+
} else {
306+
const wmtsLayerSource = layer.getSource() as OlSourceWmts;
307+
const tileGrid = wmtsLayerSource.getTileGrid() as OlTileGridWMTS;
308+
if (_isNil(tileGrid)) {
309+
return [];
323310
}
324311

325-
if (featureInfoUrl) {
326-
let opts;
327-
if (fetchOpts instanceof Function) {
328-
opts = fetchOpts(layer);
329-
} else {
330-
opts = fetchOpts[layerId];
331-
}
312+
const featureInfoTemplates = layer.get('featureInfoTemplates') as Record<string, string>;
332313

333-
const response = await fetch(featureInfoUrl, {
334-
...opts,
335-
signal: abortController.signal
336-
});
337-
338-
const format = determineInfoFormatter(infoFormat);
339-
const isJson = infoFormat === 'application/json' || infoFormat.indexOf('json') > -1;
340-
const text = isJson ? await response.json() : await response.text();
341-
342-
if (! _isNil(format)) {
343-
const features = format.readFeatures(text).map(f => ({
344-
feature: f,
345-
layer,
346-
featureType: getFeatureType(f)
347-
}));
348-
results.push(...features);
349-
}
350-
}
351-
if (!drillDown && results.length > 0) {
352-
break;
314+
featureInfoUrl = determineWmtsFeatureInfoUrl(
315+
featureInfoTemplates[infoFormat],
316+
tileGrid,
317+
coordinate,
318+
viewResolution,
319+
wmtsLayerSource.getMatrixSet()
320+
);
321+
}
322+
323+
if (featureInfoUrl) {
324+
let opts;
325+
if (fetchOpts instanceof Function) {
326+
opts = fetchOpts(layer);
327+
} else {
328+
opts = fetchOpts[layerId];
353329
}
354330

355-
} catch (error: any) {
356-
if (error.name !== 'AbortError') {
357-
Logger.error(error);
331+
const response = await fetch(featureInfoUrl, {
332+
...opts,
333+
signal: abortController.signal
334+
});
335+
336+
const format = determineInfoFormatter(infoFormat);
337+
const isJson = infoFormat === 'application/json' || infoFormat.indexOf('json') > -1;
338+
const text = isJson ? await response.json() : await response.text();
339+
340+
if (! _isNil(format)) {
341+
const features = format.readFeatures(text).map(f => ({
342+
feature: f,
343+
layer,
344+
featureType: getFeatureType(f)
345+
}));
346+
results.push(...features);
358347
}
359348
}
349+
if (!drillDown && results.length > 0) {
350+
return results;
351+
}
352+
353+
} catch (error: any) {
354+
if (error.name !== 'AbortError') {
355+
Logger.error(error);
356+
}
360357
}
361358

362359
return results;
363-
}, [map, viewResolution, viewProjection, wmsMapLayerUids, wmtsMapLayerUids,
364-
getInfoFormat, featureCount, fetchOpts, drillDown]);
360+
}, [map, viewResolution, viewProjection, getInfoFormat, featureCount, fetchOpts, drillDown]);
365361

366362
const getResultsFromWfsLayers = useCallback(async (
363+
layer: WfsLayer,
367364
coordinate: OlCoordinate
368365
): Promise<FeatureLayerResult[]> => {
369366
if (_isNil(map) || _isNil(viewProjection)) {
@@ -372,30 +369,18 @@ export const useCoordinateInfo = ({
372369

373370
const results: FeatureLayerResult[] = [];
374371

375-
for (const layerId of wfsMapLayerUids) {
376-
if (! drillDown && results.length > 0) {
377-
break;
378-
}
379-
380-
const layer = map.getAllLayers().find(l => getUid(l) === layerId) as WfsLayer | undefined;
381-
if (!layer) {
382-
continue;
383-
}
384-
385-
const wfsLayerSource = layer.getSource();
386-
if (!wfsLayerSource) {
387-
continue;
388-
}
389-
390-
results.push(...wfsLayerSource.getFeaturesAtCoordinate(coordinate).map(f => ({
391-
feature: f,
392-
layer,
393-
featureType: getFeatureType(f)
394-
})));
372+
const wfsLayerSource = layer.getSource();
373+
if (!wfsLayerSource) {
374+
return [];
395375
}
396376

377+
results.push(...wfsLayerSource.getFeaturesAtCoordinate(coordinate).map(f => ({
378+
feature: f,
379+
layer,
380+
featureType: getFeatureType(f)
381+
})));
397382
return results;
398-
}, [map, viewProjection, wfsMapLayerUids, drillDown]);
383+
}, [map, viewProjection]);
399384

400385
/**
401386
* Event handler for map events (click, double-click, pointer rest) that retrieves the coordinate of the
@@ -467,15 +452,15 @@ export const useCoordinateInfo = ({
467452
const layerId = allRelevantLayerUids.find(id => id === uid);
468453
const layer = map.getAllLayers().find(l => getUid(l) === layerId);
469454

470-
if (!layer) {
455+
if (!layer || !layerId) {
471456
continue;
472457
}
473458
if (isWmsLayer(layer)) {
474-
promises.push(getResultsFromImageLayers(mapCoordinate, true));
459+
promises.push(getResultsFromImageLayers(layerId, mapCoordinate));
475460
} else if (isWmtsLayer(layer)) {
476-
promises.push(getResultsFromImageLayers(mapCoordinate, false));
461+
promises.push(getResultsFromImageLayers(layerId, mapCoordinate));
477462
} else if (isWfsLayer(layer)) {
478-
promises.push(getResultsFromWfsLayers(mapCoordinate));
463+
promises.push(getResultsFromWfsLayers(layer, mapCoordinate));
479464
}
480465
}
481466

0 commit comments

Comments
 (0)