F21c Checkpoint
Import libraries¶
In [ ]:
Copied!
import ee
import geemap
import ee
import geemap
Create an interactive map¶
In [ ]:
Copied!
Map = geemap.Map(center=[40, -100], zoom=4)
Map = geemap.Map(center=[40, -100], zoom=4)
Add Earth Engine Python script¶
In [ ]:
Copied!
# Add Earth Engine dataset
image = ee.Image("USGS/SRTMGL1_003")
# *** Start of imports. If edited, may not auto-convert in the playground. ***#
forest = (
ee.FeatureCollection(
[
ee.Feature(
ee.Geometry.Point([9.414318330642528, 44.573441859314016]),
{"class": 0, "system:index": "0"},
),
ee.Feature(
ee.Geometry.Point([9.427705541156865, 44.58422671008443]),
{"class": 0, "system:index": "1"},
),
ee.Feature(
ee.Geometry.Point([9.400411382221318, 44.59498464279749]),
{"class": 0, "system:index": "2"},
),
ee.Feature(
ee.Geometry.Point([9.427190557026005, 44.600607315157376]),
{"class": 0, "system:index": "3"},
),
ee.Feature(
ee.Geometry.Point([9.427018895649052, 44.571877083168374]),
{"class": 0, "system:index": "4"},
),
ee.Feature(
ee.Geometry.Point([9.352346196674443, 44.56062563530188]),
{"class": 0, "system:index": "5"},
),
ee.Feature(
ee.Geometry.Point([9.584676704909665, 44.528474349106816]),
{"class": 0, "system:index": "6"},
),
ee.Feature(
ee.Geometry.Point([9.568368874099118, 44.53899788426392]),
{"class": 0, "system:index": "7"},
),
ee.Feature(
ee.Geometry.Point([9.557210884597165, 44.534470549891495]),
{"class": 0, "system:index": "8"},
),
ee.Feature(
ee.Geometry.Point([9.563219032790524, 44.508523361153955]),
{"class": 0, "system:index": "9"},
),
ee.Feature(
ee.Geometry.Point([9.652654610183102, 44.530799479832886]),
{"class": 0, "system:index": "10"},
),
ee.Feature(
ee.Geometry.Point([9.691499968871002, 44.50146601530913]),
{"class": 0, "system:index": "11"},
),
ee.Feature(
ee.Geometry.Point([9.67055728088272, 44.502567915176314]),
{"class": 0, "system:index": "12"},
),
ee.Feature(
ee.Geometry.Point([9.668154021605377, 44.4990172743743]),
{"class": 0, "system:index": "13"},
),
ee.Feature(
ee.Geometry.Point([9.668154021605377, 44.48285300484169]),
{"class": 0, "system:index": "14"},
),
ee.Feature(
ee.Geometry.Point([9.65733935485733, 44.482118158872886]),
{"class": 0, "system:index": "15"},
),
ee.Feature(
ee.Geometry.Point([9.605325957640533, 44.49448683251806]),
{"class": 0, "system:index": "16"},
),
ee.Feature(
ee.Geometry.Point([9.592108031615142, 44.47342177876237]),
{"class": 0, "system:index": "17"},
),
ee.Feature(
ee.Geometry.Point([9.69252993713272, 44.47084936162859]),
{"class": 0, "system:index": "18"},
),
ee.Feature(
ee.Geometry.Point([9.738535186156158, 44.48922128217961]),
{"class": 0, "system:index": "19"},
),
ee.Feature(
ee.Geometry.Point([9.80740370242143, 44.44605496001787]),
{"class": 0, "system:index": "20"},
),
ee.Feature(
ee.Geometry.Point([9.759338516874555, 44.4477706457772]),
{"class": 0, "system:index": "21"},
),
ee.Feature(
ee.Geometry.Point([9.753158707304243, 44.45120186611758]),
{"class": 0, "system:index": "22"},
),
ee.Feature(
ee.Geometry.Point([10.622735604942841, 44.17490267165223]),
{"class": 0, "system:index": "23"},
),
ee.Feature(
ee.Geometry.Point([10.514245614708466, 44.1906597047536]),
{"class": 0, "system:index": "24"},
),
]
),
)
developed = (
ee.FeatureCollection(
[
ee.Feature(
ee.Geometry.Point([9.19467878610836, 45.46435783697627]),
{"class": 1, "system:index": "0"},
),
ee.Feature(
ee.Geometry.Point([9.203090193579063, 45.464899612733554]),
{"class": 1, "system:index": "1"},
),
ee.Feature(
ee.Geometry.Point([9.204463484594688, 45.462852876998106]),
{"class": 1, "system:index": "2"},
),
ee.Feature(
ee.Geometry.Point([9.180087569067345, 45.458879589622896]),
{"class": 1, "system:index": "3"},
),
ee.Feature(
ee.Geometry.Point([9.18068838388668, 45.45641119434588]),
{"class": 1, "system:index": "4"},
),
ee.Feature(
ee.Geometry.Point([9.167642119238243, 45.455267267211944]),
{"class": 1, "system:index": "5"},
),
ee.Feature(
ee.Geometry.Point([9.165668013403282, 45.45340080996563]),
{"class": 1, "system:index": "6"},
),
ee.Feature(
ee.Geometry.Point([9.167556288549767, 45.45135365679475]),
{"class": 1, "system:index": "7"},
),
ee.Feature(
ee.Geometry.Point([9.167813780615196, 45.46297327527481]),
{"class": 1, "system:index": "8"},
),
ee.Feature(
ee.Geometry.Point([9.176911833593712, 45.46706666369604]),
{"class": 1, "system:index": "9"},
),
ee.Feature(
ee.Geometry.Point([9.223853948611414, 45.47248322583851]),
{"class": 1, "system:index": "10"},
),
ee.Feature(
ee.Geometry.Point([9.242822530764734, 45.47537221988128]),
{"class": 1, "system:index": "11"},
),
ee.Feature(
ee.Geometry.Point([9.211236837405359, 45.47729813363197]),
{"class": 1, "system:index": "12"},
),
ee.Feature(
ee.Geometry.Point([9.203597906130945, 45.47669629265562]),
{"class": 1, "system:index": "13"},
),
ee.Feature(
ee.Geometry.Point([9.154784524052499, 45.18401973624338]),
{"class": 1, "system:index": "14"},
),
ee.Feature(
ee.Geometry.Point([9.158217751591561, 45.185713605296485]),
{"class": 1, "system:index": "15"},
),
ee.Feature(
ee.Geometry.Point([9.156057754172835, 45.18958733293765]),
{"class": 1, "system:index": "16"},
),
ee.Feature(
ee.Geometry.Point([9.160520949973616, 45.19122054778279]),
{"class": 1, "system:index": "17"},
),
ee.Feature(
ee.Geometry.Point([9.158289352073226, 45.19599894427136]),
{"class": 1, "system:index": "18"},
),
ee.Feature(
ee.Geometry.Point([9.144728103293929, 45.194063439607106]),
{"class": 1, "system:index": "19"},
),
ee.Feature(
ee.Geometry.Point([9.145843902244124, 45.192914202565674]),
{"class": 1, "system:index": "20"},
),
ee.Feature(
ee.Geometry.Point([9.138548293723616, 45.19019223278913]),
{"class": 1, "system:index": "21"},
),
ee.Feature(
ee.Geometry.Point([9.144470611228499, 45.17924255060339]),
{"class": 1, "system:index": "22"},
),
ee.Feature(
ee.Geometry.Point([9.180176177634749, 45.18347748407685]),
{"class": 1, "system:index": "23"},
),
ee.Feature(
ee.Geometry.Point([9.177257934226546, 45.18626026884151]),
{"class": 1, "system:index": "24"},
),
]
),
)
water = (
ee.FeatureCollection(
[
ee.Feature(
ee.Geometry.Point([9.980378415331183, 43.953687197631126]),
{"class": 2, "system:index": "0"},
),
ee.Feature(
ee.Geometry.Point([9.735932614549933, 44.00704943549187]),
{"class": 2, "system:index": "1"},
),
ee.Feature(
ee.Geometry.Point([9.461274411424933, 44.12546046543348]),
{"class": 2, "system:index": "2"},
),
ee.Feature(
ee.Geometry.Point([9.282746579393683, 44.235763707116455]),
{"class": 2, "system:index": "3"},
),
ee.Feature(
ee.Geometry.Point([10.068269040331183, 43.94775509959863]),
{"class": 2, "system:index": "4"},
),
ee.Feature(
ee.Geometry.Point([10.202851559862433, 43.765549960385265]),
{"class": 2, "system:index": "5"},
),
ee.Feature(
ee.Geometry.Point([10.178132321581183, 43.72785127863614]),
{"class": 2, "system:index": "6"},
),
ee.Feature(
ee.Geometry.Point([10.213837887987433, 43.6821842672651]),
{"class": 2, "system:index": "7"},
),
ee.Feature(
ee.Geometry.Point([10.065522458299933, 43.72586647981521]),
{"class": 2, "system:index": "8"},
),
ee.Feature(
ee.Geometry.Point([9.941926266893683, 43.894339574154564]),
{"class": 2, "system:index": "9"},
),
ee.Feature(
ee.Geometry.Point([9.88486387395865, 45.077069068166345]),
{"class": 2, "system:index": "10"},
),
ee.Feature(
ee.Geometry.Point([9.848471662044588, 45.09864207259974]),
{"class": 2, "system:index": "11"},
),
ee.Feature(
ee.Geometry.Point([9.81448270940787, 45.0773115067334]),
{"class": 2, "system:index": "12"},
),
ee.Feature(
ee.Geometry.Point([9.828902265071932, 45.06179336476103]),
{"class": 2, "system:index": "13"},
),
ee.Feature(
ee.Geometry.Point([9.95455839300162, 45.11657297387295]),
{"class": 2, "system:index": "14"},
),
ee.Feature(
ee.Geometry.Point([10.002623578548494, 45.115603879971225]),
{"class": 2, "system:index": "15"},
),
ee.Feature(
ee.Geometry.Point([10.02322294378287, 45.10276183206325]),
{"class": 2, "system:index": "16"},
),
ee.Feature(
ee.Geometry.Point([10.043822309017244, 45.07149269722714]),
{"class": 2, "system:index": "17"},
),
ee.Feature(
ee.Geometry.Point([10.248517220628196, 45.02613418018433]),
{"class": 2, "system:index": "18"},
),
ee.Feature(
ee.Geometry.Point([10.263280099046165, 45.01836869065415]),
{"class": 2, "system:index": "19"},
),
ee.Feature(
ee.Geometry.Point([10.280102913987571, 45.006475743526394]),
{"class": 2, "system:index": "20"},
),
ee.Feature(
ee.Geometry.Point([10.265340035569603, 45.03802304474337]),
{"class": 2, "system:index": "21"},
),
ee.Feature(
ee.Geometry.Point([10.220021432053978, 45.023222245044195]),
{"class": 2, "system:index": "22"},
),
ee.Feature(
ee.Geometry.Point([10.182599251878196, 45.04069163407949]),
{"class": 2, "system:index": "23"},
),
ee.Feature(
ee.Geometry.Point([10.156163399827415, 45.03292811948761]),
{"class": 2, "system:index": "24"},
),
]
),
)
herbaceous = ee.FeatureCollection(
[
ee.Feature(
ee.Geometry.Point([9.647866129015439, 45.00732805319811]),
{"class": 3, "system:index": "0"},
),
ee.Feature(
ee.Geometry.Point([9.63911139879083, 45.00150212185294]),
{"class": 3, "system:index": "1"},
),
ee.Feature(
ee.Geometry.Point([9.63859641465997, 44.995432813342106]),
{"class": 3, "system:index": "2"},
),
ee.Feature(
ee.Geometry.Point([9.679108499620908, 44.99688950601573]),
{"class": 3, "system:index": "3"},
),
ee.Feature(
ee.Geometry.Point([9.680653452013486, 44.99106251314061]),
{"class": 3, "system:index": "4"},
),
ee.Feature(
ee.Geometry.Point([9.616967081163876, 44.98474926892666]),
{"class": 3, "system:index": "5"},
),
ee.Feature(
ee.Geometry.Point([9.625550150011533, 44.984627853569535]),
{"class": 3, "system:index": "6"},
),
ee.Feature(
ee.Geometry.Point([9.577313303087704, 44.99968139680247]),
{"class": 3, "system:index": "7"},
),
ee.Feature(
ee.Geometry.Point([9.682183386109417, 45.11939912570097]),
{"class": 3, "system:index": "8"},
),
ee.Feature(
ee.Geometry.Point([9.662270666382854, 45.142166966714186]),
{"class": 3, "system:index": "9"},
),
ee.Feature(
ee.Geometry.Point([9.655747534058635, 45.14483069451677]),
{"class": 3, "system:index": "10"},
),
ee.Feature(
ee.Geometry.Point([9.602875829957073, 45.13926093989158]),
{"class": 3, "system:index": "11"},
),
ee.Feature(
ee.Geometry.Point([9.57849991442973, 45.14265129011751]),
{"class": 3, "system:index": "12"},
),
ee.Feature(
ee.Geometry.Point([9.56476700427348, 45.14047180240153]),
{"class": 3, "system:index": "13"},
),
ee.Feature(
ee.Geometry.Point([9.55652725817973, 45.11794555067206]),
{"class": 3, "system:index": "14"},
),
ee.Feature(
ee.Geometry.Point([9.511208654664104, 45.12666644539884]),
{"class": 3, "system:index": "15"},
),
ee.Feature(
ee.Geometry.Point([9.601502538941448, 45.15887374651922]),
{"class": 3, "system:index": "16"},
),
ee.Feature(
ee.Geometry.Point([9.770417333863323, 45.15378959281897]),
{"class": 3, "system:index": "17"},
),
ee.Feature(
ee.Geometry.Point([9.706286368107783, 45.13319818507588]),
{"class": 3, "system:index": "18"},
),
ee.Feature(
ee.Geometry.Point([9.704827246403681, 45.13574123701328]),
{"class": 3, "system:index": "19"},
),
ee.Feature(
ee.Geometry.Point([9.699248251652705, 45.13955560228794]),
{"class": 3, "system:index": "20"},
),
ee.Feature(
ee.Geometry.Point([9.688776907658564, 45.13598342652142]),
{"class": 3, "system:index": "21"},
),
ee.Feature(
ee.Geometry.Point([9.688519415593134, 45.130655019734995]),
{"class": 3, "system:index": "22"},
),
ee.Feature(
ee.Geometry.Point([9.653500494694697, 45.13997940490131]),
{"class": 3, "system:index": "23"},
),
ee.Feature(
ee.Geometry.Point([9.67341321442126, 45.12260091467352]),
{"class": 3, "system:index": "24"},
),
]
)
# **** End of imports. If edited, may not auto-convert in the playground. ****#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Chapter: F2.1 Interpreting an Image: Classification
# Checkpoint: F21c
# Author: Andréa Puzzi Nicolau, Karen Dyson, David Saah, Nicholas Clinton
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create an Earth Engine Point object over Milan.
pt = ee.Geometry.Point([9.453, 45.424])
# Filter the Landsat 8 collection and select the least cloudy image.
landsat = (
ee.ImageCollection("LANDSAT/LC08/C02/T1_L2")
.filterBounds(pt)
.filterDate("2019-01-01", "2020-01-01")
.sort("CLOUD_COVER")
.first()
)
# Center the map on that image.
Map.centerObject(landsat, 8)
# Add Landsat image to the map.
visParams = {"bands": ["SR_B4", "SR_B3", "SR_B2"], "min": 7000, "max": 12000}
Map.addLayer(landsat, visParams, "Landsat 8 image")
# -----------------------------------------------------------------------
# CHECKPOINT
# -----------------------------------------------------------------------
# Combine training feature collections.
trainingFeatures = ee.FeatureCollection(
[forest, developed, water, herbaceous]
).flatten()
# Define prediction bands.
predictionBands = [
"SR_B1",
"SR_B2",
"SR_B3",
"SR_B4",
"SR_B5",
"SR_B6",
"SR_B7",
"ST_B10",
]
# Sample training points.
classifierTraining = landsat.select(predictionBands).sampleRegions(
{"collection": trainingFeatures, "properties": ["class"], "scale": 30}
)
######## CART Classifier #########/
# Train a CART Classifier.
classifier = ee.Classifier.smileCart().train(
{
"features": classifierTraining,
"classProperty": "class",
"inputProperties": predictionBands,
}
)
# Classify the Landsat image.
classified = landsat.select(predictionBands).classify(classifier)
# Define classification image visualization parameters.
classificationVis = {
"min": 0,
"max": 3,
"palette": ["589400", "ff0000", "1a11ff", "d0741e"],
}
# Add the classified image to the map.
Map.addLayer(classified, classificationVis, "CART classified")
#######/ Random Forest Classifier ##########/
# Train RF classifier.
RFclassifier = ee.Classifier.smileRandomForest(50).train(
{
"features": classifierTraining,
"classProperty": "class",
"inputProperties": predictionBands,
}
)
# Classify Landsat image.
RFclassified = landsat.select(predictionBands).classify(RFclassifier)
# Add classified image to the map.
Map.addLayer(RFclassified, classificationVis, "RF classified")
# -----------------------------------------------------------------------
# CHECKPOINT
# -----------------------------------------------------------------------
######## Unsupervised classification ########
# Make the training dataset.
training = landsat.sample(
{"region": landsat.geometry(), "scale": 30, "numPixels": 1000, "tileScale": 8}
)
# Instantiate the clusterer and train it.
clusterer = ee.Clusterer.wekaKMeans(4).train(training)
# Cluster the input using the trained clusterer.
Kclassified = landsat.cluster(clusterer)
# Display the clusters with random colors.
Map.addLayer(Kclassified.randomVisualizer(), {}, "K-means classified - random colors")
# -----------------------------------------------------------------------
# CHECKPOINT
# -----------------------------------------------------------------------
# Add Earth Engine dataset
image = ee.Image("USGS/SRTMGL1_003")
# *** Start of imports. If edited, may not auto-convert in the playground. ***#
forest = (
ee.FeatureCollection(
[
ee.Feature(
ee.Geometry.Point([9.414318330642528, 44.573441859314016]),
{"class": 0, "system:index": "0"},
),
ee.Feature(
ee.Geometry.Point([9.427705541156865, 44.58422671008443]),
{"class": 0, "system:index": "1"},
),
ee.Feature(
ee.Geometry.Point([9.400411382221318, 44.59498464279749]),
{"class": 0, "system:index": "2"},
),
ee.Feature(
ee.Geometry.Point([9.427190557026005, 44.600607315157376]),
{"class": 0, "system:index": "3"},
),
ee.Feature(
ee.Geometry.Point([9.427018895649052, 44.571877083168374]),
{"class": 0, "system:index": "4"},
),
ee.Feature(
ee.Geometry.Point([9.352346196674443, 44.56062563530188]),
{"class": 0, "system:index": "5"},
),
ee.Feature(
ee.Geometry.Point([9.584676704909665, 44.528474349106816]),
{"class": 0, "system:index": "6"},
),
ee.Feature(
ee.Geometry.Point([9.568368874099118, 44.53899788426392]),
{"class": 0, "system:index": "7"},
),
ee.Feature(
ee.Geometry.Point([9.557210884597165, 44.534470549891495]),
{"class": 0, "system:index": "8"},
),
ee.Feature(
ee.Geometry.Point([9.563219032790524, 44.508523361153955]),
{"class": 0, "system:index": "9"},
),
ee.Feature(
ee.Geometry.Point([9.652654610183102, 44.530799479832886]),
{"class": 0, "system:index": "10"},
),
ee.Feature(
ee.Geometry.Point([9.691499968871002, 44.50146601530913]),
{"class": 0, "system:index": "11"},
),
ee.Feature(
ee.Geometry.Point([9.67055728088272, 44.502567915176314]),
{"class": 0, "system:index": "12"},
),
ee.Feature(
ee.Geometry.Point([9.668154021605377, 44.4990172743743]),
{"class": 0, "system:index": "13"},
),
ee.Feature(
ee.Geometry.Point([9.668154021605377, 44.48285300484169]),
{"class": 0, "system:index": "14"},
),
ee.Feature(
ee.Geometry.Point([9.65733935485733, 44.482118158872886]),
{"class": 0, "system:index": "15"},
),
ee.Feature(
ee.Geometry.Point([9.605325957640533, 44.49448683251806]),
{"class": 0, "system:index": "16"},
),
ee.Feature(
ee.Geometry.Point([9.592108031615142, 44.47342177876237]),
{"class": 0, "system:index": "17"},
),
ee.Feature(
ee.Geometry.Point([9.69252993713272, 44.47084936162859]),
{"class": 0, "system:index": "18"},
),
ee.Feature(
ee.Geometry.Point([9.738535186156158, 44.48922128217961]),
{"class": 0, "system:index": "19"},
),
ee.Feature(
ee.Geometry.Point([9.80740370242143, 44.44605496001787]),
{"class": 0, "system:index": "20"},
),
ee.Feature(
ee.Geometry.Point([9.759338516874555, 44.4477706457772]),
{"class": 0, "system:index": "21"},
),
ee.Feature(
ee.Geometry.Point([9.753158707304243, 44.45120186611758]),
{"class": 0, "system:index": "22"},
),
ee.Feature(
ee.Geometry.Point([10.622735604942841, 44.17490267165223]),
{"class": 0, "system:index": "23"},
),
ee.Feature(
ee.Geometry.Point([10.514245614708466, 44.1906597047536]),
{"class": 0, "system:index": "24"},
),
]
),
)
developed = (
ee.FeatureCollection(
[
ee.Feature(
ee.Geometry.Point([9.19467878610836, 45.46435783697627]),
{"class": 1, "system:index": "0"},
),
ee.Feature(
ee.Geometry.Point([9.203090193579063, 45.464899612733554]),
{"class": 1, "system:index": "1"},
),
ee.Feature(
ee.Geometry.Point([9.204463484594688, 45.462852876998106]),
{"class": 1, "system:index": "2"},
),
ee.Feature(
ee.Geometry.Point([9.180087569067345, 45.458879589622896]),
{"class": 1, "system:index": "3"},
),
ee.Feature(
ee.Geometry.Point([9.18068838388668, 45.45641119434588]),
{"class": 1, "system:index": "4"},
),
ee.Feature(
ee.Geometry.Point([9.167642119238243, 45.455267267211944]),
{"class": 1, "system:index": "5"},
),
ee.Feature(
ee.Geometry.Point([9.165668013403282, 45.45340080996563]),
{"class": 1, "system:index": "6"},
),
ee.Feature(
ee.Geometry.Point([9.167556288549767, 45.45135365679475]),
{"class": 1, "system:index": "7"},
),
ee.Feature(
ee.Geometry.Point([9.167813780615196, 45.46297327527481]),
{"class": 1, "system:index": "8"},
),
ee.Feature(
ee.Geometry.Point([9.176911833593712, 45.46706666369604]),
{"class": 1, "system:index": "9"},
),
ee.Feature(
ee.Geometry.Point([9.223853948611414, 45.47248322583851]),
{"class": 1, "system:index": "10"},
),
ee.Feature(
ee.Geometry.Point([9.242822530764734, 45.47537221988128]),
{"class": 1, "system:index": "11"},
),
ee.Feature(
ee.Geometry.Point([9.211236837405359, 45.47729813363197]),
{"class": 1, "system:index": "12"},
),
ee.Feature(
ee.Geometry.Point([9.203597906130945, 45.47669629265562]),
{"class": 1, "system:index": "13"},
),
ee.Feature(
ee.Geometry.Point([9.154784524052499, 45.18401973624338]),
{"class": 1, "system:index": "14"},
),
ee.Feature(
ee.Geometry.Point([9.158217751591561, 45.185713605296485]),
{"class": 1, "system:index": "15"},
),
ee.Feature(
ee.Geometry.Point([9.156057754172835, 45.18958733293765]),
{"class": 1, "system:index": "16"},
),
ee.Feature(
ee.Geometry.Point([9.160520949973616, 45.19122054778279]),
{"class": 1, "system:index": "17"},
),
ee.Feature(
ee.Geometry.Point([9.158289352073226, 45.19599894427136]),
{"class": 1, "system:index": "18"},
),
ee.Feature(
ee.Geometry.Point([9.144728103293929, 45.194063439607106]),
{"class": 1, "system:index": "19"},
),
ee.Feature(
ee.Geometry.Point([9.145843902244124, 45.192914202565674]),
{"class": 1, "system:index": "20"},
),
ee.Feature(
ee.Geometry.Point([9.138548293723616, 45.19019223278913]),
{"class": 1, "system:index": "21"},
),
ee.Feature(
ee.Geometry.Point([9.144470611228499, 45.17924255060339]),
{"class": 1, "system:index": "22"},
),
ee.Feature(
ee.Geometry.Point([9.180176177634749, 45.18347748407685]),
{"class": 1, "system:index": "23"},
),
ee.Feature(
ee.Geometry.Point([9.177257934226546, 45.18626026884151]),
{"class": 1, "system:index": "24"},
),
]
),
)
water = (
ee.FeatureCollection(
[
ee.Feature(
ee.Geometry.Point([9.980378415331183, 43.953687197631126]),
{"class": 2, "system:index": "0"},
),
ee.Feature(
ee.Geometry.Point([9.735932614549933, 44.00704943549187]),
{"class": 2, "system:index": "1"},
),
ee.Feature(
ee.Geometry.Point([9.461274411424933, 44.12546046543348]),
{"class": 2, "system:index": "2"},
),
ee.Feature(
ee.Geometry.Point([9.282746579393683, 44.235763707116455]),
{"class": 2, "system:index": "3"},
),
ee.Feature(
ee.Geometry.Point([10.068269040331183, 43.94775509959863]),
{"class": 2, "system:index": "4"},
),
ee.Feature(
ee.Geometry.Point([10.202851559862433, 43.765549960385265]),
{"class": 2, "system:index": "5"},
),
ee.Feature(
ee.Geometry.Point([10.178132321581183, 43.72785127863614]),
{"class": 2, "system:index": "6"},
),
ee.Feature(
ee.Geometry.Point([10.213837887987433, 43.6821842672651]),
{"class": 2, "system:index": "7"},
),
ee.Feature(
ee.Geometry.Point([10.065522458299933, 43.72586647981521]),
{"class": 2, "system:index": "8"},
),
ee.Feature(
ee.Geometry.Point([9.941926266893683, 43.894339574154564]),
{"class": 2, "system:index": "9"},
),
ee.Feature(
ee.Geometry.Point([9.88486387395865, 45.077069068166345]),
{"class": 2, "system:index": "10"},
),
ee.Feature(
ee.Geometry.Point([9.848471662044588, 45.09864207259974]),
{"class": 2, "system:index": "11"},
),
ee.Feature(
ee.Geometry.Point([9.81448270940787, 45.0773115067334]),
{"class": 2, "system:index": "12"},
),
ee.Feature(
ee.Geometry.Point([9.828902265071932, 45.06179336476103]),
{"class": 2, "system:index": "13"},
),
ee.Feature(
ee.Geometry.Point([9.95455839300162, 45.11657297387295]),
{"class": 2, "system:index": "14"},
),
ee.Feature(
ee.Geometry.Point([10.002623578548494, 45.115603879971225]),
{"class": 2, "system:index": "15"},
),
ee.Feature(
ee.Geometry.Point([10.02322294378287, 45.10276183206325]),
{"class": 2, "system:index": "16"},
),
ee.Feature(
ee.Geometry.Point([10.043822309017244, 45.07149269722714]),
{"class": 2, "system:index": "17"},
),
ee.Feature(
ee.Geometry.Point([10.248517220628196, 45.02613418018433]),
{"class": 2, "system:index": "18"},
),
ee.Feature(
ee.Geometry.Point([10.263280099046165, 45.01836869065415]),
{"class": 2, "system:index": "19"},
),
ee.Feature(
ee.Geometry.Point([10.280102913987571, 45.006475743526394]),
{"class": 2, "system:index": "20"},
),
ee.Feature(
ee.Geometry.Point([10.265340035569603, 45.03802304474337]),
{"class": 2, "system:index": "21"},
),
ee.Feature(
ee.Geometry.Point([10.220021432053978, 45.023222245044195]),
{"class": 2, "system:index": "22"},
),
ee.Feature(
ee.Geometry.Point([10.182599251878196, 45.04069163407949]),
{"class": 2, "system:index": "23"},
),
ee.Feature(
ee.Geometry.Point([10.156163399827415, 45.03292811948761]),
{"class": 2, "system:index": "24"},
),
]
),
)
herbaceous = ee.FeatureCollection(
[
ee.Feature(
ee.Geometry.Point([9.647866129015439, 45.00732805319811]),
{"class": 3, "system:index": "0"},
),
ee.Feature(
ee.Geometry.Point([9.63911139879083, 45.00150212185294]),
{"class": 3, "system:index": "1"},
),
ee.Feature(
ee.Geometry.Point([9.63859641465997, 44.995432813342106]),
{"class": 3, "system:index": "2"},
),
ee.Feature(
ee.Geometry.Point([9.679108499620908, 44.99688950601573]),
{"class": 3, "system:index": "3"},
),
ee.Feature(
ee.Geometry.Point([9.680653452013486, 44.99106251314061]),
{"class": 3, "system:index": "4"},
),
ee.Feature(
ee.Geometry.Point([9.616967081163876, 44.98474926892666]),
{"class": 3, "system:index": "5"},
),
ee.Feature(
ee.Geometry.Point([9.625550150011533, 44.984627853569535]),
{"class": 3, "system:index": "6"},
),
ee.Feature(
ee.Geometry.Point([9.577313303087704, 44.99968139680247]),
{"class": 3, "system:index": "7"},
),
ee.Feature(
ee.Geometry.Point([9.682183386109417, 45.11939912570097]),
{"class": 3, "system:index": "8"},
),
ee.Feature(
ee.Geometry.Point([9.662270666382854, 45.142166966714186]),
{"class": 3, "system:index": "9"},
),
ee.Feature(
ee.Geometry.Point([9.655747534058635, 45.14483069451677]),
{"class": 3, "system:index": "10"},
),
ee.Feature(
ee.Geometry.Point([9.602875829957073, 45.13926093989158]),
{"class": 3, "system:index": "11"},
),
ee.Feature(
ee.Geometry.Point([9.57849991442973, 45.14265129011751]),
{"class": 3, "system:index": "12"},
),
ee.Feature(
ee.Geometry.Point([9.56476700427348, 45.14047180240153]),
{"class": 3, "system:index": "13"},
),
ee.Feature(
ee.Geometry.Point([9.55652725817973, 45.11794555067206]),
{"class": 3, "system:index": "14"},
),
ee.Feature(
ee.Geometry.Point([9.511208654664104, 45.12666644539884]),
{"class": 3, "system:index": "15"},
),
ee.Feature(
ee.Geometry.Point([9.601502538941448, 45.15887374651922]),
{"class": 3, "system:index": "16"},
),
ee.Feature(
ee.Geometry.Point([9.770417333863323, 45.15378959281897]),
{"class": 3, "system:index": "17"},
),
ee.Feature(
ee.Geometry.Point([9.706286368107783, 45.13319818507588]),
{"class": 3, "system:index": "18"},
),
ee.Feature(
ee.Geometry.Point([9.704827246403681, 45.13574123701328]),
{"class": 3, "system:index": "19"},
),
ee.Feature(
ee.Geometry.Point([9.699248251652705, 45.13955560228794]),
{"class": 3, "system:index": "20"},
),
ee.Feature(
ee.Geometry.Point([9.688776907658564, 45.13598342652142]),
{"class": 3, "system:index": "21"},
),
ee.Feature(
ee.Geometry.Point([9.688519415593134, 45.130655019734995]),
{"class": 3, "system:index": "22"},
),
ee.Feature(
ee.Geometry.Point([9.653500494694697, 45.13997940490131]),
{"class": 3, "system:index": "23"},
),
ee.Feature(
ee.Geometry.Point([9.67341321442126, 45.12260091467352]),
{"class": 3, "system:index": "24"},
),
]
)
# **** End of imports. If edited, may not auto-convert in the playground. ****#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Chapter: F2.1 Interpreting an Image: Classification
# Checkpoint: F21c
# Author: Andréa Puzzi Nicolau, Karen Dyson, David Saah, Nicholas Clinton
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create an Earth Engine Point object over Milan.
pt = ee.Geometry.Point([9.453, 45.424])
# Filter the Landsat 8 collection and select the least cloudy image.
landsat = (
ee.ImageCollection("LANDSAT/LC08/C02/T1_L2")
.filterBounds(pt)
.filterDate("2019-01-01", "2020-01-01")
.sort("CLOUD_COVER")
.first()
)
# Center the map on that image.
Map.centerObject(landsat, 8)
# Add Landsat image to the map.
visParams = {"bands": ["SR_B4", "SR_B3", "SR_B2"], "min": 7000, "max": 12000}
Map.addLayer(landsat, visParams, "Landsat 8 image")
# -----------------------------------------------------------------------
# CHECKPOINT
# -----------------------------------------------------------------------
# Combine training feature collections.
trainingFeatures = ee.FeatureCollection(
[forest, developed, water, herbaceous]
).flatten()
# Define prediction bands.
predictionBands = [
"SR_B1",
"SR_B2",
"SR_B3",
"SR_B4",
"SR_B5",
"SR_B6",
"SR_B7",
"ST_B10",
]
# Sample training points.
classifierTraining = landsat.select(predictionBands).sampleRegions(
{"collection": trainingFeatures, "properties": ["class"], "scale": 30}
)
######## CART Classifier #########/
# Train a CART Classifier.
classifier = ee.Classifier.smileCart().train(
{
"features": classifierTraining,
"classProperty": "class",
"inputProperties": predictionBands,
}
)
# Classify the Landsat image.
classified = landsat.select(predictionBands).classify(classifier)
# Define classification image visualization parameters.
classificationVis = {
"min": 0,
"max": 3,
"palette": ["589400", "ff0000", "1a11ff", "d0741e"],
}
# Add the classified image to the map.
Map.addLayer(classified, classificationVis, "CART classified")
#######/ Random Forest Classifier ##########/
# Train RF classifier.
RFclassifier = ee.Classifier.smileRandomForest(50).train(
{
"features": classifierTraining,
"classProperty": "class",
"inputProperties": predictionBands,
}
)
# Classify Landsat image.
RFclassified = landsat.select(predictionBands).classify(RFclassifier)
# Add classified image to the map.
Map.addLayer(RFclassified, classificationVis, "RF classified")
# -----------------------------------------------------------------------
# CHECKPOINT
# -----------------------------------------------------------------------
######## Unsupervised classification ########
# Make the training dataset.
training = landsat.sample(
{"region": landsat.geometry(), "scale": 30, "numPixels": 1000, "tileScale": 8}
)
# Instantiate the clusterer and train it.
clusterer = ee.Clusterer.wekaKMeans(4).train(training)
# Cluster the input using the trained clusterer.
Kclassified = landsat.cluster(clusterer)
# Display the clusters with random colors.
Map.addLayer(Kclassified.randomVisualizer(), {}, "K-means classified - random colors")
# -----------------------------------------------------------------------
# CHECKPOINT
# -----------------------------------------------------------------------
Display the interactive map¶
In [ ]:
Copied!
Map
Map