Zonal stats by group
Uncomment the following line to install geemap if needed.
In [1]:
Copied!
# !pip install geemap
# !pip install geemap
In [2]:
Copied!
import os
import ee
import geemap
import os
import ee
import geemap
Analyzing U.S. Land Cover¶
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¶
NLCD: USGS National Land Cover Database
https://developers.google.com/earth-engine/datasets/catalog/USGS_NLCD_RELEASES_2016_REL
In [4]:
Copied!
dataset = ee.Image('USGS/NLCD/NLCD2016')
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/NLCD2016')
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.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 [7]:
Copied!
nlcd_stats = os.path.join(out_dir, 'nlcd_stats_sum.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,
states,
nlcd_stats,
statistics_type='SUM',
denominator=1000000,
decimal_places=2,
)
nlcd_stats = os.path.join(out_dir, 'nlcd_stats_sum.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,
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/41a4a69f7137f14567112a2e87fe682a-137b01867d41973c312909cad66dbf6a:getFeatures Please wait ... Data downloaded to /home/runner/Downloads/nlcd_stats_sum.csv
In [8]:
Copied!
nlcd_stats = os.path.join(out_dir, 'nlcd_stats_pct.csv')
geemap.zonal_statistics_by_group(
landcover, states, nlcd_stats, statistics_type='PERCENTAGE'
)
nlcd_stats = os.path.join(out_dir, 'nlcd_stats_pct.csv')
geemap.zonal_statistics_by_group(
landcover, states, nlcd_stats, statistics_type='PERCENTAGE'
)
Computing ... Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/tables/a115ff8c27bf708ab3437ca5e186df36-8816ba41de334d05b4411051c9d3a26f:getFeatures Please wait ... Data downloaded to /home/runner/Downloads/nlcd_stats_pct.csv
Analyzing Global Land Cover¶
Add MODIS global land cover data¶
MCD12Q1.006 MODIS Land Cover Type Yearly Global 500m
https://developers.google.com/earth-engine/datasets/catalog/MODIS_006_MCD12Q1
In [9]:
Copied!
Map = geemap.Map()
landcover = ee.Image('MODIS/006/MCD12Q1/2019_01_01').select('LC_Type1')
igbpLandCoverVis = {
'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, igbpLandCoverVis, 'MODIS Land Cover')
countries = ee.FeatureCollection('users/giswqs/public/countries')
Map.addLayer(countries, {}, "Countries")
Map
Map = geemap.Map()
landcover = ee.Image('MODIS/006/MCD12Q1/2019_01_01').select('LC_Type1')
igbpLandCoverVis = {
'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, igbpLandCoverVis, 'MODIS Land Cover')
countries = ee.FeatureCollection('users/giswqs/public/countries')
Map.addLayer(countries, {}, "Countries")
Map
Out[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [10]:
Copied!
Map.add_legend(builtin_legend='MODIS/051/MCD12Q1')
Map.add_legend(builtin_legend='MODIS/051/MCD12Q1')
In [11]:
Copied!
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
global_stats = os.path.join(out_dir, 'global_stats_sum.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='SUM',
denominator=1000000,
decimal_places=2,
)
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
global_stats = os.path.join(out_dir, 'global_stats_sum.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='SUM',
denominator=1000000,
decimal_places=2,
)
Computing ... Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/tables/c061c91667ae3f30db8b554aec90d228-de86f23789c1224a978e4fe02be20816:getFeatures Please wait ... Data downloaded to /home/runner/Downloads/global_stats_sum.csv
In [12]:
Copied!
global_stats = os.path.join(out_dir, 'global_stats_pct.csv')
geemap.zonal_statistics_by_group(
landcover, countries, global_stats, statistics_type='PERCENTAGE'
)
global_stats = os.path.join(out_dir, 'global_stats_pct.csv')
geemap.zonal_statistics_by_group(
landcover, countries, global_stats, statistics_type='PERCENTAGE'
)
Computing ... Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/tables/1e32245b9cecc5b4a3c7bcede3df7ca5-882afc411d0f3dc17ba0d28ec3da3b6e:getFeatures Please wait ... Data downloaded to /home/runner/Downloads/global_stats_pct.csv
Last update:
2022-03-25