4

I have a FeatureCollection such as:

// Make a list of Features.
var features = [
  ee.Feature(ee.Geometry.Rectangle(30.01, 59.80, 30.59, 60.15), {name: 'Voronoi'}),
  ee.Feature(ee.Geometry.Point(-73.96, 40.781), {name: 'Thiessen'}),
  ee.Feature(ee.Geometry.Point(6.4806, 50.8012), {name: 'Dirichlet'})
];

// Create a FeatureCollection from the list
var fromList = ee.FeatureCollection(features);

How can I extract Features from the FeatureCollection using their index position within the FeatureCollection?

For example, fromList[0] would return the Feature named "Voronoi".

1 Answer 1

8

For the first and last elements of a collection, there are shortcuts collection.first() and collection.last().

For the nth element of a collection, cast the collection to a list, then get the nth element of the list.

var second_feature = collection.toList(3).get(1)

This would cast the first three elements of the feature collection to a list, then return the second element (zero-indexed). Allthough EarthEngine in many cases handles it automatically, to be save, recast the returned element into a feature.

var second_feature = ee.Feature(collection.toList(3).get(1))
2
  • Dear Christoph, i tested above lines of code, but are you sure of last method is running? Commented Nov 19, 2021 at 11:19
  • great answer! I would emphasize even more the fact that one needs to use n-1 as index, to avoid the easy confusion that it is in fact zero indexed!?
    – Matifou
    Commented Dec 12, 2021 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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