Zonal statistics
Uncomment the following line to install geemap if needed.
In [1]:
Copied!
# !pip install geemap
# !pip install geemap
In [2]:
Copied!
import ee
import geemap
import os
import ee
import geemap
import os
Create an interactive map¶
In [3]:
Copied!
Map = geemap.Map()
Map
Map = geemap.Map()
Map
Out[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add Earth Engine data¶
In [4]:
Copied!
# Add Earth Engine dataset
dem = ee.Image('USGS/SRTMGL1_003')
# Set visualization parameters.
dem_vis = {
'min': 0,
'max': 4000,
'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],
}
# Add Earth Engine DEM to map
Map.addLayer(dem, dem_vis, 'SRTM DEM')
# Add Landsat data to map
landsat = ee.Image('LE7_TOA_5YEAR/1999_2003')
landsat_vis = {'bands': ['B4', 'B3', 'B2'], 'gamma': 1.4}
Map.addLayer(landsat, landsat_vis, "Landsat", False)
states = ee.FeatureCollection("TIGER/2018/States")
Map.addLayer(states, {}, 'US States')
# Add Earth Engine dataset
dem = ee.Image('USGS/SRTMGL1_003')
# Set visualization parameters.
dem_vis = {
'min': 0,
'max': 4000,
'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],
}
# Add Earth Engine DEM to map
Map.addLayer(dem, dem_vis, 'SRTM DEM')
# Add Landsat data to map
landsat = ee.Image('LE7_TOA_5YEAR/1999_2003')
landsat_vis = {'bands': ['B4', 'B3', 'B2'], 'gamma': 1.4}
Map.addLayer(landsat, landsat_vis, "Landsat", False)
states = ee.FeatureCollection("TIGER/2018/States")
Map.addLayer(states, {}, 'US States')
Compute zonal statistics for one image¶
In [5]:
Copied!
out_dir = os.path.expanduser('~/Downloads')
if not os.path.exists(out_dir):
os.makedirs(out_dir)
out_dir = os.path.expanduser('~/Downloads')
if not os.path.exists(out_dir):
os.makedirs(out_dir)
In [6]:
Copied!
out_dem_stats = os.path.join(out_dir, 'dem_stats.csv')
# Allowed output formats: csv, shp, json, kml, kmz
# Allowed statistics type: MEAN, MAXIMUM, MINIMUM, MEDIAN, STD, MIN_MAX, VARIANCE, SUM
geemap.zonal_statistics(dem, states, out_dem_stats, statistics_type='MEAN', scale=1000)
out_dem_stats = os.path.join(out_dir, 'dem_stats.csv')
# Allowed output formats: csv, shp, json, kml, kmz
# Allowed statistics type: MEAN, MAXIMUM, MINIMUM, MEDIAN, STD, MIN_MAX, VARIANCE, SUM
geemap.zonal_statistics(dem, states, out_dem_stats, statistics_type='MEAN', scale=1000)
Computing statistics ... Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/tables/04e8c7b138ea4c98a9a99871c8737598-ec04c9f55156370ce0aad7df702cbd2b:getFeatures Please wait ... Data downloaded to /home/runner/Downloads/dem_stats.csv
In [7]:
Copied!
out_landsat_stats = os.path.join(out_dir, 'landsat_stats.csv')
geemap.zonal_statistics(
landsat, states, out_landsat_stats, statistics_type='SUM', scale=1000
)
out_landsat_stats = os.path.join(out_dir, 'landsat_stats.csv')
geemap.zonal_statistics(
landsat, states, out_landsat_stats, statistics_type='SUM', scale=1000
)
Computing statistics ... Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/tables/ef7da058fa8cecffdece8d714da68ec8-ad6297d0a66e53701510771001283d7b:getFeatures Please wait ... Data downloaded to /home/runner/Downloads/landsat_stats.csv
Compute zonal statistics for time-series images¶
In [8]:
Copied!
Map = geemap.Map()
collection = (
ee.ImageCollection('MODIS/MCD43A4_006_NDVI')
.filter(ee.Filter.date('2018-04-01', '2018-05-01'))
.select("NDVI")
)
vis_params = {
'min': 0.0,
'max': 1.0,
'palette': [
'FFFFFF',
'CE7E45',
'DF923D',
'F1B555',
'FCD163',
'99B718',
'74A901',
'66A000',
'529400',
'3E8601',
'207401',
'056201',
'004C00',
'023B01',
'012E01',
'011D01',
'011301',
],
}
first_image = collection.first()
Map.addLayer(first_image, vis_params, "First image")
Map.setCenter(-7.03125, 31.0529339857, 2)
Map
Map = geemap.Map()
collection = (
ee.ImageCollection('MODIS/MCD43A4_006_NDVI')
.filter(ee.Filter.date('2018-04-01', '2018-05-01'))
.select("NDVI")
)
vis_params = {
'min': 0.0,
'max': 1.0,
'palette': [
'FFFFFF',
'CE7E45',
'DF923D',
'F1B555',
'FCD163',
'99B718',
'74A901',
'66A000',
'529400',
'3E8601',
'207401',
'056201',
'004C00',
'023B01',
'012E01',
'011D01',
'011301',
],
}
first_image = collection.first()
Map.addLayer(first_image, vis_params, "First image")
Map.setCenter(-7.03125, 31.0529339857, 2)
Map
Out[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [9]:
Copied!
modis = collection.toBands()
Map.addLayer(modis, {}, "MODIS Time series", False)
modis = collection.toBands()
Map.addLayer(modis, {}, "MODIS Time series", False)
In [10]:
Copied!
out_landsat_stats = os.path.join(out_dir, 'ndvi.csv')
geemap.zonal_statistics(
modis, states, out_landsat_stats, statistics_type='MEAN', scale=1000
)
out_landsat_stats = os.path.join(out_dir, 'ndvi.csv')
geemap.zonal_statistics(
modis, states, out_landsat_stats, statistics_type='MEAN', scale=1000
)
Computing statistics ... Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/tables/0dd2ec712cd35a166923cfa585323a23-7b4167ae807df8c38dbc18347a42fb60:getFeatures Please wait ... Data downloaded to /home/runner/Downloads/ndvi.csv
Last update:
2022-03-25