Skip to main content

Quick Start

This page shows you how to get started with the functions in Terra that convert points to cell identifiers, and from cell identifiers back to geometry. These are the core indexing functions used in most Terra applications.

The examples below use the coordinates of Masmak Fortress in Riyadh (24.6877°N, 46.7219°E) at resolution 8 — the 1-hectare national base standard.

Follow along using your preferred language binding.

Convert a coordinate to a cell​

Find which Terra cell contains a given latitude and longitude:

import terra

lat = 24.6877
lng = 46.7219
res = 8 # 1-hectare base resolution

cell = terra.latlon_to_cell(lat, lng, res)
print(cell)
# "t10830cd1943ffff8"

The result is the Terra Index String (TIS) of the hexagonal cell containing this point. Every point within the bounds of this cell shares the same identifier.

Get the cell centre​

Retrieve the geographic centre of a Terra cell:

cell = "t10830cd1943ffff8"
center = terra.cell_to_center(cell)
print(center)
# (24.6880, 46.7221)

Note that the centre returned is the centroid of the hexagonal cell in the LAEA projection, reprojected to WGS84. It will differ slightly from the original input coordinates, because the cell contains a region rather than a single point.

Get the cell boundary​

Retrieve the six vertices of a Terra hexagon:

cell = "t10830cd1943ffff8"
boundary = terra.cell_to_boundary(cell)
print(boundary)
# [(24.6935, 46.7177), (24.6963, 46.7253), (24.6908, 46.7328),
# (24.6825, 46.7326), (24.6797, 46.7250), (24.6852, 46.7176)]

Get the human-readable Terra Grid Code​

Every Terra cell at Res 8 has a short human-readable address — the Terra Grid Code (TGC):

cell = "t10830cd1943ffff8"
code = terra.cell_to_saudi_code(cell)
print(code)
# "T08-0C6D351E"

The TGC format is T[RES]-[POSITION]. T08 indicates resolution 8 (1 ha). The 6-character position code encodes the geometric location with no reference to any administrative boundary.

Get the parent cell​

Move up one level in the hierarchy to get the Res 7 block (7 ha) containing this cell:

cell = "t10830cd1943ffff8"
parent = terra.cell_to_parent(cell, 7)
print(parent)
# "t1858a2f000000000" (Res 7 — 7 ha block)

Find neighbouring cells​

Get all cells within 1 ring of the origin (the 6 adjacent hexagons):

cell = "t10830cd1943ffff8"
ring = terra.grid_ring(cell, k=1)
print(len(ring)) # 6 neighbours

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