2

I am trying to use the Map function in Google Earth Engine to clip an ImageCollection to a geometry. I have multiple areas of interest (AOIs) and thus would like to apply a generic clip function multiple times (for each AOI). However, when I create a function with two parameters (the image and the geometry) to map over, I get the error image.clip is not a function. When using the function with just one parameter (the image), it works just fine, but then I need to write two (or possible more) functions to do exactly the same task (i.e. clipping an image to a certain geometry).

I have tried the solutions in this post, but they do not work.

Any ideas on how to solve this issue?

Code:

// Get NTL data
var ntl = ee.ImageCollection("NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG");

// Define start and end year
var startYear = 2015;
var endYear = 2018;

// Filter montly and select 'avg_rad' band
var ntlMonthly = ntl.filter(ee.Filter.calendarRange(startYear, endYear, 'year'))
  .filter(ee.Filter.calendarRange(1,12,'month'))
  .select(['avg_rad']);  

// Create geometries of AOIs
// -- Create a geometry of Venezuela 
var geomVenezuela = ee.Geometry.Rectangle([-73.341258, 13.363291, -59.637555, -0.372893]);
// -- Create a geometry of Caracas (Venezuela's capital)
var geomCaracas = ee.Geometry.Rectangle([-67.062383, 10.558489, -66.667078, 10.364908]);

// Functions to crop to Venezuela (nationwide) and Caracas (local) resp.
var clipImageToGeometry  = function(image, geom) {
  return image.clip(geom);
}

// Apply crop function to the ImageCollection 
var ntlMonthly_Venezuela = ntlMonthly.map(clipImageToGeometry.bind(null, geomVenezuela));
var ntlMonthly_Caracas = ntlMonthly.map(clipImageToCaracas.bind(null, geomCaracas));

// Convert ImageCollection to single Image (for exporting to Drive)
var ntlMonthly_Venezuela_image = ntlMonthly_Venezuela.toBands();
var ntlMonthly_Caracas_image = ntlMonthly_Caracas.toBands();

// Check geometry in map
Map.addLayer(geomCaracas, {color: 'red'}, 'planar polygon');
Map.addLayer(ntlMonthly_Caracas_image);

// Store scale (m. per pixel) in variable
var VenezuelaScale = 1000;
var CaracasScale = 100;

// Export the image, specifying scale and region.
Export.image.toDrive({
  image: ntlMonthly_Caracas_image,
  description: 'ntlMonthly_Caracas_'.concat(startYear, "_to_", endYear),
  folder: 'GeoScripting',
  scale: CaracasScale,
  fileFormat: 'GeoTIFF',
  maxPixels: 1e9
});
Sytze
  • 345
  • 2
  • 12

1 Answers1

2

If I understood your question correctly:

If you want to crop each image in the ImageCollection to a geometry, you could just do

var ntlMonthly_Venezuela = ntlMonthly.map(function(image){return ee.Image(image).clip(geomVenezuela)})

And just repeat for other AOIs.

If you wan to cast it into a function:

var clipImageCollection = function(ic, geom){

  return ic.map(function(image){return ee.Image(image).clip(geom)})

}

// Apply crop function to the ImageCollection 
var ntlMonthly_Venezuela = clipImageCollection(ntlMonthly, geomVenezuela);
var ntlMonthly_Caracas = clipImageCollection(ntlMonthly, geomCaracas);
Val
  • 6,585
  • 5
  • 22
  • 52
  • Thanks! This is indeed what I tried to achieve; I didn't think of putting the map function directly in the custom function... – Sytze Jan 28 '20 at 10:04