Uncomment the following line to install geemap if needed.
# !pip install geemap
Machine Learning with Earth Engine - Unsupervised Classification¶
Unsupervised classification algorithms available in Earth Engine¶
Source: https://developers.google.com/earth-engine/clustering
The ee.Clusterer
package handles unsupervised classification (or clustering) in Earth Engine. These algorithms are currently based on the algorithms with the same name in Weka. More details about each Clusterer are available in the reference docs in the Code Editor.
Clusterers are used in the same manner as classifiers in Earth Engine. The general workflow for clustering is:
- Assemble features with numeric properties in which to find clusters.
- Instantiate a clusterer. Set its parameters if necessary.
- Train the clusterer using the training data.
- Apply the clusterer to an image or feature collection.
- Label the clusters.
The training data is a FeatureCollection
with properties that will be input to the clusterer. Unlike classifiers, there is no input class value for an Clusterer
. Like classifiers, the data for the train and apply steps are expected to have the same number of values. When a trained clusterer is applied to an image or table, it assigns an integer cluster ID to each pixel or feature.
Here is a simple example of building and using an ee.Clusterer:
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('2019-01-01', '2019-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¶
props = geemap.image_props(image)
props.getInfo()
{'CLOUD_COVER': 0.03, 'CLOUD_COVER_LAND': 0.04, 'EARTH_SUN_DISTANCE': 1.016591, 'ESPA_VERSION': '2_23_0_1b', 'GEOMETRIC_RMSE_MODEL': 6.348, 'GEOMETRIC_RMSE_MODEL_X': 4.429, 'GEOMETRIC_RMSE_MODEL_Y': 4.547, 'IMAGE_DATE': '2019-07-12', 'IMAGE_QUALITY_OLI': 9, 'IMAGE_QUALITY_TIRS': 9, 'LANDSAT_ID': 'LC08_L1TP_022031_20190712_20190719_01_T1', 'LEVEL1_PRODUCTION_DATE': 1563565308000, 'NOMINAL_SCALE': 30, 'PIXEL_QA_VERSION': 'generate_pixel_qa_1.6.0', 'SATELLITE': 'LANDSAT_8', 'SENSING_TIME': '2019-07-12T16:28:51.3794760Z', 'SOLAR_AZIMUTH_ANGLE': 131.949371, 'SOLAR_ZENITH_ANGLE': 26.494972, 'SR_APP_VERSION': 'LaSRC_1.3.0', 'WRS_PATH': 22, 'WRS_ROW': 31, 'system:asset_size': '553.046839 MB', 'system:band_names': ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7'], 'system:id': 'LANDSAT/LC08/C01/T1_SR/LC08_022031_20190712', 'system:index': 'LC08_022031_20190712', 'system:time_end': '2019-07-12 16:28:51', 'system:time_start': '2019-07-12 16:28:51', 'system:version': 1564390084630490.0}
props.get('IMAGE_DATE').getInfo()
'2019-07-12'
props.get('CLOUD_COVER').getInfo()
0.03
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)
# Make the training dataset.
training = image.sample(
**{
# 'region': region,
'scale': 30,
'numPixels': 5000,
'seed': 0,
'geometries': True, # Set this to False to ignore geometries
}
)
Map.addLayer(training, {}, 'training', False)
Map
Train the clusterer¶
# Instantiate the clusterer and train it.
n_clusters = 5
clusterer = ee.Clusterer.wekaKMeans(n_clusters).train(training)
Classify the image¶
# Cluster the input using the trained clusterer.
result = image.cluster(clusterer)
# # Display the clusters with random colors.
Map.addLayer(result.randomVisualizer(), {}, 'clusters')
Map
Label the clusters¶
legend_keys = ['One', 'Two', 'Three', 'Four', 'ect']
legend_colors = ['#8DD3C7', '#FFFFB3', '#BEBADA', '#FB8072', '#80B1D3']
# Reclassify the map
result = result.remap([0, 1, 2, 3, 4], [1, 2, 3, 4, 5])
Map.addLayer(
result, {'min': 1, 'max': 5, 'palette': legend_colors}, 'Labelled clusters'
)
Map.add_legend(
legend_keys=legend_keys, legend_colors=legend_colors, position='bottomright'
)
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 [13], 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'
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, 'cluster.tif')
geemap.ee_export_image(result, filename=out_file, scale=90)
Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/18979d5b09def11ea51d29891e017efd-f01024a70f3a8539e679655e4194c76e:getPixels Please wait ... Data downloaded to /home/runner/Downloads/cluster.tif
Export the result to Google Drive:
geemap.ee_export_image_to_drive(
result, description='clusters', folder='export', scale=90
)
Exporting clusters ...