Skip to content

Commit ccb06c4

Browse files
authored
Merge pull request #198 from isaacbrodsky/bump-4.2.0
Upgrade to h3 v4.2.0
2 parents 271bf5b + 1afc176 commit ccb06c4

File tree

9 files changed

+523
-23
lines changed

9 files changed

+523
-23
lines changed

H3_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.1.0
1+
4.2.0

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ const coordinates = h3.cellsToMultiPolygon(hexagons, true);
9999

100100
* [h3](#module_h3)
101101
* [.UNITS](#module_h3.UNITS)
102+
* [.POLYGON_TO_CELLS_FLAGS](#module_h3.POLYGON_TO_CELLS_FLAGS)
102103
* [.h3IndexToSplitLong(h3Index)](#module_h3.h3IndexToSplitLong) ⇒ <code>SplitLong</code>
103104
* [.splitLongToH3Index(lower, upper)](#module_h3.splitLongToH3Index) ⇒ <code>H3Index</code>
104105
* [.isValidCell(h3Index)](#module_h3.isValidCell) ⇒ <code>boolean</code>
@@ -120,6 +121,7 @@ const coordinates = h3.cellsToMultiPolygon(hexagons, true);
120121
* [.gridDiskDistances(h3Index, ringSize)](#module_h3.gridDiskDistances) ⇒ <code>Array.&lt;Array.&lt;H3Index&gt;&gt;</code>
121122
* [.gridRingUnsafe(h3Index, ringSize)](#module_h3.gridRingUnsafe) ⇒ <code>Array.&lt;H3Index&gt;</code>
122123
* [.polygonToCells(coordinates, res, [isGeoJson])](#module_h3.polygonToCells) ⇒ <code>Array.&lt;H3Index&gt;</code>
124+
* [.polygonToCellsExperimental(coordinates, res, flags, [isGeoJson])](#module_h3.polygonToCellsExperimental) ⇒ <code>Array.&lt;H3Index&gt;</code>
123125
* [.cellsToMultiPolygon(h3Indexes, [formatAsGeoJson])](#module_h3.cellsToMultiPolygon) ⇒ <code>Array.&lt;Array.&lt;Array.&lt;CoordPair&gt;&gt;&gt;</code>
124126
* [.compactCells(h3Set)](#module_h3.compactCells) ⇒ <code>Array.&lt;H3Index&gt;</code>
125127
* [.uncompactCells(compactedSet, res)](#module_h3.uncompactCells) ⇒ <code>Array.&lt;H3Index&gt;</code>
@@ -176,6 +178,23 @@ Length/Area units
176178
| rads2 | <code>string</code> |
177179

178180

181+
* * *
182+
183+
<a name="module_h3.POLYGON_TO_CELLS_FLAGS"></a>
184+
185+
### h3.POLYGON\_TO\_CELLS\_FLAGS
186+
Mode flags for polygonToCells
187+
188+
**Properties**
189+
190+
| Name | Type |
191+
| --- | --- |
192+
| containmentCenter | <code>string</code> |
193+
| containmentFull | <code>string</code> |
194+
| containmentOverlapping | <code>string</code> |
195+
| containmentOverlappingBbox | <code>string</code> |
196+
197+
179198
* * *
180199

181200
<a name="module_h3.h3IndexToSplitLong"></a>
@@ -555,6 +574,31 @@ expected to be holes.
555574
| [isGeoJson] | <code>boolean</code> | Whether to expect GeoJson-style [lng, lat] pairs instead of [lat, lng] |
556575

557576

577+
* * *
578+
579+
<a name="module_h3.polygonToCellsExperimental"></a>
580+
581+
### h3.polygonToCellsExperimental(coordinates, res, flags, [isGeoJson]) ⇒ <code>Array.&lt;H3Index&gt;</code>
582+
Get all hexagons with centers contained in a given polygon. The polygon
583+
is specified with GeoJson semantics as an array of loops. Each loop is
584+
an array of [lat, lng] pairs (or [lng, lat] if isGeoJson is specified).
585+
The first loop is the perimeter of the polygon, and subsequent loops are
586+
expected to be holes.
587+
588+
**Returns**: <code>Array.&lt;H3Index&gt;</code> - H3 indexes for all hexagons in polygon
589+
**Throws**:
590+
591+
- <code>H3Error</code> If input is invalid or output is too large for JS
592+
593+
594+
| Param | Type | Description |
595+
| --- | --- | --- |
596+
| coordinates | <code>Array.&lt;Array.&lt;number&gt;&gt;</code> \| <code>Array.&lt;Array.&lt;Array.&lt;number&gt;&gt;&gt;</code> | Array of loops, or a single loop |
597+
| res | <code>number</code> | Resolution of hexagons to return |
598+
| flags | <code>string</code> | Value from POLYGON_TO_CELLS_FLAGS |
599+
| [isGeoJson] | <code>boolean</code> | Whether to expect GeoJson-style [lng, lat] pairs instead of [lat, lng] |
600+
601+
558602
* * *
559603

560604
<a name="module_h3.cellsToMultiPolygon"></a>

lib/bindings.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ export default [
5050
['gridRingUnsafe', H3_ERROR, [H3_LOWER, H3_UPPER, NUMBER, POINTER]],
5151
['maxPolygonToCellsSize', H3_ERROR, [POINTER, RESOLUTION, NUMBER, POINTER]],
5252
['polygonToCells', H3_ERROR, [POINTER, RESOLUTION, NUMBER, POINTER]],
53+
['maxPolygonToCellsSizeExperimental', H3_ERROR, [POINTER, RESOLUTION, NUMBER, POINTER]],
54+
[
55+
'polygonToCellsExperimental',
56+
H3_ERROR,
57+
[POINTER, RESOLUTION, NUMBER, NUMBER, NUMBER, POINTER]
58+
],
5359
['cellsToLinkedMultiPolygon', H3_ERROR, [POINTER, NUMBER, POINTER]],
5460
['destroyLinkedMultiPolygon', null, [POINTER]],
5561
['compactCells', H3_ERROR, [POINTER, POINTER, NUMBER, NUMBER]],

lib/h3core.js

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import {
2828
E_UNKNOWN_UNIT,
2929
E_ARRAY_LENGTH,
3030
E_NULL_INDEX,
31-
E_CELL_INVALID
31+
E_CELL_INVALID,
32+
E_OPTION_INVALID
3233
} from './errors';
3334

3435
/**
@@ -131,9 +132,48 @@ export const UNITS = {
131132
rads2: 'rads2'
132133
};
133134

135+
// ----------------------------------------------------------------------------
136+
// Flags
137+
138+
/**
139+
* Mode flags for polygonToCells
140+
* @static
141+
* @property {string} containmentCenter
142+
* @property {string} containmentFull
143+
* @property {string} containmentOverlapping
144+
* @property {string} containmentOverlappingBbox
145+
*/
146+
export const POLYGON_TO_CELLS_FLAGS = {
147+
containmentCenter: 'containmentCenter',
148+
containmentFull: 'containmentFull',
149+
containmentOverlapping: 'containmentOverlapping',
150+
containmentOverlappingBbox: 'containmentOverlappingBbox'
151+
};
152+
134153
// ----------------------------------------------------------------------------
135154
// Utilities and helpers
136155

156+
/**
157+
* @private
158+
* @param {string} flags Value from POLYGON_TO_CELLS_FLAGS
159+
* @returns {number} Flag value
160+
* @throws {H3Error} If invalid
161+
*/
162+
function polygonToCellsFlagsToNumber(flags) {
163+
switch (flags) {
164+
case POLYGON_TO_CELLS_FLAGS.containmentCenter:
165+
return 0;
166+
case POLYGON_TO_CELLS_FLAGS.containmentFull:
167+
return 1;
168+
case POLYGON_TO_CELLS_FLAGS.containmentOverlapping:
169+
return 2;
170+
case POLYGON_TO_CELLS_FLAGS.containmentOverlappingBbox:
171+
return 3;
172+
default:
173+
throw JSBindingError(E_OPTION_INVALID, flags);
174+
}
175+
}
176+
137177
/**
138178
* Validate a resolution, throwing an error if invalid
139179
* @private
@@ -1029,6 +1069,63 @@ export function polygonToCells(coordinates, res, isGeoJson) {
10291069
}
10301070
}
10311071

1072+
/**
1073+
* Get all hexagons with centers contained in a given polygon. The polygon
1074+
* is specified with GeoJson semantics as an array of loops. Each loop is
1075+
* an array of [lat, lng] pairs (or [lng, lat] if isGeoJson is specified).
1076+
* The first loop is the perimeter of the polygon, and subsequent loops are
1077+
* expected to be holes.
1078+
* @static
1079+
* @param {number[][] | number[][][]} coordinates
1080+
* Array of loops, or a single loop
1081+
* @param {number} res Resolution of hexagons to return
1082+
* @param {string} flags Value from POLYGON_TO_CELLS_FLAGS
1083+
* @param {boolean} [isGeoJson] Whether to expect GeoJson-style [lng, lat]
1084+
* pairs instead of [lat, lng]
1085+
* @return {H3Index[]} H3 indexes for all hexagons in polygon
1086+
* @throws {H3Error} If input is invalid or output is too large for JS
1087+
*/
1088+
export function polygonToCellsExperimental(coordinates, res, flags, isGeoJson) {
1089+
validateRes(res);
1090+
isGeoJson = Boolean(isGeoJson);
1091+
const flagsInt = polygonToCellsFlagsToNumber(flags);
1092+
// Guard against empty input
1093+
if (coordinates.length === 0 || coordinates[0].length === 0) {
1094+
return [];
1095+
}
1096+
// Wrap to expected format if a single loop is provided
1097+
const polygon = typeof coordinates[0][0] === 'number' ? [coordinates] : coordinates;
1098+
const geoPolygon = coordinatesToGeoPolygon(
1099+
// @ts-expect-error - There's no way to convince TS that polygon is now number[][][]
1100+
polygon,
1101+
isGeoJson
1102+
);
1103+
const countPtr = C._malloc(SZ_INT64);
1104+
try {
1105+
throwIfError(H3.maxPolygonToCellsSizeExperimental(geoPolygon, res, flagsInt, countPtr));
1106+
const count = validateArrayLength(readInt64AsDoubleFromPointer(countPtr));
1107+
const hexagons = C._calloc(count, SZ_H3INDEX);
1108+
try {
1109+
throwIfError(
1110+
H3.polygonToCellsExperimental(
1111+
geoPolygon,
1112+
res,
1113+
flagsInt,
1114+
count,
1115+
UNUSED_UPPER_32_BITS,
1116+
hexagons
1117+
)
1118+
);
1119+
return readArrayOfH3Indexes(hexagons, count);
1120+
} finally {
1121+
C._free(hexagons);
1122+
}
1123+
} finally {
1124+
C._free(countPtr);
1125+
destroyGeoPolygon(geoPolygon);
1126+
}
1127+
}
1128+
10321129
/**
10331130
* Get the outlines of a set of H3 hexagons, returned in GeoJSON MultiPolygon
10341131
* format (an array of polygons, each with an array of loops, each an array of

out/binding-functions

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
describeH3Error
12
latLngToCell
23
cellToLatLng
34
cellToBoundary
45
gridDisk
56
gridDiskDistances
67
gridRingUnsafe
78
polygonToCells
9+
polygonToCellsExperimental
810
cellsToMultiPolygon
911
degsToRads
1012
radsToDegs

out/libh3.js

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Pure-Javascript version of the H3 library, a hexagon-based geographic grid system",
55
"author": "Nick Rabinowitz <nickr@uber.com>",
66
"contributors": [
7-
"David Ellis <isv.damocles@gmail.com>"
7+
"David Ellis <isv.damocles@gmail.com>",
8+
"Isaac Brodsky <isaac@isaacbrodsky.com>"
89
],
910
"license": "Apache-2.0",
1011
"repository": {

0 commit comments

Comments
 (0)