当前位置:网站首页>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
边栏推荐
- TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)
- OpenGL es: (2) relationship between OpenGL es, EGL and glsl
- Looking for high school student developers with similar interests
- PLA不粘贴在床上:6个简单的解决方案
- Geoffrey Hinton:我的五十年深度学习生涯与研究心法
- Timer based on LabVIEW
- CJC8988带2个立体声耳机驱动器的低功率立体声编解码器
- This is the necessary software for college students 𞓜 knowledge management
- Code shoe set - mt3114 · interesting balance - explain it with examples
- C语言初阶——牛客网精选好题
猜你喜欢

Pla ne colle pas sur le lit: 6 solutions simples

论文学习记录随笔 多标签之LIFT

穿越派与贸大合作,为大学生增添效率

3D printer threading: five simple solutions

He struggled day and night to protect his data

我从技术到产品经理的几点体会

论文学习记录随笔 多标签之LSML

Essay learning record essay multi label Global

Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432

Infinite horizontal marble game
随机推荐
MySQL怎么存储emoji?
码蹄集 - MT3149 · AND - 数据不是很强,暴力剪枝就能骗AC
π disk, turning your computer into a personal private cloud
SystemVerilog学习-06-类的封装
Bat operation FTP upload and download command
win10、win11中Elan触摸板滚动方向反转、启动“双指点击打开右键菜单“、“双指滚动“
PLA不粘貼在床上:6個簡單的解决方案
Pla ne colle pas sur le lit: 6 solutions simples
相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同
Oracle 序列+触发器
El tooltip in the table realizes line breaking display
DEV XPO对比之XAF BO
srpingboot security demo
srpingboot security demo
如何添加葫芦儿派盘
Some errors encountered in MySQL data migration
Database problems, how to optimize Oracle SQL query statements faster and more efficient
Essay learning record essay multi label Global
我从技术到产品经理的几点体会
excel可视化