-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCDCNEW2.txt
289 lines (188 loc) · 9.51 KB
/
CCDCNEW2.txt
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
var utils = require('users/parevalo_bu/gee-ccdc-tools:ccdcUtilities/api')
// Define parameters
// Change detection parameters
//设置CCDC连续变化检测的参数
var changeDetection = {
breakpointBands: ['GREEN','RED','NIR','SWIR1','SWIR2'],
tmaskBands: ['GREEN','SWIR1'],
minObservations: 6,
chiSquareProbability: .99,
minNumOfYearsScaler: 1.33,
dateFormat: 2,
lambda: 20/10000,
maxIterations: 25000
}
// Classification parameters
var classification = {
bandNames: ['GREEN','RED','NIR','SWIR1','SWIR2'],
inputFeatures: ["INTP", "SLP","PHASE","RMSE"],
coefs: ["INTP", "SLP","COS", "SIN","RMSE","COS2","SIN2","COS3","SIN3"],
ancillaryFeatures: ["ELEVATION","ASPECT","DEM_SLOPE","RAINFALL","TEMPERATURE"],
resultFormat: 'SegCollection',
classProperty: 'landcover',
yearProperty: 'year',
classifier: ee.Classifier.smileRandomForest,
classifierParams: {
numberOfTrees: 500,
variablesPerSplit: null,
minLeafPopulation: 1,
bagFraction: 0.5,
maxNodes: null
},
outPath: 'projects/GLANCE/RESULTS/CLASSIFICATION/VERSION_1',
segs: ["S1", "S2", "S3", "S4", "S5", "S6"],
trainingPath: 'users/2404182135/SingleTrainingPoint2010',
trainingPathPredictors: 'users/2404182135/TrainingPointGEENew2',
}
var studyRegion = ee.FeatureCollection('users/2404182135/TaiYuanNew')
var params = {
start: '2000-01-01',
end: '2024-01-01',
ChangeDetection: changeDetection,
Classification: classification,
StudyRegion: studyRegion
}
// Filter by date and a location in Brazil
var filteredLandsat = utils.Inputs.getLandsat()
.filterBounds(studyRegion)
.filterDate(params.start, params.end)
.select(params.Classification.bandNames)
print(filteredLandsat.size())
params.ChangeDetection['collection'] = filteredLandsat
var results = ee.Algorithms.TemporalSegmentation.Ccdc(params.ChangeDetection)
print(results,'result')
//这段代码可能有问题,用下面那一段
// //第 2 部分:准备训练数据
// // Define bands to use in classification
// var bands = params.Classification.bandNames
// // Define coefficients to use in classification
// var coefs = params.Classification.coefs
// // Segment ids
// var segs = params.Classification.segs
// // Property corresponding to year of training data
// var yearProperty = params.Classification.yearProperty
// // Define path to change detection results
// params.Classification.changeResults = results
// // Load ccd image stack with coefficients and change information
// var ccdImage = utils.CCDC.buildCcdImage(params.Classification.changeResults, params.Classification.segs.length, params.Classification.bandNames)
// print('CCD Image:', ccdImage)
// // Finally, get ancillary topographic and climate data
// var ancillary = utils.Inputs.getAncillary()
// var exportResults=true
// if (exportResults){
// // Create a metadata dictionary with the parameters and arguments used.
// //使用使用的参数和参数创建元数据字典。创建元数据字典以跟踪用于生成 ccdResults 的参数和设置。
// //这对于可重复性和理解数据的上下文至关重要。
// var metadata=changeDetection;
// metadata['breakpointBands']=metadata['breakpointBands'].toString();//检测断点的带。
// metadata['tmaskBands']=metadata['tmaskBands'].toString();//创建掩码的带
// metadata['startDate']='2000-01-01';
// metadata['endDate']='2024-01-01';
// metadata['bands']=params.Classification.bandNames.toString();//用于分析的条带。
// //Export results, assigning the metadata as image properties.
// //导出结果,将元数据指定为图像属性。
// Export.image.toAsset({
// image: ccdImage.set(metadata),
// region: studyRegion,
// pyramidingPolicy:{
// ".default": 'sample'//金字塔。“样本”意味着聚合将获取数据样本,这通常适用于分类或离散数据,确保即使在较低分辨率下查看更改检测数据也能保持完整性。
// },
// scale: 30,//空间分辨率
// priority: 10,
// });
// }
// //将 pyramidingPolicy 的值设置为“sample”可确保输出中的所有波段都具有正确的策略。
// // trainingData = utils.Classification.remapLC(
// // trainingData, 'Class', 'Class',['Bare','Developed','Forest','Herbaceous','Shrub','Snow/Ice','Water'],[1,2,3,4,5,6,7],[1,2,3])
// // print(trainingData.aggregate_histogram('Class'))
// // trainingData = trainingData.map(function(feat) {
// // return feat.set('year',2014)})
// Define bands to use in classification
var bands = params.Classification.bandNames
// Define coefficients to use in classification
var coefs = params.Classification.coefs
// Segment ids
var segs = params.Classification.segs
// Property corresponding to year of training data
var yearProperty = params.Classification.yearProperty
// Define path to change detection resultsCCDC只是ccdImage计算的一部分
params.Classification.changeResults = ee.Image('users/2404182135/CCDCNew2_5_2')
Map.centerObject(params.Classification.changeResults, 10);
// Load ccd image stack with coefficients and change information
//这里要将CCDC,Segs,BandNames这三个结合
var ccdImage = utils.CCDC.buildCcdImage(params.Classification.changeResults, params.Classification.segs.length, params.Classification.bandNames)
//print(ccdImage,'ccdImage')
// Export.image.toAsset({
// image: ccdImage.clip(studyRegion),
// description: 'CCDImage5_6',
// assetId: 'CCDImage5_6', // 确保使用正确的参数名称
// scale: 30, // 分辨率为30米
// region: studyRegion.geometry(), // 指定研究区
// maxPixels: 1e13, // 最大像素数
// crs: "EPSG:4326" // 设置投影为WGS84
// });
// Finally, get ancillary topographic and climate data
var ancillary = utils.Inputs.getAncillary()
// // var resultsPath=
// // 'users/2404182135/TaiYuanCCDC';
// // var ccdImage=ee.Image(resultsPath);//将CCDC结果加载为图像:此行从指定路径处的数据创建图像对象
// // Map.centerObject(ccdImage, 10);//将地图居中在图像上:缩放级别为 10
// // print('CCD Image:',ccdImage);//打印图像对象:此命令将 ccdResults 图像对象的属性输出到 Earth Engine 代码编辑器中的控制台。
// // //它可用于检查图像的属性,例如波段数、波段名称、属性、投影等,这有助于了解数据结构和元数据。
// print('CCD Image:', ccdImage)
var trainingData = ee.FeatureCollection(params.Classification.trainingPath)
print(trainingData.first())
var trainingData = utils.Classification.getTrainingCoefsAtDate(
trainingData, coefs, bands, yearProperty, ancillary, ccdImage, segs)
// Filter points with no data
var testBand = params.Classification.bandNames[0] + '_' + params.Classification.coefs[0]
trainingData = trainingData.filter(ee.Filter.notNull([testBand]))
print('First training point with predictors:', trainingData.first())
Export.table.toAsset({
collection: trainingData,
description: 'trainingDataProcessed',
assetId: params.Classification.trainingPathPredictors})
var trainingData=ee.FeatureCollection('users/2404182135/SingeTraining2010Prediction')
print(trainingData)
// Now do the actual classification add the first segments classification to the map
// Get training data as FC
//var trainingData = ee.FeatureCollection(params.Classification.trainingPath)
// Optionally filter by study area
trainingData = trainingData.filterBounds(params.StudyRegion)
// Next, turn array image into image
//var imageToClassify = utils.CCDC.buildCcdImage(params.Classification.changeResults, params.Classification.segs.length, params.Classification.bandNames)
//var imageToClassify = ee.Image('users/2404182135/CCDCNew2_5_2')
var imageToClassify =ccdImage
// Now get ancillary data
var demImage = ee.Image('USGS/SRTMGL1_003').rename('ELEVATION')
var slope = ee.Terrain.slope(demImage).rename('DEM_SLOPE')
var aspect = ee.Terrain.aspect(demImage).rename('ASPECT')
//温度受影响变化大,会影响分类精度
var bio = ee.Image('WORLDCLIM/V1/BIO')
.select(['bio01','bio12'])
.rename(['TEMPERATURE','RAINFALL'])
var ancillary = ee.Image.cat([demImage, slope, aspect, bio])
// Get classifier with params
var classifier = params.Classification.classifier(params.Classification.classifierParams)
var results = utils.Classification.classifySegments(
imageToClassify, params.Classification.segs.length, params.Classification.bandNames,
ancillary, params.Classification.ancillaryFeatures,
trainingData, classifier, params.StudyRegion, params.Classification.classProperty,
params.Classification.inputFeatures)
.clip(params.StudyRegion)
Map.centerObject(params.StudyRegion, 10);
var glanceViz = {min: 1, max: 8, palette: ['#386cb0', 'white', 'red', 'black', '#33a02c', '#b2df8a', '#ffff99','#654321']}
Map.addLayer(results.select(0), glanceViz, 'Seg1 Classification')
//Map.addLayer(results.select(0).randomVisualizer(), {}, 'Seg1 Classification')
// Export to asset!
Export.image.toAsset({
image: ee.Image(results).int8(),//.clip(outGeo),
scale: 30,
description: 'classification_segments',
assetId: 'classification_segments',
maxPixels: 1e13,
region: params.StudyRegion,
pyramidingPolicy: {
'.default': 'mode'
}
})