[GIS] Google earth engine: filter a FeatureCollection by system id

filtergoogle-earth-engine

I have a FeatureCollection in Google Earth Engine, where each Feature is referenced by an id tag. How do I filter the FeatureCollection based on that id?

Following attempts to filter id=3 did not work:

 // Create a FC (from https://developers.google.com/earth-engine/guides/feature_collections)
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'})
];
var FC = ee.FeatureCollection(features);
print(FC, "FC")

// extract basedon ID?
print(FC.filter(ee.Filter.eq("id", 2)))
print(FC.filter(ee.Filter.eq("system:id", 2)))

Best Answer

This is tricky, as there are actually two issues:

  • It's called system:index even though the UI shows it as id.

  • The id is a string, rather than an integer

So the correct way to do this is:

print(FC.filter(ee.Filter.eq("system:index", '3')))