Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file. This library adheres to a versioning policy described in [the README](./README.md#versioning). The public API of this library consists of the functions exported in [h3core.js](./lib/h3core.js).

## Unreleased
### Added
- Added `reverseDirectedEdge` function
### Changed
- Updated the core library to `v4.5.0`
### Fixed
- Prevent oversized `gridDisk` and `gridDiskDistances` calls from corrupting subsequent calls.

Expand Down
2 changes: 1 addition & 1 deletion H3_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.4.1
4.5.0
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![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.4.1&color=blue)](https://github.com/uber/h3/releases/tag/v4.4.1)
[![H3 Version](https://img.shields.io/static/v1?label=h3%20api&message=v4.5.0&color=blue)](https://github.com/uber/h3/releases/tag/v4.5.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.

Expand Down Expand Up @@ -137,6 +137,7 @@ const coordinates = h3.cellsToMultiPolygon(hexagons, true);
* [.directedEdgeToCells(edgeIndex)](#module_h3.directedEdgeToCells) ⇒ <code>Array.&lt;H3Index&gt;</code>
* [.originToDirectedEdges(h3Index)](#module_h3.originToDirectedEdges) ⇒ <code>Array.&lt;H3Index&gt;</code>
* [.directedEdgeToBoundary(edgeIndex, [formatAsGeoJson])](#module_h3.directedEdgeToBoundary) ⇒ <code>Array.&lt;CoordPair&gt;</code>
* [.reverseDirectedEdge(edgeIndex)](#module_h3.reverseDirectedEdge) ⇒ <code>H3Index</code>
* [.gridDistance(origin, destination)](#module_h3.gridDistance) ⇒ <code>number</code>
* [.gridPathCells(origin, destination)](#module_h3.gridPathCells) ⇒ <code>Array.&lt;H3Index&gt;</code>
* [.cellToLocalIj(origin, destination)](#module_h3.cellToLocalIj) ⇒ <code>CoordIJ</code>
Expand Down Expand Up @@ -887,6 +888,24 @@ cross the edge of an icosahedron face, this may return 3 coordinates.
| [formatAsGeoJson] | <code>boolean</code> | Whether to provide GeoJSON output: [lng, lat] |


* * *

<a name="module_h3.reverseDirectedEdge"></a>

### h3.reverseDirectedEdge(edgeIndex) ⇒ <code>H3Index</code>
Returns the directed edge with origin and destination cells reversed

**Returns**: <code>H3Index</code> - Directed edge, reversed
**Throws**:

- <code>H3Error</code> If input is invalid


| Param | Type | Description |
| --- | --- | --- |
| edgeIndex | <code>H3IndexInput</code> | Directed edge index |


* * *

<a name="module_h3.gridDistance"></a>
Expand Down
2 changes: 1 addition & 1 deletion doc-files/README.tmpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![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.4.1&color=blue)](https://github.com/uber/h3/releases/tag/v4.4.1)
[![H3 Version](https://img.shields.io/static/v1?label=h3%20api&message=v4.5.0&color=blue)](https://github.com/uber/h3/releases/tag/v4.5.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.

Expand Down
1 change: 1 addition & 0 deletions lib/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export default [
['directedEdgeToCells', H3_ERROR, [H3_LOWER, H3_UPPER, POINTER]],
['originToDirectedEdges', H3_ERROR, [H3_LOWER, H3_UPPER, POINTER]],
['directedEdgeToBoundary', H3_ERROR, [H3_LOWER, H3_UPPER, POINTER]],
['reverseDirectedEdge', H3_ERROR, [H3_LOWER, H3_UPPER, POINTER]],
['gridDistance', H3_ERROR, [H3_LOWER, H3_UPPER, H3_LOWER, H3_UPPER, POINTER]],
['gridPathCells', H3_ERROR, [H3_LOWER, H3_UPPER, H3_LOWER, H3_UPPER, POINTER]],
['gridPathCellsSize', H3_ERROR, [H3_LOWER, H3_UPPER, H3_LOWER, H3_UPPER, POINTER]],
Expand Down
19 changes: 19 additions & 0 deletions lib/h3core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,25 @@ export function directedEdgeToBoundary(edgeIndex, formatAsGeoJson) {
}
}

/**
* Returns the directed edge with origin and destination cells reversed
* @static
* @param {H3IndexInput} edgeIndex Directed edge index
* @return {H3Index} Directed edge, reversed
* @throws {H3Error} If input is invalid
*/
export function reverseDirectedEdge(edgeIndex) {
const [lower, upper] = h3IndexToSplitLong(edgeIndex);

const out = C._malloc(SZ_H3INDEX);
try {
throwIfError(H3.reverseDirectedEdge(lower, upper, out));
return validateH3Index(readH3IndexFromPointer(out));
} finally {
C._free(out);
}
}

/**
* 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
Expand Down
1 change: 1 addition & 0 deletions out/binding-functions
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ getDirectedEdgeDestination
directedEdgeToCells
originToDirectedEdges
directedEdgeToBoundary
reverseDirectedEdge
cellToVertex
cellToVertexes
vertexToLatLng
Expand Down
12 changes: 6 additions & 6 deletions out/libh3.js

Large diffs are not rendered by default.

33 changes: 32 additions & 1 deletion test/h3core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ test('cellsToMultiPolygon - Non-contiguous 2', assert => {
const multiPolygon = h3.cellsToMultiPolygon(h3Indexes);
const vertices0 = h3.cellToBoundary(h3Indexes[0]);
const vertices1 = h3.cellToBoundary(h3Indexes[1]);
const expected = [[vertices0], [vertices1]];
const expected = [[vertices1], [vertices0]];

assertMultiPolygon(assert, multiPolygon, expected);

Expand Down Expand Up @@ -2215,6 +2215,37 @@ test('directedEdgeToBoundary - 10-vertex pentagon', assert => {
assert.end();
});

test('reverseDirectedEdge', assert => {
const origin = '85283473fffffff';
const edges = h3.originToDirectedEdges(origin);

edges.forEach((edge, i) => {
const reversed = h3.reverseDirectedEdge(edge);
const destination = h3.getDirectedEdgeDestination(reversed);
assert.equals(destination, origin, 'Destination is as expected');
});

assert.end();
});

test('reverseDirectedEdge', assert => {
assert.throws(
() => {
h3.reverseDirectedEdge('-1');
},
{code: E_DIR_EDGE_INVALID},
'Invalid input'
);
assert.throws(
() => {
h3.reverseDirectedEdge('q');
},
{code: E_DIR_EDGE_INVALID},
'Invalid input (2)'
);
assert.end();
});

test('gridDistance', assert => {
const origin = h3.latLngToCell(37.5, -122, 9);
for (let radius = 0; radius < 4; radius++) {
Expand Down
2 changes: 1 addition & 1 deletion test/legacy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ test('h3SetToMultiPolygon - Non-contiguous 2', assert => {
const multiPolygon = h3.h3SetToMultiPolygon(h3Indexes);
const vertices0 = h3.h3ToGeoBoundary(h3Indexes[0]);
const vertices1 = h3.h3ToGeoBoundary(h3Indexes[1]);
const expected = [[vertices0], [vertices1]];
const expected = [[vertices1], [vertices0]];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change makes a kind of sense to me; what we actually wanted to test was that the Set of vertices in multiPolygon matches the expected set, but the order has now changed due to a different algorithm.


assertMultiPolygon(assert, multiPolygon, expected);

Expand Down
Loading