Geographic Information Systems Asked by Vinay Dhanvada on March 23, 2021
I have a composite image collection which I used for land use classification. I want to calculate the area of the feature collections. The code I have used is:
// Composite an image collection and clip it to a boundary.
// Load Landsat 7 raw imagery and filter it to Jan-Dec 2018.
var collection = ee.ImageCollection("LANDSAT/LC08/C01/T1")
.filterDate('2018-01-01', '2018-12-31');
// Reduce the collection by taking the median.
var median = ee.Algorithms.Landsat.simpleComposite({collection: collection});
// Load a table of boundaries and filter.
var fc = ee.FeatureCollection('users/njsnigdha42/Asia');
// Clip to the output image to the Asia boundaries.
var composite = median.clipToCollection(fc);
// Display the result.
var visParams = {bands: ['B3', 'B2', 'B1'], gain: [1.4, 1.4, 1.1]};
Map.addLayer(composite, visParams, 'clipped composite');
// Merge the five geometry layers into a single FeatureCollection.
var newfc = Urban.merge(Vegetation).merge(Water).merge(Barren_Land).merge(Snow_and_Ice);
print(newfc);
// Use these bands for classification.
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7'];
// The name of the property on the points storing the class label.
var classProperty = 'landcover';
// Sample the composite to generate training data. Note that the
// class label is stored in the 'landcover' property.
var training = composite.select(bands).sampleRegions({
collection: newfc,
properties: [classProperty],
scale: 30
});
// Train a CART classifier.
var classifier = ee.Classifier.smileCart().train({
features: training,
classProperty: classProperty,
});
// Print some info about the classifier (specific to CART).
print('CART, explained', classifier.explain());
// Classify the composite.
var classified = composite.classify(classifier);
Map.centerObject(newfc);
Map.addLayer(classified, {min: 0, max: 4, palette: ['red', 'green', 'blue','yellow','white']});
// Optionally, do some accuracy assessment. Fist, add a column of
// random uniforms to the training dataset.
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.smileRandomForest(5).train({
features: trainingPartition,
classProperty: classProperty,
inputProperties: bands
});
// Classify the test FeatureCollection.
var test = testingPartition.classify(trainedClassifier);
// Print the confusion matrix.
var confusionMatrix = test.errorMatrix(classProperty, 'classification');
print('Confusion Matrix', confusionMatrix);
//Converting the data into Binary data
//Cartographic way to calculate area
var area_pxa = image.multiply(ee.Image.pixelArea())
.reduceRegion(ee.Reducer.sum(), Urban,30,null,null,false,1e13)
.get('constant');
print('Estimated urbanization (km2)',area_pxa);
Number (Error)
Dictionary.get: Dictionary does not contain key: constant.
How do I solve this issue? Also, I think I have not converted the data into binary data and I don't know how to do that. Please help!!
This all depends on how classified
looks. If classified contained a band called urban
which was either 0
or 1
, this should work. The error message is pretty clear in this case: The dictionary returned by reduceRegion()
doesn't contain the key urban
. Print the resulting dictionary to see which keys it contain:
var areaDict = classified.multiply(ee.Image.pixelArea())
.reduceRegion(ee.Reducer.sum(), urban, 30, null, null, false, 1e13)
print(areaDict)
You haven't provided enough details in your question to give much more advice than this.
Answered by Daniel Wiell on March 23, 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