Criterion B Details

# Default ecosystem code for template development.
# This line is replaced by build_ecosystem_pages.py for each ecosystem.
ecosystem_code = 'T1.2.20'

Import Python modules.

import os
import yaml
from pathlib import Path
from lonboard import Map
from rle_python_gee.ecosystems import Ecosystems
from rle_python_gee.eoo import make_eoo
from rle_python_gee.aoo import make_aoo_grid

Load the country config file.

project_root = os.environ.get('PIXI_PROJECT_ROOT', str(Path('..').resolve()))
config_path = Path(project_root) / 'config' / 'country_config.yaml'
with open(config_path) as f:
    config = yaml.safe_load(f)

Load & Filter Ecosystem Data

Load data for all the ecosystems.

source = config['ecosystem_source']
ecosystems = Ecosystems.from_file(
    source['data'],
    ecosystem_column=source['ecosystem_code_column'],
    ecosystem_name_column=source['ecosystem_name_column'],
    functional_group_column=source['functional_group_column']
)

Filter by the T1.2.20 and check the number of features.

ecosystem = ecosystems.filter(ecosystem_code)
has_data = ecosystem.size() > 0
print(f'{ecosystem.size() = }')
if not has_data:
    from IPython.display import Markdown, display
    display(Markdown(
        f'**No spatial data found for {ecosystem_code}.** '
        f'Criterion B calculations are skipped.'
    ))
ecosystem.size() = 74

Extent of Occurrence (EOO) (subcriterion B1)

Extent of occurrence (EOO). The EOO of an ecosystem is the area (km2) of a minimum convex polygon – the smallest polygon in which no internal angle exceeds 180° that encompasses all known current spatial occurrences of the ecosystem type.

The minimum convex polygon (also known as a convex hull) must not exclude any areas, discontinuities or disjunctions, regardless of whether the ecosystem can occur in those areas or not. Regions such as oceans (for terrestrial ecosystems), land (for coastal or marine ecosystems), or areas outside the study area (such as in a different country) must remain included within the minimum convex polygon to ensure that this standardised method is comparable across ecosystem types. In addition, these features contribute to spreading risks across the distribution of the ecosystem by making different parts of its distribution more spatially independent.

Calculate EOO

Start by calculating the convex hull of the ecosystem’s distribution.

import geopandas as gpd

if has_data:
    ecosystem_geometry = ecosystem.geometry.union_all()
    gdf_ecosystem_polygons = gpd.GeoDataFrame(geometry=[ecosystem_geometry], crs=ecosystem.geometry.crs)
    hull = ecosystem_geometry.convex_hull
    gdf_hull = gpd.GeoDataFrame(geometry=[hull], crs=ecosystem.geometry.crs)

Display the ecosystem’s distribution and the convex hull.

from lonboard import Map, PolygonLayer
from rle_python_gee.viz import smart_map

if has_data:
    eoo_hull = make_eoo(ecosystem).compute()
    display(smart_map([eoo_hull, ecosystem]))
/home/runner/work/rle-tyler-20/rle-tyler-20/.pixi/envs/default/lib/python3.11/site-packages/lonboard/_geoarrow/ops/reproject.py:116: UserWarning: Input being reprojected to EPSG:4326 CRS.
Lonboard is only able to render data in EPSG:4326 projection.
  warnings.warn(
/home/runner/work/rle-tyler-20/rle-tyler-20/.pixi/envs/default/lib/python3.11/site-packages/lonboard/_geoarrow/ops/reproject.py:116: UserWarning: Input being reprojected to EPSG:4326 CRS.
Lonboard is only able to render data in EPSG:4326 projection.
  warnings.warn(
if has_data:
    hull_ea = gdf_hull.to_crs("ESRI:54034")
    eoo = hull_ea.geometry.iloc[0].area / 1e6
    print(f'EOO is {eoo:.1f} km2')
EOO is 2207.7 km2

Then calculate the area of the convex hull polygon.

Direct calculation of EOO

EOO can also be calculated directly using …

if has_data:
    ecosystem.eoo

Verify that the area returned by calling make_eoo(ecosystem).compute().area_km2 is the same as the area of the convex hull polygon.

if has_data:
    assert ecosystem.eoo == eoo

Area of Occupancy (AOO) (subcriterion B2)

The protocol for this adjustment includes the following steps:

  1. Intersect AOO grid with the ecosystem’s distribution map.
  2. Calculate extent of the ecosystem type in each grid cell (area) and sum these areas to obtain the total ecosystem area (total area).
  3. Arrange grid cells in ascending order based on their area (smaller first). Calculate accumulated sum of area per cell (cumulative area).
  4. Calculate cumulative proportion by dividing cumulative area by total area (cumulative proportion takes values between 0 and 1)
  5. Calculate AOO by counting the number of cells with a cumulative proportion greater than 0.01 (i.e. exclude cells that in combination account for up to 1% of the total mapped extent of the ecosystem type).

AOO Calculation Details

Intersect AOO grid and ecosystem map

  1. Intersect AOO grid with the ecosystem’s distribution map
from pathlib import Path
from rle_python_gee.aoo import make_aoo_grid_cached

if has_data:
    cache_path = Path(project_root) / '.cache' / 'aoo_grid.parquet'
    aoo_grid = make_aoo_grid_cached(ecosystems, cache_path=cache_path)
    aoo_grid_filtered = aoo_grid.filter_by_ecosystem(ecosystem_code)

Visualize variations in the AOO grid.

from matplotlib.colors import LinearSegmentedColormap
from lonboard.colormap import apply_continuous_cmap
from rle_python_gee.aoo import slugify_ecosystem_name

ecosystem_column = slugify_ecosystem_name(ecosystem_code)
if has_data:
    cmap = LinearSegmentedColormap.from_list("white_red", ["white", "red"])
    values = aoo_grid_filtered.grid_cells[ecosystem_column].values
    normalized = (values - values.min()) / (values.max() - values.min())
    colors = apply_continuous_cmap(normalized, cmap)
    display(smart_map([(aoo_grid_filtered, {"get_fill_color": colors}), ecosystem]))
/home/runner/work/rle-tyler-20/rle-tyler-20/.pixi/envs/default/lib/python3.11/site-packages/lonboard/_geoarrow/ops/reproject.py:116: UserWarning: Input being reprojected to EPSG:4326 CRS.
Lonboard is only able to render data in EPSG:4326 projection.
  warnings.warn(

Calculate grid cell area and total area

  1. Calculate extent of the ecosystem type in each grid cell (area) and sum these areas to obtain the total ecosystem area (total area).
if has_data:
    keep = ['geometry', 'grid_col', 'grid_row', ecosystem_column]
    gdf = aoo_grid_filtered.grid_cells[keep]
    display(gdf)
geometry grid_col grid_row T1_2_20
0 POLYGON ((-77.43478 1.35668, -77.43478 1.44714... -863 15 0.011776
1 POLYGON ((-77.43478 1.44714, -77.43478 1.53761... -863 16 0.047508
2 POLYGON ((-77.43478 1.53761, -77.43478 1.62808... -863 17 0.019289
3 POLYGON ((-77.43478 1.62808, -77.43478 1.71855... -863 18 0.014410
4 POLYGON ((-77.34495 1.44714, -77.34495 1.53761... -862 16 0.029148
5 POLYGON ((-77.34495 1.53761, -77.34495 1.62808... -862 17 0.001192
6 POLYGON ((-77.34495 1.62808, -77.34495 1.71855... -862 18 0.004185
7 POLYGON ((-77.25511 1.62808, -77.25511 1.71855... -861 18 0.008390
8 POLYGON ((-77.25511 1.71855, -77.25511 1.80903... -861 19 0.104560
9 POLYGON ((-77.25511 1.80903, -77.25511 1.89951... -861 20 0.077389
10 POLYGON ((-77.25511 1.89951, -77.25511 1.99, -... -861 21 0.000240
11 POLYGON ((-77.16528 1.62808, -77.16528 1.71855... -860 18 0.086899
12 POLYGON ((-77.16528 1.80903, -77.16528 1.89951... -860 20 0.013577
13 POLYGON ((-77.16528 1.89951, -77.16528 1.99, -... -860 21 0.302064
14 POLYGON ((-77.16528 1.99, -77.16528 2.08049, -... -860 22 0.039017
15 POLYGON ((-77.07545 1.62808, -77.07545 1.71855... -859 18 0.029340
16 POLYGON ((-77.07545 1.89951, -77.07545 1.99, -... -859 21 0.261937
17 POLYGON ((-77.07545 1.99, -77.07545 2.08049, -... -859 22 0.299272
18 POLYGON ((-77.07545 2.08049, -77.07545 2.17099... -859 23 0.074083
19 POLYGON ((-76.98562 1.99, -76.98562 2.08049, -... -858 22 0.076164
20 POLYGON ((-76.98562 2.08049, -76.98562 2.17099... -858 23 0.207288
21 POLYGON ((-76.89579 1.99, -76.89579 2.08049, -... -857 22 0.008949
22 POLYGON ((-76.89579 2.08049, -76.89579 2.17099... -857 23 0.061615
23 POLYGON ((-76.80596 2.08049, -76.80596 2.17099... -856 23 0.006219

The column T1_2_20 contains the (fractional) area of the ecosystem in each grid cell.

Sum up the areas of each grid cell to get the total area.

if has_data:
    total_area = gdf[ecosystem_column].sum()
    display(total_area)
np.float64(1.7845112000669272)

Calculate cumulative area

  1. Arrange grid cells in ascending order based on their area (smaller first). Calculate accumulated sum of area per cell (cumulative area).
if has_data:
    gdf = gdf.sort_values(by=ecosystem_column)
    gdf["cumulative_area"] = gdf[ecosystem_column].cumsum()
    display(gdf)
geometry grid_col grid_row T1_2_20 cumulative_area
10 POLYGON ((-77.25511 1.89951, -77.25511 1.99, -... -861 21 0.000240 0.000240
5 POLYGON ((-77.34495 1.53761, -77.34495 1.62808... -862 17 0.001192 0.001432
6 POLYGON ((-77.34495 1.62808, -77.34495 1.71855... -862 18 0.004185 0.005617
23 POLYGON ((-76.80596 2.08049, -76.80596 2.17099... -856 23 0.006219 0.011836
7 POLYGON ((-77.25511 1.62808, -77.25511 1.71855... -861 18 0.008390 0.020226
21 POLYGON ((-76.89579 1.99, -76.89579 2.08049, -... -857 22 0.008949 0.029175
0 POLYGON ((-77.43478 1.35668, -77.43478 1.44714... -863 15 0.011776 0.040951
12 POLYGON ((-77.16528 1.80903, -77.16528 1.89951... -860 20 0.013577 0.054528
3 POLYGON ((-77.43478 1.62808, -77.43478 1.71855... -863 18 0.014410 0.068938
2 POLYGON ((-77.43478 1.53761, -77.43478 1.62808... -863 17 0.019289 0.088227
4 POLYGON ((-77.34495 1.44714, -77.34495 1.53761... -862 16 0.029148 0.117376
15 POLYGON ((-77.07545 1.62808, -77.07545 1.71855... -859 18 0.029340 0.146716
14 POLYGON ((-77.16528 1.99, -77.16528 2.08049, -... -860 22 0.039017 0.185733
1 POLYGON ((-77.43478 1.44714, -77.43478 1.53761... -863 16 0.047508 0.233240
22 POLYGON ((-76.89579 2.08049, -76.89579 2.17099... -857 23 0.061615 0.294856
18 POLYGON ((-77.07545 2.08049, -77.07545 2.17099... -859 23 0.074083 0.368938
19 POLYGON ((-76.98562 1.99, -76.98562 2.08049, -... -858 22 0.076164 0.445103
9 POLYGON ((-77.25511 1.80903, -77.25511 1.89951... -861 20 0.077389 0.522492
11 POLYGON ((-77.16528 1.62808, -77.16528 1.71855... -860 18 0.086899 0.609391
8 POLYGON ((-77.25511 1.71855, -77.25511 1.80903... -861 19 0.104560 0.713951
20 POLYGON ((-76.98562 2.08049, -76.98562 2.17099... -858 23 0.207288 0.921239
16 POLYGON ((-77.07545 1.89951, -77.07545 1.99, -... -859 21 0.261937 1.183176
17 POLYGON ((-77.07545 1.99, -77.07545 2.08049, -... -859 22 0.299272 1.482448
13 POLYGON ((-77.16528 1.89951, -77.16528 1.99, -... -860 21 0.302064 1.784511

Calculate cumulative proportion

  1. Calculate cumulative proportion by dividing cumulative area by total area (cumulative proportion takes values between 0 and 1)
if has_data:
    gdf["cumulative_proportion"] = gdf["cumulative_area"] / total_area
    display(gdf)
geometry grid_col grid_row T1_2_20 cumulative_area cumulative_proportion
10 POLYGON ((-77.25511 1.89951, -77.25511 1.99, -... -861 21 0.000240 0.000240 0.000134
5 POLYGON ((-77.34495 1.53761, -77.34495 1.62808... -862 17 0.001192 0.001432 0.000802
6 POLYGON ((-77.34495 1.62808, -77.34495 1.71855... -862 18 0.004185 0.005617 0.003148
23 POLYGON ((-76.80596 2.08049, -76.80596 2.17099... -856 23 0.006219 0.011836 0.006633
7 POLYGON ((-77.25511 1.62808, -77.25511 1.71855... -861 18 0.008390 0.020226 0.011334
21 POLYGON ((-76.89579 1.99, -76.89579 2.08049, -... -857 22 0.008949 0.029175 0.016349
0 POLYGON ((-77.43478 1.35668, -77.43478 1.44714... -863 15 0.011776 0.040951 0.022948
12 POLYGON ((-77.16528 1.80903, -77.16528 1.89951... -860 20 0.013577 0.054528 0.030556
3 POLYGON ((-77.43478 1.62808, -77.43478 1.71855... -863 18 0.014410 0.068938 0.038631
2 POLYGON ((-77.43478 1.53761, -77.43478 1.62808... -863 17 0.019289 0.088227 0.049441
4 POLYGON ((-77.34495 1.44714, -77.34495 1.53761... -862 16 0.029148 0.117376 0.065775
15 POLYGON ((-77.07545 1.62808, -77.07545 1.71855... -859 18 0.029340 0.146716 0.082216
14 POLYGON ((-77.16528 1.99, -77.16528 2.08049, -... -860 22 0.039017 0.185733 0.104080
1 POLYGON ((-77.43478 1.44714, -77.43478 1.53761... -863 16 0.047508 0.233240 0.130703
22 POLYGON ((-76.89579 2.08049, -76.89579 2.17099... -857 23 0.061615 0.294856 0.165230
18 POLYGON ((-77.07545 2.08049, -77.07545 2.17099... -859 23 0.074083 0.368938 0.206745
19 POLYGON ((-76.98562 1.99, -76.98562 2.08049, -... -858 22 0.076164 0.445103 0.249426
9 POLYGON ((-77.25511 1.80903, -77.25511 1.89951... -861 20 0.077389 0.522492 0.292793
11 POLYGON ((-77.16528 1.62808, -77.16528 1.71855... -860 18 0.086899 0.609391 0.341489
8 POLYGON ((-77.25511 1.71855, -77.25511 1.80903... -861 19 0.104560 0.713951 0.400082
20 POLYGON ((-76.98562 2.08049, -76.98562 2.17099... -858 23 0.207288 0.921239 0.516241
16 POLYGON ((-77.07545 1.89951, -77.07545 1.99, -... -859 21 0.261937 1.183176 0.663025
17 POLYGON ((-77.07545 1.99, -77.07545 2.08049, -... -859 22 0.299272 1.482448 0.830730
13 POLYGON ((-77.16528 1.89951, -77.16528 1.99, -... -860 21 0.302064 1.784511 1.000000

Count AOO cells

  1. Calculate AOO by counting the number of cells with a cumulative proportion greater than 0.01 (i.e. exclude cells that in combination account for up to 1% of the total mapped extent of the ecosystem type).
if has_data:
    aoo = len(gdf[gdf["cumulative_proportion"] > 0.01])
    print(f'AOO is {aoo} cells')
AOO is 20 cells

AOO Calculation (direct call)

if has_data:
    aoo_count = ecosystem.aoo
    print(f'AOO: {aoo_count} grid cells')
AOO: 20 grid cells
if has_data:
    display(smart_map([aoo_grid_filtered, ecosystem]))
/home/runner/work/rle-tyler-20/rle-tyler-20/.pixi/envs/default/lib/python3.11/site-packages/lonboard/_geoarrow/ops/reproject.py:116: UserWarning: Input being reprojected to EPSG:4326 CRS.
Lonboard is only able to render data in EPSG:4326 projection.
  warnings.warn(

Criterion B Summary

Ecosystem Code Ecosystem Name EOO AOO
T1.2.20 Patìa-Dagua dry forests 2208 km² 20 cells