GEE request调用自定函数文件以sentinel影像和ndvi下载为例

GEE request调用自定函数文件以批量下载sentinel影像和ndvi为例

由于经常需要批量下载某些指定日期的sentinel image和ndvi,故研究一下,写个函数方便以后项目的高效复用。现借助GEE的request功能来实现,话不多说,详见代码。

1.定义函数文件.js

在这里插入图片描述

exports.get_s2a = function get_s2a(timelist,geometry,buffer,folder){
var S2A = ee.ImageCollection("COPERNICUS/S2")

// buffer
var area = geometry.map(function(roi){return roi.buffer(buffer)})

// ndvi
var s2_ndvi = function(image){
  return image.addBands(image.normalizedDifference(["B8","B4"]).rename("NDVI"));
};

function get_ndvi(start_time){
  var end_time = ee.Date(start_time).advance(1,'day')
  var s2 = S2A.map(s2_ndvi)
  .filterBounds(area)
  .filterDate(start_time,end_time)
  // .filterMetadata("CLOUD_COVERAGE_ASSESSMENT","less_than",60)
  // .sort("CLOUD_COVERAGE_ASSESSMENT");
  var  new_ndvi = s2.max().clip(area).select("NDVI");
  return new_ndvi;
};

// image
function get_image(start_time){
  var end_time = ee.Date(start_time).advance(1,'day')
  var s2 = S2A.filterBounds(area).filterDate(start_time,end_time)
  .filterMetadata("CLOUD_COVERAGE_ASSESSMENT","less_than",80)
  .sort("CLOUD_COVERAGE_ASSESSMENT");
  var new_image = s2.mosaic().clip(area);
  return new_image;
}

// export
function export_tiff(input, till,folder){
  var exp = Export.image.toDrive ({
  image: input,
  description:till,
  fileNamePrefix:till,
  scale:10,
  region:area,
  maxPixels:9999999999999,
  crs:'EPSG:4326',
  folder:folder
});
return exp;
};

var res = ee.List([])
for(var i=0;i<timelist.length;i++){
  var start_time = timelist[i]
  var ndvi = get_ndvi(start_time)
  var image = get_image(start_time)
  var res = res.add(image.select(['B2','B3','B4','B8','B11']).toUint16())
  var res = res.add(ndvi.toFloat())
  export_tiff(image.select(['B2','B3','B4','B8','B11']).toUint16(),'image'+start_time)
  export_tiff(ndvi.toFloat(),'ndvi'+start_time)
};
return res
};

2.调用函数

var sentinel = require('users/mxhoubo/requires:get_sentinel_ndvi.js')
var imageVisParam4 = {"opacity":1,"bands":["B8","B4","B3"],"min":1251.4,"max":3046.6,"gamma":1.915}
var geometry = ee.FeatureCollection('users/mxhoubo/china/city').filterMetadata('CityName','equals','潍坊市')
var timelist = ['2022-05-21','2022-06-20','2022-06-25']
var res= sentinel.get_s2a(timelist,geometry,2000,'weifang')
var res = ee.ImageCollection.fromImages(res)
print(res)
var images = res.first()
print(images)
Map.addLayer(images,imageVisParam4,'image')
Map.addLayer(geometry)
Map.centerObject(geometry,8)

3.结果如下:


GEE request调用自定函数文件以sentinel影像和ndvi下载为例_第1张图片
以上就是本期推文的全部内容了,如果对你有帮助的话,请‘点赞’、‘收藏’,‘关注’,你们的支持是我更新的动力。

你可能感兴趣的:(gis,GEE,sentinel,javascript,开发语言)