Added option to read in old lookups.

This commit is contained in:
Kasper D. Fischer 2014-06-01 13:56:56 +00:00
parent 9617cc5fa3
commit 2eca635f6d
Notes: subgit 2018-03-07 17:59:00 +01:00
r679 www/trunk

View File

@ -31,6 +31,7 @@
$Id$
'''
def mkGeolocationTable(file=''):
# imports
try:
import xml.etree.cElementTree as ET
@ -45,13 +46,20 @@ import json as JSON
namespaces = {'sc3': 'http://geofon.gfz-potsdam.de/ns/seiscomp3-schema/0.7',
'qml': 'http://quakeml.org/xmlns/bed/1.2'}
# initialise variables
geolocationTable = {};
def simple_warning(message, category, filename, lineno, file=None, line=None):
return 'Warning: %s\n' % (message)
warnings.formatwarning = simple_warning
# try loading the file
if file :
try:
jsonfile = open(file)
jsonfileContent = jsonfile.read().split('=')[1].replace(';', '')
geolocationTable = JSON.loads(jsonfileContent)
except:
geolocationTable = {}
warnings.warn('Could not parse file %s' %file)
# parse event.xml
DOM = ET.parse(stdin).getroot()
@ -60,8 +68,16 @@ for event in DOM.iterfind('qml:eventParameters/qml:event', namespaces):
publicID = event.attrib['publicID'].split('/')[2]
lat = event.find('./qml:origin/qml:latitude/qml:value', namespaces).text
lng = event.find('./qml:origin/qml:longitude/qml:value', namespaces).text
url = 'https://open.mapquestapi.com/nominatim/v1/reverse.php?lat={0}&lon={1}&zoom=10&format=json'.format(lat, lng)
evaluationMode = event.find('./qml:origin/qml:evaluationMode', namespaces).text
if publicID in geolocationTable:
warnings.warn('Skipping cached event %s' %(publicID))
elif evaluationMode == 'automatic':
warnings.warn('Skipping automatic event %s' %(publicID))
else:
#warnings.warn('Processing event %s' %publicID)
# send and evaluate nominatim request
url = 'https://open.mapquestapi.com/nominatim/v1/reverse.php?lat={0}&lon={1}&zoom=10&format=json'.format(lat, lng)
response = URL.urlopen(url)
if ( response.msg == 'OK' ):
data = JSON.loads(response.read())
@ -71,15 +87,34 @@ for event in DOM.iterfind('qml:eventParameters/qml:event', namespaces):
except:
warnings.warn('Using county instead of city for event {0} at {1} N / {2} E (URL: {3})'.format(publicID, lat, lng, url))
city = data['address']['county']
country = data['address']['country']
countryCode = data['address']['country_code'].upper()
value = city
if ( countryCode != 'DE' ):
value = '{0} ({1})'.format(value, countryCode)
geolocationTable[publicID] = value
if ( countryCode == 'DE' ):
geolocationTable[publicID] = city.encode('utf-8')
else:
geolocationTable[publicID] = '{0} ({1})'.format(city.encode('utf-8'), countryCode)
except:
warnings.warn('Could not extract city for event {0} at {1} N / {2} E (URL: {3})'.format(publicID, lat, lng, url))
else:
warnings.warn('Request {0} failed'.format(url))
# dump json
print 'var geolocationTable = '+JSON.dumps(geolocationTable)+';'
print 'var geolocationTable = '+JSON.dumps(geolocationTable, sort_keys=True)+';'
# __main__
if __name__ == "__main__":
def printline(line):
print line
# parse arguments
import argparse
versionText = '$Revision$ ($Date$, $Author$)'.replace('$', '').replace(':','')
parser = argparse.ArgumentParser(
description='Reverese geocoding lookup of events in xml format (stdin).',
epilog=versionText)
parser.add_argument('-v', '-V', '--version', action='version',
version=versionText)
parser.add_argument('-f', '--file', action='store', dest='file',
help='read in JSON file containing old output.')
cla = parser.parse_args()
# call mkGeolocationTable(...)
mkGeolocationTable(file=cla.file)