-
Notifications
You must be signed in to change notification settings - Fork 132
/
_api_template.py
976 lines (758 loc) · 23.6 KB
/
_api_template.py
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
"""
Module to DRY-up code which is repeated across API modules.
Definitions of types
--------------------
H3Index:
An unsigned 64-bit integer representing a valid H3 cell or
unidirectional edge.
Depending on the API, an H3Index may be represented as an
unsigned integer type, or as a hexadecimal string.
H3 cell:
A pentagon or hexagon that can be represented by an H3Index.
H3Cell:
H3Index representation of an H3 cell.
H3Edge:
H3Index representation of an H3 unidirectional edge.
Definitions of collections
--------------------------
Collection types vary between APIs. We'll use the following terms:
unordered collection:
Inputs and outputs are interpreted as *unordered* collections.
Examples: `set`, `numpy.ndarray`.
ordered collection:
Inputs and outputs are interpreted as *ordered* collections.
Examples: `list`, `numpy.ndarray`.
Notes
-----
Not sure if this function closure is the best solution.
There doesn't seem to be any obvious best-practice for
programmatically/dynamically creating modules.
Another approach: we could also just use `exec()`
todo: how do we lint these functions and docstrings? it seems to currently
be skipped due to it being inside the `_api_functions` function.
"""
from .. import _cy
def _api_functions(
_in_scalar,
_out_scalar,
_in_collection,
_out_unordered,
_out_ordered,
_globals,
):
def versions():
"""
Version numbers for the Python (wrapper) and C (wrapped) libraries.
Versions are output as strings of the form ``'X.Y.Z'``.
C and Python should match on ``X`` (major) and ``Y`` (minor),
but may differ on ``Z`` (patch).
Returns
-------
dict like ``{'c': 'X.Y.Z', 'python': 'A.B.C'}``
"""
from .._version import __version__
v = {
'c': _cy.c_version(),
'python': __version__,
}
return v
def string_to_h3(h):
"""
Converts a hexadecimal string to an H3 64-bit integer index.
Parameters
----------
h : str
Hexadecimal string like ``'89754e64993ffff'``
Returns
-------
int
Unsigned 64-bit integer
"""
return _cy.hex2int(h)
def h3_to_string(x):
"""
Converts an H3 64-bit integer index to a hexadecimal string.
Parameters
----------
x : int
Unsigned 64-bit integer
Returns
-------
str
Hexadecimal string like ``'89754e64993ffff'``
"""
return _cy.int2hex(x)
def num_hexagons(resolution):
"""
Return the total number of *cells* (hexagons and pentagons)
for the given resolution.
Returns
-------
int
"""
return _cy.num_hexagons(resolution)
def hex_area(resolution, unit='km^2'):
"""
Return the average area of an H3 *hexagon*
for the given resolution.
This average *excludes* pentagons.
Returns
-------
float
"""
# todo: `mean_hex_area` in 4.0
return _cy.mean_hex_area(resolution, unit)
def edge_length(resolution, unit='km'):
"""
Return the average *hexagon* edge length
for the given resolution.
This average *excludes* pentagons.
Returns
-------
float
"""
# todo: `mean_edge_length` in 4.0
return _cy.mean_edge_length(resolution, unit)
def h3_is_valid(h):
"""
Validates an H3 cell (hexagon or pentagon).
Returns
-------
bool
"""
try:
h = _in_scalar(h)
return _cy.is_cell(h)
except (ValueError, TypeError):
return False
def h3_unidirectional_edge_is_valid(edge):
"""
Validates an H3 unidirectional edge.
Returns
-------
bool
"""
try:
e = _in_scalar(edge)
return _cy.is_edge(e)
except (ValueError, TypeError):
return False
def geo_to_h3(lat, lng, resolution):
"""
Return the cell containing the (lat, lng) point
for a given resolution.
Returns
-------
H3Cell
"""
return _out_scalar(_cy.geo_to_h3(lat, lng, resolution))
def h3_to_geo(h):
"""
Return the center point of an H3 cell as a lat/lng pair.
Parameters
----------
h : H3Cell
Returns
-------
lat : float
Latitude
lng : float
Longitude
"""
return _cy.h3_to_geo(_in_scalar(h))
def h3_get_resolution(h):
"""
Return the resolution of an H3 cell.
Parameters
----------
h : H3Cell
Returns
-------
int
"""
# todo: could also work for edges
return _cy.resolution(_in_scalar(h))
def h3_to_parent(h, res=None):
"""
Get the parent of a cell.
Parameters
----------
h : H3Cell
res : int or None, optional
The resolution for the parent
If ``None``, then ``res = resolution(h) - 1``
Returns
-------
H3Cell
"""
h = _in_scalar(h)
p = _cy.parent(h, res)
p = _out_scalar(p)
return p
def h3_distance(h1, h2):
"""
Compute the H3 distance between two cells.
The H3 distance is defined as the length of the shortest
path between the cells in the graph formed by connecting
adjacent cells.
This function will return an H3ValueError if the
cells are too far apart to compute the distance.
Parameters
----------
h1 : H3Cell
h2 : H3Cell
Returns
-------
int
"""
h1 = _in_scalar(h1)
h2 = _in_scalar(h2)
d = _cy.distance(h1, h2)
return d
def h3_to_geo_boundary(h, geo_json=False):
"""
Return tuple of lat/lng pairs describing the cell boundary.
Parameters
----------
h : H3Cell
geo_json : bool, optional
If ``True``, return output in GeoJson format:
lng/lat pairs (opposite order), and
have the last pair be the same as the first.
If ``False`` (default), return lat/lng pairs, with the last
pair distinct from the first.
Returns
-------
tuple of (float, float) tuples
"""
return _cy.cell_boundary(_in_scalar(h), geo_json)
def k_ring(h, k=1):
"""
Return unordered set of cells with H3 distance ``<= k`` from ``h``.
That is, the "filled-in" disk.
Parameters
----------
h : H3Cell
k : int
Size of disk.
Returns
-------
unordered collection of H3Cell
"""
mv = _cy.disk(_in_scalar(h), k)
return _out_unordered(mv)
def hex_range(h, k=1):
"""
Alias for `k_ring`.
"Filled-in" disk.
Notes
-----
This name differs from the C API.
"""
mv = _cy.disk(_in_scalar(h), k)
return _out_unordered(mv)
def hex_ring(h, k=1):
"""
Return unordered set of cells with H3 distance ``== k`` from ``h``.
That is, the "hollow" ring.
Parameters
----------
h : H3Cell
k : int
Size of ring.
Returns
-------
unordered collection of H3Cell
"""
mv = _cy.ring(_in_scalar(h), k)
return _out_unordered(mv)
def hex_range_distances(h, K):
"""
Ordered list of the "hollow" rings around ``h``,
up to and including distance ``K``.
Parameters
----------
h : H3Cell
K : int
Largest distance considered.
Returns
-------
ordered collection of (unordered collection of H3Cell)
"""
h = _in_scalar(h)
out = [
_out_unordered(_cy.ring(h, k))
for k in range(K + 1)
]
return out
def hex_ranges(hexes, K):
"""
Returns the dictionary ``{h: hex_range_distances(h, K) for h in hexes}``
Returns
-------
Dict[H3Cell, List[ UnorderedCollection[H3Cell] ]]
"""
# todo: can we drop this function? the user can implement if needed.
out = {
h: hex_range_distances(h, K)
for h in hexes
}
return out
def k_ring_distances(h, K):
"""Alias for `hex_range_distances`."""
return hex_range_distances(h, K)
def h3_to_children(h, res=None):
"""
Children of a hexagon.
Parameters
----------
h : H3Cell
res : int or None, optional
The resolution for the children.
If ``None``, then ``res = resolution(h) + 1``
Returns
-------
unordered collection of H3Cell
"""
mv = _cy.children(_in_scalar(h), res)
return _out_unordered(mv)
# todo: nogil for expensive C operation?
def compact(hexes):
"""
Compact a collection of H3 cells by combining
smaller cells into larger cells, if all child cells
are present.
Parameters
----------
hexes : iterable of H3Cell
Returns
-------
unordered collection of H3Cell
"""
# todo: does compact work on mixed-resolution collections?
hu = _in_collection(hexes)
hc = _cy.compact(hu)
return _out_unordered(hc)
def uncompact(hexes, res):
"""
Reverse the `compact` operation.
Return a collection of H3 cells, all of resolution ``res``.
Parameters
----------
hexes : iterable of H3Cell
res : int
Resolution of desired output cells.
Returns
-------
unordered collection of H3Cell
Raises
------
todo: add test to make sure an error is returned when input
contains hex smaller than output res.
https://github.com/uber/h3/blob/master/src/h3lib/lib/h3Index.c#L425
"""
hc = _in_collection(hexes)
hu = _cy.uncompact(hc, res)
return _out_unordered(hu)
def h3_set_to_multi_polygon(hexes, geo_json=False):
"""
Get GeoJSON-like MultiPolygon describing the outline of the area
covered by a set of H3 cells.
Parameters
----------
hexes : unordered collection of H3Cell
geo_json : bool, optional
If `True`, output geo sequences will be lng/lat pairs, with the
last the same as the first.
If `False`, output geo sequences will be lat/lng pairs, with the
last distinct from the first.
Defaults to `False`
Returns
-------
list
List of "polygons".
Each polygon is a list of "geo sequences" like
``[outer, hole1, hole2, ...]``. The holes may not be present.
Each geo sequence is a list of lat/lng or lng/lat pairs.
"""
# todo: this function output does not match with `polyfill`.
# This function returns a list of polygons, while `polyfill` returns
# a GeoJSON-like dictionary object.
hexes = _in_collection(hexes)
return _cy.h3_set_to_multi_polygon(hexes, geo_json=geo_json)
def polyfill_polygon(outer, res, holes=None, lnglat_order=False):
mv = _cy.polyfill_polygon(outer, res, holes=holes, lnglat_order=lnglat_order)
return _out_unordered(mv)
def polyfill_geojson(geojson, res):
mv = _cy.polyfill_geojson(geojson, res)
return _out_unordered(mv)
def polyfill(geojson, res, geo_json_conformant=False):
"""
Get set of hexagons whose *centers* are contained within
a GeoJSON-style polygon.
Parameters
----------
geojson : dict
GeoJSON-style input dictionary describing a polygon (optionally
including holes).
Dictionary should be formatted like:
.. code-block:: text
{
'type': 'Polygon',
'coordinates': [outer, hole1, hole2, ...],
}
`outer`, `hole1`, etc., are lists of geo coordinate tuples.
The holes are optional.
res : int
Desired output resolution for cells.
geo_json_conformant : bool, optional
When ``True``, ``outer``, ``hole1``, etc. must be sequences of
lng/lat pairs, with the last the same as the first.
When ``False``, they must be sequences of lat/lng pairs,
with the last not needing to match the first.
Returns
-------
unordered collection of H3Cell
"""
mv = _cy.polyfill(geojson, res, geo_json_conformant=geo_json_conformant)
return _out_unordered(mv)
def h3_is_pentagon(h):
"""
Identify if an H3 cell is a pentagon.
Parameters
----------
h : H3Index
Returns
-------
bool
``True`` if input is a valid H3 cell which is a pentagon.
Notes
-----
A pentagon should *also* pass ``h3_is_cell()``.
Will return ``False`` for valid H3Edge.
"""
return _cy.is_pentagon(_in_scalar(h))
def h3_get_base_cell(h):
"""
Return the base cell *number* (``0`` to ``121``) of the given cell.
The base cell *number* and the H3Index are two different representations
of the same cell: the parent cell of resolution ``0``.
The base cell *number* is encoded within the corresponding
H3Index.
todo: could work with edges
Parameters
----------
h : H3Cell
Returns
-------
int
"""
return _cy.get_base_cell(_in_scalar(h))
def h3_indexes_are_neighbors(h1, h2):
"""
Returns ``True`` if ``h1`` and ``h2`` are neighboring cells.
Parameters
----------
h1 : H3Cell
h2 : H3Cell
Returns
-------
bool
"""
h1 = _in_scalar(h1)
h2 = _in_scalar(h2)
return _cy.are_neighbors(h1, h2)
def get_h3_unidirectional_edge(origin, destination):
"""
Create an H3 Index denoting a unidirectional edge.
The edge is constructed from neighboring cells ``origin`` and
``destination``.
Parameters
----------
origin : H3Cell
destination : H3Cell
Raises
------
ValueError
When cells are not adjacent.
Returns
-------
H3Edge
"""
o = _in_scalar(origin)
d = _in_scalar(destination)
e = _cy.edge(o, d)
e = _out_scalar(e)
return e
def get_origin_h3_index_from_unidirectional_edge(e):
"""
Origin cell from an H3 directed edge.
Parameters
----------
e : H3Edge
Returns
-------
H3Cell
"""
e = _in_scalar(e)
o = _cy.edge_origin(e)
o = _out_scalar(o)
return o
def get_destination_h3_index_from_unidirectional_edge(e):
"""
Destination cell from an H3 directed edge.
Parameters
----------
e : H3Edge
Returns
-------
H3Cell
"""
e = _in_scalar(e)
d = _cy.edge_destination(e)
d = _out_scalar(d)
return d
def get_h3_indexes_from_unidirectional_edge(e):
"""
Return (origin, destination) tuple from H3 directed edge
Parameters
----------
e : H3Edge
Returns
-------
H3Cell
Origin cell of edge
H3Cell
Destination cell of edge
"""
e = _in_scalar(e)
o, d = _cy.edge_cells(e)
o, d = _out_scalar(o), _out_scalar(d)
return o, d
def get_h3_unidirectional_edges_from_hexagon(origin):
"""
Return all directed edges starting from ``origin`` cell.
Parameters
----------
origin : H3Cell
Returns
-------
unordered collection of H3Edge
"""
mv = _cy.edges_from_cell(_in_scalar(origin))
return _out_unordered(mv)
def get_h3_unidirectional_edge_boundary(edge, geo_json=False):
return _cy.edge_boundary(_in_scalar(edge), geo_json=geo_json)
def h3_line(start, end):
"""
Returns the ordered collection of cells denoting a
minimum-length non-unique path between cells.
Parameters
----------
start : H3Cell
end : H3Cell
Returns
-------
ordered collection of H3Cell
Starting with ``start``, and ending with ``end``.
"""
mv = _cy.line(_in_scalar(start), _in_scalar(end))
return _out_ordered(mv)
def h3_is_res_class_III(h):
"""
Determine if cell has orientation "Class II" or "Class III".
The orientation of pentagons/hexagons on the icosahedron can be one
of two types: "Class II" or "Class III".
All cells within a resolution have the same type, and the type
alternates between resolutions.
"Class II" cells have resolutions: 0,2,4,6,8,10,12,14
"Class III" cells have resolutions: 1,3,5,7,9,11,13,15
Parameters
----------
h : H3Cell
Returns
-------
bool
``True`` if ``h`` is "Class III".
``False`` if ``h`` is "Class II".
References
----------
1. https://uber.github.io/h3/#/documentation/core-library/coordinate-systems
"""
return _cy.is_res_class_iii(_in_scalar(h))
def h3_is_res_class_iii(h):
"""Alias for `h3_is_res_class_III`."""
return h3_is_res_class_III(h)
def get_pentagon_indexes(resolution):
"""
Return all pentagons at a given resolution.
Parameters
----------
resolution : int
Returns
-------
unordered collection of H3Cell
"""
mv = _cy.get_pentagon_indexes(resolution)
return _out_unordered(mv)
def get_res0_indexes():
"""
Return all cells at resolution 0.
Parameters
----------
None
Returns
-------
unordered collection of H3Cell
"""
mv = _cy.get_res0_indexes()
return _out_unordered(mv)
def h3_to_center_child(h, res=None):
"""
Get the center child of a cell at some finer resolution.
Parameters
----------
h : H3Cell
res : int or None, optional
The resolution for the child cell
If ``None``, then ``res = resolution(h) + 1``
Returns
-------
H3Cell
"""
h = _in_scalar(h)
p = _cy.center_child(h, res)
p = _out_scalar(p)
return p
def h3_get_faces(h):
"""
Return icosahedron faces intersecting a given H3 cell.
There are twenty possible faces, ranging from 0--19.
Note: Every interface returns a Python ``set`` of ``int``.
Parameters
----------
h : H3Cell
Returns
-------
Python ``set`` of ``int``
"""
h = _in_scalar(h)
faces = _cy.get_faces(h)
return faces
def experimental_h3_to_local_ij(origin, h):
"""
Return local (i,j) coordinates of cell ``h`` in relation to ``origin`` cell
Parameters
----------
origin : H3Cell
Origin/central cell for defining i,j coordinates.
h: H3Cell
Destination cell whose i,j coordinates we'd like, based off
of the origin cell.
Returns
-------
Tuple (i, j) of integer local coordinates of cell ``h``
Notes
-----
The ``origin`` cell does not define (0, 0) for the IJ coordinate space.
(0, 0) refers to the center of the base cell containing origin at the
resolution of `origin`.
Subtracting the IJ coordinates of ``origin`` from every cell would get
you the property of (0, 0) being the ``origin``.
This is done so we don't need to keep recomputing the coordinates of
``origin`` if not needed.
"""
origin = _in_scalar(origin)
h = _in_scalar(h)
i, j = _cy.experimental_h3_to_local_ij(origin, h)
return i, j
def experimental_local_ij_to_h3(origin, i, j):
"""
Return cell at local (i,j) position relative to the ``origin`` cell.
Parameters
----------
origin : H3Cell
Origin/central cell for defining i,j coordinates.
i, j: int
Integer coordinates with respect to ``origin`` cell.
Returns
-------
H3Cell at local (i,j) position relative to the ``origin`` cell
Notes
-----
The ``origin`` cell does not define (0, 0) for the IJ coordinate space.
(0, 0) refers to the center of the base cell containing origin at the
resolution of ``origin``.
Subtracting the IJ coordinates of ``origin`` from every cell would get
you the property of (0, 0) being the ``origin``.
This is done so we don't need to keep recomputing the coordinates of
``origin`` if not needed.
"""
origin = _in_scalar(origin)
h = _cy.experimental_local_ij_to_h3(origin, i, j)
h = _out_scalar(h)
return h
def cell_area(h, unit='km^2'):
"""
Compute the spherical surface area of a specific H3 cell.
Parameters
----------
h : H3Cell
unit: str
Unit for area result (``'km^2'``, 'm^2', or 'rads^2')
Returns
-------
The area of the H3 cell in the given units
Notes
-----
This function breaks the cell into spherical triangles, and computes
their spherical area.
The function uses the spherical distance calculation given by
`point_dist`.
"""
h = _in_scalar(h)
return _cy.cell_area(h, unit=unit)
def exact_edge_length(e, unit='km'):
"""
Compute the spherical length of a specific H3 edge.
Parameters
----------
h : H3Cell
unit: str
Unit for length result ('km', 'm', or 'rads')
Returns
-------
The length of the edge in the given units
Notes
-----
This function uses the spherical distance calculation given by
`point_dist`.
"""
e = _in_scalar(e)
return _cy.edge_length(e, unit=unit)
def point_dist(point1, point2, unit='km'):
"""
Compute the spherical distance between two (lat, lng) points.
todo: do we handle lat/lng points consistently in the api? what
about (lat1, lng1, lat2, lng2) as the input? How will this work
for vectorized versions?
Parameters
----------
point1 : tuple
(lat, lng) tuple in degrees
point2 : tuple
(lat, lng) tuple in degrees
unit: str
Unit for distance result ('km', 'm', or 'rads')
Returns
-------
Spherical (or "haversine") distance between the points
"""
lat1, lng1 = point1
lat2, lng2 = point2
return _cy.point_dist(
lat1, lng1,
lat2, lng2,
unit=unit
)
_globals.update(locals())