This repository provides two complementary workflows for deriving landcover area summaries and full transition matrices from MODIS IGBP or ESA CCI landcover datasets. Both workflows produce per‑year class area CSVs and comprehensive inter‑annual transition tables, then optionally split those tables by ISO3 code.
- Python 3.8+
- geopandas
- rasterstats
- pandas
- numpy
- rasterio
- xarray (only for NetCDF inputs)
- tqdm
Install via conda or pip:
conda install geopandas rasterio rasterstats xarray pandas numpy tqdm
# or
pip install geopandas rasterio rasterstats xarray pandas numpy tqdm
- Compute per‑year class areas: For each annual raster (GeoTIFF/NetCDF), run a zonal histogram by your admin boundaries to count pixels per IGBP/LCCS class, convert counts to hectares, and write both per‑year and combined CSV outputs.
- Build full transition matrices: For every pair of consecutive years, encode each pixel’s class change as
100*source + target
, perform zonal histograms on that code, and output a complete source→target matrix (including zeros) per year.
-
Per‑pixel transition code:
trans = arr_prev.astype(np.int32)*100 + arr_curr.astype(np.int32)
Assigns each pixel a unique integer for its
(source, target)
classes. -
Zonal counting:
stats = zonal_stats( zones, trans, affine=transform, categorical=True, all_touched=True, nodata=-999 )
Buckets pixels by transition code per zone, returning
{code: count}
dicts. -
Area conversion:
hectares = count * pixel_area_ha
Converts pixel counts into hectares (25 ha for MODIS; adjust for other resolutions).
-
Memory management: Frequent
gc.collect()
calls free Python memory between years to avoid OOM.
Splits the master transition table by ISO3 code and aggregates multiple polygons:
sub = (
df[df.iso_3 == iso]
.groupby(['year','source','target'], as_index=False)['hectares']
.sum()
.rename(columns={'hectares':'value'})
)
sub.to_csv(f'transitions_{iso}.csv', index=False)
This ensures each country’s output contains one record per (year, source, target)
with summed hectare values, even if that country has multiple boundary parts.