mapbox / mapbox-gl-js

Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
https://docs.mapbox.com/mapbox-gl-js/
Other
11.15k stars 2.21k forks source link

querySourceFeatures returns features outside the viewbox #13254

Open dee-cf opened 2 months ago

dee-cf commented 2 months ago

mapbox-gl-js version: 3.1.2

Question

We use querySourceFeatures to query features in our vector tiles (mvt). Based on the documentation, we would expect only features within the map viewport to be returned by the method. Relevant part of the docs:

The domain of the query includes all currently-loaded vector tiles and GeoJSON source tiles: this function does not check tiles outside the currently visible viewport.

What happens though is that the method sometimes returns features that are not within the viewport boundaries. The only way I could explain that behavior is that it could happen if the we are in a state between 2 zoom levels meaning that the tile doesn't end precisely on the edge of the viewport so that a part of the tile is within the viewport and part is out. And in that case the method returns features within the tile but outside of the viewport.

Questions are:

  1. Can someone confirm if that is the case? If so, should we do additional filtering of features based on the viewport bounding box?
  2. If so, could that be explained a bit more clear in the docs?

PS. Another source of confusion is that the description says the method behaves the same as queryRenderedFeatures with the difference that it looks for features that might be invisible. But I have never seen queryRenderedFeatures returning features outside of the viewport.

Links to related documentation

https://docs.mapbox.com/mapbox-gl-js/api/map/#map#querysourcefeatures

mourner commented 2 months ago

this function does not check tiles outside the currently visible viewport

Perhaps we could improve the clarity of documentation about this. querySourceFeatures indeed returns all features inside tiles loaded for the current viewport, and these tiles are often visible only partially — they are rarely aligned to viewport.

To see how this works, set map.showTileBoundaries = true in your code. If you want the features filtered by viewport strictly, you should do the additional filtering on the app side.

dee-cf commented 2 months ago

Thanks for the clarification!

Yeah, putting a sentence about it in docs would help.

thanks!