// 3) โหลดภาพ AEF Embeddings และ Sentinel-2 composite
var aefImage = ee.ImageCollection('GOOGLE/SATELLITE_EMBEDDING/V1/ANNUAL')
.filterDate(year + '-01-01', year + '-12-31')
.filterBounds(studyArea)
.mosaic()
.clip(studyArea);
var aefBands = aefImage.bandNames();
function maskS2clouds(image) {
var scl = image.select('SCL');
var mask = scl.eq(4).or(scl.eq(5)).or(scl.eq(6))
.or(scl.eq(7)).or(scl.eq(11));
return image.updateMask(mask);
}
function addIndices(image) {
var ndvi = image.normalizedDifference(['B8','B4']).rename('NDVI');
var ndwi = image.normalizedDifference(['B3','B8']).rename('NDWI');
var ndbi = image.normalizedDifference(['B11','B8']).rename('NDBI');
return image.addBands([ndvi, ndwi, ndbi]);
}
var s2Collection = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterDate(year + '-01-01', year + '-12-31')
.filterBounds(studyArea)
.map(maskS2clouds)
.map(addIndices);
var s2Image = s2Collection
.qualityMosaic('NDVI')
.clip(studyArea);
var s2Bands = [
'B2','B3','B4','B5','B6','B7','B8','B8A','B11','B12',
'NDVI','NDWI','NDBI'
];
// 4) กำหนดการแบ่ง batch และ export
var batchSize = 1000; // ปรับลดหากยัง error
var totalPoints = lulcPoints.size().getInfo();
var numBatch = Math.ceil(totalPoints / batchSize);
print('Total points:', totalPoints);
print('Number of batches:', numBatch);
var randPoints = lulcPoints.randomColumn('rand');
for (var i = 0; i < numBatch; i++) {
var minR = i / numBatch;
var maxR = (i + 1) / numBatch;
var batch = randPoints
.filter(ee.Filter.gte('rand', minR))
.filter(ee.Filter.lt('rand', maxR));
// 4.1) Export AEF samples
var aefSamples = aefImage.select(aefBands).sampleRegions({
collection: batch,
properties: [classProperty, 'longitude', 'latitude'],
scale: 10,
tileScale: 16, // ปรับเป็นค่าที่ valid (1-16)
geometries: false
});
Export.table.toDrive({
collection: aefSamples,
description: 'AEF_64bands_Rayong2024_b' + (i+1),
folder: 'GEE_Research_Data',
fileNamePrefix: 'aef_64b_rayong2024_b' + (i+1),
fileFormat: 'CSV'
});
// 4.2) Export Sentinel-2 samples
var s2Samples = s2Image.select(s2Bands).sampleRegions({
collection: batch,
properties: [classProperty, 'longitude', 'latitude'],
scale: 10,
tileScale: 16, // ปรับเป็นค่าที่ valid (1-16)
geometries: false
});
Export.table.toDrive({
collection: s2Samples,
description: 'S2_13bands_Rayong2024_b' + (i+1),
folder: 'GEE_Research_Data',
fileNamePrefix: 's2_13b_rayong2024_b' + (i+1),
fileFormat: 'CSV'
});
print('Export batch', i+1, 'size:', batch.size());
}
// 5) แสดงผลบนแผนที่ (ตัวเลือก)
Map.centerObject(studyArea, 10);
Map.addLayer(studyArea, {color: 'red'}, 'Study Area');
Map.addLayer(lulcPoints, {color: 'yellow'}, 'Ground Truth Points');