Repository navigation
Expand file tree
/
Copy pathUnsupervice_Classification.js
More file actions
96 lines (82 loc) · 3.3 KB
/
Copy pathUnsupervice_Classification.js
File metadata and controls
96 lines (82 loc) · 3.3 KB
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
// Show region of interest
Map.centerObject(ROI, 9);
Map.addLayer(ROI, {color: 'red'}, 'Wilayah Studi');
// === 2. Load ESA WorldCover 2020 dan ambil kelas Tree Cover ===
var esa = ee.Image("ESA/WorldCover/v100/2020").select('Map');
var treeCover = esa.updateMask(esa.eq(10)).clip(ROI); // 10 = Tree cover
// === 3. Load dan Masking Cloud Sentinel-2 SR Harmonized Tahun 2025 ===
var sentinel = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterDate('2024-06-01', '2024-12-31')
.filterBounds(ROI)
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
.map(function(img) {
var scl = img.select('SCL');
var mask = scl.neq(3) // shadow
.and(scl.neq(8)) // cloud medium probability
.and(scl.neq(9)) // cloud high probability
.and(scl.neq(10)) // cirrus
.and(scl.neq(1)); // saturated or defective
return img.updateMask(mask);
});
// === 4. Komposit Median ===
var median = sentinel.median();
// === 5. Masking Hasil Median dengan Tree Cover ===
var maskedMedian = median.updateMask(treeCover);
// === 6. Visualisasi RGB dan Tambahkan ke Peta ===
Map.addLayer(maskedMedian,
{min: 0, max: 3000, bands: ['B4', 'B3', 'B2']},
'Sentinel-2 2025 Masked (Tree Cover only)');
// 4. Hitung NDVI dan EVI
var ndvi = maskedMedian.normalizedDifference(['B8', 'B4']).rename('NDVI');
var evi = maskedMedian.expression(
'2.5 * ((NIR - RED) / (NIR + 6*RED - 7.5*BLUE + 1))', {
'NIR': maskedMedian.select('B8'),
'RED': maskedMedian.select('B4'),
'BLUE': maskedMedian.select('B2')
}).rename('EVI');
// 5. Gabungkan indeks
var indices = ndvi.addBands(evi);
// 6. Buat klasifikasi kerapatan (0 = Potensial, 1 = Rendah, 2 = Sedang, 3 = Tinggi)
var klasifikasi = indices.expression(
"(NDVI >= 0.8 && EVI >= 0.5) ? 4" + // Tinggi
" : (NDVI >= 0.6 && NDVI < 0.8 && EVI >= 0.3) ? 3" + // Sedang
" : (NDVI >= 0.45 && NDVI < 0.6) ? 2" + // Rendah
" : 1", // Potensial
{
'NDVI': indices.select('NDVI'),
'EVI': indices.select('EVI')
}).rename('kerapatan');
// 7. Mask hanya pada area tree cover dari PL
var refined = klasifikasi.updateMask(treeCover);
// 8. Tampilkan hasil klasifikasi
Map.centerObject(ROI, 10);
Map.addLayer(refined, {
min: 1,
max: 4,
palette: ['yellow', 'orange', 'green', 'darkgreen']
}, 'Kerapatan Hutan');
// 9. Tambahkan legenda (opsional manual)
var legend = ui.Panel({style: {position: 'bottom-left'}});
var makeRow = function(color, name) {
return ui.Panel([
ui.Label({style: {backgroundColor: color, padding: '8px', margin: '0'}}),
ui.Label(name, {margin: '0 0 4px 6px'})
], ui.Panel.Layout.Flow('horizontal'));
};
legend.add(ui.Label('Legenda Kerapatan Hutan'));
legend.add(makeRow('yellow', 'Potensial'));
legend.add(makeRow('orange', 'Rendah'));
legend.add(makeRow('green', 'Sedang'));
legend.add(makeRow('darkgreen', 'Tinggi'));
Map.add(legend);
// 10. Export hasil klasifikasi ke Google Drive
Export.image.toDrive({
image: refined,
description: 'Kerapatan_Hutan_JawaTimur_20251', //replace with your desired description
folder: 'GEE_Exports', // replace with your desired folder
fileNamePrefix: 'kerapatan_hutan_jatim_20251', //replace with your desired file name
region: ROI.geometry(),
scale: 10, // Resolusi sesuai Sentinel-2
maxPixels: 1e13,
crs: 'EPSG:4326'
});