Skip to content

Edges & Vertices

Beyond cells themselves, H3 models the edges between neighboring cells, the vertices at their corners, and a local IJ coordinate system for relative positioning. These power connectivity graphs, precise polygon rendering, and grid-relative math.

use Foysal50x\H3\H3;
 
$h3 = new H3();
$origin = $h3->latLngToCell(37.7749, -122.4194, 9);

Directed edges

A directed edge represents the connection from one cell to an adjacent cell, with a direction. It's itself an H3 index, so you can store and pass it like a cell. A hexagon has 6 directed edges leaving it; a pentagon has 5.

areNeighborCells

Check whether two cells share an edge.

public function areNeighborCells(int $origin, int $destination): bool
$neighbors = $h3->gridDisk($origin, 1);
$other = $neighbors[1];
 
var_dump($h3->areNeighborCells($origin, $other));   // true

cellsToDirectedEdge

Create the directed edge from one cell to an adjacent cell.

public function cellsToDirectedEdge(int $origin, int $destination): int
$edge = $h3->cellsToDirectedEdge($origin, $other);

Throws an H3Exception if the two cells are not neighbors.

isValidDirectedEdge

Check whether an index is a well-formed directed edge.

public function isValidDirectedEdge(int $edge): bool
if ($h3->isValidDirectedEdge($edge)) {
    // safe to use
}

getDirectedEdgeOrigin

The cell an edge starts from.

public function getDirectedEdgeOrigin(int $edge): int

getDirectedEdgeDestination

The cell an edge points to.

public function getDirectedEdgeDestination(int $edge): int
$from = $h3->getDirectedEdgeOrigin($edge);
$to   = $h3->getDirectedEdgeDestination($edge);

directedEdgeToCells

Both endpoints at once.

public function directedEdgeToCells(int $edge): array

Returns array{origin: int, destination: int}.

$pair = $h3->directedEdgeToCells($edge);
echo $h3->h3ToString($pair['origin']) . ' -> ' . $h3->h3ToString($pair['destination']);

originToDirectedEdges

Every directed edge leaving a cell — 6 for a hexagon, 5 for a pentagon.

public function originToDirectedEdges(int $origin): array
$edges = $h3->originToDirectedEdges($origin);
echo count($edges);   // 6 (or 5 for a pentagon)

directedEdgeToBoundary

The geographic line segment of an edge — the shared border between the two cells.

public function directedEdgeToBoundary(int $edge): array

Returns a list of array{lat: float, lng: float} points describing the edge geometry.

$line = $h3->directedEdgeToBoundary($edge);
foreach ($line as $point) {
    printf("  %.5f, %.5f\n", $point['lat'], $point['lng']);
}

Finding the outline of a region

To draw the outer boundary of a set of cells, walk every cell's originToDirectedEdges, and keep only the edges whose destination is not in the set. Those edges trace the perimeter — exactly the technique the service-area example uses.

Vertices

A vertex is a corner shared by adjacent cells. Vertices are also H3 indices. They're useful for placing markers exactly on hexagon corners and for generating precise polygons.

cellToVertex

The vertex at a given corner number of a cell.

public function cellToVertex(int $cell, int $vertexNum): int
ParameterTypeDescription
$cellintThe cell
$vertexNumintCorner index (0–5 for a hexagon, 0–4 for a pentagon)
$vertex = $h3->cellToVertex($origin, 0);

cellToVertexes

All corner vertices of a cell at once.

public function cellToVertexes(int $cell): array
$vertices = $h3->cellToVertexes($origin);
echo count($vertices);   // 6 (or 5 for a pentagon)

vertexToLatLng

The coordinate of a vertex.

public function vertexToLatLng(int $vertex): array

Returns array{lat: float, lng: float}.

$coord = $h3->vertexToLatLng($vertex);
printf("%.5f, %.5f\n", $coord['lat'], $coord['lng']);

isValidVertex

Check whether an index is a well-formed vertex.

public function isValidVertex(int $vertex): bool
var_dump($h3->isValidVertex($vertex));   // true

Local IJ coordinates

The local IJ system assigns each cell a pair of integer coordinates (i, j) relative to an origin cell. It turns the hex grid into something like a 2D coordinate plane, which is convenient for relative movement and grid-based algorithms.

Local IJ is approximate and origin-relative

IJ coordinates are only meaningful near their origin and can fail across pentagons or large distances. Don't treat them as global coordinates — they're a local convenience, and both functions throw if the cell is too far from the origin.

cellToLocalIj

Convert a cell to its IJ coordinates relative to an origin.

public function cellToLocalIj(int $origin, int $cell, int $mode = 0): array

Returns array{i: int, j: int}.

$ij = $h3->cellToLocalIj($origin, $someNeighbor);
printf("i=%d, j=%d\n", $ij['i'], $ij['j']);

localIjToCell

Convert IJ coordinates back to a cell, relative to the same origin.

public function localIjToCell(int $origin, int $i, int $j, int $mode = 0): int
ParameterTypeDescription
$originintThe reference origin cell
$iintI coordinate
$jintJ coordinate
$modeintMode flags (default 0)
// The cell one step east of the origin
$cell = $h3->localIjToCell($origin, 1, 0);
 
var_dump($h3->areNeighborCells($origin, $cell));   // true

Worked example — build a cell connectivity map

connectivity.php
use Foysal50x\H3\H3;
 
$h3 = H3::getInstance();
$origin = $h3->latLngToCell(37.7749, -122.4194, 9);
 
$graph = [];
foreach ($h3->originToDirectedEdges($origin) as $edge) {
    if (!$h3->isValidDirectedEdge($edge)) {
        continue;
    }
    $dest = $h3->getDirectedEdgeDestination($edge);
    $graph[$h3->h3ToString($origin)][] = $h3->h3ToString($dest);
}
 
print_r($graph);

Next steps

H3 PHP — Hexagonal geospatial indexing for PHP. Released under the MIT license.