Vector Tile API response id: (integer) Unique ID for the feature within the layer not found in hits or aggs layer

Hi
I am testing the Vector Tile API using this repo which is an excellent tool.
I would like to use the feature-id within the hits layer of a polygon, but get this: Error: The feature id parameter must be provided.
I do not find the id using:

map.on('click', () => {
// Find all features in one source layer in a vector source
const features = map.querySourceFeatures(sourceName, {
sourceLayer: renderMethod
})
console.log(JSON.stringify(features))
});

The Vector Tile API docs indicate that an id is contained in the response, but that does not appear to be the case or it is not being picked up on the client side.

@Joseph_Parish Thanks for bringing this up. I am seeing the same behavior.

For hits, feature.id is undefined, feature.properties._id contains the document id. For aggs, feature.id is undefined, feature.properties._key contains the bucket key.

This is confirmed in the Kibana integration tests. Notice the assert statement showing feature.id is getting returned as undefined.
https://github.com/elastic/kibana/blob/main/x-pack/test/api_integration/apis/maps/get_grid_tile.js#L43

      expect(clusterFeature.id).to.be(undefined);
      expect(clusterFeature.properties).to.eql({
        _count: 1,
        _key: '11/517/809',
        'avg_of_bytes.value': 9252,
      });

https://github.com/elastic/kibana/blob/main/x-pack/test/api_integration/apis/maps/get_tile.js#L54

      expect(feature.id).to.be(undefined);
      expect(feature.properties).to.eql({
        '@timestamp': '1442709961071',
        _id: 'AU_x3_BsGFA8no6Qjjug',
        _index: 'logstash-2015.09.20',
        bytes: 9252,
        'machine.os.raw': 'ios',
      });

Actually, vector tiles generated by the _mvt API do not use the id defined in the standard. This id is numeric but Elasticsearch ids are strings, therefore we are adding those identifiers as properties of the feature (hits later contains an _id property, aggs layer contains a _bucket _key property).

I have opened a PR to correct the documentation:

@Joseph_Parish apart from the answers provided by @Ignacio_Vera and @Nathan_Reese , maybe tangential comment.

I noticed you are using querySourceFeatures on a click-event. You might want to use queryRenderedFeatures (https://maplibre.org/maplibre-gl-js-docs/api/map/#map#queryrenderedfeatures) instead.

That is the method Kibana is using to query for features on a click-event: kibana/tooltip_control.tsx at e08712deef91950d1edca03af2133045206d4211 · elastic/kibana · GitHub (e.g. for tooltip on onClick event).

It takes into account the styling of the feature (e.g. line-thickness, symbol-size...). It targets a "layer", not a "source".

querySourceFeatures is lower level, grabbing all data in the visible tiles, and generally not the best to attach to a click-event.

Thanks for the quick replies! Actually I started with queryRenderedFeatures and then added querySourceFeatures when not finding a feature id in the geoJson. I am a bit unclear on the resolution. Is it correct that the id will be added as a string value (and docs fixed) ? The Mapbox GL feature states docs state that the feature.id can be an integer or a string, but supports string values only when ... the string can be cast to an integer.

We use the feature state for a variety of data styling & joining configs starting with this ([simple example])(Create a hover effect | Mapbox GL JS | Mapbox). The Vector API is huge development for our app and we are in the process of changing out pre-generated tiles for tiles served from this API

As you noted, ids can have different data types as long as they are integers :slight_smile: So no, we are not going to add values to those ids because our ids are not integers.

Unique identifiers are already added to the vector tiles as properties. In the hits layer you can find a property called _id and in the aggs layer you can find a property called _key.

See here how to use a property as an id field : An option to use a feature property as ID for feature state by mourner · Pull Request #8987 · mapbox/mapbox-gl-js · GitHub

Thanks I had assumed the promoteId was for GeoJson data sources only. This offers way more flexibility

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.