Geographic Information Systems Asked on February 3, 2021
I am new to coding and need some help debugging…I am trying to classify a region in Peru using Sentinel 2. The error I am receiving is "’image’ is not defined in this scope." I believe my issues is on line 31 and the fact that I am working within a collection not an image…but I am still completely lost. Any tips?
var growth = ee.FeatureCollection('users/gtnatia/just_trees_training_2');
growth = growth.geometry();
Map.centerObject(growth);
Map.addLayer(growth, {color: 'red'}, 'growth');
//
var all_other = ee.FeatureCollection('users/gtnatia/all_other_training');
all_other = all_other.geometry();
Map.centerObject(all_other);
Map.addLayer(all_other, {color: 'black'}, 'all_other');
// Make a cloud-free Landsat 8 TOA composite (from raw imagery).
var sentinel2 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate('2018-01-01', '2018-12-31')
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
.filterBounds(aoi);
// Use these bands for prediction.
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11'];
//
var polygons = ee.FeatureCollection([
ee.Feature(growth, {'class': 0}),
ee.Feature(all_other, {'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);
// Display the classification result and the input image.
Map.setCenter(-72.355,-13.880, 9);
Map.addLayer(image, {bands: ['B4', 'B3', 'B2'], max: 0.5, gamma: 2});
Map.addLayer(polygons, {}, 'training polygons');
Map.addLayer(classified,
{min: 0, max: 1, palette: ['red', 'green']},
'supervised_classification');
There were a few bugs in the code.
var image = sentinel2.mean();
. This line takes the imageCollection called Sentinel2
(which contains a stack of cloud-free images for the year 2018) and takes the mean of them. You can use .max()
, .min()
etc. if you wishprint(image)
or visit the documentation and click on bands. I removed "B10" from the bands
list.Answered by kkrao on February 3, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP