From ad3e9f4c32fdb5c96fa5253ced96e17cabcc5877 Mon Sep 17 00:00:00 2001 From: Ilya Zverev Date: Mon, 20 Jun 2016 18:08:59 +0300 Subject: [PATCH] Geocoding for countries --- mmwatch/process.py | 3 ++- mmwatch/server/geocode.py | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 mmwatch/server/geocode.py diff --git a/mmwatch/process.py b/mmwatch/process.py index 8d16a69..bda3e05 100755 --- a/mmwatch/process.py +++ b/mmwatch/process.py @@ -7,8 +7,9 @@ VENV_DIR = os.path.join(BASE_DIR, 'venv', 'lib', PYTHON, 'site-packages') if os.path.exists(VENV_DIR): sys.path.insert(1, VENV_DIR) -from server import mapsme_process +from server import mapsme_process, geocode mapsme_process.process() +geocode.add_countries() from db import State from server import parse_notes diff --git a/mmwatch/server/geocode.py b/mmwatch/server/geocode.py new file mode 100644 index 0000000..97f583e --- /dev/null +++ b/mmwatch/server/geocode.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +import urllib2 +import json +from db import database, Change + +QUERYAT_URL = 'http://tile.osmz.ru/queryat/' +BATCH_COUNT = 1000 + + +def geocode(lon, lat): + """Returns a country by these coordinates.""" + try: + url = '{0}qr?lon={1}&lat={2}'.format(QUERYAT_URL, lon, lat) + resp = urllib2.urlopen(url) + data = json.load(resp) + if 'countries' in data and len(data['countries']) > 0: + c = data['countries'][0] + return c['en' if 'en' in c else 'name'].encode('utf-8') + print('Could not geocode: {0}'.format(url)) + except urllib2.HTTPError: + print('HTTPError: ' + url) + except urllib2.URLError: + print('URLError: ' + url) + return None + + +def add_countries(): + database.connect() + with database.atomic(): + q = Change.select().where((Change.country >> None) & (Change.action != 'a') & (~Change.changes.startswith('[[null, null]'))).limit(BATCH_COUNT) + for ch in q: + coord = ch.changed_coord() + if coord is not None: + country = geocode(coord[0], coord[1]) + if country is not None: + ch.country = country[:150] + ch.save() + else: + print('Empty coordinates: {0} {1} {2}'.format(ch.id, ch.action, ch.changes.encode('utf-8'))) + + +if __name__ == '__main__': + add_countries()