Google Earth Engine(GEE)提取水体

今天分享一下如何用MNDWI水体指数来提取研究区的水体,主要使用的数据为landsat8

GEE代码如下:


var roi =   ee.Geometry.Polygon(
        [[[118.78042247007194, 32.632228406133],
          [118.78042247007194, 30.819493246253046],
          [120.90078379819694, 30.819493246253046],
          [120.90078379819694, 32.632228406133]]], null, false);
Map.centerObject(roi,7)
var styling = {color:"red",fillColor:"00000000"};

// reomove cloud for Landsat-8
function rmL8Cloud(image) { 
  var cloudShadowBitMask = (1 << 3); 
  var cloudsBitMask = (1 << 5); 
  var qa = image.select('pixel_qa'); 
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0) 
                 .and(qa.bitwiseAnd(cloudsBitMask).eq(0));
  var mask2 = image.select('B1').gt(2000);
  return image.updateMask(mask).updateMask(mask2.not())
              .copyProperties(image)
              .copyProperties(image, ["system:time_start",'system:time_end']);
} 
var l8_sr = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR").map(rmL8Cloud)
            .filter(ee.Filter.lte('CLOUD_COVER',5))//云量过滤;
//MNDWI水体指数
function calcWater(image) {
  var MNDWI = image.select("MNDWI");
  var NDVI = image.select("NDVI");
  var EVI = image.select("EVI");
  var water = EVI.lt(0.1)
                 .and(MNDWI.gt(NDVI)
                 .or(MNDWI.gt(EVI)));
  return image.addBands(water.rename("water"));
}
var Landsat8 = {
  scaleImage: function(image) {
    var time_start = image.get("system:time_start");
    image = image.select(["B2","B3","B4","B5","B6","B7"]);
    image = image.divide(10000);
    image = image.set("system:time_start", time_start);
    return image;
  },
  srCloudMask: function(image) {
    var cloudShadowBitMask = (1 << 3);
    var snowBitMask = (1 << 4);
    var cloudsBitMask = (1 << 5);
    var qa = image.select('pixel_qa');
    var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
                 .and(qa.bitwiseAnd(snowBitMask).eq(0))
                 .and(qa.bitwiseAnd(cloudsBitMask).eq(0));
    return image.updateMask(mask);
  },
  //NDVI
  NDVI: function(image) {
      return image.addBands(image.normalizedDifference(["B5", "B4"])
                                 .rename("NDVI"));
  },
  //MNDWI
  MNDWI: function(image) {
      return image.addBands(image.normalizedDifference(["B3", "B6"])
                                 .rename("MNDWI"));
  },
  // EVI
  EVI: function(image) {
    var evi = image.expression("EVI = 2.5 * (NIR - R) / (NIR + 6*R -7.5*B + 1)", {
      NIR: image.select("B5"),
      R: image.select("B4"),
      B: image.select("B2")
    });
    return image.addBands(evi);
  },

  /*获取Landsat8的SR数据*/
  getL8SRCollection : function(startDate, endDate, roi) {
    var dataset = l8_sr.filterDate(startDate, endDate)
                      .filterBounds(roi)
                      .map(Landsat8.srCloudMask)
                      .map(Landsat8.scaleImage)
                      .map(Landsat8.NDVI)
                      .map(Landsat8.MNDWI)
                      .map(Landsat8.EVI)
                      .map(calcWater)
                      .select("water");
    return dataset;
  }
};

//export
function exportImageToDrive(image, key, roi) {
  Export.image.toDrive({
    image: image, 
    description: "Water"+key,
    fileNamePrefix: key, 
    region: roi,
    scale: 30,
    maxPixels: 1e13
  });
}

//去掉阴影
function removeShadow(image, roi) {
  var hand = ee.ImageCollection('users/gena/global-hand/hand-100');
  var hand30 = hand.mosaic().focal_mean(0.1).rename('elevation');
  var hillShadowMask = hand30.select('elevation').lte(50);
  var waterMask = image.updateMask(hillShadowMask.and(image.gte(0.8)))
                       .gte(0.25)
                       .clip(roi);
  waterMask = waterMask.connectedPixelCount(50, true);
  waterMask = waterMask.updateMask(waterMask.gte(50));
  return image.updateMask(waterMask);
}
//生成每一年的水体 
function processYearWaterImage(year, roi) {
  var startDate = ee.Date.fromYMD(year, 1, 1);
  var endDate = ee.Date.fromYMD(year+1, 1, 1);

  var l8Water = Landsat8.getL8SRCollection(startDate, endDate, roi);
  var waterImgs = l8Water
  
  /*计算水体的频率*/
  var waterImg = waterImgs.sum()
                          .divide(waterImgs.count())
                          .clip(roi);

waterImg = waterImg.updateMask(waterImg).clip(roi);
  var key = "landsatWater-"+year;
  Map.addLayer(waterImg, {min:0,max:1,palette:['000000','blue']}, "water"+key, true);
  exportImageToDrive(waterImg, key, roi);
  print('waterImgs_'+year,waterImgs);
    var stats2 = waterImg.reduceRegion({
      reducer: ee.Reducer.sum(),
      geometry: roi,
      scale: 30,
      maxPixels: 1e13
    });
  print(stats2,year)

}

//循环导出所有的水体
function main() {
  //开始年份和结束年份
  var startYear = 2014;
  var endYear = 2021;
  for (var year=startYear; year<=endYear; year++) {
    processYearWaterImage(year, roi);
  }
}

提取结果展示:
Google Earth Engine(GEE)提取水体_第1张图片
Google Earth Engine(GEE)提取水体_第2张图片
Google Earth Engine(GEE)提取水体_第3张图片
Google Earth Engine(GEE)提取水体_第4张图片
Google Earth Engine(GEE)提取水体_第5张图片
感谢关注,欢迎转发!

声明:仅供学习使用!如果对你有帮助的话记得给小编点个赞

**更多内容请关注微信公众号“生态遥感监测笔记”

你可能感兴趣的:(GEE,图像处理,javascript,云计算,算法,大数据)