简体   繁体   English

如何将几何传递给 GEE 中的 Map 函数?

[英]How can I pass a geometry to the Map function in GEE?

I am trying to use the Map function in Google Earth Engine to clip an ImageCollection to a geometry.我正在尝试使用 Google Earth Engine 中的Map函数将 ImageCollection 裁剪为几何图形。 I have multiple areas of interest (AOIs) and thus would like to apply a generic clip function multiple times (for each AOI).我有多个感兴趣的区域 (AOI),因此想多次应用通用剪辑功能(对于每个 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 .但是,当我创建一个带有两个参数(图像和几何图形)来映射image.clip is not a function ,我得到错误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 (ie 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
});

If I understood your question correctly:如果我正确理解你的问题:

If you want to crop each image in the ImageCollection to a geometry, you could just do如果要将ImageCollection中的每个图像ImageCollection为几何图形,您可以这样做

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

And just repeat for other AOIs.对其他 AOI 重复此操作。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM