7

I'm trying to extract values from a FeatureCollection as a list. Here's some example code that I'm using in Earth Engine's code editor:

var viirs = 
    ee.Image('NOAA/VIIRS/DNB/MONTHLY_V1/VCMCFG/20120401').select('avg_rad');

var maineCounties = 
    ee.FeatureCollection('ft:1S4EB6319wWW2sWQDPhDvmSBIVrD3iEmCLYB7nMM'). 
    filter(ee.Filter.eq('StateName', 'Maine'));

var maineMeansFeatures = viirs.reduceRegions({
  collection: maineCounties,
  reducer: ee.Reducer.mean(),
  scale: 30
});

The "maineMeansFeatures" FeatureCollection has a column called "mean" -- I trying to extract the values in "mean" as a list (so, here, it'd be a list of 16 values -- one for each feature).

Any ideas on how to do this? (Note: I'm very new to javascript so I'm not that familiar with FeatureCollections).

1 Answer 1

10

Take a look at ee.FeatureCollection.aggregate_array:

Aggregates over a given property of the objects in a collection, calculating a list of all the values of the selected property.

So

maineMeansFeatures.aggregate_array("mean") 

should bring you an array of "means".

1
  • 1
    For those coming to this question (as I was) trying to get the list of unique values of Feature properties within a FeatureCollection, then use the .distinct() method on the FeatureCollection before using the .aggregate_array(): mainMeansFeatures.distinct("mean").aggregate_array("mean"); Note this would make more sense with categorical properties rather than float (in which case they are likely to all be unique!)
    – mikoontz
    May 21, 2021 at 14:20

Your Answer

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

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