108 image zonal stats
Uncomment the following line to install geemap if needed.
In [ ]:
Copied!
# !pip install geemap
# !pip install geemap
Import libraries.
In [ ]:
Copied!
import ee
import geemap
import geemap.colormaps as cm
import ee
import geemap
import geemap.colormaps as cm
Create an interactive map.
In [ ]:
Copied!
Map = geemap.Map(center=[40, -100], zoom=4)
Map
Map = geemap.Map(center=[40, -100], zoom=4)
Map
Add a DEM to the map.
In [ ]:
Copied!
dem = ee.Image("USGS/3DEP/10m")
vis = {"min": 0, "max": 4000, "palette": cm.palettes.dem}
dem = ee.Image("USGS/3DEP/10m")
vis = {"min": 0, "max": 4000, "palette": cm.palettes.dem}
In [ ]:
Copied!
Map.addLayer(dem, vis, "DEM")
Map.addLayer(dem, vis, "DEM")
Add NLCD land cover data and legend to the map.
In [ ]:
Copied!
landcover = ee.Image("USGS/NLCD_RELEASES/2019_REL/NLCD/2019").select("landcover")
landcover = ee.Image("USGS/NLCD_RELEASES/2019_REL/NLCD/2019").select("landcover")
In [ ]:
Copied!
Map.addLayer(landcover, {}, "NLCD 2019")
Map.add_legend(builtin_legend="NLCD")
Map.addLayer(landcover, {}, "NLCD 2019")
Map.add_legend(builtin_legend="NLCD")
Calculate image zonal statistics by zone. In this case, we are going to calculate the mean elevation by each land cover type. The result can be returned as a Panda DataFrame or saved as a CSV.
In [ ]:
Copied!
stats = geemap.image_stats_by_zone(dem, landcover, reducer="MEAN")
stats
stats = geemap.image_stats_by_zone(dem, landcover, reducer="MEAN")
stats
Save the resulting Pandas DataFrame as a CSV.
In [ ]:
Copied!
stats.to_csv("mean.csv", index=False)
stats.to_csv("mean.csv", index=False)
Calculate the standard deviation of elevation by each land cover type and save the result as a CSV.
In [ ]:
Copied!
geemap.image_stats_by_zone(dem, landcover, out_csv="std.csv", reducer="STD")
geemap.image_stats_by_zone(dem, landcover, out_csv="std.csv", reducer="STD")