-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop-sentinel-indices
82 lines (67 loc) · 2.91 KB
/
loop-sentinel-indices
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
// Carregar as dependências e inicializar as variáveis
var shapefile = ee.FeatureCollection("projects/santarem-ic/assets/shapefile-santarem");
var geometryCollection = ee.FeatureCollection("projects/santarem-ic/assets/poligonosSENTINEL");
var colecoesPorAno = {};
var trainingDataList = ee.List([]);
// Adicionar shapefile ao mapa e centralizá-lo
Map.addLayer(shapefile, {}, 'Shapefile');
Map.centerObject(shapefile, 12);
// Função para calcular o NDVI (Normalized Difference Vegetation Index)
var calcularNDVI = function(sentinelImage) {
return sentinelImage.addBands(sentinelImage.normalizedDifference(['B8', 'B4']).rename('NDVI'));
};
// Função para calcular o NDWI (Normalized Difference Water Index)
var calcularNDWI = function(sentinelImage) {
return sentinelImage.addBands(sentinelImage.normalizedDifference(['B3', 'B8']).rename('NDWI'));
};
// Loop para carregar a coleção de imagens do Sentinel para cada ano (de 2015 a 2023)
for (var year = 2015; year <= 2023; year++) {
var startDate = year + '-01-01';
var endDate = year + '-12-31';
// Filtrar a coleção de imagens por data e região
var colecaoAno = ee.ImageCollection('COPERNICUS/S2')
.filterDate(startDate, endDate)
.filterBounds(shapefile)
.sort('system:time_start')
.map(function(image) {
return image.clip(shapefile);
});
// Armazenar a coleção de imagens do ano atual no dicionário
colecoesPorAno[year] = colecaoAno;
}
// Dicionário contendo a imagem específica a ser usada de cada ano
var imagemDoAno = {
2022: 77,
2021: 72,
2020: 66,
2019: 82,
2018: 32,
2017: 64,
2016: 82,
};
// Loop para percorrer cada ano e coletar dados para treinamento e classificação.
for (var ano in imagemDoAno) {
// Obtém a imagem correspondente ao ano atual.
var imagem = ee.Image(colecoesPorAno[ano].toList(colecoesPorAno[ano].size()).get(imagemDoAno[ano]));
// Adiciona a imagem original ao mapa.
Map.addLayer(imagem, {bands: ['B2', 'B3', 'B4'], min: 0, max: 3000}, ano);
// Calcula os índices NDVI e NDWI para a imagem.
var imagemComIndices = calcularNDVI(imagem);
imagemComIndices = calcularNDWI(imagemComIndices);
// Coleta os dados de treinamento da imagem com os índices calculados.
var trainingData = imagemComIndices.sampleRegions({
collection: geometryCollection,
properties: ['classe'],
scale: 10
});
// Treina o classificador Random Forest usando os dados de treinamento.
var classifier = ee.Classifier.smileRandomForest(50).train({
features: trainingData,
classProperty: 'classe',
inputProperties: ['B2', 'B3', 'B4', 'B8', 'NDVI', 'NDWI']
});
// Classifica a imagem atual usando o classificador Random Forest.
var classifiedImage = imagemComIndices.classify(classifier);
// Adiciona a imagem classificada ao mapa.
Map.addLayer(classifiedImage, {min: 1, max: 5, palette: ['#0000FF', '#00FF00', '#FFFF00', '#FFA500', '#006400']}, 'Classificação ' + ano);
}