Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/docs/notebooks/108_image_zonal_stats.ipynb
2313 views
Kernel: Python 3

Open in Colab

Uncomment the following line to install geemap if needed.

# !pip install geemap

Import libraries.

import ee import geemap import geemap.colormaps as cm

Create an interactive map.

Map = geemap.Map(center=[40, -100], zoom=4) Map

Add a DEM to the map.

dem = ee.Image("USGS/3DEP/10m") vis = {"min": 0, "max": 4000, "palette": cm.palettes.dem}
Map.addLayer(dem, vis, "DEM")

Add NLCD land cover data and legend to the map.

landcover = ee.Image("USGS/NLCD_RELEASES/2019_REL/NLCD/2019").select("landcover")
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.

stats = geemap.image_stats_by_zone(dem, landcover, reducer="MEAN") stats

Save the resulting Pandas DataFrame as a CSV.

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.

geemap.image_stats_by_zone(dem, landcover, out_csv="std.csv", reducer="STD")