Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: reverse geocoder #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions geocode/geocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import hashlib
import json
from .flags import flags
from .reverse_geocoder import ReverseGeocoder


logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)-5.5s] [%(name)-12.12s]: %(message)s')
Expand Down Expand Up @@ -312,6 +313,9 @@ def get_arguments_hash(self):
geocode_args = [str(self.min_population_cutoff), str(self.large_city_population_cutoff)] + self.location_types
return hashlib.sha256(','.join(geocode_args).encode()).hexdigest()[:15]

def build_reverse_geocoder(self):
return ReverseGeocoder(self.geo_data)

# private

def _get_location_types(self, location_types):
Expand Down
18 changes: 18 additions & 0 deletions geocode/reverse_geocoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np
from sklearn.neighbors import KDTree

class ReverseGeocoder():

def __init__(self, geo_data):
self.geo_data = geo_data

coord_arr = np.asarray(list((d[3], d[4]) for d in self.geo_data))

self.tree = KDTree(coord_arr)

def nearest_geodata(self, lon, lat):
value = (lon, lat)

index = self.tree.query([value])[1][0][0]

return self.geo_data[index]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pandas>=1
tqdm
flashtext
joblib
scikit-learn