13 zonal statistics by group
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
Analyzing National Land Cover Database (NLCD)¶
In [3]:
Copied!
Map = geemap.Map()
Map
Map = geemap.Map()
Map
Out[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add NLCD data¶
In [4]:
Copied!
dataset = ee.Image('USGS/NLCD_RELEASES/2019_REL/NLCD/2019')
landcover = ee.Image(dataset.select('landcover'))
Map.addLayer(landcover, {}, 'NLCD 2016')
states = ee.FeatureCollection("TIGER/2018/States")
Map.addLayer(states, {}, 'US States')
dataset = ee.Image('USGS/NLCD_RELEASES/2019_REL/NLCD/2019')
landcover = ee.Image(dataset.select('landcover'))
Map.addLayer(landcover, {}, 'NLCD 2016')
states = ee.FeatureCollection("TIGER/2018/States")
Map.addLayer(states, {}, 'US States')
In [5]:
Copied!
Map.add_legend(builtin_legend='NLCD')
Map.add_legend(builtin_legend='NLCD')
Calculate land cover compostion of each US state¶
In [6]:
Copied!
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
nlcd_stats = os.path.join(out_dir, 'nlcd_stats.csv')
if not os.path.exists(out_dir):
os.makedirs(out_dir)
# statistics_type can be either 'SUM' or 'PERCENTAGE'
# denominator can be used to convert square meters to other areal units, such as square kilometers
geemap.zonal_statistics_by_group(
landcover,
states,
nlcd_stats,
statistics_type='SUM',
denominator=1000000,
decimal_places=2,
)
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
nlcd_stats = os.path.join(out_dir, 'nlcd_stats.csv')
if not os.path.exists(out_dir):
os.makedirs(out_dir)
# statistics_type can be either 'SUM' or 'PERCENTAGE'
# denominator can be used to convert square meters to other areal units, such as square kilometers
geemap.zonal_statistics_by_group(
landcover,
states,
nlcd_stats,
statistics_type='SUM',
denominator=1000000,
decimal_places=2,
)
Computing ... Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/tables/133d101cfb013c1f804933a0d6c5b076-89868a9f243fe90531fc7bc1ce15a962:getFeatures Please wait ... Data downloaded to /home/runner/Downloads/nlcd_stats.csv
In [7]:
Copied!
geemap.create_download_link(nlcd_stats)
geemap.create_download_link(nlcd_stats)
Analyzing Global Land Cover¶
In [8]:
Copied!
Map = geemap.Map()
Map
Map = geemap.Map()
Map
Out[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add MODIS global land cover data¶
MODIS MCD12Q1.006 Land Cover Type Yearly Global 500m
https://developers.google.com/earth-engine/datasets/catalog/MODIS_006_MCD12Q1
In [9]:
Copied!
landcover = ee.Image('MODIS/006/MCD12Q1/2013_01_01').select('LC_Type1')
vis = {
'min': 1.0,
'max': 17.0,
'palette': [
'05450a',
'086a10',
'54a708',
'78d203',
'009900',
'c6b044',
'dcd159',
'dade48',
'fbff13',
'b6ff05',
'27ff87',
'c24f44',
'a5a5a5',
'ff6d4c',
'69fff8',
'f9ffa4',
'1c0dff',
],
}
Map.setCenter(6.746, 46.529, 2)
Map.addLayer(landcover, vis, 'MODIS Land Cover')
landcover = ee.Image('MODIS/006/MCD12Q1/2013_01_01').select('LC_Type1')
vis = {
'min': 1.0,
'max': 17.0,
'palette': [
'05450a',
'086a10',
'54a708',
'78d203',
'009900',
'c6b044',
'dcd159',
'dade48',
'fbff13',
'b6ff05',
'27ff87',
'c24f44',
'a5a5a5',
'ff6d4c',
'69fff8',
'f9ffa4',
'1c0dff',
],
}
Map.setCenter(6.746, 46.529, 2)
Map.addLayer(landcover, vis, 'MODIS Land Cover')
In [10]:
Copied!
Map.add_legend(builtin_legend='MODIS/006/MCD12Q1')
Map.add_legend(builtin_legend='MODIS/006/MCD12Q1')
In [11]:
Copied!
countries_shp = '../data/countries.shp'
countries = geemap.shp_to_ee(countries_shp)
Map.addLayer(countries, {}, 'Countries')
countries_shp = '../data/countries.shp'
countries = geemap.shp_to_ee(countries_shp)
Map.addLayer(countries, {}, 'Countries')
In [12]:
Copied!
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
global_stats = os.path.join(out_dir, 'global_stats.csv')
# statistics_type can be either 'SUM' or 'PERCENTAGE'
# denominator can be used to convert square meters to other areal units, such as square kilimeters
geemap.zonal_statistics_by_group(
landcover,
countries,
global_stats,
statistics_type='PERCENTAGE',
denominator=1000000,
decimal_places=2,
)
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
global_stats = os.path.join(out_dir, 'global_stats.csv')
# statistics_type can be either 'SUM' or 'PERCENTAGE'
# denominator can be used to convert square meters to other areal units, such as square kilimeters
geemap.zonal_statistics_by_group(
landcover,
countries,
global_stats,
statistics_type='PERCENTAGE',
denominator=1000000,
decimal_places=2,
)
Computing ... Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/tables/e9d7fd3bbb8e0fe7705dc60777027980-c7466b37b504e737f7da41d7eebaf0d5:getFeatures Please wait ... Data downloaded to /home/runner/Downloads/global_stats.csv
In [13]:
Copied!
geemap.create_download_link(global_stats)
geemap.create_download_link(global_stats)
Last update:
2022-03-14