Google Earth Engine – Subsetting FeatureCollection by Position in Google Earth Engine

filtergoogle-earth-enginesubset

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".

Best Answer

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))
Related Question