0

Dear Earth Engine community,

Can someone help me solving the following problem: I want to compute the aggregate nightlight intensities (sum) within all first level administrative regions of the world. For that purpose I use a shapefile which contains the regional boundaries (GADM) and raster data on nightlight (VIIRS).

The issue with the following code is that 1) I am getting an error that say "Unknown element type provided: object. Expected: ee.Image, ee.ImageCollection, ee.FeatureCollection or ee.Element." for the nighttime.reduceRegion operation and 2) that only the last feature of the selection is returned on print(final).

Unfortunately I do not manage to solve these problems. It would be great if someone could help me improving the code. I am sure there are many issues since Javascript and the Earth Engine API are completely new to me..

Thanks a lot in advance!


// Import nighttime raster data.
var nighttimeCollection = ee.ImageCollection('NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG');
// Import shapefile containing region boundaries.
var region_boundaries = ee.FeatureCollection("users/hendrikscheewel/gadm_level_1");

// Select a specific year ::: Later this should be done within a loop.
var year = "2014";

// Aggregate monthly nighttime data to year x.
var nighttime = nighttimeCollection
                .filter(ee.Filter.date(year+'-01-01', year+'-12-31'))
                .select('avg_rad')
                .reduce(ee.Reducer.mean());

// This function does the following:
// * Aggregrate nightlight data within a shape/feature by taking its sum,
// * Assign the result to the feature,
// * Create copy of feature with simplified geometry (centroid) and fewer columns,
// * Return the copy.
var compute_nightlight = function(feature) {

  // Compute mean of average radiance for feature shape
  var result = nighttime.reduceRegion({
      geometry: feature.geometry(),
      reducer: ee.Reducer.sum(),
      scale: 30,
      maxPixels: 1e9,
    });

  // Set "nightlight" as a new property.
  feature = ee.Feature(feature.set('nightlight',result.get('avg_rad_mean')));
  
  // Get the centroid of the feature's geometry.
  var featureSimplified = feature.centroid();
  
  // Keep this list of properties.
  var keepProperties = ['GID_0','GID_1','NAME_0','NAME_1','nightlight'];
  featureSimplified = featureSimplified.copyProperties(feature, keepProperties);
  
  // Return a new Feature, copying properties from the old Feature.
  return featureSimplified;
};

//print(compute_nightlight(region_boundaries.first()));
var final = region_boundaries.filter(ee.Filter.eq('NAME_0','Belgium')).iterate(compute_nightlight);

print(final)

Export.table.toDrive({
  collection: final,
  description: 'nl_'+year,
  fileFormat: 'CSV'
});
hendriksc1
  • 59
  • 5

1 Answers1

0

Ok, I found my main mistake: Instead of using the .iterate() method I should have used the .map() method.

After some cleaning the code looks like this:

// Select a specific year
var year = "2014";

// Aggregate monthly nighttime data to year x.
var nighttime = nighttimeCollection
                .filter(ee.Filter.date(year+'-01-01', year+'-12-31'))
                .select('avg_rad')
                .reduce(ee.Reducer.mean());

// This function does the following:
// * Aggregrate nightlight data within a shape/feature by taking its sum,
// * Assign the result to the feature,
// * Create copy of feature with simplified geometry (centroid) and fewer columns,
// * Return the copy.
var compute_nightlight = function(feature) {

  // Compute mean of average radiance for feature shape
  var result = nighttime.reduceRegion({
      geometry: feature.geometry(),
      reducer: ee.Reducer.sum(),
      scale: 30,
      maxPixels: 1e9,
    });

  // Set "nightlight" as a new property.
  feature = ee.Feature(feature.set('nightlight',result.get('avg_rad_mean')));

  // Return a new Feature, copying properties from the old Feature.
  return feature.centroid();
};

var final = ee.FeatureCollection((region_boundaries).map(compute_nightlight));

Export.table.toDrive({
  collection: final,
  description: 'nl_'+year,
  fileFormat: 'CSV'
});

hendriksc1
  • 59
  • 5