In this tutorial we will learn how to:
- Define hand-located points to train a classifier.
- Classify the training points into different classes (Urban, Vegetation, Water, and Bare Land)
- Merge the four geometry layers into a single Feature Collection.
- Sample the composite to generate training data. Note that the class label is stored in the ‘landcover’ property.
- Train a CART classifier.
- Split the data into 70% training, 30% testing. and classify the composite.
- Print the confusion matrix.
Learn about Data
USGS Landsat 8 Collection 1 Tier 1 and Real-Time data TOA Reflectance
Landsat 8 Collection 1 Tier 1 and Real-Time data calibrated top-of-atmosphere (TOA) reflectance. Calibration coefficients are extracted from the image metadata. See Chander et al. (2009) for details on the TOA computation. The Landsat 8 images are of 30 m and 16 days spatial and temporal resolutions respectively.
Here is the code:
Define hand-located points to train a classifier and classify the training points into different classes (Urban, Vegetation, Water, and Bare Land)
Map.setOptions('HYBRID')
// This demonstration uses hand-located points to train a classifier.
// Each training point has a field called 'landcover' containing
// class labels at that location. The following block contains
// construction code for the points. Hover on the 'urban' variable and click, 'Convert' in the dialog.
var urban = /* color: #ff0000 */ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Point([54.98782, 25.00813]),
{
"landcover": 0,
"system:index": "0"
}),
ee.Feature(
ee.Geometry.Point([55.00701, 25.00921]),
{
"landcover": 0,
"system:index": "1"
}),
ee.Feature(
ee.Geometry.Point([55.02134, 25.00509]),
{
"landcover": 0,
"system:index": "2"
}),
ee.Feature(
ee.Geometry.Point([54.97456, 24.98008]),
{
"landcover": 0,
"system:index": "3"
}),
ee.Feature(
ee.Geometry.Point([55.008853, 24.972773]),
{
"landcover": 0,
"system:index": "4"
}),
ee.Feature(
ee.Geometry.Point([55.011437, 24.959278]),
{
"landcover": 0,
"system:index": "5"
}),
ee.Feature(
ee.Geometry.Point([55.01884, 24.960008]),
{
"landcover": 0,
"system:index": "6"
}),
ee.Feature(
ee.Geometry.Point([55.096962, 24.973413]),
{
"landcover": 0,
"system:index": "7"
}),
ee.Feature(
ee.Geometry.Point([55.105782, 24.970865]),
{
"landcover": 0,
"system:index": "8"
}),
ee.Feature(
ee.Geometry.Point([55.088422, 24.973141]),
{
"landcover": 0,
"system:index": "9"
}),
ee.Feature(
ee.Geometry.Point([55.096126, 24.972265]),
{
"landcover": 0,
"system:index": "10"
}),
ee.Feature(
ee.Geometry.Point([55.109815, 24.971972]),
{
"landcover": 0,
"system:index": "11"
}),
ee.Feature(
ee.Geometry.Point([55.107385, 24.972483]),
{
"landcover": 0,
"system:index": "12"
}),
ee.Feature(
ee.Geometry.Point([55.16087, 24.99318]),
{
"landcover": 0,
"system:index": "13"
}),
ee.Feature(
ee.Geometry.Point([55.197765, 24.982634]),
{
"landcover": 0,
"system:index": "14"
}),
ee.Feature(
ee.Geometry.Point([55.194938, 24.983373]),
{
"landcover": 0,
"system:index": "15"
}),
ee.Feature(
ee.Geometry.Point([55.200849, 24.982916]),
{
"landcover": 0,
"system:index": "16"
}),
ee.Feature(
ee.Geometry.Point([55.200082, 24.984779]),
{
"landcover": 0,
"system:index": "17"
}),
ee.Feature(
ee.Geometry.Point([55.194583, 24.984681]),
{
"landcover": 0,
"system:index": "18"
}),
ee.Feature(
ee.Geometry.Point([55.197282, 24.985143]),
{
"landcover": 0,
"system:index": "19"
}),
ee.Feature(
ee.Geometry.Point([55.197866, 24.985343]),
{
"landcover": 0,
"system:index": "20"
}),
ee.Feature(
ee.Geometry.Point([55.198065, 24.988566]),
{
"landcover": 0,
"system:index": "21"
}),
ee.Feature(
ee.Geometry.Point([55.195839, 24.989077]),
{
"landcover": 0,
"system:index": "22"
}),
ee.Feature(
ee.Geometry.Point([55.162646, 25.224986]),
{
"landcover": 0,
"system:index": "23"
})]),
vegetation = /* color: #3b8b00 */ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Point([55.163816, 25.224768]),
{
"landcover": 1,
"system:index": "0"
}),
ee.Feature(
ee.Geometry.Point([55.1643239, 25.224684]),
{
"landcover": 1,
"system:index": "1"
}),
ee.Feature(
ee.Geometry.Point([55.296349, 25.22884]),
{
"landcover": 1,
"system:index": "2"
}),
ee.Feature(
ee.Geometry.Point([55.294716, 25.228656]),
{
"landcover": 1,
"system:index": "3"
}),
ee.Feature(
ee.Geometry.Point([55.297929, 25.229015]),
{
"landcover": 1,
"system:index": "4"
}),
ee.Feature(
ee.Geometry.Point([55.3011044, 25.2282833]),
{
"landcover": 1,
"system:index": "5"
}),
ee.Feature(
ee.Geometry.Point([55.3016972, 25.228373]),
{
"landcover": 1,
"system:index": "6"
}),
ee.Feature(
ee.Geometry.Point([55.3021947, 25.2283112]),
{
"landcover": 1,
"system:index": "7"
}),
ee.Feature(
ee.Geometry.Point([55.303292, 25.2284399]),
{
"landcover": 1,
"system:index": "8"
}),
ee.Feature(
ee.Geometry.Point([55.304638, 25.226559]),
{
"landcover": 1,
"system:index": "9"
}),
ee.Feature(
ee.Geometry.Point([54.319891, 24.464857]),
{
"landcover": 1,
"system:index": "10"
}),
ee.Feature(
ee.Geometry.Point([54.320613, 24.465277]),
{
"landcover": 1,
"system:index": "11"
}),
ee.Feature(
ee.Geometry.Point([55.317124, 25.227442]),
{
"landcover": 1,
"system:index": "12"
}),
ee.Feature(
ee.Geometry.Point([55.327469, 25.224825]),
{
"landcover": 1,
"system:index": "13"
}),
ee.Feature(
ee.Geometry.Point([55.323607, 25.22598]),
{
"landcover": 1,
"system:index": "14"
}),
ee.Feature(
ee.Geometry.Point([55.167234, 25.065902]),
{
"landcover": 1,
"system:index": "15"
}),
ee.Feature(
ee.Geometry.Point([55.161671, 25.066825]),
{
"landcover": 1,
"system:index": "16"
}),
ee.Feature(
ee.Geometry.Point([55.16409, 25.066952]),
{
"landcover": 1,
"system:index": "17"
}),
ee.Feature(
ee.Geometry.Point([55.1674542, 25.0657046]),
{
"landcover": 1,
"system:index": "18"
}),
ee.Feature(
ee.Geometry.Point([55.204913, 25.017411]),
{
"landcover": 1,
"system:index": "19"
}),
ee.Feature(
ee.Geometry.Point([55.203901, 25.017394]),
{
"landcover": 1,
"system:index": "20"
}),
ee.Feature(
ee.Geometry.Point([55.408108, 25.087062]),
{
"landcover": 1,
"system:index": "21"
}),
ee.Feature(
ee.Geometry.Point([55.41432, 25.088751]),
{
"landcover": 1,
"system:index": "22"
})]),
water = /* color: #0300ff */ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Point([55.08505, 25.11592]),
{
"landcover": 2,
"system:index": "0"
}),
ee.Feature(
ee.Geometry.Point([55.11415, 25.11778]),
{
"landcover": 2,
"system:index": "1"
}),
ee.Feature(
ee.Geometry.Point([54.9287, 25.0073]),
{
"landcover": 2,
"system:index": "2"
}),
ee.Feature(
ee.Geometry.Point([54.58927, 24.5179]),
{
"landcover": 2,
"system:index": "3"
}),
ee.Feature(
ee.Geometry.Point([54.59731, 24.48138]),
{
"landcover": 2,
"system:index": "4"
}),
ee.Feature(
ee.Geometry.Point([54.58576, 24.45349]),
{
"landcover": 2,
"system:index": "5"
}),
ee.Feature(
ee.Geometry.Point([54.535059, 24.471845]),
{
"landcover": 2,
"system:index": "6"
}),
ee.Feature(
ee.Geometry.Point([55.33871, 25.20018]),
{
"landcover": 2,
"system:index": "7"
}),
ee.Feature(
ee.Geometry.Point([55.30318, 25.19284]),
{
"landcover": 2,
"system:index": "8"
}),
ee.Feature(
ee.Geometry.Point([55.35359, 25.30263]),
{
"landcover": 2,
"system:index": "9"
}),
ee.Feature(
ee.Geometry.Point([55.32355, 25.29487]),
{
"landcover": 2,
"system:index": "10"
}),
ee.Feature(
ee.Geometry.Point([55.28613, 25.29317]),
{
"landcover": 2,
"system:index": "11"
}),
ee.Feature(
ee.Geometry.Point([55.309795, 25.262414]),
{
"landcover": 2,
"system:index": "12"
}),
ee.Feature(
ee.Geometry.Point([55.34921, 25.30837]),
{
"landcover": 2,
"system:index": "13"
}),
ee.Feature(
ee.Geometry.Point([54.664727, 24.757627]),
{
"landcover": 2,
"system:index": "14"
}),
ee.Feature(
ee.Geometry.Point([54.42667, 24.47841]),
{
"landcover": 2,
"system:index": "15"
}),
ee.Feature(
ee.Geometry.Point([54.4271, 24.47599]),
{
"landcover": 2,
"system:index": "16"
}),
ee.Feature(
ee.Geometry.Point([54.47023, 24.4845]),
{
"landcover": 2,
"system:index": "17"
}),
ee.Feature(
ee.Geometry.Point([54.58531, 24.51603]),
{
"landcover": 2,
"system:index": "18"
}),
ee.Feature(
ee.Geometry.Point([54.62381, 24.513337]),
{
"landcover": 2,
"system:index": "19"
}),
ee.Feature(
ee.Geometry.Point([54.44637, 24.41185]),
{
"landcover": 2,
"system:index": "20"
}),
ee.Feature(
ee.Geometry.Point([54.48804, 24.40563]),
{
"landcover": 2,
"system:index": "21"
}),
ee.Feature(
ee.Geometry.Point([54.33605, 24.44495]),
{
"landcover": 2,
"system:index": "22"
}),
ee.Feature(
ee.Geometry.Point([54.48436, 24.42838]),
{
"landcover": 2,
"system:index": "23"
}),
ee.Feature(
ee.Geometry.Point([55.10882, 25.1748]),
{
"landcover": 2,
"system:index": "24"
}),
ee.Feature(
ee.Geometry.Point([55.27399, 25.35773]),
{
"landcover": 2,
"system:index": "25"
}),
ee.Feature(
ee.Geometry.Point([55.35038, 25.3644]),
{
"landcover": 2,
"system:index": "26"
}),
ee.Feature(
ee.Geometry.Point([55.38243, 25.330931]),
{
"landcover": 2,
"system:index": "27"
}),
ee.Feature(
ee.Geometry.Point([55.379876, 25.335431]),
{
"landcover": 2,
"system:index": "28"
}),
ee.Feature(
ee.Geometry.Point([55.382623, 25.344972]),
{
"landcover": 2,
"system:index": "29"
}),
ee.Feature(
ee.Geometry.Point([55.37646, 25.35219]),
{
"landcover": 2,
"system:index": "30"
})]),
bareland = /* color: #B9AD6A */ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Point([55.7269, 25.3729]),
{
"landcover": 3,
"system:index": "0"
}),
ee.Feature(
ee.Geometry.Point([55.6157, 25.1419]),
{
"landcover": 3,
"system:index": "1"
}),
ee.Feature(
ee.Geometry.Point([55.753, 25.3803]),
{
"landcover": 3,
"system:index": "2"
}),
ee.Feature(
ee.Geometry.Point([56.084, 25.0896]),
{
"landcover": 3,
"system:index": "3"
}),
ee.Feature(
ee.Geometry.Point([55.264428, 25.175528]),
{
"landcover": 3,
"system:index": "4"
}),
ee.Feature(
ee.Geometry.Point([55.265318, 25.177217]),
{
"landcover": 3,
"system:index": "5"
}),
ee.Feature(
ee.Geometry.Point([55.273011, 25.176266]),
{
"landcover": 3,
"system:index": "6"
}),
ee.Feature(
ee.Geometry.Point([55.301458, 25.201684]),
{
"landcover": 3,
"system:index": "7"
}),
ee.Feature(
ee.Geometry.Point([55.30899, 25.21635]),
{
"landcover": 3,
"system:index": "8"
}),
ee.Feature(
ee.Geometry.Point([55.397345, 25.209051]),
{
"landcover": 3,
"system:index": "9"
}),
ee.Feature(
ee.Geometry.Point([55.38929, 25.187195]),
{
"landcover": 3,
"system:index": "10"
}),
ee.Feature(
ee.Geometry.Point([55.4011, 25.18801]),
{
"landcover": 3,
"system:index": "11"
}),
ee.Feature(
ee.Geometry.Point([55.4763, 25.17446]),
{
"landcover": 3,
"system:index": "12"
}),
ee.Feature(
ee.Geometry.Point([55.55775, 25.33953]),
{
"landcover": 3,
"system:index": "13"
}),
ee.Feature(
ee.Geometry.Point([55.57844, 25.33313]),
{
"landcover": 3,
"system:index": "14"
}),
ee.Feature(
ee.Geometry.Point([55.55668, 25.33845]),
{
"landcover": 3,
"system:index": "15"
}),
ee.Feature(
ee.Geometry.Point([55.360233, 25.300299]),
{
"landcover": 3,
"system:index": "16"
}),
ee.Feature(
ee.Geometry.Point([55.423706, 25.28803]),
{
"landcover": 3,
"system:index": "17"
}),
ee.Feature(
ee.Geometry.Point([55.422247, 25.288277]),
{
"landcover": 3,
"system:index": "18"
}),
ee.Feature(
ee.Geometry.Point([55.4312532, 25.2830029]),
{
"landcover": 3,
"system:index": "19"
}),
ee.Feature(
ee.Geometry.Point([55.4323301, 25.2828392]),
{
"landcover": 3,
"system:index": "20"
}),
ee.Feature(
ee.Geometry.Point([55.467375, 25.302659]),
{
"landcover": 3,
"system:index": "21"
}),
ee.Feature(
ee.Geometry.Point([55.4736, 25.30915]),
{
"landcover": 3,
"system:index": "22"
}),
ee.Feature(
ee.Geometry.Point([55.476627, 25.316622]),
{
"landcover": 3,
"system:index": "23"
}),
ee.Feature(
ee.Geometry.Point([55.542115, 25.331223]),
{
"landcover": 3,
"system:index": "24"
}),
ee.Feature(
ee.Geometry.Point([55.69196, 25.34876]),
{
"landcover": 5,
"system:index": "25"
}),
ee.Feature(
ee.Geometry.Point([55.810398, 25.284462]),
{
"landcover": 3,
"system:index": "26"
}),
ee.Feature(
ee.Geometry.Point([55.89562, 25.32646]),
{
"landcover": 3,
"system:index": "27"
}),
ee.Feature(
ee.Geometry.Point([56.021509, 25.384165]),
{
"landcover": 3,
"system:index": "28"
}),
ee.Feature(
ee.Geometry.Point([54.82216, 24.36272]),
{
"landcover": 3,
"system:index": "29"
}),
ee.Feature(
ee.Geometry.Point([54.84731, 24.3753]),
{
"landcover": 3,
"system:index": "30"
})]);
Land Cover Classification
////////////////////////////////////////////////////////////////
// Landcover 2018
//////////////////////////////////////////////////////////////
// Load the Landsat 8 scaled radiance image collection.
var landsatCollectionL8 = ee.ImageCollection('LANDSAT/LC08/C01/T1')
.filterDate('2018-01-01', '2018-12-31');
// Make a cloud-free composite.
var composite = ee.Algorithms.Landsat.simpleComposite({
collection: landsatCollectionL8,
asFloat: true
});
// Merge the four geometry layers into a single FeatureCollection.
var newfc_2018 = urban.merge(vegetation).merge(water).merge(bareland);
// 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_2018,
properties: [classProperty],
scale: 30
});
// Train a CART classifier.
var classifier = ee.Classifier.cart().train({
features: training,
classProperty: classProperty,
});
// Print some info about the classifier (specific to CART).
print('CART, explained', classifier.explain());
// Classify the composite.
var classified_2018 = composite.classify(classifier);
Map.centerObject(newfc_2018, 11);
Map.addLayer(classified_2018.clip(roi), {min: 0, max: 3, palette: ['red','green','blue','LIGHTSALMON']},'LandCover 2018');
// 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.gmoMaxEnt().train({
features: trainingPartition,
classProperty: classProperty,
inputProperties: bands
});
// Classify the test FeatureCollection.
var test = testingPartition.classify(trainedClassifier);
// Print the confusion matrix.
var confusionMatrix_2018 = test.errorMatrix(classProperty, 'classification');
print('Confusion Matrix 2018', confusionMatrix_2018);

Add Title and Legends
////////////////////////////////////////////////////////////////
/************************ Title ****************************/
/////////////////////////////////////////////////////////////////
Map.add(ui.Label(
'2018 Land Cover Map of Dubai - Palm Jumeirah', {
fontWeight: 'bold', BackgroundColor: 'FBF9F5',fontSize: '14px'}));
/////////////////////////////////////////////////////////////
/************************ legend ****************************/
//////////////////////////////////////////////////////////////
var names = ['Urban', 'Vegetation', 'Water', 'Bare Land' ];
var values = [ '1', '2', '3', '4' ];
var legendsPalette = ['F93107','04FD0A','040AFD','D0435B'];
// set position of panel
var legend = ui.Panel({style: { position: 'bottom-left', padding: '8px 15px'}});
// Create legend title
var legendTitle = ui.Label({value: 'Legends ',style: {
fontWeight: 'bold', fontSize: '18px', margin: '0 0 4px 0', padding: '0' }});
// Add the title to the panel
legend.add(legendTitle);
var makeRow = function(color, name) {
// Create the label that is actually the colored box.
var colorBox = ui.Label({
style: {
backgroundColor: '#' + color, padding: '8px',margin: '0 0 4px 0'} });
// Create the label filled with the description text.
var description = ui.Label({
value: name, style: {margin: '0 0 4px 6px'}});
// return the panel
return ui.Panel({
widgets: [colorBox, description],layout: ui.Panel.Layout.Flow('horizontal')})};
// Add color and and names
for (var i = 0; i < 4; i++) {
legend.add(makeRow(legendsPalette[i], names[i]));
}
// Add the legend to the map.
Map.add(legend);
What is the meaning of system:index?
Nice! Do you have any code that uses the training data from the present, in order to look at the past? I’ve made some classification code just like yours, and now I want to classify the land use from the past using the training polygons from this year.