Notebook Example#

from gee_zonal import Catalog, ZonalStats
import ee
import geopandas as gpd

Input Features#

Input target features can be referenced directly as a GEE asset, or can be supplied as a geopandas.GeoDataFrame, or a path to a shapefile/GeoJSON (will be automatically converted to ee.FeatureCollection).

AOIs = ee.FeatureCollection('users/afche18/Ethiopia_AOI')
AOIs.getInfo()
{'type': 'FeatureCollection',
 'columns': {'Area': 'Float', 'Id': 'Integer', 'system:index': 'String'},
 'version': 1570633589922603,
 'id': 'users/afche18/Ethiopia_AOI',
 'properties': {'system:asset_size': 11288},
 'features': [{'type': 'Feature',
   'geometry': {'type': 'Polygon',
    'coordinates': [[[42.06158510035763, 9.397823252053993],
      [42.06119314725495, 9.20601121183654],
      [42.18901118834056, 9.20584119448768],
      [42.316813505733144, 9.205625705724506],
      [42.3125304107434, 9.301530485292622],
      [42.30825425836041, 9.397435608019856],
      [42.184926941544006, 9.39765098188209],
      [42.06158510035763, 9.397823252053993]]]},
   'id': '00000000000000000000',
   'properties': {'Area': 586.656, 'Id': 0}},
  {'type': 'Feature',
   'geometry': {'type': 'Polygon',
    'coordinates': [[[35.164007676333824, 7.315648092803201],
      [35.164482803968816, 7.270435726027228],
      [35.25634053363444, 7.210965732212082],
      [35.348179623989004, 7.151464739986416],
      [35.50236858461292, 7.092454680579472],
      [35.51569123843248, 7.11196715143379],
      [35.461916853024185, 7.22665425180959],
      [35.41052043644675, 7.307558148286968],
      [35.23872404563077, 7.330402310785946],
      [35.164007676333824, 7.315648092803201]]]},
   'id': '00000000000000000001',
   'properties': {'Area': 523.326, 'Id': 1}}]}
gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
gdf = gdf.head().copy()
gdf
pop_est continent name iso_a3 gdp_md_est geometry
0 889953.0 Oceania Fiji FJI 5496 MULTIPOLYGON (((180.00000 -16.06713, 180.00000...
1 58005463.0 Africa Tanzania TZA 63177 POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...
2 603253.0 Africa W. Sahara ESH 907 POLYGON ((-8.66559 27.65643, -8.66512 27.58948...
3 37589262.0 North America Canada CAN 1736425 MULTIPOLYGON (((-122.84000 49.00000, -122.9742...
4 328239523.0 North America United States of America USA 21433226 MULTIPOLYGON (((-122.84000 49.00000, -120.0000...

Initialize Zonal Stats#

Option A: Retrieve results directly#

Output: DataFrame with statistics
Recommended for small areas / low n

zs = ZonalStats(
    collection_id='LANDSAT/LC08/C01/T1_8DAY_NDVI', 
    target_features=AOIs, 
    statistic_type="all",
    frequency="annual",
    temporal_stat="mean"
)
res = zs.runZonalStats()
res
2013_NDVI_mean_max 2013_NDVI_mean_mean 2013_NDVI_mean_min 2013_NDVI_mean_stdDev 2014_NDVI_mean_max 2014_NDVI_mean_mean 2014_NDVI_mean_min 2014_NDVI_mean_stdDev 2015_NDVI_mean_max 2015_NDVI_mean_mean ... 2021_NDVI_mean_max 2021_NDVI_mean_mean 2021_NDVI_mean_min 2021_NDVI_mean_stdDev 2022_NDVI_mean_max 2022_NDVI_mean_mean 2022_NDVI_mean_min 2022_NDVI_mean_stdDev Area Id
00000000000000000000 0.478839 0.312925 0.125459 0.041014 0.536693 0.328255 0.133519 0.039759 0.547818 0.325812 ... 0.435856 0.292027 0.107515 0.037247 NaN NaN NaN NaN 586.656 0.0
00000000000000000001 0.669967 0.561163 0.058939 0.056626 0.549813 0.449039 0.028017 0.044816 0.600975 0.491076 ... 0.630799 0.478778 0.107342 0.057189 NaN NaN NaN NaN 523.326 1.0

2 rows × 42 columns

Option B: Submit a task#

Output: csv table on Google Drive
Recommended for big areas / high n

zs = ZonalStats(
    collection_id='LANDSAT/LC08/C01/T1_8DAY_NDVI', 
    target_features=AOIs, 
    statistic_type="mean",
    frequency="annual",
    temporal_stat="mean",
    scale=1000,
    output_dir="pretty_folder",
    output_name="pretty_file"
)
zs.runZonalStats()
zs.reportRunTime()
Completed
Runtime: 0 minutes and 7 seconds