إنتقل إلى المحتوى الرئيسي

Hierarchy functions

Terra cells form a strict hierarchy across 15 resolution levels. Each cell at resolution n has exactly one parent at resolution n-1 and exactly 7 children at resolution n+1 (aperture-7). These functions navigate that hierarchy.

cellToParent​

Returns the ancestor of a cell at the specified (coarser) resolution.

terra.cell_to_parent(cell, res)
# Returns: str (TIS of parent cell)

cell = terra.latlon_to_cell(24.6877, 46.7219, 8) # Res 8 — 1 ha

parent_7 = terra.cell_to_parent(cell, 7) # Res 7 — 7 ha block
parent_5 = terra.cell_to_parent(cell, 5) # Res 5 — 3.43 km² district
parent_3 = terra.cell_to_parent(cell, 3) # Res 3 — 168 km² province

# Aggregate delivery data by Res 5 district
import pandas as pd
df = pd.read_csv("deliveries.csv")
df["district"] = df["terra_cell"].apply(
lambda c: terra.cell_to_parent(c, 5)
)
print(df.groupby("district")["value"].sum())

cellToChildren​

Returns all cells at the specified (finer) resolution that are contained by the given cell. A cell at resolution n has exactly 7 children at resolution n+1, 49 at n+2, and 7^k at n+k.

terra.cell_to_children(cell, res)
# Returns: list[str]

block = terra.latlon_to_cell(24.6877, 46.7219, 7) # Res 7 — 7 ha

children_8 = terra.cell_to_children(block, 8) # 7 × 1 ha cells
print(len(children_8)) # 7

children_9 = terra.cell_to_children(block, 9) # 49 cells (7²)
print(len(children_9)) # 49

cellToCenterChild​

Returns only the centre child — the single child at the finer resolution whose centre is closest to the parent's centre. This is child digit 0.

terra.cell_to_center_child(cell, res)
# Returns: str

center_child = terra.cell_to_center_child("t1858a2f000000000", 8)

compactCells​

Takes a set of cells and replaces groups of 7 sibling cells that share a parent with that parent cell. Recursively compacts until no further simplification is possible.

Compaction dramatically reduces the size of large cell sets. A filled disk of radius k at Res 8 collapses to a compact representation where most of the interior is represented by coarser cells.

terra.compact_cells(cell_list)
# Returns: list[str] — mixed resolutions

# Polyfill Riyadh and compact
import json
with open("riyadh.geojson") as f:
polygon = json.load(f)

cells = terra.polygon_to_cells(polygon, 8)
print(len(cells)) # ~120,000 cells

compact = terra.compact_cells(cells)
print(len(compact)) # ~8,000 cells — same coverage, fewer records

uncompactCells​

Expands a compact cell set back to a uniform resolution. Inverse of compactCells.

terra.uncompact_cells(compact_list, res)
# Returns: list[str] — all at target resolution

# Restore compact representation to Res 8 base cells
cells = terra.uncompact_cells(compact, 8)

The Terra System is designed and developed by Tec Solution KSA.