Uncomment the following line to install geemap if needed.
# !pip install geemap
Machine Learning with Earth Engine - Supervised Classification¶
Supervised classification algorithms available in Earth Engine¶
Source: https://developers.google.com/earth-engine/classification
The Classifier
package handles supervised classification by traditional ML algorithms running in Earth Engine. These classifiers include CART, RandomForest, NaiveBayes and SVM. The general workflow for classification is:
- Collect training data. Assemble features which have a property that stores the known class label and properties storing numeric values for the predictors.
- Instantiate a classifier. Set its parameters if necessary.
- Train the classifier using the training data.
- Classify an image or feature collection.
- Estimate classification error with independent validation data.
The training data is a FeatureCollection
with a property storing the class label and properties storing predictor variables. Class labels should be consecutive, integers starting from 0. If necessary, use remap() to convert class values to consecutive integers. The predictors should be numeric.
import ee
import geemap
Create an interactive map¶
Map = geemap.Map()
Map
Add data to the map¶
point = ee.Geometry.Point([-122.4439, 37.7538])
# point = ee.Geometry.Point([-87.7719, 41.8799])
image = (
ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterBounds(point)
.filterDate('2016-01-01', '2016-12-31')
.sort('CLOUD_COVER')
.first()
.select('B[1-7]')
)
vis_params = {'min': 0, 'max': 3000, 'bands': ['B5', 'B4', 'B3']}
Map.centerObject(point, 8)
Map.addLayer(image, vis_params, "Landsat-8")
Check image properties¶
ee.Date(image.get('system:time_start')).format('YYYY-MM-dd').getInfo()
'2016-11-18'
image.get('CLOUD_COVER').getInfo()
0.08
Make training dataset¶
There are several ways you can create a region for generating the training dataset.
- Draw a shape (e.g., rectangle) on the map and the use
region = Map.user_roi
- Define a geometry, such as
region = ee.Geometry.Rectangle([-122.6003, 37.4831, -121.8036, 37.8288])
- Create a buffer zone around a point, such as
region = ee.Geometry.Point([-122.4439, 37.7538]).buffer(10000)
- If you don't define a region, it will use the image footprint by default
# region = Map.user_roi
# region = ee.Geometry.Rectangle([-122.6003, 37.4831, -121.8036, 37.8288])
# region = ee.Geometry.Point([-122.4439, 37.7538]).buffer(10000)
In this example, we are going to use the USGS National Land Cover Database (NLCD) to create label dataset for training
nlcd = ee.Image('USGS/NLCD/NLCD2016').select('landcover').clip(image.geometry())
Map.addLayer(nlcd, {}, 'NLCD')
Map
# Make the training dataset.
points = nlcd.sample(
**{
'region': image.geometry(),
'scale': 30,
'numPixels': 5000,
'seed': 0,
'geometries': True, # Set this to False to ignore geometries
}
)
Map.addLayer(points, {}, 'training', False)
print(points.size().getInfo())
3583
print(points.first().getInfo())
{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.25798986874739, 38.2706212827936]}, 'id': '0', 'properties': {'landcover': 31}}
Train the classifier¶
# Use these bands for prediction.
bands = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7']
# This property of the table stores the land cover labels.
label = 'landcover'
# Overlay the points on the imagery to get training.
training = image.select(bands).sampleRegions(
**{'collection': points, 'properties': [label], 'scale': 30}
)
# Train a CART classifier with default parameters.
trained = ee.Classifier.smileCart().train(training, label, bands)
print(training.first().getInfo())
{'type': 'Feature', 'geometry': None, 'id': '0_0', 'properties': {'B1': 575, 'B2': 814, 'B3': 1312, 'B4': 1638, 'B5': 1980, 'B6': 2091, 'B7': 1967, 'landcover': 31}}
Classify the image¶
# Classify the image with the same bands used for training.
result = image.select(bands).classify(trained)
# # Display the clusters with random colors.
Map.addLayer(result.randomVisualizer(), {}, 'classified')
Map
Render categorical map¶
To render a categorical map, we can set two image properties: landcover_class_values
and landcover_class_palette
. We can use the same style as the NLCD so that it is easy to compare the two maps.
class_values = nlcd.get('landcover_class_values').getInfo()
class_values
[11, 12, 21, 22, 23, 24, 31, 41, 42, 43, 51, 52, 71, 72, 73, 74, 81, 82, 90, 95]
class_palette = nlcd.get('landcover_class_palette').getInfo()
class_palette
['476ba1', 'd1defa', 'decaca', 'd99482', 'ee0000', 'ab0000', 'b3aea3', '68ab63', '1c6330', 'b5ca8f', 'a68c30', 'ccba7d', 'e3e3c2', 'caca78', '99c247', '78ae94', 'dcd93d', 'ab7028', 'bad9eb', '70a3ba']
landcover = result.set('classification_class_values', class_values)
landcover = landcover.set('classification_class_palette', class_palette)
Map.addLayer(landcover, {}, 'Land cover')
Map
Visualize the result¶
print('Change layer opacity:')
cluster_layer = Map.layers[-1]
cluster_layer.interact(opacity=(0, 1, 0.1))
Change layer opacity:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [19], in <cell line: 2>() 1 print('Change layer opacity:') ----> 2 cluster_layer = Map.layers[-1] 3 cluster_layer.interact(opacity=(0, 1, 0.1)) AttributeError: 'Map' object has no attribute 'layers'
Add a legend to the map¶
Map.add_legend(builtin_legend='NLCD')
Map
Export the result¶
Export the result directly to your computer:
import os
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
out_file = os.path.join(out_dir, 'landcover.tif')
geemap.ee_export_image(landcover, filename=out_file, scale=900)
Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/1f869d6448a8272a76aa60b524e5ae11-1278c09068c0e2ec898c61e66dd42254:getPixels Please wait ... Data downloaded to /home/runner/Downloads/landcover.tif
Export the result to Google Drive:
geemap.ee_export_image_to_drive(
landcover, description='landcover', folder='export', scale=900
)
Exporting landcover ...