-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
For GeoJSON sources, can query*Features return the original geometry? #5639
Comments
This change has also been requested on the native side in mapbox/mapbox-gl-native#6178 and mapbox/mapbox-gl-native#7376, where it could help to unblock reimplementing annotations atop GeoJSON sources. |
I'm trying to use the coordinate out of the querySourceFeatures to animate a marker, however since the coordinate changes on different zoom levels I run into issues where the coordinate is "invalid" so the marker "disappears". Is there a temporary alternative to getting the original coordinate? |
In my case I'm trying to incorporate dynamic behaviour such that when someone selects a feature's Id (elsewhere in the app) I'd like to be able to query the feature (to derive the centroid or bounds) and then fly towards (or fit the bounds) of the selected feature. When zoomed-in and the feature is off-screen, or if only partially (split over adjacent tiles?) then the query returns null or off-centre locations, respectively. Long story short, it would be really useful to have access to the original features, or at least the original feature centroids or bounds. This saves having to preprocess the geometry elsewhere and storing bounds / centroids. |
As a workaround, I've been using the polygon-clipping libraries Leaving it here in case it's useful for someone: import { union } from 'polygon-clipping'
function merge (inputs) {
const output = {
id: inputs[0].id,
type: inputs[0].type,
geometry: {
coordinates: union(...inputs.map(i => i.geometry.coordinates)),
type: 'MultiPolygon'
},
properties: inputs[0].properties
}
return output
}
map.on('click', (e) => {
const features = map.queryRenderedFeatures(e.point)
if (features.length) {
const sourceFeatures = map.querySourceFeatures(features[0].source, {
sourceLayer: features[0].sourceLayer,
filter: ['in', 'ID', features[0].properties.ID]
})
const originalFeature = merge(sourceFeatures)
// work with the retained feature
}
}) |
Currently,
queryRenderedFeatures
orquerySourceFeatures
on a GeoJSON source return geometries that have gone through a round trip to tiled form, reducing coordinate precision and therefore giving different coordinates at different zoom levels. This has proven to be confusing: #5638, #4733, #5397, mapbox/mapbox-gl-native#8992.For GeoJSON sources specifically, we may be able to change the behavior of
queryRenderedFeatures
orquerySourceFeatures
such that features are always returned with their original, input geometry, regardless of zoom level.I can't guarantee that we'll be able to do this -- there are considerations that we'll need to work through, like whether this means that GeoJSON sources will have different behavior with regards to features being clipped along tile edges, and whether we'd have to give up on #3220. But we should at least investigate it.
The text was updated successfully, but these errors were encountered: