Geocoding for countries
This commit is contained in:
parent
a72a63c290
commit
ad3e9f4c32
2 changed files with 45 additions and 1 deletions
|
@ -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
|
||||
|
|
43
mmwatch/server/geocode.py
Normal file
43
mmwatch/server/geocode.py
Normal file
|
@ -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()
|
Loading…
Add table
Reference in a new issue