-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathutil.pyx
84 lines (60 loc) · 2.11 KB
/
util.pyx
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
from .h3lib cimport H3int, H3str, isValidCell, isValidDirectedEdge
cimport h3lib
from .error_system import (
H3ResDomainError,
H3DomainError,
H3DirEdgeInvalidError,
H3CellInvalidError,
)
cdef h3lib.LatLng deg2coord(double lat, double lng) nogil:
cdef:
h3lib.LatLng c
c.lat = h3lib.degsToRads(lat)
c.lng = h3lib.degsToRads(lng)
return c
cdef (double, double) coord2deg(h3lib.LatLng c) nogil:
return (
h3lib.radsToDegs(c.lat),
h3lib.radsToDegs(c.lng)
)
cpdef basestring c_version():
v = (
h3lib.H3_VERSION_MAJOR,
h3lib.H3_VERSION_MINOR,
h3lib.H3_VERSION_PATCH,
)
return '{}.{}.{}'.format(*v)
cpdef H3int str_to_int(H3str h) except? 0:
return int(h, 16)
cpdef H3str int_to_str(H3int x):
""" Convert H3 integer to hex string representation
Need to be careful in Python 2 because `hex(x)` may return a string
with a trailing `L` character (denoting a "large" integer).
The formatting approach below avoids this.
Also need to be careful about unicode/str differences.
"""
return '{:x}'.format(x)
cdef check_cell(H3int h):
""" Check if valid H3 "cell" (hexagon or pentagon).
Does not check if a valid H3 edge, for example.
Since this function is used by multiple interfaces (int or str),
we want the error message to be informative to the user
in either case.
We use the builtin `hex` function instead of `int_to_str` to
prepend `0x` to indicate that this **integer** representation
is incorrect, but in a format that is easily compared to
`str` inputs.
"""
if isValidCell(h) == 0:
raise H3CellInvalidError('Integer is not a valid H3 cell: {}'.format(hex(h)))
cdef check_edge(H3int e):
if isValidDirectedEdge(e) == 0:
raise H3DirEdgeInvalidError('Integer is not a valid H3 edge: {}'.format(hex(e)))
cdef check_res(int res):
if (res < 0) or (res > 15):
raise H3ResDomainError(res)
cdef check_distance(int k):
if k < 0:
raise H3DomainError(
'Grid distances must be nonnegative. Received: {}'.format(k)
)