前言:
一个工程的前期预实验,水体提取比较粗糙,可以改进。
结果展示:
一、 研究区范围
var geometry =
/* color: #98ff00 */
/* displayProperties: [
{
"type": "rectangle"
}
] */
ee.Geometry.Polygon(
[[[133.39612274412107, 45.73460678862749],
[133.39612274412107, 45.68402125013954],
[133.51422577146482, 45.68402125013954],
[133.51422577146482, 45.73460678862749]]], null, false)
Map.centerObject(geometry);
去云函数
//去云的算法 var cloudMaskL457 = function(image) { var qa = image.select('QA_PIXEL'); // Bit 3 is cloud var mask = qa.bitwiseAnd(1 << 3) .not(); return image.updateMask(mask); };
选择数据集与去云和裁剪:
// M选取Landsat5地表反射率数据 var composite = ee.ImageCollection("LANDSAT/LT05/C02/T1_L2") .filterDate('1990-04-01', '1990-10-01') .filterBounds(geometry).filter(ee.Filter.lte('CLOUD_COVER',30)) var pixelArea = ee.Image.pixelArea(); var dataset= composite .map(cloudMaskL457) .map(function(image){ return image.clip(geometry)//裁剪 }) print("dataset",dataset)
计算NDWI
var calcNDWI = function(image) { var ndwi = image.normalizedDifference(['SR_B2', 'SR_B4']).rename('NDWI'); // var ndwi = image.normalizedDifference(['B4', 'B6']).rename('NDWI'); // 在Landsat5中,B2是绿光波段,B4是近红外波段 return image.addBands(ndwi); }; // 在数据集中的每个图像上应用NDWI计算 var withNDWI = dataset.map(calcNDWI); // 设定NDWI阈值,将NDWI大于0的地方判定为水体(可以改) var yuzhi = function(image){ image = image.select(['NDWI']) var water = image.gt(0); return water.updateMask(water); } var water = withNDWI.map(yuzhi)
计算总区域面积
var totalArea = ee.Image.pixelArea().reduceRegion({ reducer:ee.Reducer.sum(), geometry:geometry, scale :30, maxPixels:1e13 }).getNumber('area'); print('totalArea',totalArea)
计算水体面积
var calculateWaterArea = function(image) { var waterArea = image.multiply(ee.Image.pixelArea()).rename('WaterArea'); return waterArea; }; var waterAreaCollection = water.map(calculateWaterArea); var extractWater = waterAreaCollection.map(function(image){ var waterArea = image.reduceRegion({ reducer: ee.Reducer.sum(), geometry: geometry, scale: 30, maxPixels: 1e13 }).getNumber('WaterArea'); var waterAreaProportion = ee.Number(waterArea).divide(totalArea).multiply(100); return ee.Feature(null, { 'WaterArea': waterArea, 'WaterAreaProportion':waterAreaProportion, 'Date': ee.String(image.get('system:index')).slice(-8) });}) var featureCollection = ee.FeatureCollection(extractWater);
按水体面积降序排序并获取水体面积最大的影像和日期信息
var sortedFeatures = featureCollection.sort('WaterArea', false); var maxWaterAreaFeature = sortedFeatures.first(); var maxWaterAreaDate = ee.String(maxWaterAreaFeature.get('system:index')).slice(-8); var data = ee.Date.fromYMD( ee.Number.parse(maxWaterAreaDate.slice(0, 4)), ee.Number.parse(maxWaterAreaDate.slice(4, 6)), ee.Number.parse(maxWaterAreaDate.slice(6, 8)) ); print("data",data) var nextDay = data.advance(1, 'day'); var maxWaterAreaImage = withNDWI .filterDate(data,nextDay) .filterBounds(geometry).first()
输出结果
var waterAreaProportion = ee.Number(maxWaterAreaFeature.get('WaterArea')).divide(totalArea).multiply(100); print('最大水体面积日期:', maxWaterAreaDate); print('最大水体面积:', maxWaterAreaFeature.get('WaterArea')); print('最大水体面积比例:', waterAreaProportion); // // 在地图上显示结果 Map.centerObject(geometry, 10); Map.addLayer(maxWaterAreaImage,{min:0, max:30000, bands:['SR_B4', 'SR_B3', 'SR_B2']},'yanmo');
导出比例结果
// 导出比例EXCEL结果 Export.table.toDrive({ collection: sortedFeatures, description: 'water_area_statistics'+ maxWaterAreaDate.getInfo(), fileNamePrefix:'water_area_statistics'+ maxWaterAreaDate.getInfo(), folder: 'GEE/kuche/', fileFormat: 'CSV' }); // 导出水体影像 var waterMask = yuzhi(maxWaterAreaImage); waterMask = waterMask.toInt16(); Export.image.toDrive({ image: waterMask, description: 'max_proportion_water' + maxWaterAreaDate.getInfo(), fileNamePrefix: 'max_proportion_water' + maxWaterAreaDate.getInfo(), folder: 'GEE/kuche/', scale: 30, region: geometry, crs: "EPSG:4326", maxPixels: 1e13 }); Map.addLayer(waterMask, {min: 0, max: 1, palette: ['white', 'blue']}, 'Water'); maxWaterAreaImage = maxWaterAreaImage.toInt16(); // 导出原始影像 Export.image.toDrive({ image: maxWaterAreaImage, description: 'max_proportion_image' + maxWaterAreaDate.getInfo(), fileNamePrefix: 'max_proportion_image' + maxWaterAreaDate.getInfo(), folder: 'GEE/kuche/', scale: 30, region: geometry, crs: "EPSG:4326", maxPixels: 1e13 });
结尾:随便写写,如果能帮到的话,就太好啦