diff --git a/main.py b/main.py index 6104d33..69cd71f 100644 --- a/main.py +++ b/main.py @@ -63,7 +63,6 @@ def buildGJSON(data): }""" headers = [["Content-Type", "application/vnd.geo+json"], ['Content-Disposition', 'attachment; filename="export.geojson"']] - return headers, output def buildGPX(data): @@ -97,6 +96,38 @@ def buildGPX(data): headers = [["Content-Type", "application/gpx+xml"], ['Content-Disposition', 'attachment; filename="export.gpx"']] return headers, output +def buildKML(data): + output = """ + + + + +#style +1a1ac94e +#ffff00 +0.4980392156862745 +4 +#00ff00 +0.4980392156862745 + + + +""" + + for row in data: + output += "%s,%s,%s " % (row["lon"], row["lat"], row["alt"] or "0") + + output += """ + + + + + +""" + + headers = [["Content-Type", "application/vnd.google-earth.kml+xml"], ['Content-Disposition', 'attachment; filename="export.kml"']] + return headers, output + def application(env, re): if env["REQUEST_METHOD"] == "POST": args = cgi.parse_qs(env['wsgi.input'].readline().decode(), True) @@ -206,18 +237,17 @@ def application(env, re): frm = on if on else args["from"][0] if "from" in args else None to = on if on else args["to"][0] if "to" in args else None - if "format" in args: - if args["format"][0] in ("json", "gjson", "geojson", "gj") or not args["format"]: - builder = buildGJSON - elif args["format"][0] == "gpx": - builder = buildGPX - else: - re("400 Bad Request", []) - yield "

400 Bad Request

".encode() - yield "Unknown format: %s" % args["format"] - return - else: + if not "format" in args or args["format"][0] in ("json", "gjson", "geojson", "gj") or not args["format"]: builder = buildGJSON + elif args["format"][0] == "gpx": + builder = buildGPX + elif args["format"][0] == "kml": + builder = buildKML + else: + re("400 Bad Request", []) + yield "

400 Bad Request

".encode() + yield "Unknown format: %s" % args["format"] + return frm = frm or "2000-01-01" to = to or datetime.datetime.now().strftime('%Y-%m-%d') @@ -233,7 +263,7 @@ def application(env, re): conn, cur = getDatabase() - sql = "SELECT ts, lat, lon FROM tracker WHERE device=%s AND DATE(ts)>=%s and DATE(ts)<=%s ORDER BY ts ASC;"; + sql = "SELECT * FROM tracker WHERE device=%s AND DATE(ts)>=%s and DATE(ts)<=%s ORDER BY ts ASC;"; cur.execute(sql, (device, frm, to)) data = cur.fetchall() diff --git a/setup.py b/setup.py index cfb8854..66661ff 100755 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ config.set('Database', 'user', user) config.set('Database', 'pass', pwd) config.set('Database', 'name', name) -sql1 = "CREATE TABLE IF NOT EXISTS tracker ( ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, device VARCHAR(38), lat DOUBLE, lon DOUBLE, PRIMARY KEY(ts, device) );"; +sql1 = "CREATE TABLE IF NOT EXISTS tracker ( ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, device VARCHAR(38), lat DOUBLE, lon DOUBLE, alt DOUBLE, PRIMARY KEY(ts, device) );"; sql2 = "CREATE TABLE IF NOT EXISTS users ( user VARCHAR(64) PRIMARY KEY, password VARCHAR(128), admin BOOLEAN );"; sql3 = "CREATE TABLE IF NOT EXISTS device ( device VARCHAR(38) PRIMARY KEY, passkey VARCHAR(128) );";