2021-10-16 15:51:38 +02:00
|
|
|
#! /usr/bin/env python3
|
2014-05-06 13:26:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
'''
|
2014-06-01 15:56:56 +02:00
|
|
|
Script to lookup city names of events with Nominatim service
|
2014-05-06 13:26:47 +02:00
|
|
|
|
2014-06-01 15:56:56 +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
|
|
|
|
2021-10-16 15:51:38 +02:00
|
|
|
The script should be updated regularly keep the total number of all
|
2014-06-01 15:56:56 +02:00
|
|
|
AJAX calls to the Nominatim service small, e. g. :
|
2021-10-16 15:51:38 +02:00
|
|
|
curl -s "https://fdsnws.geophysik.ruhr-uni-bochum.de/fdsnws/event/1/query?minlat=50&maxlat=54&minlon=3&maxlon=10&minmag=1" | mkGeolocationTable.py > geolocationTable.js
|
2014-05-06 14:32:24 +02:00
|
|
|
|
2021-10-27 10:27:32 +02:00
|
|
|
Version: VVVVV
|
|
|
|
|
2014-06-01 15:56:56 +02:00
|
|
|
License
|
2021-10-16 19:27:16 +02:00
|
|
|
Copyright 2020-2021 Kasper D. Fischer <kasper.fischer@rub.de>
|
2014-05-06 14:32:24 +02:00
|
|
|
|
2014-06-01 15:56:56 +02:00
|
|
|
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.
|
2014-05-06 14:32:24 +02:00
|
|
|
|
2014-06-01 15:56:56 +02:00
|
|
|
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.
|
2014-05-06 14:32:24 +02:00
|
|
|
|
2014-06-01 15:56:56 +02:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program. If not, see http://www.gnu.org/licenses/.
|
2014-05-06 13:26:47 +02:00
|
|
|
'''
|
|
|
|
|
2014-06-01 15:56:56 +02:00
|
|
|
def mkGeolocationTable(file=''):
|
2021-10-16 19:27:16 +02:00
|
|
|
## imports
|
|
|
|
|
|
|
|
# XML ElementTree
|
2014-06-01 15:56:56 +02:00
|
|
|
try:
|
|
|
|
import xml.etree.cElementTree as ET
|
|
|
|
except ImportError:
|
|
|
|
import xml.etree.ElementTree as ET
|
2021-10-16 19:27:16 +02:00
|
|
|
|
|
|
|
# json
|
|
|
|
import json as JSON
|
|
|
|
|
|
|
|
# sys stdin
|
2014-06-01 15:56:56 +02:00
|
|
|
from sys import stdin
|
2021-10-16 19:27:16 +02:00
|
|
|
|
|
|
|
# geopy
|
2015-10-01 16:37:41 +02:00
|
|
|
from geopy.geocoders import Photon
|
2021-10-16 15:53:15 +02:00
|
|
|
from geopy.extra.rate_limiter import RateLimiter
|
|
|
|
from geopy.exc import GeocoderServiceError
|
2014-11-25 10:53:29 +01:00
|
|
|
|
2021-10-16 19:27:16 +02:00
|
|
|
## constants
|
|
|
|
URL = 'https://photon.komoot.io/reverse?lon={lng:.3f}&lat={lat:.3f}&limit=5'
|
|
|
|
NAMESPACES = {'sc3': 'http://geofon.gfz-potsdam.de/ns/seiscomp3-schema/0.7',
|
2014-06-01 15:56:56 +02:00
|
|
|
'qml': 'http://quakeml.org/xmlns/bed/1.2'}
|
|
|
|
|
|
|
|
# try loading the file
|
2015-10-01 16:37:41 +02:00
|
|
|
geolocationTable = {}
|
2014-06-01 15:56:56 +02:00
|
|
|
if file :
|
|
|
|
try:
|
|
|
|
jsonfile = open(file)
|
|
|
|
jsonfileContent = jsonfile.read().split('=')[1].replace(';', '')
|
|
|
|
geolocationTable = JSON.loads(jsonfileContent)
|
|
|
|
except:
|
|
|
|
geolocationTable = {}
|
2021-10-16 19:27:16 +02:00
|
|
|
logging.warning('Could not parse file %s' %file)
|
2014-06-01 15:56:56 +02:00
|
|
|
|
|
|
|
# parse event.xml
|
|
|
|
DOM = ET.parse(stdin).getroot()
|
2015-10-01 16:37:41 +02:00
|
|
|
geolocator = Photon()
|
2021-10-16 15:53:15 +02:00
|
|
|
reverse_geolocate = RateLimiter(geolocator.reverse, min_delay_seconds=1)
|
2014-06-01 15:56:56 +02:00
|
|
|
|
|
|
|
# iterate over all events
|
2021-10-16 19:27:16 +02:00
|
|
|
count = 0
|
|
|
|
for event in DOM.iterfind('qml:eventParameters/qml:event', NAMESPACES):
|
|
|
|
count += 1
|
2014-06-01 15:56:56 +02:00
|
|
|
publicID = event.attrib['publicID'].split('/')[2]
|
2021-10-16 19:27:16 +02:00
|
|
|
lat = float(event.find('./qml:origin/qml:latitude/qml:value', NAMESPACES).text)
|
|
|
|
lng = float(event.find('./qml:origin/qml:longitude/qml:value', NAMESPACES).text)
|
|
|
|
evaluationMode = event.find('./qml:origin/qml:evaluationMode', NAMESPACES).text
|
2014-06-01 15:56:56 +02:00
|
|
|
|
|
|
|
if publicID in geolocationTable:
|
2021-10-16 19:27:16 +02:00
|
|
|
logging.warning('Skipping cached event {id}'.format(id=publicID))
|
2014-06-01 15:56:56 +02:00
|
|
|
elif evaluationMode == 'automatic':
|
2021-10-16 19:27:16 +02:00
|
|
|
logging.warning('Skipping automatic event {id}'.format(id=publicID))
|
2014-06-01 15:56:56 +02:00
|
|
|
else:
|
2021-10-16 19:27:16 +02:00
|
|
|
logging.info('Processing event {id}'.format(id=publicID))
|
2015-10-01 16:37:41 +02:00
|
|
|
try:
|
2021-10-16 15:53:15 +02:00
|
|
|
locations = reverse_geolocate("{lat:.3f}, {lng:.3f}".format(lat=lat, lng=lng),exactly_one=False,limit=5)
|
|
|
|
except GeocoderServiceError:
|
2021-10-16 19:27:16 +02:00
|
|
|
logging.warning('Reverse Geolocation failed. Skipping event.')
|
2015-10-01 16:37:41 +02:00
|
|
|
continue
|
|
|
|
place = []
|
2021-10-16 15:53:15 +02:00
|
|
|
for location in locations:
|
2015-10-01 16:37:41 +02:00
|
|
|
try:
|
2021-10-16 15:53:15 +02:00
|
|
|
place = location.raw['properties']['city']
|
2015-10-01 16:37:41 +02:00
|
|
|
except KeyError:
|
|
|
|
try:
|
2021-10-16 15:53:15 +02:00
|
|
|
place = location.raw['properties']['town']
|
2015-10-01 16:37:41 +02:00
|
|
|
except KeyError:
|
|
|
|
try:
|
2021-10-16 15:53:15 +02:00
|
|
|
place = location.raw['properties']['village']
|
2015-10-01 16:37:41 +02:00
|
|
|
except KeyError:
|
2014-11-25 10:53:29 +01:00
|
|
|
try:
|
2021-10-16 15:53:15 +02:00
|
|
|
place = location.raw['properties']['county']
|
|
|
|
except KeyError:
|
2021-10-16 19:27:16 +02:00
|
|
|
logging.debug('Could not extract city for event {id} at {lat:.3f} N / {lng:.3f} E (Service: {url}), trying next returned result'
|
|
|
|
.format(id=publicID, lat=lat, lng=lng, url=URL.format(lat=lat,lng=lng)))
|
|
|
|
logging.debug(location.raw)
|
2021-10-16 15:53:15 +02:00
|
|
|
if not place:
|
2021-10-16 19:27:16 +02:00
|
|
|
logging.critical('Could not extract city for event {id} at {lat:.3f} N / {lng:.3f} E (Service: {url})'
|
|
|
|
.format(id=publicID, lat=lat, lng=lng, url=URL.format(lat=lat,lng=lng)))
|
2021-10-16 15:53:15 +02:00
|
|
|
geolocationTable[publicID] = place
|
|
|
|
|
2014-06-01 15:56:56 +02:00
|
|
|
# dump json
|
2021-10-16 15:51:38 +02:00
|
|
|
print('var geolocationTable = {0};'.format(JSON.dumps(geolocationTable, sort_keys=True)))
|
2021-10-16 19:27:16 +02:00
|
|
|
logging.info("processed %d events", count)
|
2014-06-01 15:56:56 +02:00
|
|
|
|
|
|
|
# __main__
|
|
|
|
if __name__ == "__main__":
|
2021-10-16 19:27:16 +02:00
|
|
|
# use module logging
|
|
|
|
import logging
|
|
|
|
|
2014-06-01 15:56:56 +02:00
|
|
|
# parse arguments
|
|
|
|
import argparse
|
2021-10-27 10:27:32 +02:00
|
|
|
versionText = 'VVVVV'
|
2014-06-01 15:56:56 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
2021-10-16 15:51:38 +02:00
|
|
|
description='Reverse geocoding lookup of events in xml format (stdin).',
|
2014-06-01 15:56:56 +02:00
|
|
|
epilog=versionText)
|
2021-10-16 19:27:16 +02:00
|
|
|
parser.add_argument('-V', '--version', action='version', version=versionText,
|
|
|
|
help="show version")
|
2014-06-01 15:56:56 +02:00
|
|
|
parser.add_argument('-f', '--file', action='store', dest='file',
|
|
|
|
help='read in JSON file containing old output.')
|
2021-10-16 19:27:16 +02:00
|
|
|
group = parser.add_mutually_exclusive_group()
|
|
|
|
group.add_argument("-v", "--verbose", action="store_true",
|
|
|
|
help="increase output verbosity")
|
|
|
|
group.add_argument("-q", "--quiet", action="store_true",
|
|
|
|
help="disable output")
|
|
|
|
group.add_argument("-d", "--debug", action="store_true",
|
|
|
|
help="show debugging output",
|
|
|
|
)
|
2014-06-01 15:56:56 +02:00
|
|
|
cla = parser.parse_args()
|
|
|
|
|
2021-10-16 19:27:16 +02:00
|
|
|
# set logging level
|
|
|
|
if cla.quiet:
|
|
|
|
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.ERROR)
|
|
|
|
elif cla.verbose:
|
|
|
|
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
|
|
|
|
elif cla.debug:
|
|
|
|
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
|
|
|
|
else:
|
|
|
|
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.WARN)
|
|
|
|
|
2014-06-01 15:56:56 +02:00
|
|
|
# call mkGeolocationTable(...)
|
|
|
|
mkGeolocationTable(file=cla.file)
|