This commit is contained in:
Kasper D. Fischer 2014-06-03 13:59:42 +00:00
commit d1f9b64ab0
Notes: subgit 2018-03-07 17:59:01 +01:00
r683 www/branches/life
3 changed files with 104 additions and 68 deletions

View File

@ -31,37 +31,53 @@
$Id$
'''
# imports
try:
def mkGeolocationTable(file=''):
# imports
try:
import xml.etree.cElementTree as ET
except ImportError:
except ImportError:
import xml.etree.ElementTree as ET
from sys import stdin
import warnings
import urllib2 as URL
import json as JSON
from sys import stdin
import warnings
import urllib2 as URL
import json as JSON
# constants
namespaces = {'sc3': 'http://geofon.gfz-potsdam.de/ns/seiscomp3-schema/0.7',
# constants
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):
def simple_warning(message, category, filename, lineno, file=None, line=None):
return 'Warning: %s\n' % (message)
warnings.formatwarning = simple_warning
warnings.formatwarning = simple_warning
# parse event.xml
DOM = ET.parse(stdin).getroot()
# 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)
# iterate over all events
for event in DOM.iterfind('qml:eventParameters/qml:event', namespaces):
# parse event.xml
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]
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)+';'
# dump json
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)

View File

@ -153,7 +153,7 @@
<!-- Info -->
<div class="tab" id="infotab"></div>
</div>
<div class="rublogo"><a href="http://www.gmg.ruhr-uni-bochum.de/geophysik/seisobs"><img class="rublogo" src="logo_RUB_155x30.png" alt="Ruhr-Universität Bochum" title="Ruhr-Universität Bochum" /></a></div>
<div class="rublogo"><a href="http://www.gmg.ruhr-uni-bochum.de/geophysik/seisobs"><img class="rublogo" src="logo_RUB_155x30.png" alt="Ruhr-Universität Bochum" title="Ruhr-Universität Bochum" border="0"/></a></div>
<div id="map" class="map"></div>
</body>
</html>

View File

@ -1,5 +1,6 @@
/* $Id$ */
var specialEvents = [
'bug2014kowj', // Vogtland 5.0
'bug2014jptq', // Seeheim-Jugenheim
'bug2014infb', // Troisdorf
'bug2014ilxd', // Bassum