2

I am using Random Forest in Earth Engine to classify land use from Sentinel 2 images. I want to eventually have a raster file of the classification with each pixel classified using the RF model. At the moment my classification is a feature collection, can I convert/export this as a tif file?

Here is the code and the link to the code:

ee_code

In the code below "classified" is the clssification feature collection.

Map.addLayer(guan_ca);

var vis = sen2.filter(ee.Filter.calendarRange(2017,2019,'year'))
    .filter(ee.Filter.calendarRange(01,01,'month'))
    .filterBounds(guan_ca)
    .map(function(image){
    return image.clip(guan_ca);
});

var vis = vis.mean();

var trueColour = {
         bands: ["B4", "B3", "B2"],
         min: 0,
         max: 3000
     };

     // Add image to map
Map.addLayer(vis, trueColour, "true-colour image");
// Define false-colour visualization parameters
var falseColour = {
    bands: ["B8", "B4", "B3"],
    min: 0,
    max: 3000
};

// // Define false-colour visualization parameters
var falseColour = {
    bands: ["B8", "B4", "B3"],
    min: 0,
    max: 3000
};

// Add image to map
Map.addLayer(vis, falseColour, "false-colour image");


// // Create the training data

var newfc = ca.merge(water).merge(ca).merge(baresoil).merge(crops).merge(urban).merge(clouds).merge(shadows).merge(forest).merge(lightcover);

print(newfc);

var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8']; 
var training = vis.select(bands).sampleRegions({
    collection: newfc,
    properties: ['landcover'],
    scale: 30
});    

print(training);

// Train the classifier, in this case, I use the Random Forest algorithm
var classifier = ee.Classifier.randomForest(100)
   .train({
           features: training,
           classProperty: 'landcover',
           inputProperties: bands
});

// Run the classification
var classified = vis.select(bands).classify(classifier);
print(classified);

// Display the classification
Map.centerObject(newfc, 11);
Map.addLayer(vis, 
{bands: ['B2', 'B3', 'B4'], max: 0.3}, 
'Sentinel 2 image');
Map.addLayer(classified, {min: 0, max: 8, palette: ['126CC6', 'EBEB07', '613D05', '2CBE17', 'B9BFB8', 'FFFFFF', '000000', '076105', 'D47606']}, 
'classification');
Map.addLayer(newfc); 

Export.table.toDrive({
  collection: newfc,
  description: ('landuse_trainingData'),
  fileFormat: 'KML',
});

Export.table.toDrive({
  collection: classified,
  description: ('land_use_cassification'),
  fileFormat: 'KML',
});

// Error estimation
var trainAccuracy = classifier.confusionMatrix();
print('Resubstitution error matrix: ', trainAccuracy);
print('Training overall accuracy: ', trainAccuracy.accuracy());

var withRandom = training.randomColumn('random');

// We want to reserve some of the data for testing, to avoid overfitting the model.
var split = 0.7;  // Roughly 70% training, 30% testing.
var trainingPartition = withRandom.filter(ee.Filter.lt('random', split));
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));

// Trained with 70% of our data.
var trainedClassifier = ee.Classifier.randomForest().train({
  features: trainingPartition,
  classProperty: 'landcover',
  inputProperties: bands
});

// Classify the test FeatureCollection.
var test = testingPartition.classify(trainedClassifier);

// Print the confusion matrix.
var confusionMatrix = test.errorMatrix('landcover', 'classification');
print('RF testing error matrix', confusionMatrix);
print('RF testing accuraccy', confusionMatrix.accuracy());
print('RF testing accuracy', confusionMatrix.array());

var matrix = ee.Feature(null, {matrix: confusionMatrix.array ()});

1 Answer 1

2

ee.FeatureCollection.style() and ee.Image.paint() both can be used to draw a feature collection as an image, which you can then export.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.