pygeohash 2.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pygeohash/__init__.py +73 -0
- pygeohash/distances.py +96 -0
- pygeohash/geohash.py +182 -0
- pygeohash/nbgeohash.py +175 -0
- pygeohash/neighbor.py +70 -0
- pygeohash/stats.py +115 -0
- pygeohash-2.0.2.dist-info/METADATA +116 -0
- pygeohash-2.0.2.dist-info/RECORD +10 -0
- pygeohash-2.0.2.dist-info/WHEEL +4 -0
- pygeohash-2.0.2.dist-info/licenses/LICENSE.txt +697 -0
pygeohash/__init__.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""
|
|
2
|
+
.. module:: pygeohash
|
|
3
|
+
:platform: Unix, Windows
|
|
4
|
+
:synopsis: A module for interacting with geohashes in python
|
|
5
|
+
|
|
6
|
+
.. moduleauthor:: Will McGinnis <will@pedalwrencher.com>
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from .distances import geohash_approximate_distance, geohash_haversine_distance
|
|
12
|
+
from .geohash import (
|
|
13
|
+
ExactLatLong,
|
|
14
|
+
LatLong,
|
|
15
|
+
decode,
|
|
16
|
+
decode_exactly,
|
|
17
|
+
encode,
|
|
18
|
+
encode_strictly,
|
|
19
|
+
)
|
|
20
|
+
from .neighbor import get_adjacent
|
|
21
|
+
from .stats import eastern, mean, northern, southern, std, variance, western
|
|
22
|
+
|
|
23
|
+
__author__ = "willmcginnis"
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"geohash_approximate_distance",
|
|
27
|
+
"geohash_haversine_distance",
|
|
28
|
+
"LatLong",
|
|
29
|
+
"ExactLatLong",
|
|
30
|
+
"encode",
|
|
31
|
+
"encode_strictly",
|
|
32
|
+
"decode",
|
|
33
|
+
"decode_exactly",
|
|
34
|
+
"mean",
|
|
35
|
+
"northern",
|
|
36
|
+
"southern",
|
|
37
|
+
"eastern",
|
|
38
|
+
"western",
|
|
39
|
+
"variance",
|
|
40
|
+
"std",
|
|
41
|
+
"get_adjacent",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
try:
|
|
45
|
+
# Soft dependency
|
|
46
|
+
import numba
|
|
47
|
+
import numpy
|
|
48
|
+
|
|
49
|
+
from .nbgeohash import (
|
|
50
|
+
nb_decode_exactly,
|
|
51
|
+
nb_point_decode,
|
|
52
|
+
nb_point_encode,
|
|
53
|
+
nb_vector_decode,
|
|
54
|
+
nb_vector_encode,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
__all__ += [
|
|
58
|
+
"nb_point_encode",
|
|
59
|
+
"nb_point_decode",
|
|
60
|
+
"nb_vector_encode",
|
|
61
|
+
"nb_vector_decode",
|
|
62
|
+
"nb_decode_exactly",
|
|
63
|
+
"nb_point_decode",
|
|
64
|
+
"nb_point_encode",
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
except ImportError:
|
|
68
|
+
import logging
|
|
69
|
+
|
|
70
|
+
logging.warning(
|
|
71
|
+
"Numpy and Numba are soft dependencies to use the numba geohashing functions. \n"
|
|
72
|
+
"Can only import/use native python functions."
|
|
73
|
+
)
|
pygeohash/distances.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""
|
|
2
|
+
.. module:: distances
|
|
3
|
+
:platform: Unix, Windows
|
|
4
|
+
:synopsis: A module for calculating distance measures with geohashes
|
|
5
|
+
|
|
6
|
+
.. moduleauthor:: Will McGinnis <will@pedalwrencher.com>
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import math
|
|
12
|
+
from typing import Dict
|
|
13
|
+
|
|
14
|
+
from pygeohash.geohash import __base32, decode_exactly
|
|
15
|
+
|
|
16
|
+
__author__ = "Will McGinnis"
|
|
17
|
+
|
|
18
|
+
# the distance between geohashes based on matching characters, in meters.
|
|
19
|
+
_PRECISION: Dict[int, float] = {
|
|
20
|
+
0: 20000000,
|
|
21
|
+
1: 5003530,
|
|
22
|
+
2: 625441,
|
|
23
|
+
3: 123264,
|
|
24
|
+
4: 19545,
|
|
25
|
+
5: 3803,
|
|
26
|
+
6: 610,
|
|
27
|
+
7: 118,
|
|
28
|
+
8: 19,
|
|
29
|
+
9: 3.71,
|
|
30
|
+
10: 0.6,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def geohash_approximate_distance(geohash_1: str, geohash_2: str, check_validity: bool = False) -> float:
|
|
35
|
+
"""
|
|
36
|
+
Returns the approximate great-circle distance between two geohashes in meters.
|
|
37
|
+
|
|
38
|
+
:param geohash_1:
|
|
39
|
+
:param geohash_2:
|
|
40
|
+
:return:
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
if check_validity:
|
|
44
|
+
if len([x for x in geohash_1 if x in __base32]) != len(geohash_1):
|
|
45
|
+
raise ValueError(f"Geohash 1: {geohash_1} is not a valid geohash")
|
|
46
|
+
|
|
47
|
+
if len([x for x in geohash_2 if x in __base32]) != len(geohash_2):
|
|
48
|
+
raise ValueError(f"Geohash 2: {geohash_2} is not a valid geohash")
|
|
49
|
+
|
|
50
|
+
# normalize the geohashes to the length of the shortest
|
|
51
|
+
len_1 = len(geohash_1)
|
|
52
|
+
len_2 = len(geohash_2)
|
|
53
|
+
if len_1 > len_2:
|
|
54
|
+
geohash_1 = geohash_1[:len_2]
|
|
55
|
+
elif len_2 > len_1:
|
|
56
|
+
geohash_2 = geohash_2[:len_1]
|
|
57
|
+
|
|
58
|
+
# find how many leading characters are matching
|
|
59
|
+
matching = 0
|
|
60
|
+
for g1, g2 in zip(geohash_1, geohash_2):
|
|
61
|
+
if g1 == g2:
|
|
62
|
+
matching += 1
|
|
63
|
+
else:
|
|
64
|
+
break
|
|
65
|
+
|
|
66
|
+
# we only have precision metrics up to 10 characters
|
|
67
|
+
matching = min(matching, 10)
|
|
68
|
+
|
|
69
|
+
return _PRECISION[matching]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def geohash_haversine_distance(geohash_1: str, geohash_2: str) -> float:
|
|
73
|
+
"""
|
|
74
|
+
converts the geohashes to lat/lon and then calculates the haversine great circle distance in meters.
|
|
75
|
+
|
|
76
|
+
:param geohash_1:
|
|
77
|
+
:param geohash_2:
|
|
78
|
+
:return:
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
lat_1, lon_1, _, _ = decode_exactly(geohash_1)
|
|
82
|
+
lat_2, lon_2, _, _ = decode_exactly(geohash_2)
|
|
83
|
+
|
|
84
|
+
R = 6_371_000
|
|
85
|
+
phi_1 = math.radians(lat_1)
|
|
86
|
+
phi_2 = math.radians(lat_2)
|
|
87
|
+
|
|
88
|
+
delta_phi = math.radians(lat_2 - lat_1)
|
|
89
|
+
delta_lambda = math.radians(lon_2 - lon_1)
|
|
90
|
+
|
|
91
|
+
a = math.sin(delta_phi / 2.0) * math.sin(delta_phi / 2.0) + math.cos(phi_1) * math.cos(phi_2) * math.sin(
|
|
92
|
+
delta_lambda / 2
|
|
93
|
+
) * math.sin(delta_lambda / 2)
|
|
94
|
+
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
|
|
95
|
+
|
|
96
|
+
return R * c
|
pygeohash/geohash.py
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (C) 2008 Leonard Norrgard <leonard.norrgard@gmail.com>
|
|
3
|
+
Copyright (C) 2015 Leonard Norrgard <leonard.norrgard@gmail.com>
|
|
4
|
+
|
|
5
|
+
This file is part of Geohash.
|
|
6
|
+
|
|
7
|
+
Geohash is free software: you can redistribute it and/or modify it
|
|
8
|
+
under the terms of the GNU Affero General Public License as published
|
|
9
|
+
by the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
(at your option) any later version.
|
|
11
|
+
|
|
12
|
+
Geohash is distributed in the hope that it will be useful, but WITHOUT
|
|
13
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14
|
+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
|
15
|
+
License for more details.
|
|
16
|
+
|
|
17
|
+
You should have received a copy of the GNU Affero General Public
|
|
18
|
+
License along with Geohash. If not, see
|
|
19
|
+
<http://www.gnu.org/licenses/>.
|
|
20
|
+
|
|
21
|
+
Modified 2015 by Will McGinnis for pygeohash.
|
|
22
|
+
Modified 2023 by Paarth Shah for pygeohash.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
from math import log10
|
|
28
|
+
from typing import Dict, NamedTuple
|
|
29
|
+
|
|
30
|
+
# Note: the alphabet in geohash differs from the common base32 alphabet described in IETF's RFC 4648
|
|
31
|
+
# (http://tools.ietf.org/html/rfc4648)
|
|
32
|
+
|
|
33
|
+
__base32 = "0123456789bcdefghjkmnpqrstuvwxyz"
|
|
34
|
+
__decodemap: Dict[str, int] = {base32_char: i for i, base32_char in enumerate(__base32)}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class LatLong(NamedTuple):
|
|
38
|
+
latitude: float
|
|
39
|
+
longitude: float
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class ExactLatLong(NamedTuple):
|
|
43
|
+
latitude: float
|
|
44
|
+
longitude: float
|
|
45
|
+
latitude_error: float
|
|
46
|
+
longitude_error: float
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def decode_exactly(geohash: str) -> ExactLatLong:
|
|
50
|
+
"""
|
|
51
|
+
Decode the geohash to its exact values, including the error
|
|
52
|
+
margins of the result. Returns four float values: latitude,
|
|
53
|
+
longitude, the plus/minus error for latitude (as a positive
|
|
54
|
+
number) and the plus/minus error for longitude (as a positive
|
|
55
|
+
number).
|
|
56
|
+
"""
|
|
57
|
+
lat_interval, lon_interval = (-90.0, 90.0), (-180.0, 180.0)
|
|
58
|
+
lat_err, lon_err = 90.0, 180.0
|
|
59
|
+
is_even = True
|
|
60
|
+
for c in geohash:
|
|
61
|
+
cd = __decodemap[c]
|
|
62
|
+
for mask in [16, 8, 4, 2, 1]:
|
|
63
|
+
if is_even: # adds longitude info
|
|
64
|
+
lon_err /= 2
|
|
65
|
+
if cd & mask:
|
|
66
|
+
lon_interval = (
|
|
67
|
+
(lon_interval[0] + lon_interval[1]) / 2,
|
|
68
|
+
lon_interval[1],
|
|
69
|
+
)
|
|
70
|
+
else:
|
|
71
|
+
lon_interval = (
|
|
72
|
+
lon_interval[0],
|
|
73
|
+
(lon_interval[0] + lon_interval[1]) / 2,
|
|
74
|
+
)
|
|
75
|
+
else: # adds latitude info
|
|
76
|
+
lat_err /= 2
|
|
77
|
+
if cd & mask:
|
|
78
|
+
lat_interval = (
|
|
79
|
+
(lat_interval[0] + lat_interval[1]) / 2,
|
|
80
|
+
lat_interval[1],
|
|
81
|
+
)
|
|
82
|
+
else:
|
|
83
|
+
lat_interval = (
|
|
84
|
+
lat_interval[0],
|
|
85
|
+
(lat_interval[0] + lat_interval[1]) / 2,
|
|
86
|
+
)
|
|
87
|
+
is_even = not is_even
|
|
88
|
+
lat = (lat_interval[0] + lat_interval[1]) / 2
|
|
89
|
+
lon = (lon_interval[0] + lon_interval[1]) / 2
|
|
90
|
+
return ExactLatLong(lat, lon, lat_err, lon_err)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def decode(geohash: str) -> LatLong:
|
|
94
|
+
"""
|
|
95
|
+
Decode geohash, returning two float with latitude and longitude
|
|
96
|
+
containing only relevant digits and with trailing zeroes removed.
|
|
97
|
+
"""
|
|
98
|
+
lat, lon, lat_err, lon_err = decode_exactly(geohash)
|
|
99
|
+
# Format to the number of decimals that are known
|
|
100
|
+
lats = "%.*f" % (max(1, int(round(-log10(lat_err)))) - 1, lat)
|
|
101
|
+
lons = "%.*f" % (max(1, int(round(-log10(lon_err)))) - 1, lon)
|
|
102
|
+
if "." in lats:
|
|
103
|
+
lats = lats.rstrip("0")
|
|
104
|
+
if "." in lons:
|
|
105
|
+
lons = lons.rstrip("0")
|
|
106
|
+
return LatLong(float(lats), float(lons))
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def encode(latitude: float, longitude: float, precision=12) -> str:
|
|
110
|
+
"""
|
|
111
|
+
Encode a position given in float arguments latitude, longitude to
|
|
112
|
+
a geohash which will have the character count precision.
|
|
113
|
+
"""
|
|
114
|
+
lat_interval = (-90.0, 90.0)
|
|
115
|
+
lon_interval = (-180.0, 180.0)
|
|
116
|
+
geohash = []
|
|
117
|
+
bits = [16, 8, 4, 2, 1]
|
|
118
|
+
bit = 0
|
|
119
|
+
ch = 0
|
|
120
|
+
even = True
|
|
121
|
+
while len(geohash) < precision:
|
|
122
|
+
if even:
|
|
123
|
+
mid = (lon_interval[0] + lon_interval[1]) / 2
|
|
124
|
+
if longitude >= mid:
|
|
125
|
+
ch |= bits[bit]
|
|
126
|
+
lon_interval = (mid, lon_interval[1])
|
|
127
|
+
else:
|
|
128
|
+
lon_interval = (lon_interval[0], mid)
|
|
129
|
+
else:
|
|
130
|
+
mid = (lat_interval[0] + lat_interval[1]) / 2
|
|
131
|
+
if latitude >= mid:
|
|
132
|
+
ch |= bits[bit]
|
|
133
|
+
lat_interval = (mid, lat_interval[1])
|
|
134
|
+
else:
|
|
135
|
+
lat_interval = (lat_interval[0], mid)
|
|
136
|
+
even = not even
|
|
137
|
+
if bit < 4:
|
|
138
|
+
bit += 1
|
|
139
|
+
else:
|
|
140
|
+
geohash += __base32[ch]
|
|
141
|
+
bit = 0
|
|
142
|
+
ch = 0
|
|
143
|
+
return "".join(geohash)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def encode_strictly(latitude, longitude, precision=12):
|
|
147
|
+
"""
|
|
148
|
+
Encode a position given in float arguments latitude, longitude to
|
|
149
|
+
a geohash which will have the character count precision.
|
|
150
|
+
When compared to mid, mid should be included.
|
|
151
|
+
Provide a separate method for backward compatibility.
|
|
152
|
+
"""
|
|
153
|
+
lat_interval = (-90.0, 90.0)
|
|
154
|
+
lon_interval = (-180.0, 180.0)
|
|
155
|
+
geohash = []
|
|
156
|
+
bits = [16, 8, 4, 2, 1]
|
|
157
|
+
bit = 0
|
|
158
|
+
ch = 0
|
|
159
|
+
even = True
|
|
160
|
+
while len(geohash) < precision:
|
|
161
|
+
if even:
|
|
162
|
+
mid = (lon_interval[0] + lon_interval[1]) / 2
|
|
163
|
+
if longitude >= mid:
|
|
164
|
+
ch |= bits[bit]
|
|
165
|
+
lon_interval = (mid, lon_interval[1])
|
|
166
|
+
else:
|
|
167
|
+
lon_interval = (lon_interval[0], mid)
|
|
168
|
+
else:
|
|
169
|
+
mid = (lat_interval[0] + lat_interval[1]) / 2
|
|
170
|
+
if latitude >= mid:
|
|
171
|
+
ch |= bits[bit]
|
|
172
|
+
lat_interval = (mid, lat_interval[1])
|
|
173
|
+
else:
|
|
174
|
+
lat_interval = (lat_interval[0], mid)
|
|
175
|
+
even = not even
|
|
176
|
+
if bit < 4:
|
|
177
|
+
bit += 1
|
|
178
|
+
else:
|
|
179
|
+
geohash += __base32[ch]
|
|
180
|
+
bit = 0
|
|
181
|
+
ch = 0
|
|
182
|
+
return "".join(geohash)
|
pygeohash/nbgeohash.py
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"""
|
|
2
|
+
.. module:: nbgeohash
|
|
3
|
+
:platform: Unix, Windows
|
|
4
|
+
:synopsis: A module for encoding to and decoding from the geohash system using numba
|
|
5
|
+
|
|
6
|
+
.. moduleauthor:: Ilyas Moutawwakil <ilyas.moutawwakil@gmail.com>
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from math import log10
|
|
12
|
+
from typing import List
|
|
13
|
+
|
|
14
|
+
import numpy as np
|
|
15
|
+
from numba import njit, types
|
|
16
|
+
|
|
17
|
+
from pygeohash.geohash import ExactLatLong, LatLong, __base32
|
|
18
|
+
|
|
19
|
+
__author__ = "ilyasmoutawwakil"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@njit(cache=True, fastmath=True)
|
|
23
|
+
def base32_to_int(s: types.char) -> types.uint8:
|
|
24
|
+
"""
|
|
25
|
+
Returns the equivalent value of a base 32 character.
|
|
26
|
+
Surprisingly, on Numba this approach is faster than all the others.
|
|
27
|
+
More specifically, because the numba hashtable (dictionary) is slower.
|
|
28
|
+
"""
|
|
29
|
+
assert s in __base32
|
|
30
|
+
res = ord(s) - 48
|
|
31
|
+
if res > 9:
|
|
32
|
+
res -= 40
|
|
33
|
+
if res > 16:
|
|
34
|
+
res -= 1
|
|
35
|
+
if res > 18:
|
|
36
|
+
res -= 1
|
|
37
|
+
if res > 20:
|
|
38
|
+
res -= 1
|
|
39
|
+
return res
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@njit(fastmath=True)
|
|
43
|
+
def nb_decode_exactly(geohash: str) -> ExactLatLong:
|
|
44
|
+
"""
|
|
45
|
+
Decode the geohash to its exact values, including the error
|
|
46
|
+
margins of the result. Returns four float values: latitude,
|
|
47
|
+
longitude, the plus/minus error for latitude (as a positive
|
|
48
|
+
number) and the plus/minus error for longitude (as a positive
|
|
49
|
+
number).
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
lat_interval_neg, lat_interval_pos, lon_interval_neg, lon_interval_pos = (
|
|
53
|
+
-90,
|
|
54
|
+
90,
|
|
55
|
+
-180,
|
|
56
|
+
180,
|
|
57
|
+
)
|
|
58
|
+
lat_err, lon_err = 90.0, 180.0
|
|
59
|
+
is_even = True
|
|
60
|
+
for c in geohash:
|
|
61
|
+
cd = base32_to_int(c)
|
|
62
|
+
for mask in (16, 8, 4, 2, 1):
|
|
63
|
+
if is_even: # adds longitude info
|
|
64
|
+
lon_err /= 2
|
|
65
|
+
if cd & mask:
|
|
66
|
+
lon_interval_neg = (lon_interval_neg + lon_interval_pos) / 2
|
|
67
|
+
else:
|
|
68
|
+
lon_interval_pos = (lon_interval_neg + lon_interval_pos) / 2
|
|
69
|
+
else: # adds latitude info
|
|
70
|
+
lat_err /= 2
|
|
71
|
+
if cd & mask:
|
|
72
|
+
lat_interval_neg = (lat_interval_neg + lat_interval_pos) / 2
|
|
73
|
+
else:
|
|
74
|
+
lat_interval_pos = (lat_interval_neg + lat_interval_pos) / 2
|
|
75
|
+
is_even = not is_even
|
|
76
|
+
lat = (lat_interval_neg + lat_interval_pos) / 2
|
|
77
|
+
lon = (lon_interval_neg + lon_interval_pos) / 2
|
|
78
|
+
return ExactLatLong(lat, lon, lat_err, lon_err)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@njit(fastmath=True)
|
|
82
|
+
def nb_point_decode(geohash: str) -> LatLong:
|
|
83
|
+
"""
|
|
84
|
+
Decode geohash, returning two float with latitude and longitude containing only relevant digits.
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
lat, lon, lat_err, lon_err = nb_decode_exactly(geohash)
|
|
88
|
+
# Format to the number of decimals that are known
|
|
89
|
+
lat_dec = max(1, round(-log10(lat_err))) - 1
|
|
90
|
+
lon_dec = max(1, round(-log10(lon_err))) - 1
|
|
91
|
+
lat = round(lat, lat_dec)
|
|
92
|
+
lon = round(lon, lon_dec)
|
|
93
|
+
|
|
94
|
+
return LatLong(lat, lon)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@njit(fastmath=True)
|
|
98
|
+
def nb_vector_decode(geohashes: List[str]) -> types.Tuple:
|
|
99
|
+
"""
|
|
100
|
+
Decode geohashes, returning two Arrays of floats with latitudes and longitudes containing only relevant digits.
|
|
101
|
+
This is not exactly a vectorized version of nb_point_decode, but it is way faster and gets faster
|
|
102
|
+
as the number of geohashes increase.
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
n = len(geohashes)
|
|
106
|
+
lats = np.empty(n)
|
|
107
|
+
lons = np.empty(n)
|
|
108
|
+
for i, geohash in enumerate(geohashes):
|
|
109
|
+
lat, lon, lat_err, lon_err = nb_decode_exactly(str(geohash))
|
|
110
|
+
# Format to the number of decimals that are known
|
|
111
|
+
lat_dec = max(1, round(-log10(lat_err))) - 1
|
|
112
|
+
lon_dec = max(1, round(-log10(lon_err))) - 1
|
|
113
|
+
lats[i] = round(lat, lat_dec)
|
|
114
|
+
lons[i] = round(lon, lon_dec)
|
|
115
|
+
|
|
116
|
+
return lats, lons
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@njit(fastmath=True)
|
|
120
|
+
def nb_point_encode(latitude: types.float64, longitude: types.float64, precision: types.int8 = 12) -> types.string:
|
|
121
|
+
"""
|
|
122
|
+
Encode a point given by latitude and longitude to a geohash of the specified precision.
|
|
123
|
+
"""
|
|
124
|
+
lat_interval_neg, lat_interval_pos, lon_interval_neg, lon_interval_pos = (
|
|
125
|
+
-90,
|
|
126
|
+
90,
|
|
127
|
+
-180,
|
|
128
|
+
180,
|
|
129
|
+
)
|
|
130
|
+
geohash = np.zeros(precision, dtype="<U1")
|
|
131
|
+
bits = np.array([16, 8, 4, 2, 1])
|
|
132
|
+
bit = 0
|
|
133
|
+
ch = 0
|
|
134
|
+
n = 0
|
|
135
|
+
even = True
|
|
136
|
+
while n < precision:
|
|
137
|
+
if even:
|
|
138
|
+
mid = (lon_interval_neg + lon_interval_pos) / 2
|
|
139
|
+
if longitude > mid:
|
|
140
|
+
ch |= bits[bit]
|
|
141
|
+
lon_interval_neg = mid
|
|
142
|
+
else:
|
|
143
|
+
lon_interval_pos = mid
|
|
144
|
+
else:
|
|
145
|
+
mid = (lat_interval_neg + lat_interval_pos) / 2
|
|
146
|
+
if latitude > mid:
|
|
147
|
+
ch |= bits[bit]
|
|
148
|
+
lat_interval_neg = mid
|
|
149
|
+
else:
|
|
150
|
+
lat_interval_pos = mid
|
|
151
|
+
even = not even
|
|
152
|
+
|
|
153
|
+
if bit < 4:
|
|
154
|
+
bit += 1
|
|
155
|
+
else:
|
|
156
|
+
geohash[n] = __base32[ch]
|
|
157
|
+
bit = 0
|
|
158
|
+
ch = 0
|
|
159
|
+
n += 1
|
|
160
|
+
|
|
161
|
+
return "".join(geohash)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
@njit(fastmath=True)
|
|
165
|
+
def nb_vector_encode(latitudes: types.Array, longitudes: types.Array, precision: types.int8 = 12) -> types.Array:
|
|
166
|
+
"""
|
|
167
|
+
Encode a vector of points given by latitudes and longitudes to a vector of geohashes of the specified precision.
|
|
168
|
+
This is not exactly a vectorized version of nb_point_encode, but it is way faster and gets faster
|
|
169
|
+
as the number of points increase.
|
|
170
|
+
"""
|
|
171
|
+
n = len(latitudes)
|
|
172
|
+
geohashes = np.empty(n, dtype="<U12")
|
|
173
|
+
for i in range(n):
|
|
174
|
+
geohashes[i] = nb_point_encode(latitudes[i], longitudes[i], precision)
|
|
175
|
+
return geohashes
|
pygeohash/neighbor.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""
|
|
2
|
+
.. module:: neighbor
|
|
3
|
+
:platform: Unix, Windows
|
|
4
|
+
:synopsis: A module for calculating adjacent geohash
|
|
5
|
+
|
|
6
|
+
.. moduleauthor:: Nicolas Mine
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from pygeohash.geohash import __base32
|
|
12
|
+
|
|
13
|
+
# Configuration -- from https://github.com/davetroy/geohash-js/blob/master/geohash.js
|
|
14
|
+
NEIGHBORS = {
|
|
15
|
+
"right": {
|
|
16
|
+
"even": "bc01fg45238967deuvhjyznpkmstqrwx",
|
|
17
|
+
"odd": "p0r21436x8zb9dcf5h7kjnmqesgutwvy", # = top-even
|
|
18
|
+
},
|
|
19
|
+
"left": {
|
|
20
|
+
"even": "238967debc01fg45kmstqrwxuvhjyznp",
|
|
21
|
+
"odd": "14365h7k9dcfesgujnmqp0r2twvyx8zb", # = bottom-even
|
|
22
|
+
},
|
|
23
|
+
"top": {
|
|
24
|
+
"even": "p0r21436x8zb9dcf5h7kjnmqesgutwvy",
|
|
25
|
+
"odd": "bc01fg45238967deuvhjyznpkmstqrwx", # = right-even
|
|
26
|
+
},
|
|
27
|
+
"bottom": {
|
|
28
|
+
"even": "14365h7k9dcfesgujnmqp0r2twvyx8zb",
|
|
29
|
+
"odd": "238967debc01fg45kmstqrwxuvhjyznp", # = left-even
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
# Used change of parent tile
|
|
34
|
+
BORDERS = {
|
|
35
|
+
"right": {
|
|
36
|
+
"even": "bcfguvyz",
|
|
37
|
+
"odd": "prxz", # top-even
|
|
38
|
+
},
|
|
39
|
+
"left": {
|
|
40
|
+
"even": "0145hjnp",
|
|
41
|
+
"odd": "028b", # bottom-even
|
|
42
|
+
},
|
|
43
|
+
"top": {
|
|
44
|
+
"even": "prxz",
|
|
45
|
+
"odd": "bcfguvyz", # right-even
|
|
46
|
+
},
|
|
47
|
+
"bottom": {
|
|
48
|
+
"even": "028b",
|
|
49
|
+
"odd": "0145hjnp", # left-even
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def get_adjacent(geohash: str, direction: str) -> str:
|
|
55
|
+
"""
|
|
56
|
+
return the adjacent hash of a given geohash.
|
|
57
|
+
Direction can be right, left, top, bottom
|
|
58
|
+
"""
|
|
59
|
+
if len(geohash) == 0:
|
|
60
|
+
raise ValueError("The geohash length cannot be 0. Possible when close to poles")
|
|
61
|
+
source_hash = geohash.lower()
|
|
62
|
+
last_char = source_hash[-1]
|
|
63
|
+
base = source_hash[:-1]
|
|
64
|
+
|
|
65
|
+
split_direction = ["even", "odd"][len(source_hash) % 2]
|
|
66
|
+
|
|
67
|
+
if last_char in BORDERS[direction][split_direction]:
|
|
68
|
+
base = get_adjacent(base, direction)
|
|
69
|
+
|
|
70
|
+
return base + __base32[NEIGHBORS[direction][split_direction].index(last_char)]
|
pygeohash/stats.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""
|
|
2
|
+
.. module:: distances
|
|
3
|
+
:platform: Unix, Windows
|
|
4
|
+
:synopsis: A module for calculating distance measures with geohashes
|
|
5
|
+
|
|
6
|
+
.. moduleauthor:: Will McGinnis <will@pedalwrencher.com>
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import math
|
|
12
|
+
import statistics
|
|
13
|
+
from typing import Callable, Generator, Iterable, List
|
|
14
|
+
|
|
15
|
+
from pygeohash.distances import geohash_haversine_distance
|
|
16
|
+
from pygeohash.geohash import LatLong, decode, encode
|
|
17
|
+
|
|
18
|
+
__author__ = "Will McGinnis"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def __latitude(coordinate: LatLong) -> float:
|
|
22
|
+
return coordinate.latitude
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def __longitude(coordinate: LatLong) -> float:
|
|
26
|
+
return coordinate.longitude
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _max_cardinal(geohashes: Iterable[str], key: Callable[[LatLong], float], reverse: bool) -> str:
|
|
30
|
+
"""
|
|
31
|
+
Takes in an iterable of geohashes and returns the furthest position of the group for the cardinality as a geohash.
|
|
32
|
+
"""
|
|
33
|
+
coordinates: Generator[LatLong] = (decode(x) for x in geohashes)
|
|
34
|
+
m = max if reverse else min
|
|
35
|
+
coordinate = m(coordinates, key=key)
|
|
36
|
+
return encode(coordinate.latitude, coordinate.longitude)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def northern(geohashes: Iterable[str]) -> str:
|
|
40
|
+
"""
|
|
41
|
+
Takes in an iterable of geohashes and returns the northernmost position of the group as a geohash.
|
|
42
|
+
|
|
43
|
+
:param geohashes:
|
|
44
|
+
:return:
|
|
45
|
+
"""
|
|
46
|
+
return _max_cardinal(geohashes, __latitude, reverse=True)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def eastern(geohashes: Iterable[str]) -> str:
|
|
50
|
+
"""
|
|
51
|
+
Takes in an iterable of geohashes and returns the easternmost position of the group as a geohash.
|
|
52
|
+
|
|
53
|
+
:param geohashes:
|
|
54
|
+
:return:
|
|
55
|
+
"""
|
|
56
|
+
return _max_cardinal(geohashes, __longitude, reverse=True)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def western(geohashes: Iterable[str]) -> str:
|
|
60
|
+
"""
|
|
61
|
+
Takes in an iterable of geohashes and returns the westernmost position of the group as a geohash.
|
|
62
|
+
|
|
63
|
+
:param geohashes:
|
|
64
|
+
:return:
|
|
65
|
+
"""
|
|
66
|
+
return _max_cardinal(geohashes, __longitude, reverse=False)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def southern(geohashes: Iterable[str]) -> str:
|
|
70
|
+
"""
|
|
71
|
+
Takes in an iterable of geohashes and returns the southernmost position of the group as a geohash.
|
|
72
|
+
|
|
73
|
+
:param geohashes:
|
|
74
|
+
:return:
|
|
75
|
+
"""
|
|
76
|
+
return _max_cardinal(geohashes, __latitude, reverse=False)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def mean(geohashes: Iterable[str]) -> str:
|
|
80
|
+
"""
|
|
81
|
+
Takes in an iterable of geohashes and returns the mean position of the group as a geohash.
|
|
82
|
+
|
|
83
|
+
:param geohashes:
|
|
84
|
+
:return:
|
|
85
|
+
"""
|
|
86
|
+
coordinates: List[LatLong] = [decode(x) for x in geohashes]
|
|
87
|
+
return encode(
|
|
88
|
+
statistics.mean(x.latitude for x in coordinates),
|
|
89
|
+
statistics.mean(x.longitude for x in coordinates),
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def variance(geohashes: Iterable[str]) -> float:
|
|
94
|
+
"""
|
|
95
|
+
Calculates the variance of a set of geohashes (in meters)
|
|
96
|
+
|
|
97
|
+
:param geohashes:
|
|
98
|
+
:return:
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
mean_v = mean(geohashes)
|
|
102
|
+
dists = [geohash_haversine_distance(x, mean_v) for x in geohashes]
|
|
103
|
+
var = sum([x**2 for x in dists]) / float(len(dists))
|
|
104
|
+
return var
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def std(geohashes: Iterable[str]) -> float:
|
|
108
|
+
"""
|
|
109
|
+
Calculates the standard deviation of a set of geohashes (in meters)
|
|
110
|
+
|
|
111
|
+
:param geohashes:
|
|
112
|
+
:return:
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
return math.sqrt(variance(geohashes))
|