依据shapefile文件从Google Earth Engine中下载点位数据

发布时间 2023-11-14 00:20:45作者: GEOLI

Google Earth Engine平台不仅有着快速的运算能力,其本身涵盖的大量数据也令人垂涎不已。这里分享下通过点状Shapefile文件和Export函数,将点位上的数据导出的代码。

首先,我们需要将shapefile文件上传到我们平台的assets中,具体方式不多介绍,网上自行搜索;下面代码中的table即我上传的点形状的shapefile;imageCollection可以GEE平台中任意的栅格数据,这里以简单的Worldpop人口数据为例

var table = ee.FeatureCollection("projects/xxxxx"),
var imageCollection = ee.ImageCollection("WorldPop/GP/100m/pop")

其次,我们可以对shapefile和栅格数据做一些预处理,比如按照shapefile中的字段进行过滤,比方这里是筛选出year等于2023的点位。同样的,如果我们也可以对栅格数据做同样的处理,选择自己感兴趣的区域、时间,尽量保证研究时间仅包含一景影像;

var table = table.filter(ee.Filter.eq("year","2023"))
var imcol = imageCollection.filterDate("2020-1-1", "2021-1-1").select(['population']).sum()
var imcol = ee.ImageCollection.fromImages([imcol])

接着,定义fill函数,用于我们对栅格数据采样。文档中sampleRegions的解释是:“Converts each pixel of an image (at a given scale) that intersects one or more regions to a Feature, returning them as a FeatureCollection.”,所以将scale设置成影像的空间分辨率即可。properties设置为shapefile中想要在输出结果中保留的字段。

function fill(img, ini) {
  var ft1 = ee.FeatureCollection(ini)
  var ft2 = img.sampleRegions({
    collection:table,
    properties:["name","latitude","longitude"],
    scale:100
  });
  return ft1.merge(ft2)
}

最后是导出代码。定义变量newft,作为容器,存储从imcol获取的值。在fill函数迭代完之后,整体导出到Google Drive。

var newft = ee.FeatureCollection(ee.List([]))
newft = ee.FeatureCollection(imcol.iterate(fill,newft))
Export.table.toDrive({
     collection: newft,
     description: "Worldpop",
     folder: "folder",
     fileFormat: 'csv',
     selectors:["name","latitude","longitude","population"]
});```