c = self.conn.cursor()
c.execute("SELECT * FROM geolocations WHERE dvrid=? AND provider=?", \
(dvrid, provider))
- return c.fetchone()
+ res = c.fetchone()
+ if res is None:
+ return None
+ return res[2:4]
def add_geolocation(self, dvrid, provider, lat, lon):
"""Add a geolocatoin for a given DVR-ID."""
return True
+def processMap(db, query, providers):
+ """Print a map webpage to stdout of the datasets selected by query, using
+ the given geocoding providers."""
+
+ print("""
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <script type="text/javascript" src="https://www.google.com/jsapi"></script>
+ <script type="text/javascript">
+ google.load("visualization", "1", {packages:["map"]});
+ google.setOnLoadCallback(drawMap);
+ function drawMap() {
+ var data = google.visualization.arrayToDataTable([
+ ['Lat', 'Lon', 'Name'],
+ """)
+
+ ids = db.query(query)
+ for id in ids:
+
+ # Get DVR
+ dvr = db.get_dvr(id)
+ if dvr is None:
+ continue
+
+ # Get location of ID
+ loc = None
+ for p in providers:
+ pidx = GeolocationProviders.getIndexByName(p)
+ loc = db.get_geolocation(id, pidx)
+ if loc is not None:
+ break
+
+ if loc is None:
+ print("Unknown location for ID %d." % id, file=sys.stderr)
+ continue
+
+ reg = db.get_registration(id)
+ link = "https://dvr.dsk.gv.at/at.gv.bka.dvr.public/AuftraggeberDetail.aspx?Id=%d" % id
+
+ text = "<b>ID:</b> %d, <b>DVR:</b> %d" % (id, dvr)
+ text += ", <a href=\"%s\">Link</a><br/>" % link
+ text += "<b>Name:</b> %s<br/>" % reg[1]
+ text += "<b>Address:</b> %s<br/>" % reg[2]
+ print("[%s, %s, %s]," % (loc[0], loc[1], repr(text)))
+
+ print("""
+ ]);
+
+ var map = new google.visualization.Map(document.getElementById('map_div'));
+ map.draw(data, {showTip: true, enableScrollWheel: true, useMapTypeControl: true});
+ }
+ </script>
+ </head>
+
+ <body>
+ <div id="map_div" style="width: 800px; height: 600px"></div>
+ </body>
+</html>
+ """)
+
+
def processGeolocation(db, query, provider):
"""Fetch and add geolocations for IDs selected by query."""
+ provider = providers.getIndexByName(provider)
ids = db.query(query)
for id in ids:
{0} -d FILE -a PATH [PATH...]
{0} -d FILE -q COND -l
{0} -d FILE -q COND -g PROVIDER [PROVIDER...]
+ {0} -d FILE -q COND -m PROVIDER [PROVIDER...]
{0} -h
COMMAND:
-l List the selected datasets.
-g Fetch and store geolocation of selected datasets from given
provider, if not existent. Provider is either 'google' or 'bing'.
+ -m Write a map website of selected datasets to stdout using the
+ coordinates from the given provider. If multiple providers are
+ given, take the coordinates from the first provider for which
+ coordinates are known.
OPTIONS:
-d FILE Use given sqlite3 database.
cmd = None
try:
- opts, args = getopt.getopt(sys.argv[1:], "ad:ghq:l")
+ opts, args = getopt.getopt(sys.argv[1:], "ad:ghq:lm")
for opt, arg in opts:
- if opt in ["-a", "-l", "-g"]:
+ if opt in ["-a", "-l", "-g", "-m"]:
cmd = opt
elif opt == "-d":
dbfn = arg
for arg in args:
processAdd(db, arg)
- if cmd in ["-l", "-g"]:
+ if cmd in ["-l", "-g", "-m"]:
if query is None:
print("No query option given.", file=sys.stderr)
sys.exit(os.EX_USAGE)
if cmd == "-g":
providers = GeolocationProviders
for arg in args:
-
if not providers.isNameValid(arg):
print("Unknown provider '%s'." % arg, file=sys.stderr)
sys.exit(os.EX_USAGE)
+ processGeolocation(db, query, arg)
- processGeolocation(db, query, providers.getIndexByName(arg))
+ if cmd == "-m":
+ providers = GeolocationProviders
+ for arg in args:
+ if not providers.isNameValid(arg):
+ print("Unknown provider '%s'." % arg, file=sys.stderr)
+ sys.exit(os.EX_USAGE)
+ processMap(db, query, args)
sys.exit(os.EX_OK)