diff --git a/README.md b/README.md index 3c7b051..8ff5d9d 100644 --- a/README.md +++ b/README.md @@ -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". diff --git a/geolocator.py b/geolocator.py new file mode 100644 index 0000000..d954187 --- /dev/null +++ b/geolocator.py @@ -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) diff --git a/main.py b/main.py index d181c52..1ffbcd5 100755 --- a/main.py +++ b/main.py @@ -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__': diff --git a/service.py b/service.py index 97645f5..128dca9 100644 --- a/service.py +++ b/service.py @@ -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"))