Skip to main content

Grid traversal functions

Grid traversal allows finding cells in the vicinity of an origin cell, and determining how to move across the grid from one cell to another.

gridDisk​

Returns all cells within grid distance k of the origin cell — a filled hexagonal disk. Output order is not guaranteed.

The number of cells returned is 1 + 6 + 12 + ... + 6k = 3k² + 3k + 1.

terra.grid_disk(origin, k)
# Returns: list[str] — cell TIS values

# All cells within 2 rings of a Riyadh cell (19 cells)
disk = terra.grid_disk("t10830cd1943ffff8", 2)
print(len(disk)) # 19

# All Res 8 (1 ha) cells within 500 m of a point
# Res 8 hex width ≈ 107 m, so k=5 covers ~535 m radius
cell = terra.latlon_to_cell(24.6877, 46.7219, 8)
nearby = terra.grid_disk(cell, 5)
print(len(nearby)) # 91

gridRing​

Returns only the cells at exactly grid distance k from the origin — the hollow ring. Returns 6k cells for k > 0, and 1 cell for k = 0.

terra.grid_ring(origin, k)
# Returns: list[str]

ring1 = terra.grid_ring("t10830cd1943ffff8", 1)
print(len(ring1)) # 6 — the 6 direct neighbours

gridDistance​

Returns the minimum number of cell hops between two cells at the same resolution.

terra.grid_distance(origin, dest)
# Returns: int

cell_a = terra.latlon_to_cell(24.6877, 46.7219, 8) # Riyadh
cell_b = terra.latlon_to_cell(24.7136, 46.6753, 8) # ~5 km away
dist = terra.grid_distance(cell_a, cell_b)
print(dist) # approx 50 hops at Res 8 (each hop ~107 m)

gridPathCells​

Returns a minimal-length contiguous path of cells between two cells (inclusive of both endpoints). Path length equals gridDistance(start, end) + 1.

terra.grid_path_cells(start, end)
# Returns: list[str]

start = terra.latlon_to_cell(24.6877, 46.7219, 8)
end = terra.latlon_to_cell(24.7003, 46.7401, 8)
path = terra.grid_path_cells(start, end)
print(len(path)) # gridDistance + 1

areNeighborCells​

Returns true if two cells share an edge (grid distance = 1).

terra.are_neighbor_cells(cell_a, cell_b)
# Returns: bool

neighbors = terra.grid_ring("t10830cd1943ffff8", 1)
print(terra.are_neighbor_cells("t10830cd1943ffff8", neighbors[0]))
# True

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