GEE水体提取之NDWI(矢量裁剪、统计面积、数据导出)

GEE专栏:GEE水体提取之MNDWI(矢量裁剪、数据导出)

计算区域2018年6-10月武汉市所有Landsat 8数据。

效果图

1、基于otsu计算结果,基于otsu提取结果,之前一直处于途中计算状态,原因推测数据量大,计算耗时,此外GEE给每个人分配的计算资源是有一定的限制的,非无线计算资源。
GEE水体提取之NDWI(矢量裁剪、统计面积、数据导出)_第1张图片

源码部分

/*
author by Lbta     https://blog.csdn.net/qq_48104689?spm=1000.2115.3001.5343
*/


function maskL8sr(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));
  return image.updateMask(mask);
}
var dataset2016 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR').filterDate('2018-05-01', '2018-10-31').map(maskL8sr).filterBounds(table).map(addS2VIs);
var visParams = {bands: ['B4', 'B3', 'B2'],min: 0,max: 3000,gamma: 1.4,};
Map.setCenter(114.5, 30.25);
Map.addLayer(dataset2016.median().clip(table), visParams);
//计算MNDWI指数
function ND_VI(image,b1,b2,bName)
{
  var VI = image.normalizedDifference([b1,b2]).rename(bName);
  return VI.updateMask(VI.gt(-1).and(VI.lt(1)));
}
function addS2VIs(img)
{
  var MNDWI=ND_VI(img,'B3','B6','mNDWI')
  return img.addBands(MNDWI);
}
//var mNDWI = adds2VIs(dataset2016);
var MNDWI_landsat8=dataset2016.select('mNDWI').median();
Map.addLayer(MNDWI_landsat8.clip(table),{min:-0.2,max:0.05},'MNDWI_landsat8');

//otsu算法
function otsu(histogram) {
  var counts = ee.Array(ee.Dictionary(histogram).get('histogram'));
  var means = ee.Array(ee.Dictionary(histogram).get('bucketMeans'));
  var size = means.length().get([0]);
  var total = counts.reduce(ee.Reducer.sum(), [0]).get([0]);
  var sum = means.multiply(counts).reduce(ee.Reducer.sum(), [0]).get([0]);
  var mean = sum.divide(total);
  var indices = ee.List.sequence(1, size);
  var bss = indices.map(function(i) {
    var aCounts = counts.slice(0, 0, i);
    var aCount = aCounts.reduce(ee.Reducer.sum(), [0]).get([0]);
    var aMeans = means.slice(0, 0, i);
    var aMean = aMeans.multiply(aCounts)
        .reduce(ee.Reducer.sum(), [0]).get([0])
        .divide(aCount);
    var bCount = total.subtract(aCount);
    var bMean = sum.subtract(aCount.multiply(aMean)).divide(bCount);
    return aCount.multiply(aMean.subtract(mean).pow(2)).add(
           bCount.multiply(bMean.subtract(mean).pow(2)));
  });
  return means.sort(bss).get([-1]);
}

var histogram = MNDWI_landsat8.reduceRegion({
    reducer: ee.Reducer.histogram(), 
    geometry: table, 
    scale: 30,
    maxPixels: 1e13,
    tileScale: 8
  });

var threshold = otsu(histogram.get("mNDWI"));
//var threshold = ee.Number.parse(threshold1.format("%.4f"));
  var mask = MNDWI_landsat8.gte(threshold);
  print(threshold,'threshold_Landsat')
  var water_landsat8 = mask.rename("water");//.updateMask(mask) 
Map.addLayer(water_landsat8.clip(table),{min:0,max:1,palette:['black','blue']},'water_landsat8_otsu_based');
//输出
Export.image.toDrive({
  image:water_landsat8,
  description: 'water_landsat8_2017-2018',
  scale:30,
  region:table,
  fileFormat: 'GeoTIFF',
  maxPixels:1e13,
});
//统计面积
var waterarea_landsat8_based=ee.Number((((water_landsat8.eq(1)).multiply(ee.Image.pixelArea()))
                        .reduceRegion({
                        reducer: ee.Reducer.sum(),
                        geometry: table,
                        scale: 30,
                        maxPixels: 1e13,})).get('water'))
//var waterarea_landsat8_based = ee.Number.parse(waterarea_landsat8_based1.format("%.4f"));
print('waterarea_landsat8_based',waterarea_landsat8_based)       

你可能感兴趣的:(Google,Earth,Engine学习专栏,大数据,经验分享,程序人生)