1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
/*
* Copyright 2016-2020 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @file h3api.h
* @brief Primary H3 core library entry points.
*
* This file defines the public API of the H3 library. Incompatible changes to
* these functions require the library's major version be increased.
*/
#ifndef H3API_H
#define H3API_H
/*
* Preprocessor code to support renaming (prefixing) the public API.
* All public functions should be wrapped in H3_EXPORT so they can be
* renamed.
*/
#ifdef H3_PREFIX
#define XTJOIN(a, b) a##b
#define TJOIN(a, b) XTJOIN(a, b)
/* export joins the user provided prefix with our exported function name */
#define H3_EXPORT(name) TJOIN(H3_PREFIX, name)
#else
#define H3_EXPORT(name) name
#endif
/* For uint64_t */
#include <stdint.h>
/* For size_t */
#include <stdlib.h>
/*
* H3 is compiled as C, not C++ code. `extern "C"` is needed for C++ code
* to be able to use the library.
*/
#ifdef __cplusplus
extern "C" {
#endif
/** @brief the H3Index fits within a 64-bit unsigned integer */
typedef uint64_t H3Index;
/* library version numbers generated from VERSION file */
// clang-format off
#define H3_VERSION_MAJOR 3
#define H3_VERSION_MINOR 7
#define H3_VERSION_PATCH 2
// clang-format on
/** Maximum number of cell boundary vertices; worst case is pentagon:
* 5 original verts + 5 edge crossings
*/
#define MAX_CELL_BNDRY_VERTS 10
/** @struct GeoCoord
@brief latitude/longitude in radians
*/
typedef struct {
double lat; ///< latitude in radians
double lon; ///< longitude in radians
} GeoCoord;
/** @struct GeoBoundary
@brief cell boundary in latitude/longitude
*/
typedef struct {
int numVerts; ///< number of vertices
GeoCoord verts[MAX_CELL_BNDRY_VERTS]; ///< vertices in ccw order
} GeoBoundary;
/** @struct Geofence
* @brief similar to GeoBoundary, but requires more alloc work
*/
typedef struct {
int numVerts;
GeoCoord *verts;
} Geofence;
/** @struct GeoPolygon
* @brief Simplified core of GeoJSON Polygon coordinates definition
*/
typedef struct {
Geofence geofence; ///< exterior boundary of the polygon
int numHoles; ///< number of elements in the array pointed to by holes
Geofence *holes; ///< interior boundaries (holes) in the polygon
} GeoPolygon;
/** @struct GeoMultiPolygon
* @brief Simplified core of GeoJSON MultiPolygon coordinates definition
*/
typedef struct {
int numPolygons;
GeoPolygon *polygons;
} GeoMultiPolygon;
/** @struct LinkedGeoCoord
* @brief A coordinate node in a linked geo structure, part of a linked list
*/
typedef struct LinkedGeoCoord LinkedGeoCoord;
struct LinkedGeoCoord {
GeoCoord vertex;
LinkedGeoCoord *next;
};
/** @struct LinkedGeoLoop
* @brief A loop node in a linked geo structure, part of a linked list
*/
typedef struct LinkedGeoLoop LinkedGeoLoop;
struct LinkedGeoLoop {
LinkedGeoCoord *first;
LinkedGeoCoord *last;
LinkedGeoLoop *next;
};
/** @struct LinkedGeoPolygon
* @brief A polygon node in a linked geo structure, part of a linked list.
*/
typedef struct LinkedGeoPolygon LinkedGeoPolygon;
struct LinkedGeoPolygon {
LinkedGeoLoop *first;
LinkedGeoLoop *last;
LinkedGeoPolygon *next;
};
/** @struct CoordIJ
* @brief IJ hexagon coordinates
*
* Each axis is spaced 120 degrees apart.
*/
typedef struct {
int i; ///< i component
int j; ///< j component
} CoordIJ;
/** @defgroup geoToH3 geoToH3
* Functions for geoToH3
* @{
*/
/** @brief find the H3 index of the resolution res cell containing the lat/lng
*/
H3Index H3_EXPORT(geoToH3)(const GeoCoord *g, int res);
/** @} */
/** @defgroup h3ToGeo h3ToGeo
* Functions for h3ToGeo
* @{
*/
/** @brief find the lat/lon center point g of the cell h3 */
void H3_EXPORT(h3ToGeo)(H3Index h3, GeoCoord *g);
/** @} */
/** @defgroup h3ToGeoBoundary h3ToGeoBoundary
* Functions for h3ToGeoBoundary
* @{
*/
/** @brief give the cell boundary in lat/lon coordinates for the cell h3 */
void H3_EXPORT(h3ToGeoBoundary)(H3Index h3, GeoBoundary *gp);
/** @} */
/** @defgroup kRing kRing
* Functions for kRing
* @{
*/
/** @brief maximum number of hexagons in k-ring */
int H3_EXPORT(maxKringSize)(int k);
/** @brief hexagons neighbors in all directions, assuming no pentagons */
int H3_EXPORT(hexRange)(H3Index origin, int k, H3Index *out);
/** @} */
/** @brief hexagons neighbors in all directions, assuming no pentagons,
* reporting distance from origin */
int H3_EXPORT(hexRangeDistances)(H3Index origin, int k, H3Index *out,
int *distances);
/** @brief collection of hex rings sorted by ring for all given hexagons */
int H3_EXPORT(hexRanges)(H3Index *h3Set, int length, int k, H3Index *out);
/** @brief hexagon neighbors in all directions */
void H3_EXPORT(kRing)(H3Index origin, int k, H3Index *out);
/** @} */
/** @defgroup kRingDistances kRingDistances
* Functions for kRingDistances
* @{
*/
/** @brief hexagon neighbors in all directions, reporting distance from origin
*/
void H3_EXPORT(kRingDistances)(H3Index origin, int k, H3Index *out,
int *distances);
/** @} */
/** @defgroup hexRing hexRing
* Functions for hexRing
* @{
*/
/** @brief hollow hexagon ring at some origin */
int H3_EXPORT(hexRing)(H3Index origin, int k, H3Index *out);
/** @} */
/** @defgroup polyfill polyfill
* Functions for polyfill
* @{
*/
/** @brief maximum number of hexagons in the geofence */
int H3_EXPORT(maxPolyfillSize)(const GeoPolygon *geoPolygon, int res);
/** @brief hexagons within the given geofence */
void H3_EXPORT(polyfill)(const GeoPolygon *geoPolygon, int res, H3Index *out);
/** @} */
/** @defgroup h3SetToMultiPolygon h3SetToMultiPolygon
* Functions for h3SetToMultiPolygon (currently a binding-only concept)
* @{
*/
/** @brief Create a LinkedGeoPolygon from a set of contiguous hexagons */
void H3_EXPORT(h3SetToLinkedGeo)(const H3Index *h3Set, const int numHexes,
LinkedGeoPolygon *out);
/** @brief Free all memory created for a LinkedGeoPolygon */
void H3_EXPORT(destroyLinkedPolygon)(LinkedGeoPolygon *polygon);
/** @} */
/** @defgroup degsToRads degsToRads
* Functions for degsToRads
* @{
*/
/** @brief converts degrees to radians */
double H3_EXPORT(degsToRads)(double degrees);
/** @} */
/** @defgroup radsToDegs radsToDegs
* Functions for radsToDegs
* @{
*/
/** @brief converts radians to degrees */
double H3_EXPORT(radsToDegs)(double radians);
/** @} */
/** @defgroup pointDist pointDist
* Functions for pointDist
* @{
*/
/** @brief "great circle distance" between pairs of GeoCoord points in radians*/
double H3_EXPORT(pointDistRads)(const GeoCoord *a, const GeoCoord *b);
/** @brief "great circle distance" between pairs of GeoCoord points in
* kilometers*/
double H3_EXPORT(pointDistKm)(const GeoCoord *a, const GeoCoord *b);
/** @brief "great circle distance" between pairs of GeoCoord points in meters*/
double H3_EXPORT(pointDistM)(const GeoCoord *a, const GeoCoord *b);
/** @} */
/** @defgroup hexArea hexArea
* Functions for hexArea
* @{
*/
/** @brief average hexagon area in square kilometers (excludes pentagons) */
double H3_EXPORT(hexAreaKm2)(int res);
/** @brief average hexagon area in square meters (excludes pentagons) */
double H3_EXPORT(hexAreaM2)(int res);
/** @} */
/** @defgroup cellArea cellArea
* Functions for cellArea
* @{
*/
/** @brief exact area for a specific cell (hexagon or pentagon) in radians^2 */
double H3_EXPORT(cellAreaRads2)(H3Index h);
/** @brief exact area for a specific cell (hexagon or pentagon) in kilometers^2
*/
double H3_EXPORT(cellAreaKm2)(H3Index h);
/** @brief exact area for a specific cell (hexagon or pentagon) in meters^2 */
double H3_EXPORT(cellAreaM2)(H3Index h);
/** @} */
/** @defgroup edgeLength edgeLength
* Functions for edgeLength
* @{
*/
/** @brief average hexagon edge length in kilometers (excludes pentagons) */
double H3_EXPORT(edgeLengthKm)(int res);
/** @brief average hexagon edge length in meters (excludes pentagons) */
double H3_EXPORT(edgeLengthM)(int res);
/** @} */
/** @defgroup exactEdgeLength exactEdgeLength
* Functions for exactEdgeLength
* @{
*/
/** @brief exact length for a specific unidirectional edge in radians*/
double H3_EXPORT(exactEdgeLengthRads)(H3Index edge);
/** @brief exact length for a specific unidirectional edge in kilometers*/
double H3_EXPORT(exactEdgeLengthKm)(H3Index edge);
/** @brief exact length for a specific unidirectional edge in meters*/
double H3_EXPORT(exactEdgeLengthM)(H3Index edge);
/** @} */
/** @defgroup numHexagons numHexagons
* Functions for numHexagons
* @{
*/
/** @brief number of cells (hexagons and pentagons) for a given resolution */
int64_t H3_EXPORT(numHexagons)(int res);
/** @} */
/** @defgroup getRes0Indexes getRes0Indexes
* Functions for getRes0Indexes
* @{
*/
/** @brief returns the number of resolution 0 cells (hexagons and pentagons) */
int H3_EXPORT(res0IndexCount)();
/** @brief provides all base cells in H3Index format*/
void H3_EXPORT(getRes0Indexes)(H3Index *out);
/** @} */
/** @defgroup getPentagonIndexes getPentagonIndexes
* Functions for getPentagonIndexes
* @{
*/
/** @brief returns the number of pentagons per resolution */
int H3_EXPORT(pentagonIndexCount)();
/** @brief generates all pentagons at the specified resolution */
void H3_EXPORT(getPentagonIndexes)(int res, H3Index *out);
/** @} */
/** @defgroup h3GetResolution h3GetResolution
* Functions for h3GetResolution
* @{
*/
/** @brief returns the resolution of the provided H3 index
* Works on both cells and unidirectional edges. */
int H3_EXPORT(h3GetResolution)(H3Index h);
/** @} */
/** @defgroup h3GetBaseCell h3GetBaseCell
* Functions for h3GetBaseCell
* @{
*/
/** @brief returns the base cell "number" (0 to 121) of the provided H3 cell
*
* Note: Technically works on H3 edges, but will return base cell of the
* origin cell. */
int H3_EXPORT(h3GetBaseCell)(H3Index h);
/** @} */
/** @defgroup stringToH3 stringToH3
* Functions for stringToH3
* @{
*/
/** @brief converts the canonical string format to H3Index format */
H3Index H3_EXPORT(stringToH3)(const char *str);
/** @} */
/** @defgroup h3ToString h3ToString
* Functions for h3ToString
* @{
*/
/** @brief converts an H3Index to a canonical string */
void H3_EXPORT(h3ToString)(H3Index h, char *str, size_t sz);
/** @} */
/** @defgroup h3IsValid h3IsValid
* Functions for h3IsValid
* @{
*/
/** @brief confirms if an H3Index is a valid cell (hexagon or pentagon)
* In particular, returns 0 (False) for H3 unidirectional edges or invalid data
*/
int H3_EXPORT(h3IsValid)(H3Index h);
/** @} */
/** @defgroup h3ToParent h3ToParent
* Functions for h3ToParent
* @{
*/
/** @brief returns the parent (or grandparent, etc) hexagon of the given hexagon
*/
H3Index H3_EXPORT(h3ToParent)(H3Index h, int parentRes);
/** @} */
/** @defgroup h3ToChildren h3ToChildren
* Functions for h3ToChildren
* @{
*/
/** @brief determines the maximum number of children (or grandchildren, etc)
* that could be returned for the given hexagon */
int H3_EXPORT(maxH3ToChildrenSize)(H3Index h, int childRes);
/** @brief provides the children (or grandchildren, etc) of the given hexagon */
void H3_EXPORT(h3ToChildren)(H3Index h, int childRes, H3Index *children);
/** @} */
/** @defgroup h3ToCenterChild h3ToCenterChild
* Functions for h3ToCenterChild
* @{
*/
/** @brief returns the center child of the given hexagon at the specified
* resolution */
H3Index H3_EXPORT(h3ToCenterChild)(H3Index h, int childRes);
/** @} */
/** @defgroup compact compact
* Functions for compact
* @{
*/
/** @brief compacts the given set of hexagons as best as possible */
int H3_EXPORT(compact)(const H3Index *h3Set, H3Index *compactedSet,
const int numHexes);
/** @} */
/** @defgroup uncompact uncompact
* Functions for uncompact
* @{
*/
/** @brief determines the maximum number of hexagons that could be uncompacted
* from the compacted set */
int H3_EXPORT(maxUncompactSize)(const H3Index *compactedSet, const int numHexes,
const int res);
/** @brief uncompacts the compacted hexagon set */
int H3_EXPORT(uncompact)(const H3Index *compactedSet, const int numHexes,
H3Index *h3Set, const int maxHexes, const int res);
/** @} */
/** @defgroup h3IsResClassIII h3IsResClassIII
* Functions for h3IsResClassIII
* @{
*/
/** @brief determines if a hexagon is Class III (or Class II) */
int H3_EXPORT(h3IsResClassIII)(H3Index h);
/** @} */
/** @defgroup h3IsPentagon h3IsPentagon
* Functions for h3IsPentagon
* @{
*/
/** @brief determines if an H3 cell is a pentagon */
int H3_EXPORT(h3IsPentagon)(H3Index h);
/** @} */
/** @defgroup h3GetFaces h3GetFaces
* Functions for h3GetFaces
* @{
*/
/** @brief Max number of icosahedron faces intersected by an index */
int H3_EXPORT(maxFaceCount)(H3Index h3);
/** @brief Find all icosahedron faces intersected by a given H3 index */
void H3_EXPORT(h3GetFaces)(H3Index h3, int *out);
/** @} */
/** @defgroup h3IndexesAreNeighbors h3IndexesAreNeighbors
* Functions for h3IndexesAreNeighbors
* @{
*/
/** @brief returns whether or not the provided hexagons border */
int H3_EXPORT(h3IndexesAreNeighbors)(H3Index origin, H3Index destination);
/** @} */
/** @defgroup getH3UnidirectionalEdge getH3UnidirectionalEdge
* Functions for getH3UnidirectionalEdge
* @{
*/
/** @brief returns the unidirectional edge H3Index for the specified origin and
* destination */
H3Index H3_EXPORT(getH3UnidirectionalEdge)(H3Index origin, H3Index destination);
/** @} */
/** @defgroup h3UnidirectionalEdgeIsValid h3UnidirectionalEdgeIsValid
* Functions for h3UnidirectionalEdgeIsValid
* @{
*/
/** @brief returns whether the H3Index is a valid unidirectional edge */
int H3_EXPORT(h3UnidirectionalEdgeIsValid)(H3Index edge);
/** @} */
/** @defgroup getOriginH3IndexFromUnidirectionalEdge \
* getOriginH3IndexFromUnidirectionalEdge
* Functions for getOriginH3IndexFromUnidirectionalEdge
* @{
*/
/** @brief Returns the origin hexagon H3Index from the unidirectional edge
* H3Index */
H3Index H3_EXPORT(getOriginH3IndexFromUnidirectionalEdge)(H3Index edge);
/** @} */
/** @defgroup getDestinationH3IndexFromUnidirectionalEdge \
* getDestinationH3IndexFromUnidirectionalEdge
* Functions for getDestinationH3IndexFromUnidirectionalEdge
* @{
*/
/** @brief Returns the destination hexagon H3Index from the unidirectional edge
* H3Index */
H3Index H3_EXPORT(getDestinationH3IndexFromUnidirectionalEdge)(H3Index edge);
/** @} */
/** @defgroup getH3IndexesFromUnidirectionalEdge \
* getH3IndexesFromUnidirectionalEdge
* Functions for getH3IndexesFromUnidirectionalEdge
* @{
*/
/** @brief Returns the origin and destination hexagons from the unidirectional
* edge H3Index */
void H3_EXPORT(getH3IndexesFromUnidirectionalEdge)(H3Index edge,
H3Index *originDestination);
/** @} */
/** @defgroup getH3UnidirectionalEdgesFromHexagon \
* getH3UnidirectionalEdgesFromHexagon
* Functions for getH3UnidirectionalEdgesFromHexagon
* @{
*/
/** @brief Returns the 6 (or 5 for pentagons) edges associated with the H3Index
*/
void H3_EXPORT(getH3UnidirectionalEdgesFromHexagon)(H3Index origin,
H3Index *edges);
/** @} */
/** @defgroup getH3UnidirectionalEdgeBoundary getH3UnidirectionalEdgeBoundary
* Functions for getH3UnidirectionalEdgeBoundary
* @{
*/
/** @brief Returns the GeoBoundary containing the coordinates of the edge */
void H3_EXPORT(getH3UnidirectionalEdgeBoundary)(H3Index edge, GeoBoundary *gb);
/** @} */
/** @defgroup h3Distance h3Distance
* Functions for h3Distance
* @{
*/
/** @brief Returns grid distance between two indexes */
int H3_EXPORT(h3Distance)(H3Index origin, H3Index h3);
/** @} */
/** @defgroup h3Line h3Line
* Functions for h3Line
* @{
*/
/** @brief Number of indexes in a line connecting two indexes */
int H3_EXPORT(h3LineSize)(H3Index start, H3Index end);
/** @brief Line of h3 indexes connecting two indexes */
int H3_EXPORT(h3Line)(H3Index start, H3Index end, H3Index *out);
/** @} */
/** @defgroup experimentalH3ToLocalIj experimentalH3ToLocalIj
* Functions for experimentalH3ToLocalIj
* @{
*/
/** @brief Returns two dimensional coordinates for the given index */
int H3_EXPORT(experimentalH3ToLocalIj)(H3Index origin, H3Index h3,
CoordIJ *out);
/** @} */
/** @defgroup experimentalLocalIjToH3 experimentalLocalIjToH3
* Functions for experimentalLocalIjToH3
* @{
*/
/** @brief Returns index for the given two dimensional coordinates */
int H3_EXPORT(experimentalLocalIjToH3)(H3Index origin, const CoordIJ *ij,
H3Index *out);
/** @} */
#ifdef __cplusplus
} // extern "C"
#endif
#endif
|