Skip to content
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

Open
jfirebaugh opened this issue Nov 9, 2017 · 4 comments
Open

Comments

@jfirebaugh
Copy link
Contributor

jfirebaugh commented Nov 9, 2017

Currently, queryRenderedFeatures or querySourceFeatures 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 or querySourceFeatures 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.

@1ec5
Copy link
Contributor

1ec5 commented Nov 17, 2017

For GeoJSON sources specifically, we may be able to change the behavior of queryRenderedFeatures or querySourceFeatures such that features are always returned with their original, input geometry, regardless of zoom level.

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.

@rigoneri
Copy link

rigoneri commented Jan 9, 2018

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?
Is it possible to reverse calculate the coordinate based on tile & zoom level?
Or should I also put the original coordinate as a property in the geojson?

@songololo
Copy link

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.

@chrispahm
Copy link

chrispahm commented Jun 11, 2021

As a workaround, I've been using the polygon-clipping libraries union function to merge the tiled results of querySourceFeatures .

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
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants