Skip to content

Commit

Permalink
geocode aggregation, no caching yet
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruskov Martin Consulente authored and Ruskov Martin Consulente committed Jan 14, 2019
1 parent 9f52203 commit 292fca9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This is a test service for her prototype for http://cargocollective.com/lialessa
- Python3 (https://www.python.org)
- bottle (http://bottlepy.org)
- SQLAlchemy (http://www.sqlalchemy.org) using SQLite (https://sqlite.org)
- GeoPy (https://github.com/geopy) using GeocodeFarm (https://geocode.farm)
- GeoPy (https://github.com/geopy) using GeocodeFarm (https://geocode.farm), ArcGIS (https://www.arcgis.com) and Yandex (https://yandex.com/maps/)

## Executables
- To test web services, run "python test.py".
Expand Down
36 changes: 36 additions & 0 deletions geolocator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import numpy as np

# The geolocators in geopy that do not expect api_key
from geopy.geocoders import GeocodeFarm, Yandex, ArcGIS

locators = [GeocodeFarm(), Yandex(), ArcGIS()]

class Coordinates:
def __init__(self, latitude, longitude):
self.latitude=latitude
self.longitude=longitude

def reject_outliers(data, alpha = 90):
mask = (data.lat < np.percentile(data.lat, alpha)) & (data.long < np.percentile(data.long, alpha))
return data[mask]

def geocode(address):
#candidates = np.array([], dtype=[('long',float),('lat', float)])
candidates = []
for locator in locators:
rloc = locator.geocode(address)
if rloc:
#print(rloc.raw)
candidates.append((rloc.latitude, rloc.longitude))
#coords = np.core.records.fromrecords([x.values() for x in candidates], names=candidates[0].keys())
#candidates.append(rloc)
#a = np.array([(1, 2.0), (1, 2.0)], dtype=[('x', int), ('y', float)])
coords = np.core.records.fromrecords(candidates, names='lat,long')
if len(coords) > 2:
coords = reject_outliers(coords)
#return {"latitude": np.average(coords.lat), "longitude": np.average(coords.long)}
return Coordinates(np.average(coords.lat), np.average(coords.long))

if __name__ == '__main__':
result = geocode("VIA DELLA CASETTA MATTEI 205")
print(result)
11 changes: 9 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,17 @@ def root():
return static_file('index.html', root=static_path)

def start_server():
import os

from settings import curdir, db_url
#print ("Working in " + curdir)

from db import reset_db

print("Starting in %s"%curdir)
print("With database %s"%db_url)
if os.path.exists(db_url):
print("With database %s"%db_url)
else:
reset_db(blank=True)
run(host=host, port=port, debug=debug)

if __name__ == '__main__':
Expand Down
5 changes: 1 addition & 4 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
from datetime import datetime
from urllib.parse import unquote

# from geopy.geocoders import ArcGIS as Geocoder
from geopy.geocoders import GeocodeFarm as Geocoder
import geolocator as locator

from settings import debug

from db import Base, engine, Session
from db import Locations

locator = Geocoder()

def loc2csv(loc):
return "%s,%s"%(getattr(loc, "latitude"),getattr(loc, "longitude"))

Expand Down

0 comments on commit 292fca9

Please sign in to comment.