-
Notifications
You must be signed in to change notification settings - Fork 1
/
Session 2 Clustering_GEE_SOLVED
160 lines (112 loc) · 5.47 KB
/
Session 2 Clustering_GEE_SOLVED
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
150
151
152
153
154
155
156
157
158
159
160
The link to the solved exercise can be found here: https://code.earthengine.google.com/e99d8135f7976608ed3cc73aabedcddb
///////
//+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//S. Aparício (Solenix), I. Alonso (Solenix)
//
//MIT LICENSE CC BY EUMETSAT
//+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//❄️☀️
// Session 2 “Hands-on: Clustring two image of different seasons
// for change detection"
// PARTICIPANT'S VERSION
//
//
//+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// EXERCICE 2: CLUSTER A S2 CLOUD FREE IMAGE OF ENDALEN (SVALBARD)
// FROM TWO DIFFEREN SEASONS AND APPLY A CHANGE DETECTION BETWEEN THEM
//+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
/////////////////////////////////////////////////////////////////////////////
////////////////////// 1. DEFINED AND VISUALIZE AOI /////////////
/////////////////////////////////////////////////////////////////////////////
//Create a geometry over Endalen, in Svalbard - and name it AOI and visualize it
//Center the visualization there
Map.centerObject(AOI,10);
//////////////////////////////////////////////////////////////////////////////
////////////////////// 2. PREPARING SATELLITE IMAGERY /////////////////
//////////////////////////////////////////////////////////////////////////////
//Define a "summer range date" with ee.Date could be from 2022-06-01 to 2022-08-28 > e.g. ee.Date('2022-01-01')
//e.g. ee.Date('2022-01-01')
var start = ee.Date('2022-06-01');
var end = ee.Date('2022-08-28');
//Define a "winter range date" could be from 2022-05-30 to 2022-07-02
var start2 = ee.Date('2022-05-30');
var end2 = ee.Date('2022-07-02');
//Create a summer collection
var summerCol = ee.ImageCollection('COPERNICUS/S2')
.filterDate(start,end)
.filterBounds(AOI)
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',10))
//Create a winter collection
var winterCol = ee.ImageCollection('COPERNICUS/S2')
.filterDate(start2,end2)
.filterBounds(AOI)
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',5))
var summerImage = summerCol.first()
var winterImage = winterCol.first()
var visRGB = {
min: 1400,
max: 9500,
gamma: 2.24,
bands: ['B4', 'B3', 'B2']
};
Map.addLayer(summerImage.clip(AOI), newVis, 'Sentinel-2 Summer Image')
Map.addLayer(winterImage.clip(AOI), visRGB, 'Sentinel-2 Winter Image')
Export.image.toDrive(summerImage)
//////////////////////////////////////////////////////////////////////////////
//////////// 3. Create image of absolute differences ///////////////
//////////////////////////////////////////////////////////////////////////////
var changeCover = summerImage.subtract(winterImage).clip(AOI)
Map.addLayer(changeCover.clip(AOI), {}, 'Reflectance Abs. differences Image')
//////////////////////////////////////////////////////////////////////////////
////////////// 4. Collecting training data /////////////////////////
//////////////////////////////////////////////////////////////////////////////
//During the supervised classification we started at this point to
//collect data about the differnt clases.
//Here we won't be doing it manually but we will create a function
//to give as 100 random points in the area.
//Ideally should be more than 100 points, but this is just an example to speed things up
//This sample function will return a FeatureCollection with 100 points
//containing the spectral value of the image they are located on
var training_data = changeCover.sample({
region: AOI,
scale:20,
numPixels: 100
})
//////////////////////////////////////////////////////////////////////////////
////////////// 4. Create a cluster and train it /////////////////////////
//////////////////////////////////////////////////////////////////////////////
// The next stop is to create a clusterer object from one of the different
//algorithms available:
//var clusterer = ee.Clusterer.wekaXMeans()
var clusterKmeans = ee.Clusterer.wekaKMeans(3);
// This is still need to be trained. This is done by
//applying the train() function on the clusterer and passing in the
//training data
var trainedKmeans = clusterKmeans.train(training_data);
//This cluster has now looked at all this points, and group them by similiarity
// while these groups are as distinc as possible from other groups of points
//When we apply this cluster to our image, it will take all pixels of the image
//and determine, to which of these groups the pixel is most similar
var clusteredImage = changeCover.cluster(trainedKmeans)
//////////////////////////////////////////////////////////////////////////////
////////////// 5. Visualize results /////////////////////////
//////////////////////////////////////////////////////////////////////////////
var visCluster = {
min: 0,
max: 1,
palette: ['white', 'red']
};
Map.addLayer(clusteredImage.clip(AOI), visCluster, 'Clustered Change Image')
///////////////////////////////////////////////////////////////////////
/////////////// 6. EXTRA: EXPORTING WITH NICE VISUALIZATIONS///////////
//////////////////////////////////////////////////////////////////////////////
var finalImage = clusteredImage.clip(AOI)
var visToExport = clusteredImage.visualize({
min:0,
max:1,
palette: ['white', 'red']
})
Export.image.toDrive({
image: visToExport,
description: 'Exporting_10m_resolution'
});