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
@ -2,84 +2,119 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Script to lookup city names of events with Nominatim service
|
Script to lookup city names of events with Nominatim service
|
||||||
|
|
||||||
The input should be an valid quakeML file passed to stdin.
|
The input should be an valid quakeML file passed to stdin.
|
||||||
The output will will be a javascript structure to be included in the
|
The output will will be a javascript structure to be included in the
|
||||||
SeisObs map service.
|
SeisObs map service.
|
||||||
|
|
||||||
The script should be updated regulary keep the total number of all
|
The script should be updated regulary keep the total number of all
|
||||||
AJAX calls to the Nominatim service small, e. g. :
|
AJAX calls to the Nominatim service small, e. g. :
|
||||||
curl -s "https://ariadne.geophysik.ruhr-uni-bochum.de/fdsnws/event/1/query?minlat=50&maxlat=54&minlon=3&maxlon=10&minmag=1" | mkGeolocationTable.py > geolocationTable.js
|
curl -s "https://ariadne.geophysik.ruhr-uni-bochum.de/fdsnws/event/1/query?minlat=50&maxlat=54&minlon=3&maxlon=10&minmag=1" | mkGeolocationTable.py > geolocationTable.js
|
||||||
|
|
||||||
License
|
License
|
||||||
Copyright 2014 Kasper D. Fischer <kasper.fischer@rub.de>
|
Copyright 2014 Kasper D. Fischer <kasper.fischer@rub.de>
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify it
|
This program is free software: you can redistribute it and/or modify it
|
||||||
under the terms of the GNU General Public License as published by the Free
|
under the terms of the GNU General Public License as published by the Free
|
||||||
Software Foundation, either version 3 of the License, or (at your option)
|
Software Foundation, either version 3 of the License, or (at your option)
|
||||||
any later version.
|
any later version.
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, but
|
This program is distributed in the hope that it will be useful, but
|
||||||
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
for more details.
|
for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License along
|
You should have received a copy of the GNU General Public License along
|
||||||
with this program. If not, see http://www.gnu.org/licenses/.
|
with this program. If not, see http://www.gnu.org/licenses/.
|
||||||
|
|
||||||
$Id$
|
$Id$
|
||||||
'''
|
'''
|
||||||
|
|
||||||
# imports
|
def mkGeolocationTable(file=''):
|
||||||
try:
|
# imports
|
||||||
import xml.etree.cElementTree as ET
|
try:
|
||||||
except ImportError:
|
import xml.etree.cElementTree as ET
|
||||||
import xml.etree.ElementTree as ET
|
except ImportError:
|
||||||
from sys import stdin
|
import xml.etree.ElementTree as ET
|
||||||
import warnings
|
from sys import stdin
|
||||||
import urllib2 as URL
|
import warnings
|
||||||
import json as JSON
|
import urllib2 as URL
|
||||||
|
import json as JSON
|
||||||
|
|
||||||
|
# constants
|
||||||
|
namespaces = {'sc3': 'http://geofon.gfz-potsdam.de/ns/seiscomp3-schema/0.7',
|
||||||
|
'qml': 'http://quakeml.org/xmlns/bed/1.2'}
|
||||||
|
|
||||||
# constants
|
def simple_warning(message, category, filename, lineno, file=None, line=None):
|
||||||
namespaces = {'sc3': 'http://geofon.gfz-potsdam.de/ns/seiscomp3-schema/0.7',
|
return 'Warning: %s\n' % (message)
|
||||||
'qml': 'http://quakeml.org/xmlns/bed/1.2'}
|
warnings.formatwarning = simple_warning
|
||||||
|
|
||||||
# initialise variables
|
# try loading the file
|
||||||
geolocationTable = {};
|
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)
|
||||||
|
|
||||||
def simple_warning(message, category, filename, lineno, file=None, line=None):
|
# parse event.xml
|
||||||
return 'Warning: %s\n' % (message)
|
DOM = ET.parse(stdin).getroot()
|
||||||
warnings.formatwarning = simple_warning
|
|
||||||
|
|
||||||
# parse event.xml
|
# iterate over all events
|
||||||
DOM = ET.parse(stdin).getroot()
|
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
|
||||||
|
evaluationMode = event.find('./qml:origin/qml:evaluationMode', namespaces).text
|
||||||
|
|
||||||
# iterate over all events
|
if publicID in geolocationTable:
|
||||||
for event in DOM.iterfind('qml:eventParameters/qml:event', namespaces):
|
warnings.warn('Skipping cached event %s' %(publicID))
|
||||||
publicID = event.attrib['publicID'].split('/')[2]
|
elif evaluationMode == 'automatic':
|
||||||
lat = event.find('./qml:origin/qml:latitude/qml:value', namespaces).text
|
warnings.warn('Skipping automatic event %s' %(publicID))
|
||||||
lng = event.find('./qml:origin/qml:longitude/qml:value', namespaces).text
|
else:
|
||||||
url = 'https://open.mapquestapi.com/nominatim/v1/reverse.php?lat={0}&lon={1}&zoom=10&format=json'.format(lat, lng)
|
#warnings.warn('Processing event %s' %publicID)
|
||||||
# send and evaluate nominatim request
|
# send and evaluate nominatim request
|
||||||
response = URL.urlopen(url)
|
url = 'https://open.mapquestapi.com/nominatim/v1/reverse.php?lat={0}&lon={1}&zoom=10&format=json'.format(lat, lng)
|
||||||
if ( response.msg == 'OK' ):
|
response = URL.urlopen(url)
|
||||||
data = JSON.loads(response.read())
|
if ( response.msg == 'OK' ):
|
||||||
try:
|
data = JSON.loads(response.read())
|
||||||
try:
|
try:
|
||||||
city = data['address']['city']
|
try:
|
||||||
except:
|
city = data['address']['city']
|
||||||
warnings.warn('Using county instead of city for event {0} at {1} N / {2} E (URL: {3})'.format(publicID, lat, lng, url))
|
except:
|
||||||
city = data['address']['county']
|
warnings.warn('Using county instead of city for event {0} at {1} N / {2} E (URL: {3})'.format(publicID, lat, lng, url))
|
||||||
country = data['address']['country']
|
city = data['address']['county']
|
||||||
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