Criterion B Details

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

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 T3.1.1 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() = 50

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-ruritania-thomas/rle-ruritania-thomas/.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-ruritania-thomas/rle-ruritania-thomas/.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 129228.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-ruritania-thomas/rle-ruritania-thomas/.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 T3_1_1
0 POLYGON ((-73.2127 0.54263, -73.2127 0.63307, ... -816 6 0.123263
1 POLYGON ((-73.2127 0.63307, -73.2127 0.72351, ... -816 7 0.059392
2 POLYGON ((-73.12286 0.27131, -73.12286 0.36175... -815 3 0.102231
3 POLYGON ((-73.12286 0.36175, -73.12286 0.45219... -815 4 0.139359
4 POLYGON ((-73.12286 0.45219, -73.12286 0.54263... -815 5 0.349538
... ... ... ... ...
170 POLYGON ((-68.27196 2.17099, -68.27196 2.2615,... -761 24 0.007212
171 POLYGON ((-68.18213 2.08049, -68.18213 2.17099... -760 23 0.381188
172 POLYGON ((-68.18213 2.17099, -68.18213 2.2615,... -760 24 0.564259
173 POLYGON ((-68.0923 2.08049, -68.0923 2.17099, ... -759 23 0.206402
174 POLYGON ((-68.0923 2.17099, -68.0923 2.2615, -... -759 24 0.000011

175 rows × 4 columns

The column T3_1_1 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(37.59302761708342)

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 T3_1_1 cumulative_area
9 POLYGON ((-73.03303 0.54263, -73.03303 0.63307... -814 6 2.836323e-07 2.836323e-07
21 POLYGON ((-72.76354 0.36175, -72.76354 0.45219... -811 4 5.195489e-06 5.479121e-06
174 POLYGON ((-68.0923 2.17099, -68.0923 2.2615, -... -759 24 1.069085e-05 1.616997e-05
66 POLYGON ((-72.31438 -0.18087, -72.31438 -0.090... -806 -2 5.918903e-05 7.535900e-05
139 POLYGON ((-70.60758 0.36175, -70.60758 0.45219... -787 4 7.005047e-05 1.454095e-04
... ... ... ... ... ...
112 POLYGON ((-70.78724 0.09044, -70.78724 0.18087... -789 1 7.638137e-01 3.435782e+01
132 POLYGON ((-70.69741 0.72351, -70.69741 0.81396... -788 8 7.833165e-01 3.514114e+01
108 POLYGON ((-70.87708 0.99486, -70.87708 1.08531... -790 11 8.050010e-01 3.594614e+01
72 POLYGON ((-71.41607 1.62808, -71.41607 1.71855... -796 18 8.211107e-01 3.676725e+01
131 POLYGON ((-70.69741 0.63307, -70.69741 0.72351... -788 7 8.257762e-01 3.759303e+01

175 rows × 5 columns

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 T3_1_1 cumulative_area cumulative_proportion
9 POLYGON ((-73.03303 0.54263, -73.03303 0.63307... -814 6 2.836323e-07 2.836323e-07 7.544812e-09
21 POLYGON ((-72.76354 0.36175, -72.76354 0.45219... -811 4 5.195489e-06 5.479121e-06 1.457483e-07
174 POLYGON ((-68.0923 2.17099, -68.0923 2.2615, -... -759 24 1.069085e-05 1.616997e-05 4.301322e-07
66 POLYGON ((-72.31438 -0.18087, -72.31438 -0.090... -806 -2 5.918903e-05 7.535900e-05 2.004601e-06
139 POLYGON ((-70.60758 0.36175, -70.60758 0.45219... -787 4 7.005047e-05 1.454095e-04 3.867990e-06
... ... ... ... ... ... ...
112 POLYGON ((-70.78724 0.09044, -70.78724 0.18087... -789 1 7.638137e-01 3.435782e+01 9.139414e-01
132 POLYGON ((-70.69741 0.72351, -70.69741 0.81396... -788 8 7.833165e-01 3.514114e+01 9.347781e-01
108 POLYGON ((-70.87708 0.99486, -70.87708 1.08531... -790 11 8.050010e-01 3.594614e+01 9.561917e-01
72 POLYGON ((-71.41607 1.62808, -71.41607 1.71855... -796 18 8.211107e-01 3.676725e+01 9.780338e-01
131 POLYGON ((-70.69741 0.63307, -70.69741 0.72351... -788 7 8.257762e-01 3.759303e+01 1.000000e+00

175 rows × 6 columns

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 134 cells

AOO Calculation (direct call)

if has_data:
    aoo_count = ecosystem.aoo
    print(f'AOO: {aoo_count} grid cells')
AOO: 134 grid cells
if has_data:
    display(smart_map([aoo_grid_filtered, ecosystem]))
/home/runner/work/rle-ruritania-thomas/rle-ruritania-thomas/.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
T3.1.1 Pantepui 129229 km² 134 cells