]> git.sthu.org Git - dvrdb.git/commitdiff
dvr-mangedb: Add -m command
authorStefan Huber <shuber@sthu.org>
Fri, 17 Jan 2014 19:00:56 +0000 (20:00 +0100)
committerStefan Huber <shuber@sthu.org>
Fri, 17 Jan 2014 19:00:56 +0000 (20:00 +0100)
dvr-managedb

index 001ce394eddb2640e726f2120727f860df9772ba..7db2daabaa43f0831ffbfd2e5bdb3a10829d0ab6 100755 (executable)
@@ -237,7 +237,10 @@ class Database:
         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."""
@@ -292,8 +295,71 @@ def printDataset(db, 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:
@@ -432,6 +498,7 @@ USAGE:
   {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:
@@ -443,6 +510,10 @@ 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.
@@ -457,10 +528,10 @@ if __name__ == "__main__":
     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
@@ -492,7 +563,7 @@ if __name__ == "__main__":
         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)
@@ -503,11 +574,17 @@ if __name__ == "__main__":
         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)