Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions reverse_geocoder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,23 @@ def __init__(self, mode=2, verbose=True, stream=None):
else:
coordinates, self.locations = self.extract(rel_path(RG_FILE))

ecef_coordinates = geodetic_in_ecef(coordinates)
if mode == 1: # Single-process
self.tree = KDTree(coordinates)
self.tree = KDTree(ecef_coordinates)
else: # Multi-process
self.tree = KDTree_MP.cKDTree_MP(coordinates)
self.tree = KDTree_MP.cKDTree_MP(ecef_coordinates)

def query(self, coordinates):
"""
Function to query the K-D tree to find the nearest city
Args:
coordinates (list): List of tuple coordinates, i.e. [(latitude, longitude)]
"""
ecef_coordinates = geodetic_in_ecef(coordinates)
if self.mode == 1:
_, indices = self.tree.query(coordinates, k=1)
_, indices = self.tree.query(ecef_coordinates, k=1)
else:
_, indices = self.tree.pquery(coordinates, k=1)
_, indices = self.tree.pquery(ecef_coordinates, k=1)
return [self.locations[index] for index in indices]

def load(self, stream):
Expand Down Expand Up @@ -260,7 +262,7 @@ def geodetic_in_ecef(geo_coords):

x = normal * np.cos(lat_r) * np.cos(lon_r)
y = normal * np.cos(lat_r) * np.sin(lon_r)
z = normal * (1 - E2) * np.sin(lat)
z = normal * (1 - E2) * np.sin(lat_r)

return np.column_stack([x, y, z])

Expand Down