1

I have performed a supervisioned classification to identify lakes in a study region.

How can I transform a classification that are in Image to ShapeFile?

The code that I wrote:

var image = ee.Algorithms.Landsat.simpleComposite({
  collection: l8.filterDate('2015-01-01', '2019-12-31'),
  asFloat: true
});

var image = image.clip(geometry);

// Use these bands for prediction.
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11'];

// Manually created polygons.

// Make a FeatureCollection from the hand-made geometries.
var polygons = ee.FeatureCollection([
  ee.Feature(nonlake, {'class': 0}),
  ee.Feature(nonlake1, {'class': 0}),
  ee.Feature(lake1, {'class': 1}),
  ee.Feature(lake2, {'class': 1}),
  ee.Feature(lake3, {'class': 1})
]);


// Get the values for all pixels in each polygon in the training.
var training = image.sampleRegions({
  // Get the sample from the polygons FeatureCollection.
  collection: polygons,
  // Keep this list of properties from the polygons.
  properties: ['class'],
  // Set the scale to get Landsat pixels in the polygons.
  scale: 30
});

// Create an SVM classifier with custom parameters.
var classifier = ee.Classifier.libsvm({
  kernelType: 'RBF',
  gamma: 0.5,
  cost: 10
});

// Train the classifier.
var trained = classifier.train(training, 'class', bands);

// Classify the image.
var classified = image.classify(trained);

//Map.addLayer(polygons, {}, 'training polygons');
Map.addLayer(classified, {min: 0, max: 1, palette: ['white', 'black']},
             'delakeation');
1

For testing the validity of your script, I selected an arbitrary region in Argentina where there are a lot of lakes. I also selected an arbitrary geometry for clipping images. It looks as follows in GEE:

enter image description here

As you described in your question, I manually created each polygon for using in the training with same nomenclature. It can be observed in following image:

enter image description here

The code snippet for transforming a classification that is in your image to shapefile is summarized below. Full code is here.

var classes = classified.reduceToVectors({
  reducer: ee.Reducer.countEvery(), 
  geometry: geometry, 
  scale: 500,
  maxPixels: 1e10
});

print(classes);

// Export the FeatureCollection to a SHP file.
Export.table.toDrive({
  collection: classes,
  folder: 'GEE_folder',
  description:'classified_area',
  fileFormat: 'SHP'
});

Map.addLayer(classes, {}, 'classes');

After running complete code in GEE code editor I got result of following image. I had to vary scale factor until to obtain a good result. The contours of the water reservoirs (expressed as Feature Collection classes) appear to have been well defined. This vector file (classes) was also exported to my Google Drive as shapefile.

enter image description here

2
  • Thank you very much for your help with the script. It worked exactly as I needed it. – Thierry Alexandre Pellegrinett Apr 12 at 20:03
  • You're welcome. – xunilk Apr 12 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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