I'm trying to use the for-loop function for a supervised classification over several dates. I have trouble exporting the accuracy metrics that are useful to me in a single CSV file. With my code I can export the data for each day separately. But I would like to have a CSV file containing all the dates for which the images have been classified. Please help me. Here is the code:
//Can be converted to import records
var Bagre =
/* color: #d63000 */
/* displayProperties: [
{
"type": "rectangle"
}
] */
ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Polygon(
[[[-0.9131136144573282, 11.80001340655708],
[-0.9131136144573282, 11.44960967007124],
[-0.4901399816448282, 11.44960967007124],
[-0.4901399816448282, 11.80001340655708]]], null, false),
{
"system:index": "0"
})]),
other = /* color: #98ff00 */ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Point([-0.5993166173870157, 11.584173738244099]),
{
"landcover": 0,
"system:index": "0"
}),
ee.Feature(
ee.Geometry.Point([-0.7167329992229532, 11.6124239897709]),
{
"landcover": 0,
"system:index": "1"
}),
ee.Feature(
ee.Geometry.Point([-0.7263460363323282, 11.676312799623682]),
{
"landcover": 0,
"system:index": "2"
}),
ee.Feature(
ee.Geometry.Point([-0.6569948400432657, 11.641343901167035]),
{
"landcover": 0,
"system:index": "3"
}),
ee.Feature(
ee.Geometry.Point([-0.6734743322307657, 11.580137754769975]),
{
"landcover": 0,
"system:index": "4"
}),
ee.Feature(
ee.Geometry.Point([-0.7435121740276407, 11.584846396491287]),
{
"landcover": 0,
"system:index": "5"
}),
ee.Feature(
ee.Geometry.Point([-0.7874574865276407, 11.531701414565942]),
{
"landcover": 0,
"system:index": "6"
}),
ee.Feature(
ee.Geometry.Point([-0.8286562169963907, 11.580137754769975]),
{
"landcover": 0,
"system:index": "7"
}),
ee.Feature(
ee.Geometry.Point([-0.8149233068401407, 11.635963680007837]),
{
"landcover": 0,
"system:index": "8"
}),
ee.Feature(
ee.Geometry.Point([-0.7950105871135782, 11.61645950650287]),
{
"landcover": 0,
"system:index": "9"
}),
ee.Feature(
ee.Geometry.Point([-0.6789674962932657, 11.487294094934224]),
{
"landcover": 0,
"system:index": "10"
}),
ee.Feature(
ee.Geometry.Point([-0.6466951574260782, 11.474508871642358]),
{
"landcover": 0,
"system:index": "11"
}),
ee.Feature(
ee.Geometry.Point([-0.6082430089885782, 11.54919328080972]),
{
"landcover": 0,
"system:index": "12"
}),
ee.Feature(
ee.Geometry.Point([-0.5828371251995157, 11.463068970402034]),
{
"landcover": 0,
"system:index": "13"
}),
ee.Feature(
ee.Geometry.Point([-0.6391420568401407, 11.603680169832181]),
{
"landcover": 0,
"system:index": "14"
})]),
water = /* color: #0b4a8b */ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Point([-0.5615511144573282, 11.487294094934224]),
{
"landcover": 1,
"system:index": "0"
}),
ee.Feature(
ee.Geometry.Point([-0.5993166173870157, 11.499405876960067]),
{
"landcover": 1,
"system:index": "1"
}),
ee.Feature(
ee.Geometry.Point([-0.6260957921917032, 11.51824539131339]),
{
"landcover": 1,
"system:index": "2"
}),
ee.Feature(
ee.Geometry.Point([-0.6487550939495157, 11.533719762462855]),
{
"landcover": 1,
"system:index": "3"
}),
ee.Feature(
ee.Geometry.Point([-0.6762209142620157, 11.544484039481423]),
{
"landcover": 1,
"system:index": "4"
}),
ee.Feature(
ee.Geometry.Point([-0.6913271154338907, 11.537083643381099]),
{
"landcover": 1,
"system:index": "5"
}),
ee.Feature(
ee.Geometry.Point([-0.7050600255901407, 11.524300681517477]),
{
"landcover": 1,
"system:index": "6"
}),
ee.Feature(
ee.Geometry.Point([-0.7290926183635782, 11.549866023115023]),
{
"landcover": 1,
"system:index": "7"
}),
ee.Feature(
ee.Geometry.Point([-0.7647981847698282, 11.570047541254711]),
{
"landcover": 1,
"system:index": "8"
}),
ee.Feature(
ee.Geometry.Point([-0.7723512853557657, 11.586864361519334]),
{
"landcover": 1,
"system:index": "9"
}),
ee.Feature(
ee.Geometry.Point([-0.6666078771526407, 11.539101952582197]),
{
"landcover": 1,
"system:index": "10"
}),
ee.Feature(
ee.Geometry.Point([-0.5855837072307657, 11.491331413449927]),
{
"landcover": 1,
"system:index": "11"
}),
ee.Feature(
ee.Geometry.Point([-0.5512514318401407, 11.512862801646012]),
{
"landcover": 1,
"system:index": "12"
}),
ee.Feature(
ee.Geometry.Point([-0.5519380773479532, 11.482583816929104]),
{
"landcover": 1,
"system:index": "13"
}),
ee.Feature(
ee.Geometry.Point([-0.5759706701213907, 11.486621202893533]),
{
"landcover": 1,
"system:index": "14"
})]);
var Domain = Bagre;
var S2 = ee.ImageCollection("COPERNICUS/S2");
function maskS2clouds(image) {
var qa = image.select('QA60');
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000);
}
var gcp = other.merge(water);
var gcp_random = gcp.randomColumn();
var trainingGcp = gcp_random.filter(ee.Filter.lt('random', 0.7));
var validationGcp = gcp_random.filter(ee.Filter.gte('random', 0.7));
// List the years, months, and dates
var years = [2021];
var months = [1,1,1,1,1,1];
var days1 = [1,6,11,16,21,26];
var i;
var j;
for (i = 0; i<years.length;i++){
for(j = 0; j<months.length;j++){
var dayOfInterest = ee.Date(years[i] + "-" + months[j] + "-" + days1[j]);
var loop = S2
.filter(ee.Filter.date(dayOfInterest, dayOfInterest.advance(1, 'day')))
.filter(ee.Filter.bounds(Domain))
.map(maskS2clouds)
.select('B.*')
.median()
.clip(Domain);
// Extract Digital Numbers of pixels in training polygons
var training = loop.sampleRegions({
collection: trainingGcp,
properties: ['landcover'],
scale: 10,
tileScale: 16,
});
// Train a classifier.
var classifier = ee.Classifier.smileRandomForest(100)
.train({
features: training,
classProperty: 'landcover',
inputProperties: loop.bandNames()
});
// Classify the satellite image
var classified = loop.classify(classifier);
/* Accuracy Assessment*/
var test = classified.sampleRegions({
collection: validationGcp,
properties: ['landcover'],
scale: 10,
tileScale: 16,
});
var testConfusionMatrix = test.errorMatrix('landcover', 'classification');
// Export accuracy assessment metrics as CSV
var fc = ee.FeatureCollection([
ee.Feature(null, {
'Producers Accuracy': testConfusionMatrix.producersAccuracy().get([1,-1]).multiply(100),
'Consumers Accuracy': testConfusionMatrix.consumersAccuracy().get([0,1]).multiply(100),
'Overall Accuracy': testConfusionMatrix.accuracy().multiply(100),
'Kappa coefficient': testConfusionMatrix.kappa().multiply(100),
'Days': dayOfInterest.format('YYYY-MM-dd')
})
]);
Export.table.toDrive({
collection: fc,
description: 'Bagre_Area_Sq_Km' + "_" + years[i] + "-" + months[j] + "-" + days1[j],
folder: 'earthengine',
fileFormat: 'CSV',
selectors:['Days','Overall Accuracy', 'Producers Accuracy', 'Consumers Accuracy', 'Kappa coefficient']
});
}
}