I have a geojsonObject that is a featurecollection of X number of features. see picture


I Then add this to a vector:

var Source = new ol.source.Vector({
        features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)

I add this to a layer, and call a style function:

var Layer = new ol.layer.Vector({
        source: Source,
        style: stylefcn(Source.getFeatures())

function stylefcn(_features) {
//When i log my features here, the order of the features have changed. 

Why does the order change?

E.g: Feature number 0 from the picture above, has become feature number 4 in the style function.

(I have a small suspicion that this might happen when the number of features pass 10)


From the doc:

Get all features on the source in random order.

| improve this answer | |
  • Gee! No quickFix then? I guess i need to tag all my features with a feature-id then? Or any suggestions for a good solution? :) – otk Jul 2 '19 at 11:28
  • 1
    To be honest I don't know. I systematically assume to be getting items from a data source in an unpredictable and inconsistent order unless I have explicitly specified a sort order. – JGH Jul 2 '19 at 11:33

To access features in order you would need a features collection:

var Source = new ol.source.Vector({
        features: new ol.Collection()

Source.addFeatures(new ol.format.GeoJSON().readFeatures(geojsonObject));

| improve this answer | |
  • Perfect :) Thanks. Both answers are "correct" for my part. But JGH's answer is the answer to the "Heading questions. Not possible to accept both. – otk Jul 2 '19 at 12:01
  • For me i found this solution to be the easiest one: github.com/openlayers/openlayers/issues/5729 by using useSpatialIndex: false, – otk Jul 2 '19 at 12:55

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.