2014-05-06 13:26:47 +02:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
'''
|
2014-05-06 14:32:24 +02:00
|
|
|
Script to lookup city names of events with Nominatim service
|
2014-05-06 13:26:47 +02:00
|
|
|
|
2014-05-06 14:32:24 +02:00
|
|
|
The input should be an valid quakeML file passed to stdin.
|
|
|
|
The output will will be a javascript structure to be included in the
|
|
|
|
SeisObs map service.
|
2014-05-06 13:26:47 +02:00
|
|
|
|
2014-05-06 14:32:24 +02:00
|
|
|
The script should be updated regulary keep the total number of all
|
|
|
|
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
|
|
|
|
|
|
|
|
License
|
|
|
|
Copyright 2014 Kasper D. Fischer <kasper.fischer@rub.de>
|
|
|
|
|
|
|
|
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
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program. If not, see http://www.gnu.org/licenses/.
|
|
|
|
|
|
|
|
$Id$
|
2014-05-06 13:26:47 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
# imports
|
|
|
|
try:
|
|
|
|
import xml.etree.cElementTree as ET
|
|
|
|
except ImportError:
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
from sys import stdin
|
2014-05-06 14:32:24 +02:00
|
|
|
import warnings
|
2014-05-06 13:26:47 +02:00
|
|
|
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'}
|
|
|
|
|
|
|
|
# initialise variables
|
|
|
|
geolocationTable = {};
|
|
|
|
|
2014-05-06 14:32:24 +02:00
|
|
|
def simple_warning(message, category, filename, lineno, file=None, line=None):
|
|
|
|
return 'Warning: %s\n' % (message)
|
|
|
|
warnings.formatwarning = simple_warning
|
|
|
|
|
2014-05-06 13:26:47 +02:00
|
|
|
# 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)
|
|
|
|
# send and evaluate nominatim request
|
|
|
|
response = URL.urlopen(url)
|
|
|
|
if ( response.msg == 'OK' ):
|
|
|
|
data = JSON.loads(response.read())
|
|
|
|
try:
|
2014-05-06 14:32:24 +02:00
|
|
|
try:
|
|
|
|
city = data['address']['city']
|
|
|
|
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']
|
2014-05-06 13:26:47 +02:00
|
|
|
country = data['address']['country']
|
|
|
|
countryCode = data['address']['country_code'].upper()
|
|
|
|
value = city
|
|
|
|
if ( countryCode != 'DE' ):
|
|
|
|
value = '{0} ({1})'.format(value, countryCode)
|
|
|
|
geolocationTable[publicID] = value
|
|
|
|
except:
|
2014-05-06 14:32:24 +02:00
|
|
|
warnings.warn('Could not extract city for event {0} at {1} N / {2} E (URL: {3})'.format(publicID, lat, lng, url))
|
2014-05-06 13:26:47 +02:00
|
|
|
else:
|
2014-05-06 14:32:24 +02:00
|
|
|
warnings.warn('Request {0} failed'.format(url))
|
2014-05-06 13:26:47 +02:00
|
|
|
# dump json
|
2014-05-06 14:32:24 +02:00
|
|
|
print 'var geolocationTable = '+JSON.dumps(geolocationTable)+';'
|