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$ $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)

View File

@ -153,7 +153,7 @@
<!-- Info --> <!-- Info -->
<div class="tab" id="infotab"></div> <div class="tab" id="infotab"></div>
</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> <div id="map" class="map"></div>
</body> </body>
</html> </html>

View File

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