-
Notifications
You must be signed in to change notification settings - Fork 4
/
Random_Forest(GEE)
150 lines (120 loc) · 4.81 KB
/
Random_Forest(GEE)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//Code Written by Fauzan Muzakki, ITC, University of Twente
//Internship at Arizona State University 2021
//Zoom in to the study area
Map.centerObject(Iburi, 11)
//DEM Extraction
var elevation = SRTM.select('elevation')
.clip(JapanAOI)
var terrain = ee.Terrain.products(elevation)
function threshold(image) {
var slope = terrain.select('slope')
var thres = slope.gte(8).rename('thres')
return image.addBands(thres);
}
var slope = terrain.select('slope')
//cloudmasking Sentinel 2
function maskS2clouds(image) {
var qa = image.select('QA60');
// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000);
}
//map vegetation indices
function addindices(image) {
var params= {'NIR':image.select('B8'),
'RED':image.select('B4'),
'BLUE':image.select('B2'),
'SWIR':image.select('B11'),
'GREEN': image.select('B3') }
var ndvi = image.normalizedDifference(['B8', 'B4']).rename(['ndvi'])
var bsi = image.expression('((SWIR+RED)-(NIR+BLUE))/((SWIR+RED)+(NIR+BLUE))', params).rename(['bsi'])
var br = image.expression ('RED/(RED+GREEN+BLUE)', params).rename(['br'])
var ndwi = image.normalizedDifference(['B8', 'B3']).rename(['ndwi'])
return image.addBands(ndvi).addBands(bsi).addBands(br).addBands(ndwi);
}
////////////////////////////////////////////
//image collection Sentinel 2
/////////////////////////////////////////////
var pre_event_s2 = Sentinel2.filterDate ('2016-05-01', '2016-09-30')
.filterMetadata("CLOUDY_PIXEL_PERCENTAGE", "less_than", 5)
.filterBounds(JapanAOI)
.map(maskS2clouds)
.map(addindices)
.map(threshold)
//.median()
print(pre_event_s2, 'total_image_pre' )
var post_event_s2 = Sentinel2.filterDate('2018-09-01', '2018-10-30')
.filterMetadata("CLOUDY_PIXEL_PERCENTAGE", "less_than", 5)
.filterBounds(JapanAOI)
.map(maskS2clouds)
.map(addindices)
.map(threshold)
//.median()
print(post_event_s2, 'total_image_post' )
var visualization = {
min: 0.0,
max: 0.5,
bands: ['B4', 'B3', 'B2'],
}
//////////////////////////////////////////////////////////////////
//Change Detection
//////////////////////////////////////////////////////////////////
var change_detection = post_event_s2.median().subtract(pre_event_s2.median()) // the visualization of change compared before and after landslide
var change_ratio =change_detection.divide(pre_event_s2.median()) //the proportion of change caused by landslide
//////////////////////////////////////////////////////////////////
//Change Detection
//////////////////////////////////////////////////////////////////
var change_detection = post_event_s2.median().subtract(pre_event_s2.median()) // the visualization of change compared before and after landslide
var change_ratio =change_detection.divide(pre_event_s2.median()) //the proportion of change caused by landslide
//////////////////////////////////////////////////////////////
///Random Forest
/////////////////////////////////////////////////////////////
///Samples preparation
//Assigning a property as numeric value ('class') to landslide and non landslide class
var landslide = ee.FeatureCollection.randomPoints(Iburi, 500)
.map(function(f){
return f.set('class',1)
})
print(landslide.first())
var nonlandslide = ee.FeatureCollection.randomPoints(outlandslide, 3000)
.map(function(f){
return f.set('class',0)
})
print(nonlandslide.first())
var samples = landslide.merge(nonlandslide) //merging two samples
print(samples)
/////Random Forest Classification
var bands1 = ['ndvi', 'bsi', 'br']
var detailAOI = change_ratio.clip(JapanAOI2)
var training = change_ratio.select(bands1).sampleRegions({
collection : samples,
properties : ['class'],
scale :10
})
var classifier = ee.Classifier.smileRandomForest(500).train({
features : training,
classProperty : 'class',
inputProperties : bands1
})
var classified = detailAOI.select(bands1).classify(classifier)
////////////////////////////
Download
//////////////////////////////
Export.image.toDrive({
image: classified,
description: 'RF classfied',
folder: 'Landslide_Aufar',
region: JapanAOI,
scale: 10,
maxPixels : 1e8
})
})
//////////////////////////////////////////////
//Data Visualization
/////////////////////////////////////////////
Map.addLayer(change_ratio.clip(JapanAOI2), {}, 'change');
Map.addLayer(classified, {min : 0 , max: 1 ,pallete:['red','blue']}, 'RFresult')