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

query*Features should preserve non-integer feature IDs #2716

Open
danvk opened this issue Jun 10, 2016 · 38 comments
Open

query*Features should preserve non-integer feature IDs #2716

danvk opened this issue Jun 10, 2016 · 38 comments

Comments

@danvk
Copy link

danvk commented Jun 10, 2016

Here's a jsbin demonstrating the issue:
https://jsbin.com/bafewapela/edit?html,console,output

The GeoJSON spec states that features may have a top-level id property:

If a feature has a commonly used identifier, that identifier should be included as a member of the feature object with the name "id".

However, when I have a source with such a feature:

var markers = {
    "type": "FeatureCollection",
    "features": [{
        "id": "my-feature-id",  // <--- feature has an ID
        "type": "Feature",
        "properties": {
            "marker-symbol": "theatre",
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-77.038659, 38.931567]
        }
    }]
};

add it to the map and then get features back via queryRenderedFeatures, the id is gone. The properties and geometry properties are there, but id is not. It would be really helpful if this special property were preserved.

The workaround is to duplicate IDs into properties.id before adding features to the map, but this creates extra work and contradicts the GeoJSON spec.

mapbox-gl-js version: 0.19.1

@jfirebaugh
Copy link
Contributor

Upstream PR which this depends on: mapbox/geojson-vt#60.

@lucaswoj lucaswoj changed the title GeoJSON features lose their IDs Return features with their original ids from query*Features Jul 29, 2016
@mourner mourner reopened this Aug 9, 2016
@mourner
Copy link
Member

mourner commented Aug 9, 2016

This was fixed on the geojson-vt side, but now ids don't survive the transfer through vt-pbf. We'll probably want to switch to something like Geobuf.

@lucaswoj
Copy link
Contributor

lucaswoj commented Aug 9, 2016

@mourner I'm excited to (finally!) make geobuf part of GL JS!!

Have you considered transferring the feature ids through a specially named property, like $id?

@mourner
Copy link
Member

mourner commented Aug 10, 2016

@lucaswoj we could do this, but this feels kinda hacky and fragile to me (e.g. what if the data has both id and a property $id?). It might be good enough as a stopgap though.

@lucaswoj
Copy link
Contributor

what if the data has both id and a property $id

Then our $id filtering will break anyway!

@anandthakker
Copy link
Contributor

@mourner see mapbox/vt-pbf#8

@mourner
Copy link
Member

mourner commented Aug 15, 2016

@anandthakker thanks! I was referring to the vector-tile-spec limitation of having ids as uints which John pointed out in the PR, so the original issue would still be present as string ids are not encoded.

@anandthakker
Copy link
Contributor

Ahhh, this problem goes deeper than I realized -- I read too quickly 😄. And anyway, the geobuf thing sounds like a much better solution, ultimately!

@anandthakker
Copy link
Contributor

Note that per #4494 (comment), polygon layers manifest this problem more severely, crashing (due to pbf parsing problems) rather than simply stripping the id. This may indicate an upstream bug in vt-pbf

@gmaclennan
Copy link
Contributor

The crashing was introduced by a change in v0.33.0. Prior to that polygon layers would simply strip the id but continue to work.

@gmaclennan
Copy link
Contributor

gmaclennan commented Mar 29, 2017

@anandthakker just thinking about this a little more. It's your choice, but I'm wondering if this makes sense to be the same issue? query*Features() has always ignored non-integer ids, and it seems non-trivial to fix. Crashing when adding GeoJSON polygons with non-integer ids is a related bug, but it is a new bug introduced by a recent change, and seems like it needs a fix outside the fix to this bug. At least the bug label should be added here, since this part is not a new feature request.

@anandthakker
Copy link
Contributor

good point @gmaclennan - I think you're right. I'll reopen 4944 to track the crash. Thanks for following up / setting me straight!

@mike-marcacci
Copy link
Contributor

Hi there! It's looking like the lack of string ID support is going to be a blocker for me shortly, and I'm definitely willing to work on a PR to get this fixed. I just want to double check: is the plan here still to swap in geobuf for the current encoding scheme when communicating between the main js thread and the worker?

@mike-marcacci
Copy link
Contributor

So, today's the day I need to start working on this – I'm going to try swapping in geobuf for communication to the web worker, and I'll submit a PR if I get it working, but I would definitely appreciate an early nudge in a better direction or confirmation that this is still the best strategy to fixing this.

@anandthakker
Copy link
Contributor

Hey @mike-marcacci -- sorry for the delay! Confirming that using geobufinstead of vt-pbf for transferring geojson data back to the main thread does seem like our most promising option. A PR attempting this would be most welcome!

@asheemmamoowala
Copy link
Contributor

Are there any performance benefits from adding root level uint ids to features, for example when updating sources using in setData()?

@hyperknot there are no performance benefits, but the Map#featureState APIs only work with uint ids, so there is the performance benefit if using that option.

@aliir74
Copy link

aliir74 commented Jan 14, 2019

Why mapbox didn't support string ids? I read comments on this issue but don't understand why id forced to be number?
For preventing from brute force I need some random string id not integer.

@bloigge
Copy link

bloigge commented Jan 14, 2019

I am confused as well. For example it makes things unnecessarily difficult if you are working with geojson WFS data hosted from a geoserver where the id comes as a string in the format <layer-name>.<serial-id>. It would be awesome if mapbox-gl could preserve non-integer feature IDs on geojson features

@strech345
Copy link

For me it is also important, because we use ubid for buildings which is a string. To use states we need to use this as id.

@karen1au
Copy link

Is there any update on the type for feature.id ? I'm using uuid (string) as well so allowing string would keep things consistent.

@pgoshulak
Copy link

Is there any update on the type for feature.id ? I'm using uuid (string) as well so allowing string would keep things consistent.

Bump

Same, our project uses String type ids everywhere for our polygons but in our Mapbox components we can't use these :(

@jeanpul
Copy link

jeanpul commented Jan 24, 2020

Is there any update on the type for feature.id ? I'm using uuid (string) as well so allowing string would keep things consistent.

Bump

Same, our project uses String type ids everywhere for our polygons but in our Mapbox components we can't use these :(

Same here, I'm using mapbox-gl-draw that returns IDs on feature but then removed when using setData

@asheemmamoowala
Copy link
Contributor

While this is not directly supported now, starting with v1.7.0 you can use promoteId (ref: #8987) to use a string-type feature property as the identifier for feature-state operations

@mingjunlu
Copy link

mingjunlu commented Apr 1, 2020

@asheemmamoowala Thank you! Using promoteId solved my problem.

@karen1au
Copy link

karen1au commented Apr 2, 2020

@asheemmamoowala thanks for replying! I'm trying to use promoteId with my geojson type source, however after adding that, the feature simply doesn't render on screen anymore... am I missing anything??

this.map.addSource(sector.id, { type: 'geojson', promoteId: sector.id, data: { type: 'Feature', geometry: { type: 'Polygon', coordinates: sector.boundaries.coordinates } } })

@mingjunlu
Copy link

mingjunlu commented Apr 4, 2020

@karen1au does this work?

this.map.addSource(sector.id, {
  type: 'geojson',
  promoteId: 'id',
  data: {
    type: 'Feature',
    geometry: {
      type: 'Polygon',
      coordinates: sector.boundaries.coordinates
    },
    properties: {
      id: sector.id
    }
  }
});

@chriszrc
Copy link

@mingjunlu that works, but I think @karen1au is maybe wondering (and me too), if there's a way to use the feature.id property rather than the feature.properties.id

@hmendezm
Copy link

hmendezm commented Aug 7, 2020

Hi, @chriszrc Do you find the way to use the feature.Id as promoted? I do not have Id in properties and I do not have control over the data. Also, I want to avoid adding the id in properties because of performance.
best
hmendez

@chriszrc
Copy link

chriszrc commented Aug 7, 2020

@hmendezm unfortunately no. I moved the id to the properties, and then it worked. I never was able to have it work from feature.id

@hmendezm
Copy link

hmendezm commented Aug 7, 2020

ohk. Thanks @chriszrc. in the case, I have to do the same. sucks

CloudNiner added a commit to CloudNiner/adiffer that referenced this issue Oct 16, 2020
Refactor to remove react-mapbox-gl dep, since our
use case is simple (just lots of layer declarations)
and the dependency adds complexity and larger bundles.

String geojson feature IDs don't get copied into the
vector tiles, thus can't be used for "feature state":
    mapbox/mapbox-gl-js#2716

Work around just uses osm object id via "promoteId".
It's possible but unlikely that two objects of different
types could share the same int id in a single diff. I
think the behavior in this case is just that both
elements with the same id would receive the same hover state.
The duplicate is likely offscreen.
@felix-ht
Copy link

felix-ht commented Nov 8, 2021

Even tho promoteId works, this is pretty clunky to use. So allowing for strings in ids directly would be much nicer!

@sienki-jenki
Copy link

Bumping. Any update on that? When string id will be preserved without any additional config like promoteId?

@jaybo
Copy link

jaybo commented Oct 16, 2023

Bumping. I run into this on an annual basis, and each time I can't believe this incompatibility with the GeoJSON spec hasn't been fixed yet! I'd have to say it's my major pain point using Mapbox.

@michaeljfazio
Copy link

How is this still a thing....

@michaelabela
Copy link

Bumping this. Running into major headaches not being able to use strings as feature ids.

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

Successfully merging a pull request may close this issue.