0

Using OpenLayers 4. I can get the list of drawn features by calling getFeatures(), but calling getId() on each feature returns "undefined". Here is a screenshot from console with the list of drawn features.

enter image description here

As far as I see "c" is the "id" which is returned by getId() and is "undefined" for all drawn features by default. So do drawn features have some automatically generated unique ids? The "xp" value seems to be the one. how can I get it then?

  • Possible duplicate of stackoverflow.com/questions/34547408/… – JGH May 2 '18 at 1:19
  • 1
    You can use ol-debug.js to get meaningful variable names – JGH May 2 '18 at 1:20
  • Thanks for the hint @JGH. The variable is called ol_uid. Is it possible to get its value somehow? – yaugenka May 2 '18 at 18:21
1

You can get the variable content of a given feature, such as xp using

feature.xp

For instance, to label each feature using its xp value, modify the example with

var vectorLayer = new ol.layer.Vector({
  source: new ol.source.Vector({
    url: 'https://openlayers.org/en/v4.6.5/examples/data/geojson/countries.geojson',
    format: new ol.format.GeoJSON()
  }),
  style: function(feature) {
    style.getText().setText(feature.xp.toString());
    return style;
  }
});

However since it is an internal variable name, there is no guarantee that it remains the same between versions. Similarly, there is no guarantee that a given feature keeps the same ID all the time. It is therefore dangerous to rely on this information, and even if it works today, it will eventually blow up one day.

As identified in the two related/duplicate answers and in @ThomasG77 answer, in order to use a reliable ID, you would need to manually set the ID when loading the data.

  • Thanks, JGH. It works. Do you happen to known if the "xp" variable name can get changed, e.g. at each OpenLayers release or is it safe to use it? – yaugenka May 3 '18 at 20:42
  • Unsafe: it related to the build system & it will change along time – ThomasG77 May 4 '18 at 2:16
  • @yaugenka see edited answer – JGH May 4 '18 at 12:55
0

So do drawn features have some automatically generated unique ids?

Features have generated id you can get with feature.getRevision() This id is mainly useful to manage when editing a vector layer otherwise don't use them as a primary key as they can change.

If you need a stable id for each feature, you have to set the id yourself using the recipe already described at this related question "OpenLayers 3: Access feature by ID"

  • It is a different variable, not the internal uid. – JGH May 3 '18 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.