Repository navigation
Description
mapbox-gl-js version
v3.29.0 and v3.35.1. The same code is on main (00740c7).
Browser and version
Chromium 156 (headless, Playwright).
Expected behavior
A GeoJSON source with dynamic: true should behave like a regular GeoJSON source for hit testing. queryRenderedFeatures at any point inside a polygon should return that polygon.
Actual behavior
When a polygon is larger than a tile, so that it gets clipped at the tile edges, queryRenderedFeatures misses it at points inside the tile. The fill still renders over the whole area.
In the demo below (one 0.02° square polygon, map at zoom 17, 400×400 canvas), I sample a 5×5 grid of points. All 25 are inside the polygon:
| features found at the 25 points | ring from querySourceFeatures |
|
|---|---|---|
dynamic: false |
25/25 | 5 coordinates (closed square) |
dynamic: true |
15/25 | 4 coordinates (a triangle) |
Link to the demonstration
The demo is inline below and needs only an access token. No style or tiles are required.
Steps to trigger the unexpected behavior
<div id="map" style="width:400px;height:400px"></div>
<link href="https://api.mapbox.com/mapbox-gl-js/v3.35.1/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.35.1/mapbox-gl.js"></script>
<script>
mapboxgl.accessToken = '<token>';
const DYNAMIC = true; // false: all points hit
const map = new mapboxgl.Map({ container: 'map', style: { version: 8, sources: {}, layers: [] }, center: [0, 51], zoom: 17 });
map.on('load', () => {
map.addSource('s', { type: 'geojson', dynamic: DYNAMIC, data: { type: 'FeatureCollection', features: [{
type: 'Feature', id: 1, properties: {},
geometry: { type: 'Polygon', coordinates: [[[-0.01, 50.99], [0.01, 50.99], [0.01, 51.01], [-0.01, 51.01], [-0.01, 50.99]]] }
}] } });
map.addLayer({ id: 'fill', type: 'fill', source: 's', paint: { 'fill-color': '#f00' } });
map.once('idle', () => {
let hits = 0;
for (const fx of [0.1, 0.3, 0.5, 0.7, 0.9]) for (const fy of [0.1, 0.3, 0.5, 0.7, 0.9]) {
if (map.queryRenderedFeatures([400 * fx, 400 * fy]).length) hits++;
}
console.log('hits', hits + '/25', map.querySourceFeatures('s').map(f => f.geometry.coordinates[0].length));
});
});
</script>Cause
transformAndClipPolygon in src/source/geojson_rt.ts clips each ring Sutherland–Hodgman style, but it does not close the result. When a ring is clipped, the output's last point is no longer equal to its first. Code downstream treats polygon rings as closed. For example, the vector tile writer drops the last point of each polygon ring, and the query and toGeoJSON paths expect closed rings. As a result:
- one real vertex of every clipped ring is lost (the square becomes a triangle);
- hit testing on that tile fails over the missing part.
Earcut doesn't need closed rings, so rendering looks fine. geojson-vt's clipPolygon closes clipped rings explicitly.
Closing the ring before it is written fixes it. With this change applied to the built worker, the demo hits 25/25 with 5-coordinate rings:
// end of transformAndClipPolygon, before building `ring`
if (clipped[0] !== clipped[clipped.length - 2] || clipped[1] !== clipped[clipped.length - 1]) {
clipped.push(clipped[0], clipped[1]);
}Relevant log output
dynamic: false hits 25/25 rings [5]
dynamic: true hits 15/25 rings [4]
Activity