diff --git a/src/data/feature_index.js b/src/data/feature_index.js index 9a1589e671a..f89ad9b4e22 100644 --- a/src/data/feature_index.js +++ b/src/data/feature_index.js @@ -242,7 +242,7 @@ class FeatureIndex { if (layerResult === undefined) { layerResult = result[layerID] = []; } - layerResult.push(geojsonFeature); + layerResult.push({ featureIndex: index, feature: geojsonFeature }); } } } diff --git a/src/source/query_features.js b/src/source/query_features.js index c5e8e7ad4f0..9fae1ca590d 100644 --- a/src/source/query_features.js +++ b/src/source/query_features.js @@ -51,35 +51,22 @@ function sortTilesIn(a, b) { } function mergeRenderedFeatureLayers(tiles) { - // Avoid merge work for common cases - if (!tiles.length) { - return {}; - } else if (tiles.length === 1) { - return tiles[0].queryResults; - } - // Merge results from all tiles, but if two tiles share the same // wrapped ID, don't duplicate features between the two tiles const result = {}; - const wrappedIDFeatureMap = {}; + const wrappedIDLayerMap = {}; for (const tile of tiles) { const queryResults = tile.queryResults; const wrappedID = tile.wrappedTileID; - const wrappedIDFeatures = wrappedIDFeatureMap[wrappedID] = wrappedIDFeatureMap[wrappedID] || {}; + const wrappedIDLayers = wrappedIDLayerMap[wrappedID] = wrappedIDLayerMap[wrappedID] || {}; for (const layerID in queryResults) { const tileFeatures = queryResults[layerID]; - let resultFeatures = result[layerID]; - if (resultFeatures === undefined) { - resultFeatures = result[layerID] = tileFeatures; - tileFeatures.forEach((tileFeature) => { - wrappedIDFeatures[tileFeature.id] = true; - }); - } else { - for (const tileFeature of tileFeatures) { - if (!tileFeature.id || !wrappedIDFeatures[tileFeature.id]) { - wrappedIDFeatures[tileFeature.id] = true; - resultFeatures.push(tileFeature); - } + const wrappedIDFeatures = wrappedIDLayers[layerID] = wrappedIDLayers[layerID] || {}; + const resultFeatures = result[layerID] = result[layerID] || []; + for (const tileFeature of tileFeatures) { + if (!wrappedIDFeatures[tileFeature.featureIndex]) { + wrappedIDFeatures[tileFeature.featureIndex] = true; + resultFeatures.push(tileFeature.feature); } } }