Google Earth Engine: Extract percentiles from raster by polygons

google-earth-engine

I have an image with land surface temperatures (LST) and polygons with administrative boundaries (adm3) and want to extract and export spatial statistics as a shapefile of said polyogns so that each feature contains the average raster value.

This works for the mean reducer

// Apply mean reducer 
 var means = LST.reduceRegions({
  collection: adm3,
  scale: 30,
  reducer: ee.Reducer.mean()
});

// Create empty feature
var feature = ee.Feature(null, means);

// Wrap the Feature in a FeatureCollection for export.
var featureCollection = ee.FeatureCollection([feature]);

// Export the FeatureCollection.
Export.table.toDrive({
  collection: featureCollection,
  folder: 'GEE_VN',
  description: 'adm3_LST_mean',
  fileFormat: 'SHP'
});

However, when I want to apply a percentile reducer (10 and 90 percent), the export fails

var p10 = LST.reduceRegions({
  collection: adm3,
  scale: 30,
  reducer: ee.Reducer.percentile([10]) 
});

This is the error message and I see that this is because the percentile reducer returns a dictionary, but I can't manage to address the 10p percentile correctly.

Error: Feature, argument 'metadata': Invalid type. Expected type: Dicationary<Object>. Actual type: FeatureCollection (Error code: 3)

Where do I have to convert the dictionary to a collection?

Best Answer

The output of reduceRegions with a percentile reduce ris already a featureCollection, so no need (and not possible) to define it as a feature and featureCollection. You can just export it directly:

Export.table.toDrive({
  collection: p10,
  folder: 'GEE_VN',
  description: 'adm3_LST_mean',
  fileFormat: 'SHP'
});

With a working example: https://code.earthengine.google.com/1a7916d16dff7991a52bb264e5150c0b