3.1节 循坏for,while,map,evaluate和iterate的异同

  • 利用 landsat8 的数据,导出 2013-2020 年武汉市年平均 NDVI 值的 GIF 图

  • 实现同一个目的,分别展示for,map,evaluate,iterate 之间的不同用法

1 结果显示

NDVI

真彩色

2 异同

  • for、while:就是基本的循环结构语句,但是在GEE里很少用,基本用 map 替代,因为是顺序执行代码,效率太慢

  • map:用的最多,执行效率最快,相当于“并行执行任务”,能用 map,就用 map,但是函数里一般不能用 print,Map,toDrive 等方法

  • iterate:“迭代”,顺序执行,每次执行都在前面执行的结果上继续执行,很遗憾,这次的例子没有体现“迭代”的意思

  • evaluate:主要用来异步导出和显示,加快速度,一般没有返回值,结合 for 和 map 一起用,这里的 map 里可以有 print,Map,toDrive 方法

3 代码

// 去云操作
var maskcloud = function(image) {
  var qa = image.select('BQA');
  var mask = qa.bitwiseAnd(1 << 4)
            .or(qa.bitwiseAnd(1 << 8));
  return image.updateMask(mask.not());
};

// 计算NDVI
var ndvi = function(image){
      var ndvi = image.normalizedDifference(['B5','B4']).rename('NDVI');
      return image.addBands(ndvi)
    };

var roi = ee.FeatureCollection("users/comingboy1118/China/CH_shi");

var roi = roi.filter(ee.Filter.eq("市","武汉市")).geometry()
Map.addLayer(roi,{"color":'red',},"roi")
Map.centerObject(roi, 8)

// for 很少用,顺序执行,计算慢
var time = ["2013","2014","2015","2016","2017","2018","2019","2020"];
var list = ee.List([])//
for(var index in time){
  var year = time[index]+"-01"+"-01"
  var image = ee.ImageCollection("LANDSAT/LC08/C01/T1_RT_TOA")
    .filterBounds(roi)
    .filterDate(year,ee.Date(year).advance(1,"year"))
    .map(maskcloud)
    .map(ndvi)
    .median()
    .select(["B2","B3","B4","NDVI"],['blue','green','red','NDVI'])
    .clip(roi)
    .setMulti({"system:index":year,'system:time_start':ee.Date(year)})
  list = list.add(image)
}
var imgMonth = ee.ImageCollection.fromImages(list);
print(imgMonth,"imgMonth_for")

// map 一般计算最快,用的最多,异步计算,能用map,就用map,但是函数里一般不能用print,Map,toDrive
var time = ee.List.sequence(2013,2020)
var imgMonth = time.map(function(year){
  var year = ee.Number(year).format("%04d").cat("-01-01")
  var image = ee.ImageCollection("LANDSAT/LC08/C01/T1_RT_TOA")
    .filterBounds(roi)
    .filterDate(year,ee.Date(year).advance(1,"year"))
    .map(maskcloud)
    .map(ndvi)
    .median()
    .select(["B2","B3","B4","NDVI"],['blue','green','red','NDVI'])
    .clip(roi).setMulti({"system:index":year,'system:time_start':ee.Date(year)})

    return image
  })
var  imgMonth = ee.ImageCollection(imgMonth)
print(imgMonth,"imgMonth_map")

// itetate 迭代的方式和 for 很相似,都是往原list添加元素
var time = ee.List.sequence(2013,2020)
var filterImg = function(year,first){

  var year = ee.Number(year).format("%04d").cat("-01-01")
  var image = ee.ImageCollection("LANDSAT/LC08/C01/T1_RT_TOA")
    .filterBounds(roi)
    .filterDate(year,ee.Date(year).advance(1,"year"))
    .map(maskcloud)
    .map(ndvi)
    .median()
    .select(["B2","B3","B4","NDVI"],['blue','green','red','NDVI'])
    .clip(roi).setMulti({"system:index":year,'system:time_start':ee.Date(year)})
  return ee.List(first).add(image);
}
var imgMonth = time.iterate(filterImg,ee.List([]))
var  imgMonth= ee.ImageCollection.fromImages(imgMonth)
print(imgMonth,"imgMonth_iterate")

// evaluate --主要用来异步导出和显示,一般没有返回值,结合for 和map 一起用,这里的map里可以有print,Map,toDrive方法
var  time = imgMonth.reduceColumns(ee.Reducer.toList(), ['system:index'])
                        .get('list');
var visParams = {min: -0.8, max: 0.8, palette: ['blue', 'white','green']};
time.evaluate(function(years) {

  years.map(function(year){
    var image = ee.Image(imgMonth.filter(ee.Filter.eq("system:index", year)).first());
    Map.addLayer(image.select("NDVI"), visParams, year);
  })

})

// gif - NDVI  
var params01 = { 
  crs: 'EPSG:3857', 
  framesPerSecond: 2, 
  region: roi, 
  min: -0.8, 
  max: 0.8, 
  bands: ["NDVI"], 
  dimensions: 512, 
  format:"gif",
  palette: ['blue', 'white','green']
}; 
print(ui.Thumbnail({image:imgMonth, params:params01}))
print(imgMonth.getVideoThumbURL(params01));

// gif - 真彩色
var params02 = { 
  crs: 'EPSG:3857', 
  framesPerSecond: 2, 
  region: roi, 
  min: 0, 
  max: 0.3, 
  bands: ["red","green","blue"], 
  dimensions: 512, 
  format:"gif",
}; 
print(ui.Thumbnail({image:imgMonth, params:params02}))
print(imgMonth.getVideoThumbURL(params02));

激励自己,尽可能每周更新1-2篇,2020加油!!!

需要交流或者有项目合作可以加微信好友 (备注GEE)

微信号:comingboy0701

Last updated