Link Search Menu Expand Document

Tutorial 13

Unsupervised Classification

// Construct a study area for further analysis.
var studysite = ee.Geometry.Polygon(
[[[-76.1015, 43.0793],
[-76.0981, 43.0190],
[-76.0203, 43.0225],
[-76.0258, 43.0808]]]);

// Find and import a Landsat 5 image scene to classify.
var input = ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA')
.filterBounds(studysite) //filter based on the study site
.filterDate('2011-06-01','2011-06-04') //to match the NAIP imagery used for validation
.sort('CLOUD_COVER') //sort all images by cloud cover to obtain the best image to classify
.mosaic() //this converts the output of this ee.ImageCollection into an image
.clip(studysite); //clip the selected image scene with the study site

// Set map display to center on the study area at a zoom level of 13
Map.centerObject(studysite, 13);

// Display the input image as a CIR composite.
Map.addLayer(input, {bands: ['B4', 'B3', 'B2']}, 'input image');

/* Sample the image to construct a “training” sample of pixels to characterize the image. Unsupervised classifiers do not need training data; however, processing time is decreased by generating image statistics from a subset of pixels.  Rather than sampling pixels, you can increase the numPixels value to include all pixels in the scene but this will also increase script run time.*/

var training = input.sample({
region: studysite,
scale: 30,
numPixels: 5000
});

// Instantiate the unsupervised classifier and train it. Here we specify three classes.  
var clusterer = ee.Clusterer.wekaKMeans(3).train(training);

// Classify the input image using the trained unsupervised classifier.
var result = input.cluster(clusterer);

// Display the classified image with random colors for each of the three classes
Map.addLayer(result.randomVisualizer(), {}, 'classified image');

//Visually interpret the classified image. What do you think the three classes should be?

Supervised Classification

//training data
var Veg_train = /* color: #d63000 */ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Point([-76.04514190514682, 43.05125390556197]),
{
"class": 1,
"system:index": "0"
}),
ee.Feature(
ee.Geometry.Point([-76.04497024376987, 43.055267867190885]),
{
"class": 1,
"system:index": "1"
}),
ee.Feature(
ee.Geometry.Point([-76.03844711144565, 43.05551873106963]),
{
"class": 1,
"system:index": "2"
}),
ee.Feature(
ee.Geometry.Point([-76.0386187728226, 43.05175566513292]),
{
"class": 1,
"system:index": "3"
}),
ee.Feature(
ee.Geometry.Point([-76.03655883629916, 43.04849415454599]),
{
"class": 1,
"system:index": "4"
}),
ee.Feature(
ee.Geometry.Point([-76.03261062462924, 43.047866921088364]),
{
"class": 1,
"system:index": "5"
}),
ee.Feature(
ee.Geometry.Point([-76.03072234948276, 43.04560882753369]),
{
"class": 1,
"system:index": "6"
}),
ee.Feature(
ee.Geometry.Point([-76.03587219079135, 43.05689846405862]),
{
"class": 1,
"system:index": "7"
}),
ee.Feature(
ee.Geometry.Point([-76.03724548180698, 43.03971230247596]),
{
"class": 1,
"system:index": "8"
}),
ee.Feature(
ee.Geometry.Point([-76.03947707970737, 43.03971230247596]),
{
"class": 1,
"system:index": "9"
}),
ee.Feature(
ee.Geometry.Point([-76.0411936934769, 43.03921044438776]),
{
"class": 1,
"system:index": "10"
}),
ee.Feature(
ee.Geometry.Point([-76.08359405358432, 43.03657562208008]),
{
"class": 1,
"system:index": "11"
}),
ee.Feature(
ee.Geometry.Point([-76.08496734459995, 43.03594826675961]),
{
"class": 1,
"system:index": "12"
}),
ee.Feature(
ee.Geometry.Point([-76.0851390059769, 43.035697322835595]),
{
"class": 1,
"system:index": "13"
}),
ee.Feature(
ee.Geometry.Point([-76.08651229699252, 43.03444258782248]),
{
"class": 1,
"system:index": "14"
})]),
Nonveg_train = /* color: #98ff00 */ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Point([-76.05990478356479, 43.05426440141344]),
{
"class": 0,
"system:index": "0"
}),
ee.Feature(
ee.Geometry.Point([-76.06179305871127, 43.054766136354665]),
{
"class": 0,
"system:index": "1"
}),
ee.Feature(
ee.Geometry.Point([-76.0660845931351, 43.04736512970422]),
{
"class": 0,
"system:index": "2"
}),
ee.Feature(
ee.Geometry.Point([-76.06814452965854, 43.04736512970422]),
{
"class": 0,
"system:index": "3"
}),
ee.Feature(
ee.Geometry.Point([-76.06797286828159, 43.0454833754545]),
{
"class": 0,
"system:index": "4"
}),
ee.Feature(
ee.Geometry.Point([-76.06711456139682, 43.04435429519665]),
{
"class": 0,
"system:index": "5"
}),
ee.Feature(
ee.Geometry.Point([-76.06539794762729, 43.04661243493134]),
{
"class": 0,
"system:index": "6"
}),
ee.Feature(
ee.Geometry.Point([-76.06144973595737, 43.04698878347229]),
{
"class": 0,
"system:index": "7"
}),
ee.Feature(
ee.Geometry.Point([-76.06265136559604, 43.04861960046784]),
{
"class": 0,
"system:index": "8"
}),
ee.Feature(
ee.Geometry.Point([-76.05904647668002, 43.0491213815897]),
{
"class": 0,
"system:index": "9"
}),
ee.Feature(
ee.Geometry.Point([-76.05904647668002, 43.0471142324728]),
{
"class": 0,
"system:index": "10"
}),
ee.Feature(
ee.Geometry.Point([-76.06093475182651, 43.04096692973783]),
{
"class": 0,
"system:index": "11"
}),
ee.Feature(
ee.Geometry.Point([-76.06368133385776, 43.04084146816613]),
{
"class": 0,
"system:index": "12"
}),
ee.Feature(
ee.Geometry.Point([-76.06505462487338, 43.04096692973783]),
{
"class": 0,
"system:index": "13"
}),
ee.Feature(
ee.Geometry.Point([-76.06505462487338, 43.03783031347974]),
{
"class": 0,
"system:index": "14"
}),
ee.Feature(
ee.Geometry.Point([-76.0660845931351, 43.04071600633788]),
{
"class": 0,
"system:index": "15"
})]);



// Determine a study area for further analysis
var studysite = ee.Geometry.Polygon(
[[[-76.1015, 43.0793],
[-76.0981, 43.0190],
[-76.0203, 43.0225],
[-76.0258, 43.0808]]]);

// Import all the available Landsat 5 images then filter by location and sort by cloud cover.
var L5filtered = ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA')
.filterBounds(studysite) //filter to limit images to those over the study site
.sort('CLOUD_COVER'); //sort by cloud cover to obtain an ideal image for classification

// Select the first image (i.e. the scene with the least cloud cover) in the sorted collection above.
var input = ee.Image(L5filtered.first()) //first in sort on cloud cover is least cloudy
.clip(studysite); //clip the selected image scene to the study site

// Set map display to center on the study area at a zoom level of 13
Map.centerObject(studysite, 13);

// Display the input image as a color infrared composite. 
Map.addLayer(input, {bands: ['B4', 'B3', 'B2']}, 'input image');

/* Manually label training points as vegetation or non-vegetation. 
To do this you need to first create two new layers (under "Geometry Imports").  Click on the gear 
icon next to each layer name, renaming the layers as Veg_train and Nonveg_train, and change both to FeatureCollections. Once the layers are created, select one of the layers and then click on the 
point icon and each identify reference points that belong to the class. After all of the reference 
points are labeled click on the gear icon again and add a property for both Veg_ref and 
Nonveg_ref layers, which has name = class and value = 1 for veg or 0 for non-veg.
*/

// Combine the veg and non-veg layers into a reference dataset
var training = Veg_train.merge(Nonveg_train);

/* Use the training data to sample the classified image and create a new band called class that 
contains the corresponding value for each reference point based on the classified image.
*/
var training = input.sampleRegions({
collection: training,
properties: ['class'],
scale: 30
});

/* Create and train a random forest classifier. A random forest classifier uses a combination of decision tree classifiers where each is generated using a random vector sampled independently from the input vector. Each tree casts a unit vote for the most popular class to classify an input vector (Pal 2005). */
var classifier = ee.Classifier.randomForest(100) //set the number of trees in random forest to 100
.train(training, 'class'); //'nd' is 0 for non-vegetation and 1 for vegetation cover types.

// Classify the input imagery.
var classified = input.classify(classifier);

// Display classified image in the interactive map. Non-vegetation land cover has a pixel value of 0 and vegetation cover has a value of 1.
Map.addLayer(classified,{min:0,max:1},'classified image');