I have generated a vector feature collection which is great but I would like to add to this data before saving it out as a CSV. So the data is areas of forest loss in the county of Gwent and I want to add extra fields like average, min and max elevation to each polygon.

How would I do that before I save out the CSV?

var fromList = ee.FeatureCollection("FAO/GAUL/2015/level2");

var filter = ee.Filter.inList('ADM2_NAME', ['Gwent']);
var filteredArea = fromList.filter(filter);

Map.setCenter(1.1, 51.5, 8);
Map.addLayer(filteredArea, {}, 'Regions');

var gfc2018= ee.Image('UMD/hansen/global_forest_change_2018_v1_6').clip(filteredArea);

var lossImage = gfc2018.select(['lossyear']).gte(7);
var lossBand = lossImage.and(gfc2018.select(['lossyear']).lte(20));
var zones = lossBand.updateMask(lossBand.neq(0));

// Add the loss layer in red.
            {palette: ['FF0000']}, 'Loss 2007 onwards');

var area_threshold = 300; / about 100 hectares?

// Define whole county as a region
var geom = filteredArea.geometry();

// Convert the zones of the forest loss areas to vectors.
var vectors = zones.addBands(zones).reduceToVectors({
  geometry: geom,
  crs: lossBand.projection(),
  scale: 30,
  geometryType: 'polygon',
  eightConnected: true,
  labelProperty: 'zone',
  reducer: ee.Reducer.count()
}).filterMetadata("count","greater_than", area_threshold);

// Make a display image for the vectors, add it to the map.
var display = ee.Image(0).updateMask(0).paint(vectors, '000000', 1);
Map.addLayer(display, {palette: '000000'}, 'vectors');

// Export the FeatureCollection to a CSV file.
  collection: vectors,
  description: 'Forest',
  fileFormat: 'CSV'

You will have to choose an elevation image (e.g. the SRTM), and apply reduceRegions with the vectors as input feature collection.

var SRTM = ee.Image("USGS/SRTMGL1_003"); // exampleelevation image

// add min, max and mean elevation of each geometry
var reducers = ee.Reducer.min()
                .combine(ee.Reducer.max(), '', true)
                .combine(ee.Reducer.mean(), '', true);
vectors = SRTM.reduceRegions(vectors, reducers, 30);

Properties are automatically names 'mean', 'min', and 'max', which is probably fine for your csv file. Link code

| improve this answer | |
  • Absolutely superb! Thank you so much!! – Rik Yapp May 23 at 16:29
  • Is there any way you can change the names too? – Rik Yapp May 24 at 13:03
  • see two examples in the link. Note that you will always have to look up how the properties are named, so you carefully rename the right values. – Kuik May 24 at 16:24
  • Fantastic! Feel like I am asking a lot but how could you then add a field from FAO/GAUL/2015/level2 to each shape such as ADM2_CODE? – Rik Yapp May 26 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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