Indexing functions
These functions are used for finding the H3 cell index containing coordinates, and for finding the center and boundary of H3 cells.
The C library works in radians: the lat and lng fields of LatLng
(also used by CellBoundary, GeoLoop and GeoPolygon) are in radians.
The bindings and the h3 command line tool take and return degrees.
In C, use degsToRads and radsToDegs
to convert.
latLngToCell
Indexes the location at the specified resolution, providing the index of the cell containing the location. This buckets the geographic point into the H3 grid. For more information, see the algorithm description.
- C
- Java
- JavaScript (Live)
- Python
- Go
- DuckDB
- Shell
H3Error latLngToCell(const LatLng *g, int res, H3Index *out);
The latitude and longitude in g are in radians.
Returns 0 (E_SUCCESS) on success.
long latLngToCell(double lat, double lng, int res);
String latLngToCellAddress(double lat, double lng, int res);
h3.latLngToCell(lat, lng, res)
function example() { const lat = 45; const lng = 40; const res = 2; return h3.latLngToCell(lat, lng, res); }
h3.latlng_to_cell(lat, lng, resolution)
h3.NewLatLng(lat, lng).Cell(resolution)
h3_latlng_to_cell(lat, lng, resolution)
h3_latlng_to_cell_string(lat, lng, resolution)
$ h3 latLngToCell --help
h3: Convert degrees latitude/longitude coordinate to an H3 cell
H3 4.1.0
latLngToCell Convert degrees latitude/longitude coordinate to an H3 cell
-h, --help Show this help message.
-r, --resolution <res> Required. Resolution, 0-15 inclusive.
--lat, --latitude <lat> Required. Latitude in degrees.
--lng, --longitude <lng> Required. Longitude in degrees.
-f, --format <FMT> 'json' for "CELL"\n, 'newline' for CELL\n (Default: json)
$ h3 latLngToCell --lat 45 --lng 40 -r 2
"822d57fffffffff"
cellToLatLng
Finds the center of the cell in grid space. See the algorithm description for more information.
The center will drift versus the centroid of the cell on Earth due to distortion from the gnomonic projection within the icosahedron face it resides on and its distance from the center of the icosahedron face.
- C
- Java
- JavaScript (Live)
- Python
- Go
- DuckDB
- Shell
H3Error cellToLatLng(H3Index cell, LatLng *g);
The latitude and longitude written to g are in radians.
Returns 0 (E_SUCCESS) on success.
LatLng cellToLatLng(long cell);
LatLng cellToLatLng(String cellAddress);
h3.cellToLatLng(cell)
function example() { const cell = '85283473fffffff'; return h3.cellToLatLng(cell); }
h3.cell_to_latlng(cell)
h3.CellToLatLng(cell)
h3_cell_to_latlng(cell)
h3_cell_to_lat(cell)
h3_cell_to_lng(cell)
$ h3 cellToLatLng --help
h3: Convert an H3Cell to a coordinate
H3 4.1.0
cellToLatLng Convert an H3Cell to a coordinate
-h, --help Show this help message.
-c, --cell <index> Required. H3 Cell
-f, --format <FMT> 'json' for [lat, lng], 'wkt' for a WKT POINT, 'newline' for lat\nlng\n (Default: json)
$ h3 cellToLatLng -c 85283473fffffff
[37.3457933754, -121.9763759726]
cellToBoundary
Finds the boundary of the cell. For more information, see the algorithm description.
- C
- Java
- JavaScript (Live)
- Python
- Go
- DuckDB
- Shell
H3Error cellToBoundary(H3Index cell, CellBoundary *bndry);
The vertices written to bndry are in radians.
Returns 0 (E_SUCCESS) on success.
List<LatLng> cellToBoundary(long cell);
List<LatLng> cellToBoundary(String cellAddress);
h3.cellToBoundary(cell, [formatAsGeoJson])
function example() { const cell = '85283473fffffff'; return h3.cellToBoundary(cell); }
h3.cell_to_boundary(cell)
Returns tuple of lat/lng tuples.
For GeoJSON compatibility (lng/lat order), see the
h3-py docs.
cell.Boundary()
h3_cell_to_boundary_wkt(cell)
h3_cell_to_boundary_wkb(cell)
$ h3 cellToBoundary --help
h3: Convert an H3 cell to a polygon defining its boundary
H3 4.1.0
cellToBoundary Convert an H3 cell to a polygon defining its boundary
-h, --help Show this help message.
-c, --cell <index> Required. H3 Cell
-f, --format <FMT> 'json' for [[lat, lng], ...], 'wkt' for a WKT POLYGON, 'newline' for lat\nlng\n... (Default: json)
$ h3 cellToBoundary -c 85283473fffffff
[[37.2713558667, -121.9150803271], [37.3539264509, -121.8622232890], [37.4283411861, -121.9235499963], [37.4201286777, -122.0377349643], [37.3375560844, -122.0904289290], [37.2631979746, -122.0291013092]]