当前位置:网站首页>Index method and random forest to realize the information of surface water body in wet season in Shandong Province
Index method and random forest to realize the information of surface water body in wet season in Shandong Province
2022-07-01 06:02:00 【z6q6k6】
The course is just a big assignment , For reference only
Random forests , The functions are quite complete

var table = ee.FeatureCollection("users/gis418670826/shandong_province");
// Shandong
var geometry = table
Map.centerObject(geometry, 7);
Map.addLayer(geometry, {}, "geometry", false)
// According to the water body data set description ,1 No data ,2 Not a body of water ,2 Seasonal water ,3 Permanent water body
// But there is no 0 and 1, Only through unmask hold unmask The value is filled with 1
// And pass gt(1), It is divided into water bodies 1 And non water 0
var jrc_year = ee.ImageCollection("JRC/GSW1_3/YearlyHistory")
.filterDate('2020-01-01', '2021-01-01')
.first()
.clip(geometry)
.select('waterClass')
var jrc_year_Water = jrc_year.reduceRegion({
crs: 'EPSG:4326',
reducer: ee.Reducer.count(),
geometry : geometry,
scale : 30,
maxPixels :1e13,
})
jrc_year = jrc_year.unmask(ee.Image(1).clip(geometry))
.gt(1)
jrc_year = jrc_year.unmask(ee.Image(1).clip(geometry))
Map.addLayer(jrc_year, {min:0,max:1,palette:['#ffffff','red','#0000ff']}, "jrc_year")
// Water body and non water body shall be taken respectively 500 A little bit
var point = jrc_year.stratifiedSample({
numPoints : 500,
classBand : "waterClass",
region : geometry,
scale :30,
geometries :true
});
Map.addLayer(point,{}, "point")
print(point)
// landsat8 Data processing
// Quyun
function remove_cloud_L8sr(image) {
var cloudShadowBitMask = 1 << 3;
var cloudsBitMask = 1 << 5;
var qa = image.select('QA_PIXEL');
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
.and(qa.bitwiseAnd(cloudsBitMask).eq(0));
return image.updateMask(mask)
}
// Already exist 7 Add... To the band mndwi
function computermNdwi(image)
{
var mndwi = image.normalizedDifference(['SR_B3','SR_B5']).rename('mndwi');
mndwi.updateMask(mndwi.gt(-1).and(mndwi.lt(1)));
return image.addBands(mndwi)
}
// LANDSAT/LC08/C02/T1_L2
var landsat8 = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2")
.filterDate('2020-04-01', '2020-11-1')
.filterBounds(geometry)
.map(remove_cloud_L8sr)
.map(computermNdwi)
.mean() // Cloud free data synthesis
.clip(geometry)
Map.addLayer(landsat8,{}, "landsat8")
print(landsat8)
var bands = ['SR_B1','SR_B2','SR_B3','SR_B4','SR_B5','SR_B6','SR_B7','mndwi']
// hold landsat8 The band information is assigned to the sample points
var training = landsat8.select(bands).sampleRegions({
collection: point,
properties: ['waterClass'],
scale: 30
});
print(training)
var withRandom = training.randomColumn('random');// Random arrangement of sample points
// Keep some data for testing , To avoid over fitting the model .
var split = 0.7;
var trainingPartition = withRandom.filter(ee.Filter.lt('random', split));// Screening 70% As a training sample
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));// Screening 30% As a test sample
var classifier = ee.Classifier.smileRandomForest(10).train({
features:trainingPartition,
classProperty:"waterClass",
inputProperties:bands
})
// print(classifier)
// Yes Landsat-8 To classify
var class_img = landsat8.select(bands).classify(classifier);
// Use test samples to classify , Determine the data set and function to perform the function operation
var test = testingPartition.classify(classifier);
// Calculating the confusion matrix
var confusionMatrix = test.errorMatrix('waterClass', 'classification');
print('confusionMatrix',confusionMatrix);// The confusion matrix is displayed on the panel
print('overall accuracy', confusionMatrix.accuracy());// The overall accuracy is displayed on the panel
print('kappa accuracy', confusionMatrix.kappa());// Display on panel kappa value
Map.addLayer(class_img, {min: 0, max: 1, palette: ['black','yellow']}, 'result');
print(class_img)
// Calculated area
function computeWaterPixel(image) {
var Sum = image.reduceRegion({
crs: 'EPSG:4326',
reducer: ee.Reducer.count(),
geometry : geometry,
scale : 30,
maxPixels :1e13,
})
// print(Sum)
var mask = image.eq(1)
var Water = image.updateMask(mask).reduceRegion({
crs: 'EPSG:4326',
reducer: ee.Reducer.count(),
geometry : geometry,
scale : 30,
maxPixels :1e13,
})
// data.get('sur_refl_b01').getInfo()
var water_Pixel = Water.get('classification').getInfo()
var sum_Pixel = Sum.get('classification').getInfo()
print(" Number of pixels occupied by water body ",water_Pixel)
print(" Number of pixels in Shandong Province ",sum_Pixel)
var area = 155800
print(" The known area of Shandong Province is 155800 Square kilometers , The area of the water body is ", water_Pixel/sum_Pixel*area)
print("jrc", jrc_year_Water.get('waterClass').getInfo()/sum_Pixel*area)
}
computeWaterPixel(class_img)
mndwi + otsu Algorithm
// Shandong
var geometry = table
Map.centerObject(geometry, 7);
Map.addLayer(geometry, {}, "geometry")
var jrc_year = ee.ImageCollection("JRC/GSW1_3/YearlyHistory")
.filterDate('2020-01-01', '2021-01-01')
.first()
.clip(geometry)
.select('waterClass')
.eq(3)//3 Permanent water body 2 Seasonal water body
var point = jrc_year.stratifiedSample({
numPoints : 100,
classBand : "waterClass",
region : geometry,
scale :30,
geometries :true
});
Map.addLayer(point)
print(point)
// Quyun
function remove_cloud_L8sr(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)
}
// data
var landsat8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
.filterDate('2020-05-01', '2020-10-1')
.select(['B3','B5','pixel_qa'])
.filterBounds(geometry)
.map(remove_cloud_L8sr)
.mean()
.clip(geometry)
print(landsat8)
// computer mndwi for landsat8
function computermNdwi(image)
{
var mndwi = image.normalizedDifference(['B3','B5']).rename('mndwi');
return mndwi.updateMask(mndwi.gt(-1).and(mndwi.lt(1)));
}
//otsu Algorithm
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]);
}
// landsat8
function computer_water(imageCol){
var img_mean = ee.Image(computermNdwi(imageCol));
Map.addLayer(img_mean,{}, "img_mean")
var histogram = img_mean.reduceRegion({
reducer: ee.Reducer.histogram(),
geometry: geometry,
scale: 300,
maxPixels: 1e13,
tileScale: 16 // Increase speed
});
var threshold = otsu(histogram.get("mndwi"));
// var threshold = -0.23836122208868166
print(threshold)
var mask = img_mean.gte(threshold);
var water = mask.rename("water").setMulti({'time':"2020"});
print(water)
// Map.addLayer(ee.Image(water),{}, "water")
return ee.Image(water)
}
var new_image = ee.List([]);
var new_imag = computer_water(landsat8,new_image);
print(new_imag)
Map.addLayer(new_imag,{min:0,max:1,palette:['black','red']},'water_landsat8_otsu_based')
Reference resources :
stay GEE Using time series sentinels in 1 Extract permanent water body - You know
边栏推荐
猜你喜欢

El tooltip in the table realizes line breaking display

excel高级绘图技巧100讲(一)-用甘特图来展示项目进度情况

OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline

Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills

excel动态图表

My experience from technology to product manager

Know the future of "edge computing" from the Nobel prize!

Preliminary level of C language -- selected good questions on niuke.com

Crossing pie · pie pan + Mountain duck = local data management

TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)
随机推荐
穿越派·派盘 + Mountain Duck = 数据本地管理
How to add a gourd pie plate
QT write custom control - self drawn battery
【笔记】电商订单数据分析实战
Continue to learn MySQL
excel动态图表
论文学习记录随笔 多标签之LIFT
2022第八届中国国际“互联网+”大学生创新创业大赛产业命题赛道开启报名!
4GB大文件,如何实时远程传输和共享?
Oracle sequence + trigger
论文学习记录随笔 多标签之GLOCAL
Infinite horizontal marble game
OpenGL ES: (4) EGL API详解 (转)
DEV XPO对比之UOW
1034 Head of a Gang
Stack Title: parsing Boolean expressions
68 Cesium代码datasource加载czml
Talking from mlperf: how to lead the next wave of AI accelerator
Huluer app help
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills