Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/uber/h3-js

h3-js provides a JavaScript version of H3, a hexagon-based geospatial indexing system.
https://github.com/uber/h3-js

emscripten geospatial h3 hexagon javascript spatial-indexing uber

Last synced: 13 days ago
JSON representation

h3-js provides a JavaScript version of H3, a hexagon-based geospatial indexing system.

Awesome Lists containing this project

README

        

H3 Logo

# h3-js

[![Build Status](https://github.com/uber/h3-js/workflows/test/badge.svg)](https://github.com/uber/h3-js/actions)
[![Coverage Status](https://coveralls.io/repos/github/uber/h3-js/badge.svg?branch=master)](https://coveralls.io/github/uber/h3-js?branch=master)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
[![npm version](https://badge.fury.io/js/h3-js.svg)](https://badge.fury.io/js/h3-js)
[![H3 Version](https://img.shields.io/static/v1?label=h3%20api&message=v4.1.0&color=blue)](https://github.com/uber/h3/releases/tag/v4.1.0)

The `h3-js` library provides a pure-JavaScript version of the [H3 Core Library](https://github.com/uber/h3), a hexagon-based geographic grid system. It can be used either in Node >= 6 or in the browser. The core library is transpiled from C using [emscripten](http://kripken.github.io/emscripten-site), offering full parity with the C API and highly efficient operations.

For more information on H3 and for the full API documentation, please see the [H3 Documentation](https://h3geo.org).

- Post **bug reports or feature requests** to the [Github Issues page](https://github.com/uber/h3-js/issues)
- Ask **questions** by posting to the [H3 tag on StackOverflow](https://stackoverflow.com/questions/tagged/h3)

## Install

npm install h3-js

## Usage

> :tada: **Note:** The following usage docs apply to **H3 v4**, which was released on August 23, 2022.
>
> - For v3 docs, [see the latest v3.x.x release](https://github.com/uber/h3-js/blob/v3.7.2/README.md).
> - For breaking changes in v4, [see the CHANGELOG](./CHANGELOG.md). In particular, most [function names have changed](https://h3geo.org/docs/library/migration-3.x/functions).

The library uses ES6 modules. Bundles for Node and the browser are built to the `dist` folder.

### Import

ES6 usage:

```js
import {latLngToCell} from "h3-js";
```

CommonJS usage:

```js
const h3 = require("h3-js");
```

Pre-bundled script (library is available as an `h3` global):

```html

```

### Core functions

```js
// Convert a lat/lng point to a hexagon index at resolution 7
const h3Index = h3.latLngToCell(37.3615593, -122.0553238, 7);
// -> '87283472bffffff'

// Get the center of the hexagon
const hexCenterCoordinates = h3.cellToLatLng(h3Index);
// -> [37.35171820183272, -122.05032565263946]

// Get the vertices of the hexagon
const hexBoundary = h3.cellToBoundary(h3Index);
// -> [ [37.341099093235684, -122.04156135164334 ], ...]
```

### Useful algorithms

```js
// Get all neighbors within 1 step of the hexagon
const disk = h3.gridDisk(h3Index, 1);
// -> ['87283472bffffff', '87283472affffff', ...]

// Get the set of hexagons within a polygon
const polygon = [
[37.813318999983238, -122.4089866999972145],
[37.7198061999978478, -122.3544736999993603],
[37.8151571999998453, -122.4798767000009008]
];
const hexagons = h3.polygonToCells(polygon, 7);
// -> ['872830828ffffff', '87283082effffff', ...]

// Get the outline of a set of hexagons, as a GeoJSON-style MultiPolygon
const coordinates = h3.cellsToMultiPolygon(hexagons, true);
// -> [[[
// [-122.37681938644465, 37.76546768434345],
// [-122.3856345540363,37.776004200673846],
// ...
// ]]]
```

## API Reference

## h3

* [h3](#module_h3)
* [.UNITS](#module_h3.UNITS)
* [.h3IndexToSplitLong(h3Index)](#module_h3.h3IndexToSplitLong) ⇒ SplitLong
* [.splitLongToH3Index(lower, upper)](#module_h3.splitLongToH3Index) ⇒ H3Index
* [.isValidCell(h3Index)](#module_h3.isValidCell) ⇒ boolean
* [.isPentagon(h3Index)](#module_h3.isPentagon) ⇒ boolean
* [.isResClassIII(h3Index)](#module_h3.isResClassIII) ⇒ boolean
* [.getBaseCellNumber(h3Index)](#module_h3.getBaseCellNumber) ⇒ number
* [.getIcosahedronFaces(h3Index)](#module_h3.getIcosahedronFaces) ⇒ Array.<number>
* [.getResolution(h3Index)](#module_h3.getResolution) ⇒ number
* [.latLngToCell(lat, lng, res)](#module_h3.latLngToCell) ⇒ H3Index
* [.cellToLatLng(h3Index)](#module_h3.cellToLatLng) ⇒ CoordPair
* [.cellToBoundary(h3Index, [formatAsGeoJson])](#module_h3.cellToBoundary) ⇒ Array.<CoordPair>
* [.cellToParent(h3Index, res)](#module_h3.cellToParent) ⇒ H3Index
* [.cellToChildren(h3Index, res)](#module_h3.cellToChildren) ⇒ Array.<H3Index>
* [.cellToChildrenSize(h3Index, res)](#module_h3.cellToChildrenSize) ⇒ number
* [.cellToCenterChild(h3Index, res)](#module_h3.cellToCenterChild) ⇒ H3Index
* [.cellToChildPos(h3Index, parentRes)](#module_h3.cellToChildPos) ⇒ number
* [.childPosToCell(childPos, h3Index, childRes)](#module_h3.childPosToCell) ⇒ H3Index
* [.gridDisk(h3Index, ringSize)](#module_h3.gridDisk) ⇒ Array.<H3Index>
* [.gridDiskDistances(h3Index, ringSize)](#module_h3.gridDiskDistances) ⇒ Array.<Array.<H3Index>>
* [.gridRingUnsafe(h3Index, ringSize)](#module_h3.gridRingUnsafe) ⇒ Array.<H3Index>
* [.polygonToCells(coordinates, res, [isGeoJson])](#module_h3.polygonToCells) ⇒ Array.<H3Index>
* [.cellsToMultiPolygon(h3Indexes, [formatAsGeoJson])](#module_h3.cellsToMultiPolygon) ⇒ Array.<Array.<Array.<CoordPair>>>
* [.compactCells(h3Set)](#module_h3.compactCells) ⇒ Array.<H3Index>
* [.uncompactCells(compactedSet, res)](#module_h3.uncompactCells) ⇒ Array.<H3Index>
* [.areNeighborCells(origin, destination)](#module_h3.areNeighborCells) ⇒ boolean
* [.cellsToDirectedEdge(origin, destination)](#module_h3.cellsToDirectedEdge) ⇒ H3Index
* [.getDirectedEdgeOrigin(edgeIndex)](#module_h3.getDirectedEdgeOrigin) ⇒ H3Index
* [.getDirectedEdgeDestination(edgeIndex)](#module_h3.getDirectedEdgeDestination) ⇒ H3Index
* [.isValidDirectedEdge(edgeIndex)](#module_h3.isValidDirectedEdge) ⇒ boolean
* [.directedEdgeToCells(edgeIndex)](#module_h3.directedEdgeToCells) ⇒ Array.<H3Index>
* [.originToDirectedEdges(h3Index)](#module_h3.originToDirectedEdges) ⇒ Array.<H3Index>
* [.directedEdgeToBoundary(edgeIndex, [formatAsGeoJson])](#module_h3.directedEdgeToBoundary) ⇒ Array.<CoordPair>
* [.gridDistance(origin, destination)](#module_h3.gridDistance) ⇒ number
* [.gridPathCells(origin, destination)](#module_h3.gridPathCells) ⇒ Array.<H3Index>
* [.cellToLocalIj(origin, destination)](#module_h3.cellToLocalIj) ⇒ CoordIJ
* [.localIjToCell(origin, coords)](#module_h3.localIjToCell) ⇒ H3Index
* [.greatCircleDistance(latLng1, latLng2, unit)](#module_h3.greatCircleDistance) ⇒ number
* [.cellArea(h3Index, unit)](#module_h3.cellArea) ⇒ number
* [.edgeLength(edge, unit)](#module_h3.edgeLength) ⇒ number
* [.getHexagonAreaAvg(res, unit)](#module_h3.getHexagonAreaAvg) ⇒ number
* [.getHexagonEdgeLengthAvg(res, unit)](#module_h3.getHexagonEdgeLengthAvg) ⇒ number
* [.cellToVertex(h3Index, vertexNum)](#module_h3.cellToVertex) ⇒ H3Index
* [.cellToVertexes(h3Index)](#module_h3.cellToVertexes) ⇒ Array.<H3Index>
* [.vertexToLatLng(h3Index)](#module_h3.vertexToLatLng) ⇒ CoordPair
* [.isValidVertex(h3Index)](#module_h3.isValidVertex) ⇒ boolean
* [.getNumCells(res)](#module_h3.getNumCells) ⇒ number
* [.getRes0Cells()](#module_h3.getRes0Cells) ⇒ Array.<H3Index>
* [.getPentagons(res)](#module_h3.getPentagons) ⇒ Array.<H3Index>
* [.degsToRads(deg)](#module_h3.degsToRads) ⇒ number
* [.radsToDegs(rad)](#module_h3.radsToDegs) ⇒ number
* [.H3Index](#module_h3.H3Index) : string
* [.H3IndexInput](#module_h3.H3IndexInput) : string \| Array.<number>
* [.CoordIJ](#module_h3.CoordIJ)
* [.H3Error](#module_h3.H3Error)
* [.CoordPair](#module_h3.CoordPair) : Array.<number>
* [.SplitLong](#module_h3.SplitLong) : Array.<number>

* * *

### h3.UNITS
Length/Area units

**Properties**

| Name | Type |
| --- | --- |
| m | string |
| m2 | string |
| km | string |
| km2 | string |
| rads | string |
| rads2 | string |

* * *

### h3.h3IndexToSplitLong(h3Index) ⇒ SplitLong
Convert an H3 index (64-bit hexidecimal string) into a "split long" - a pair of 32-bit ints

**Returns**: SplitLong - A two-element array with 32 lower bits and 32 upper bits

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index to check |

* * *

### h3.splitLongToH3Index(lower, upper) ⇒ H3Index
Get a H3 index string from a split long (pair of 32-bit ints)

**Returns**: H3Index - H3 index

| Param | Type | Description |
| --- | --- | --- |
| lower | number | Lower 32 bits |
| upper | number | Upper 32 bits |

* * *

### h3.isValidCell(h3Index) ⇒ boolean
Whether a given string represents a valid H3 index

**Returns**: boolean - Whether the index is valid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index to check |

* * *

### h3.isPentagon(h3Index) ⇒ boolean
Whether the given H3 index is a pentagon

**Returns**: boolean - isPentagon

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index to check |

* * *

### h3.isResClassIII(h3Index) ⇒ boolean
Whether the given H3 index is in a Class III resolution (rotated versus
the icosahedron and subject to shape distortion adding extra points on
icosahedron edges, making them not true hexagons).

**Returns**: boolean - isResClassIII

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index to check |

* * *

### h3.getBaseCellNumber(h3Index) ⇒ number
Get the number of the base cell for a given H3 index

**Returns**: number - Index of the base cell (0-121)

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index to get the base cell for |

* * *

### h3.getIcosahedronFaces(h3Index) ⇒ Array.<number>
Get the indices of all icosahedron faces intersected by a given H3 index

**Returns**: Array.<number> - Indices (0-19) of all intersected faces
**Throws**:

- H3Error If input is invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index to get faces for |

* * *

### h3.getResolution(h3Index) ⇒ number
Returns the resolution of an H3 index

**Returns**: number - The number (0-15) resolution, or -1 if invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index to get resolution |

* * *

### h3.latLngToCell(lat, lng, res) ⇒ H3Index
Get the hexagon containing a lat,lon point

**Returns**: H3Index - H3 index
**Throws**:

- H3Error If input is invalid

| Param | Type | Description |
| --- | --- | --- |
| lat | number | Latitude of point |
| lng | number | Longtitude of point |
| res | number | Resolution of hexagons to return |

* * *

### h3.cellToLatLng(h3Index) ⇒ CoordPair
Get the lat,lon center of a given hexagon

**Returns**: CoordPair - Point as a [lat, lng] pair
**Throws**:

- H3Error If input is invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index |

* * *

### h3.cellToBoundary(h3Index, [formatAsGeoJson]) ⇒ Array.<CoordPair>
Get the vertices of a given hexagon (or pentagon), as an array of [lat, lng]
points. For pentagons and hexagons on the edge of an icosahedron face, this
function may return up to 10 vertices.

**Returns**: Array.<CoordPair> - Array of [lat, lng] pairs
**Throws**:

- H3Error If input is invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index |
| [formatAsGeoJson] | boolean | Whether to provide GeoJSON output: [lng, lat], closed loops |

* * *

### h3.cellToParent(h3Index, res) ⇒ H3Index
Get the parent of the given hexagon at a particular resolution

**Returns**: H3Index - H3 index of parent, or null for invalid input
**Throws**:

- H3Error If input is invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index to get parent for |
| res | number | Resolution of hexagon to return |

* * *

### h3.cellToChildren(h3Index, res) ⇒ Array.<H3Index>
Get the children/descendents of the given hexagon at a particular resolution

**Returns**: Array.<H3Index> - H3 indexes of children, or empty array for invalid input
**Throws**:

- H3Error If resolution is invalid or output is too large for JS

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index to get children for |
| res | number | Resolution of hexagons to return |

* * *

### h3.cellToChildrenSize(h3Index, res) ⇒ number
Get the number of children for a cell at a given resolution

**Returns**: number - Number of children at res for the given cell
**Throws**:

- H3Error If cell or parentRes are invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index to get child count for |
| res | number | Child resolution |

* * *

### h3.cellToCenterChild(h3Index, res) ⇒ H3Index
Get the center child of the given hexagon at a particular resolution

**Returns**: H3Index - H3 index of child, or null for invalid input
**Throws**:

- H3Error If resolution is invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index to get center child for |
| res | number | Resolution of cell to return |

* * *

### h3.cellToChildPos(h3Index, parentRes) ⇒ number
Get the position of the cell within an ordered list of all children of the
cell's parent at the specified resolution.

**Returns**: number - Position of child within parent at parentRes
**Throws**:

- H3Error If cell or parentRes are invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index to get child pos for |
| parentRes | number | Resolution of reference parent |

* * *

### h3.childPosToCell(childPos, h3Index, childRes) ⇒ H3Index
Get the child cell at a given position within an ordered list of all children
at the specified resolution

**Returns**: H3Index - H3 index of child
**Throws**:

- H3Error If input is invalid

| Param | Type | Description |
| --- | --- | --- |
| childPos | number | Position of the child cell to get |
| h3Index | H3IndexInput | H3 index of the parent cell |
| childRes | number | Resolution of child cell to return |

* * *

### h3.gridDisk(h3Index, ringSize) ⇒ Array.<H3Index>
Get all hexagons in a k-ring around a given center. The order of the hexagons is undefined.

**Returns**: Array.<H3Index> - H3 indexes for all hexagons in ring
**Throws**:

- H3Error If input is invalid or output is too large for JS

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index of center hexagon |
| ringSize | number | Radius of k-ring |

* * *

### h3.gridDiskDistances(h3Index, ringSize) ⇒ Array.<Array.<H3Index>>
Get all hexagons in a k-ring around a given center, in an array of arrays
ordered by distance from the origin. The order of the hexagons within each ring is undefined.

**Returns**: Array.<Array.<H3Index>> - Array of arrays with H3 indexes for all hexagons each ring
**Throws**:

- H3Error If input is invalid or output is too large for JS

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index of center hexagon |
| ringSize | number | Radius of k-ring |

* * *

### h3.gridRingUnsafe(h3Index, ringSize) ⇒ Array.<H3Index>
Get all hexagons in a hollow hexagonal ring centered at origin with sides of a given length.
Unlike gridDisk, this function will throw an error if there is a pentagon anywhere in the ring.

**Returns**: Array.<H3Index> - H3 indexes for all hexagons in ring
**Throws**:

- Error If the algorithm could not calculate the ring
- H3Error If input is invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index of center hexagon |
| ringSize | number | Radius of ring |

* * *

### h3.polygonToCells(coordinates, res, [isGeoJson]) ⇒ Array.<H3Index>
Get all hexagons with centers contained in a given polygon. The polygon
is specified with GeoJson semantics as an array of loops. Each loop is
an array of [lat, lng] pairs (or [lng, lat] if isGeoJson is specified).
The first loop is the perimeter of the polygon, and subsequent loops are
expected to be holes.

**Returns**: Array.<H3Index> - H3 indexes for all hexagons in polygon
**Throws**:

- H3Error If input is invalid or output is too large for JS

| Param | Type | Description |
| --- | --- | --- |
| coordinates | Array.<Array.<number>> \| Array.<Array.<Array.<number>>> | Array of loops, or a single loop |
| res | number | Resolution of hexagons to return |
| [isGeoJson] | boolean | Whether to expect GeoJson-style [lng, lat] pairs instead of [lat, lng] |

* * *

### h3.cellsToMultiPolygon(h3Indexes, [formatAsGeoJson]) ⇒ Array.<Array.<Array.<CoordPair>>>
Get the outlines of a set of H3 hexagons, returned in GeoJSON MultiPolygon
format (an array of polygons, each with an array of loops, each an array of
coordinates). Coordinates are returned as [lat, lng] pairs unless GeoJSON
is requested.

It is the responsibility of the caller to ensure that all hexagons in the
set have the same resolution and that the set contains no duplicates. Behavior
is undefined if duplicates or multiple resolutions are present, and the
algorithm may produce unexpected or invalid polygons.

**Returns**: Array.<Array.<Array.<CoordPair>>> - MultiPolygon-style output.
**Throws**:

- H3Error If input is invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Indexes | Array.<H3IndexInput> | H3 indexes to get outlines for |
| [formatAsGeoJson] | boolean | Whether to provide GeoJSON output: [lng, lat], closed loops |

* * *

### h3.compactCells(h3Set) ⇒ Array.<H3Index>
Compact a set of hexagons of the same resolution into a set of hexagons across
multiple levels that represents the same area.

**Returns**: Array.<H3Index> - Compacted H3 indexes
**Throws**:

- H3Error If the input is invalid (e.g. duplicate hexagons)

| Param | Type | Description |
| --- | --- | --- |
| h3Set | Array.<H3IndexInput> | H3 indexes to compact |

* * *

### h3.uncompactCells(compactedSet, res) ⇒ Array.<H3Index>
Uncompact a compacted set of hexagons to hexagons of the same resolution

**Returns**: Array.<H3Index> - The uncompacted H3 indexes
**Throws**:

- H3Error If the input is invalid (e.g. invalid resolution)

| Param | Type | Description |
| --- | --- | --- |
| compactedSet | Array.<H3IndexInput> | H3 indexes to uncompact |
| res | number | The resolution to uncompact to |

* * *

### h3.areNeighborCells(origin, destination) ⇒ boolean
Whether two H3 indexes are neighbors (share an edge)

**Returns**: boolean - Whether the hexagons share an edge
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| origin | H3IndexInput | Origin hexagon index |
| destination | H3IndexInput | Destination hexagon index |

* * *

### h3.cellsToDirectedEdge(origin, destination) ⇒ H3Index
Get an H3 index representing a unidirectional edge for a given origin and destination

**Returns**: H3Index - H3 index of the edge, or null if no edge is shared
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| origin | H3IndexInput | Origin hexagon index |
| destination | H3IndexInput | Destination hexagon index |

* * *

### h3.getDirectedEdgeOrigin(edgeIndex) ⇒ H3Index
Get the origin hexagon from an H3 index representing a unidirectional edge

**Returns**: H3Index - H3 index of the edge origin
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| edgeIndex | H3IndexInput | H3 index of the edge |

* * *

### h3.getDirectedEdgeDestination(edgeIndex) ⇒ H3Index
Get the destination hexagon from an H3 index representing a unidirectional edge

**Returns**: H3Index - H3 index of the edge destination
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| edgeIndex | H3IndexInput | H3 index of the edge |

* * *

### h3.isValidDirectedEdge(edgeIndex) ⇒ boolean
Whether the input is a valid unidirectional edge

**Returns**: boolean - Whether the index is valid

| Param | Type | Description |
| --- | --- | --- |
| edgeIndex | H3IndexInput | H3 index of the edge |

* * *

### h3.directedEdgeToCells(edgeIndex) ⇒ Array.<H3Index>
Get the [origin, destination] pair represented by a unidirectional edge

**Returns**: Array.<H3Index> - [origin, destination] pair as H3 indexes
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| edgeIndex | H3IndexInput | H3 index of the edge |

* * *

### h3.originToDirectedEdges(h3Index) ⇒ Array.<H3Index>
Get all of the unidirectional edges with the given H3 index as the origin (i.e. an edge to
every neighbor)

**Returns**: Array.<H3Index> - List of unidirectional edges
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index of the origin hexagon |

* * *

### h3.directedEdgeToBoundary(edgeIndex, [formatAsGeoJson]) ⇒ Array.<CoordPair>
Get the vertices of a given edge as an array of [lat, lng] points. Note that for edges that
cross the edge of an icosahedron face, this may return 3 coordinates.

**Returns**: Array.<CoordPair> - Array of geo coordinate pairs
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| edgeIndex | H3IndexInput | H3 index of the edge |
| [formatAsGeoJson] | boolean | Whether to provide GeoJSON output: [lng, lat] |

* * *

### h3.gridDistance(origin, destination) ⇒ number
Get the grid distance between two hex indexes. This function may fail
to find the distance between two indexes if they are very far apart or
on opposite sides of a pentagon.

**Returns**: number - Distance between hexagons
**Throws**:

- H3Error If input is invalid or the distance could not be calculated

| Param | Type | Description |
| --- | --- | --- |
| origin | H3IndexInput | Origin hexagon index |
| destination | H3IndexInput | Destination hexagon index |

* * *

### h3.gridPathCells(origin, destination) ⇒ Array.<H3Index>
Given two H3 indexes, return the line of indexes between them (inclusive).

This function may fail to find the line between two indexes, for
example if they are very far apart. It may also fail when finding
distances for indexes on opposite sides of a pentagon.

Notes:

- The specific output of this function should not be considered stable
across library versions. The only guarantees the library provides are
that the line length will be `h3Distance(start, end) + 1` and that
every index in the line will be a neighbor of the preceding index.
- Lines are drawn in grid space, and may not correspond exactly to either
Cartesian lines or great arcs.

**Returns**: Array.<H3Index> - H3 indexes connecting origin and destination
**Throws**:

- H3Error If input is invalid or the line cannot be calculated

| Param | Type | Description |
| --- | --- | --- |
| origin | H3IndexInput | Origin hexagon index |
| destination | H3IndexInput | Destination hexagon index |

* * *

### h3.cellToLocalIj(origin, destination) ⇒ CoordIJ
Produces IJ coordinates for an H3 index anchored by an origin.

- The coordinate space used by this function may have deleted
regions or warping due to pentagonal distortion.
- Coordinates are only comparable if they come from the same
origin index.
- Failure may occur if the index is too far away from the origin
or if the index is on the other side of a pentagon.
- This function is experimental, and its output is not guaranteed
to be compatible across different versions of H3.

**Returns**: CoordIJ - Coordinates as an `{i, j}` pair
**Throws**:

- H3Error If the IJ coordinates cannot be calculated

| Param | Type | Description |
| --- | --- | --- |
| origin | H3IndexInput | Origin H3 index |
| destination | H3IndexInput | H3 index for which to find relative coordinates |

* * *

### h3.localIjToCell(origin, coords) ⇒ H3Index
Produces an H3 index for IJ coordinates anchored by an origin.

- The coordinate space used by this function may have deleted
regions or warping due to pentagonal distortion.
- Coordinates are only comparable if they come from the same
origin index.
- Failure may occur if the index is too far away from the origin
or if the index is on the other side of a pentagon.
- This function is experimental, and its output is not guaranteed
to be compatible across different versions of H3.

**Returns**: H3Index - H3 index at the relative coordinates
**Throws**:

- H3Error If the H3 index cannot be calculated

| Param | Type | Description |
| --- | --- | --- |
| origin | H3IndexInput | Origin H3 index |
| coords | CoordIJ | Coordinates as an `{i, j}` pair |

* * *

### h3.greatCircleDistance(latLng1, latLng2, unit) ⇒ number
Great circle distance between two geo points. This is not specific to H3,
but is implemented in the library and provided here as a convenience.

**Returns**: number - Great circle distance
**Throws**:

- H3Error If the unit is invalid

| Param | Type | Description |
| --- | --- | --- |
| latLng1 | Array.<number> | Origin coordinate as [lat, lng] |
| latLng2 | Array.<number> | Destination coordinate as [lat, lng] |
| unit | string | Distance unit (either UNITS.m, UNITS.km, or UNITS.rads) |

* * *

### h3.cellArea(h3Index, unit) ⇒ number
Exact area of a given cell

**Returns**: number - Cell area
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | H3 index of the hexagon to measure |
| unit | string | Distance unit (either UNITS.m2, UNITS.km2, or UNITS.rads2) |

* * *

### h3.edgeLength(edge, unit) ⇒ number
Calculate length of a given unidirectional edge

**Returns**: number - Cell area
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| edge | H3IndexInput | H3 index of the edge to measure |
| unit | string | Distance unit (either UNITS.m, UNITS.km, or UNITS.rads) |

* * *

### h3.getHexagonAreaAvg(res, unit) ⇒ number
Average hexagon area at a given resolution

**Returns**: number - Average area
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| res | number | Hexagon resolution |
| unit | string | Area unit (either UNITS.m2, UNITS.km2, or UNITS.rads2) |

* * *

### h3.getHexagonEdgeLengthAvg(res, unit) ⇒ number
Average hexagon edge length at a given resolution

**Returns**: number - Average edge length
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| res | number | Hexagon resolution |
| unit | string | Distance unit (either UNITS.m, UNITS.km, or UNITS.rads) |

* * *

### h3.cellToVertex(h3Index, vertexNum) ⇒ H3Index
Find the index for a vertex of a cell.

**Returns**: H3Index - Vertex index
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | Cell to find the vertex for |
| vertexNum | number | Number (index) of the vertex to calculate |

* * *

### h3.cellToVertexes(h3Index) ⇒ Array.<H3Index>
Find the indexes for all vertexes of a cell.

**Returns**: Array.<H3Index> - All vertex indexes of this cell
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | Cell to find all vertexes for |

* * *

### h3.vertexToLatLng(h3Index) ⇒ CoordPair
Get the lat, lng of a given vertex

**Returns**: CoordPair - Latitude, longitude coordinates of the vertex
**Throws**:

- H3Error If the input is invalid

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | A vertex index |

* * *

### h3.isValidVertex(h3Index) ⇒ boolean
Returns true if the input is a valid vertex index.

**Returns**: boolean - True if the index represents a vertex

| Param | Type | Description |
| --- | --- | --- |
| h3Index | H3IndexInput | An index to test for being a vertex index |

* * *

### h3.getNumCells(res) ⇒ number
The total count of hexagons in the world at a given resolution. Note that above
resolution 8 the exact count cannot be represented in a JavaScript 32-bit number,
so consumers should use caution when applying further operations to the output.

**Returns**: number - Count
**Throws**:

- H3Error If the resolution is invalid

| Param | Type | Description |
| --- | --- | --- |
| res | number | Hexagon resolution |

* * *

### h3.getRes0Cells() ⇒ Array.<H3Index>
Get all H3 indexes at resolution 0. As every index at every resolution > 0 is
the descendant of a res 0 index, this can be used with h3ToChildren to iterate
over H3 indexes at any resolution.

**Returns**: Array.<H3Index> - All H3 indexes at res 0

* * *

### h3.getPentagons(res) ⇒ Array.<H3Index>
Get the twelve pentagon indexes at a given resolution.

**Returns**: Array.<H3Index> - All H3 pentagon indexes at res
**Throws**:

- H3Error If the resolution is invalid

| Param | Type | Description |
| --- | --- | --- |
| res | number | Hexagon resolution |

* * *

### h3.degsToRads(deg) ⇒ number
Convert degrees to radians

**Returns**: number - Value in radians

| Param | Type | Description |
| --- | --- | --- |
| deg | number | Value in degrees |

* * *

### h3.radsToDegs(rad) ⇒ number
Convert radians to degrees

**Returns**: number - Value in degrees

| Param | Type | Description |
| --- | --- | --- |
| rad | number | Value in radians |

* * *

### h3.H3Index : string
64-bit hexidecimal string representation of an H3 index

* * *

### h3.H3IndexInput : string \| Array.<number>
64-bit hexidecimal string representation of an H3 index,
or two 32-bit integers in little endian order in an array.

* * *

### h3.CoordIJ
Coordinates as an `{i, j}` pair

**Properties**

| Name | Type |
| --- | --- |
| i | number |
| j | number |

* * *

### h3.H3Error
Custom JS Error instance with an attached error code. Error codes come from the
core H3 library and can be found [in the H3 docs](https://h3geo.org/docs/library/errors#table-of-error-codes).

**Properties**

| Name | Type |
| --- | --- |
| message | string |
| code | number |

* * *

### h3.CoordPair : Array.<number>
Pair of lat,lng coordinates (or lng,lat if GeoJSON output is specified)

* * *

### h3.SplitLong : Array.<number>
Pair of lower,upper 32-bit ints representing a 64-bit value

* * *

## Legacy API

H3 v4 renamed the majority of the functions in the library. To help ease migration from H3 v3 to H3 v4, we offer a legacy API wrapper at `h3-js/legacy`, which exports the v4 functions with the v3 names. Users are welcome to use the legacy API wrapper as a transitional support, but are encouraged to upgrade to the H3 v4 API as soon as possible.

Note that the legacy API is _not_ 100% backwards compatible - it's a thin wrapper on top of the v4 functions, so in cases where behavior has changed, the v4 behavior will be used. In particular, many of the v4 functions will throw errors for invalid input, where v3 functions would return null.

Installation:

```
npm install h3-js
```

Usage:

```
import {geoToH3} from 'h3-js/legacy';

const h3Index = geoToH3(37.3615593, -122.0553238, 7);
```

## Development

The `h3-js` library uses `yarn` as the preferred package manager. To install the dev dependencies, just run:

yarn

To lint the code:

yarn lint

To run the tests:

yarn test

Code must be formatted with `prettier`; unformatted code will fail the build. To format all files:

yarn prettier

### Benchmarks

The `h3-js` library includes a basic benchmark suite using [Benchmark.js](https://benchmarkjs.com/). Because many of the functions may be called over thousands of hexagons in a "hot loop", performance is an important concern. Benchmarks are run against the transpiled ES5 code by default.

To run the benchmarks in Node:

yarn benchmark-node

To run the benchmarks in a browser:

yarn benchmark-browser

Sample Node output (Macbook Pro running Node 12):

```
isValidCell x 3,650,995 ops/sec ±1.67% (87 runs sampled)
latLngToCell x 429,982 ops/sec ±1.39% (86 runs sampled)
cellToLatLng x 1,161,867 ops/sec ±1.11% (84 runs sampled)
cellToLatLng - integers x 1,555,791 ops/sec ±1.29% (86 runs sampled)
cellToBoundary x 375,938 ops/sec ±1.25% (87 runs sampled)
cellToBoundary - integers x 377,181 ops/sec ±1.18% (85 runs sampled)
getIcosahedronFaces x 992,946 ops/sec ±1.13% (85 runs sampled)
gridDisk x 194,400 ops/sec ±1.16% (85 runs sampled)
polygonToCells_9 x 4,919 ops/sec ±0.79% (87 runs sampled)
polygonToCells_11 x 368 ops/sec ±0.76% (86 runs sampled)
polygonToCells_10ring x 76.88 ops/sec ±0.57% (68 runs sampled)
cellsToMultiPolygon x 760 ops/sec ±1.06% (86 runs sampled)
compactCells x 2,466 ops/sec ±0.75% (86 runs sampled)
uncompactCells x 715 ops/sec ±1.15% (85 runs sampled)
areNeighborCells x 1,073,086 ops/sec ±1.56% (89 runs sampled)
cellsToDirectedEdge x 692,172 ops/sec ±1.06% (86 runs sampled)
getDirectedEdgeOrigin x 995,390 ops/sec ±1.60% (85 runs sampled)
getDirectedEdgeDestination x 930,473 ops/sec ±1.11% (86 runs sampled)
isValidDirectedEdge x 3,505,407 ops/sec ±1.05% (87 runs sampled)
```

When making code changes that may affect performance, please run benchmarks against `master` and then against your branch to identify any regressions.

### Transpiling the C Source

The core library is transpiled using [emscripten](http://kripken.github.io/emscripten-site). The easiest way to build from source locally is by using Docker. Make sure Docker is installed, then:

yarn docker-boot
yarn build-emscripten

The build script uses the `H3_VERSION` file to determine the version of the core library to build. To use a different version of the library (e.g. to test local changes), clone the desired H3 repo to `./h3c` and then run `yarn docker-emscripten`.

## Contributing

Pull requests and [Github issues](https://github.com/uber/h3-js/issues) are welcome. Please include tests for new work, and keep the library test coverage at 100%. Please note that the purpose of this module is to expose the API of the [H3 Core library](https://github.com/uber/h3), so we will rarely accept new features that are not part of that API. New proposed feature work is more appropriate in the core C library or in a new JS library that depends on `h3-js`.

Before we can merge your changes, you must agree to the [Uber Contributor License Agreement](http://cla-assistant.io/uber/h3-js).

## Versioning

The [H3 core library](https://github.com/uber/h3) adheres to [Semantic Versioning](http://semver.org/). The `h3-js` library has a `major.minor.patch` version scheme. The major and minor version numbers of `h3-js` are the major and minor version of the bound core library, respectively. The patch version is incremented independently of the core library.

## Legal and Licensing

The `h3-js` library is licensed under the [Apache 2.0 License](https://github.com/uber/h3-js/blob/master/LICENSE).

DGGRID Copyright (c) 2015 Southern Oregon University