为什么`ee.Image.clip`不适用下载指定区域数据?

首页 / 学习区 / 正文

踩坑经历

这是我在下载Landform数据时遇到的坑,一开始并不理解原因,后来经实践才找到了问题所在。

一开始,我使用了下面的写法,结果发现每个区域都会导出多个.tiff文件,且每个文件中的栅格值都为0。

for (var i = 0; i < shp_list.length; i++){
  var landform = dataset.select('constant').clip(shp_list[i]);
  Export.image.toDrive({
    image: landform,
    description: dir_names[i].split('/')[3] + "_landform",
    folder: "type1_landform",
    scale: 90,
    maxPixels: 314664204700
  })
}

我不知道这是为什么,查阅了文档后尝试增加了region参数,去除了datasetclip,这样做以后下载的结果就正确了,栅格的值都具备了正确的结果,并且它们都是按 shp 文件范围裁定好的一个外接矩形。

for (var i = 0; i < shp_list.length; i++){
  var landform = dataset.select('constant');  // 去掉 clip
  Export.image.toDrive({
    image: landform,
    description: dir_names[i].split('/')[3] + "_landform",
    folder: "type1_landform",
    scale: 90,
    maxPixels: 314664204700,
    region: shp_list[i]  // 手动加上region,成功!
  })
}

辨析两个方法

ee.Image.clip:Clips an image to a Geometry or Feature. The output bands correspond exactly the input bands, except data not covered by the geometry is masked. The output image retains the metadata of the input image.

Export.image.toDriveregionA LinearRing, Polygon, or coordinates representing region to export. These may be specified as the Geometry objects or coordinates serialized as a string. If not specified, the region defaults to the viewport at the time of invocation.

据我查证发现,clip方法将几何图形之外区域的蒙版值设为0,本质上就是屏蔽几何体之外的像素。但需要注意的是,clip方法是作用于图像(Image)而非图像集(ImageCollection)上的,因此我不应该在设置 dataset 的时候就使用clip方法。要想导出指定区域的数据,还是需要设定region参数。

参考

https://developers.google.com/earth-engine/apidocs/ee-image-clip

https://developers.google.com/earth-engine/apidocs/export-image-todrive

评论区
头像
文章目录