Added option to read in old lookups.
This commit is contained in:
parent
9617cc5fa3
commit
2eca635f6d
Notes:
subgit
2018-03-07 17:59:00 +01:00
r679 www/trunk
@ -31,6 +31,7 @@
|
|||||||
$Id$
|
$Id$
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
def mkGeolocationTable(file=''):
|
||||||
# imports
|
# imports
|
||||||
try:
|
try:
|
||||||
import xml.etree.cElementTree as ET
|
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',
|
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
|
|
||||||
geolocationTable = {};
|
|
||||||
|
|
||||||
def simple_warning(message, category, filename, lineno, file=None, line=None):
|
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
|
||||||
|
|
||||||
|
# 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
|
# parse event.xml
|
||||||
DOM = ET.parse(stdin).getroot()
|
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]
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user