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,37 +31,53 @@
$Id$ $Id$
''' '''
# imports def mkGeolocationTable(file=''):
try: # imports
try:
import xml.etree.cElementTree as ET import xml.etree.cElementTree as ET
except ImportError: except ImportError:
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from sys import stdin from sys import stdin
import warnings import warnings
import urllib2 as URL import urllib2 as URL
import json as JSON import json as JSON
# constants # constants
namespaces = {'sc3': 'http://geofon.gfz-potsdam.de/ns/seiscomp3-schema/0.7', namespaces = {'sc3': 'http://geofon.gfz-potsdam.de/ns/seiscomp3-schema/0.7',
'qml': 'http://quakeml.org/xmlns/bed/1.2'} 'qml': 'http://quakeml.org/xmlns/bed/1.2'}
# initialise variables def simple_warning(message, category, filename, lineno, file=None, line=None):
geolocationTable = {};
def simple_warning(message, category, filename, lineno, file=None, line=None):
return 'Warning: %s\n' % (message) return 'Warning: %s\n' % (message)
warnings.formatwarning = simple_warning warnings.formatwarning = simple_warning
# parse event.xml # try loading the file
DOM = ET.parse(stdin).getroot() 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)
# iterate over all events # parse event.xml
for event in DOM.iterfind('qml:eventParameters/qml:event', namespaces): DOM = ET.parse(stdin).getroot()
# iterate over all events
for event in DOM.iterfind('qml:eventParameters/qml:event', namespaces):
publicID = event.attrib['publicID'].split('/')[2] publicID = event.attrib['publicID'].split('/')[2]
lat = event.find('./qml:origin/qml:latitude/qml:value', namespaces).text lat = event.find('./qml:origin/qml:latitude/qml:value', namespaces).text
lng = event.find('./qml:origin/qml:longitude/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 # 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) response = URL.urlopen(url)
if ( response.msg == 'OK' ): if ( response.msg == 'OK' ):
data = JSON.loads(response.read()) data = JSON.loads(response.read())
@ -71,15 +87,34 @@ for event in DOM.iterfind('qml:eventParameters/qml:event', namespaces):
except: except:
warnings.warn('Using county instead of city for event {0} at {1} N / {2} E (URL: {3})'.format(publicID, lat, lng, url)) 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'] city = data['address']['county']
country = data['address']['country']
countryCode = data['address']['country_code'].upper() countryCode = data['address']['country_code'].upper()
value = city if ( countryCode == 'DE' ):
if ( countryCode != 'DE' ): geolocationTable[publicID] = city.encode('utf-8')
value = '{0} ({1})'.format(value, countryCode) else:
geolocationTable[publicID] = value geolocationTable[publicID] = '{0} ({1})'.format(city.encode('utf-8'), countryCode)
except: except:
warnings.warn('Could not extract city for event {0} at {1} N / {2} E (URL: {3})'.format(publicID, lat, lng, url)) warnings.warn('Could not extract city for event {0} at {1} N / {2} E (URL: {3})'.format(publicID, lat, lng, url))
else: else:
warnings.warn('Request {0} failed'.format(url)) warnings.warn('Request {0} failed'.format(url))
# dump json # 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)