Re-implement KML output, create altitude database field in setup

This commit is contained in:
Klaus-Uwe Mitterer 2017-12-09 23:27:33 +01:00
parent 043f4a8569
commit cb9feea896
2 changed files with 44 additions and 14 deletions

42
main.py
View file

@ -63,7 +63,6 @@ def buildGJSON(data):
}""" }"""
headers = [["Content-Type", "application/vnd.geo+json"], ['Content-Disposition', 'attachment; filename="export.geojson"']] headers = [["Content-Type", "application/vnd.geo+json"], ['Content-Disposition', 'attachment; filename="export.geojson"']]
return headers, output return headers, output
def buildGPX(data): def buildGPX(data):
@ -97,6 +96,38 @@ def buildGPX(data):
headers = [["Content-Type", "application/gpx+xml"], ['Content-Disposition', 'attachment; filename="export.gpx"']] headers = [["Content-Type", "application/gpx+xml"], ['Content-Disposition', 'attachment; filename="export.gpx"']]
return headers, output return headers, output
def buildKML(data):
output = """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<ExtendedData>
<Data name="styleUrl"><value>#style</value></Data>
<Data name="styleHash"><value>1a1ac94e</value></Data>
<Data name="stroke"><value>#ffff00</value></Data>
<Data name="stroke-opacity"><value>0.4980392156862745</value></Data>
<Data name="stroke-width"><value>4</value></Data>
<Data name="fill"><value>#00ff00</value></Data>
<Data name="fill-opacity"><value>0.4980392156862745</value></Data>
</ExtendedData>
<LineString>
<coordinates>
"""
for row in data:
output += "%s,%s,%s " % (row["lon"], row["lat"], row["alt"] or "0")
output += """
</coordinates>
</LineString>
</Placemark>
</Document>
</kml>
"""
headers = [["Content-Type", "application/vnd.google-earth.kml+xml"], ['Content-Disposition', 'attachment; filename="export.kml"']]
return headers, output
def application(env, re): def application(env, re):
if env["REQUEST_METHOD"] == "POST": if env["REQUEST_METHOD"] == "POST":
args = cgi.parse_qs(env['wsgi.input'].readline().decode(), True) 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 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 to = on if on else args["to"][0] if "to" in args else None
if "format" in args: if not "format" in args or args["format"][0] in ("json", "gjson", "geojson", "gj") or not args["format"]:
if args["format"][0] in ("json", "gjson", "geojson", "gj") or not args["format"]:
builder = buildGJSON builder = buildGJSON
elif args["format"][0] == "gpx": elif args["format"][0] == "gpx":
builder = buildGPX builder = buildGPX
elif args["format"][0] == "kml":
builder = buildKML
else: else:
re("400 Bad Request", []) re("400 Bad Request", [])
yield "<h1>400 Bad Request</h1>".encode() yield "<h1>400 Bad Request</h1>".encode()
yield "Unknown format: %s" % args["format"] yield "Unknown format: %s" % args["format"]
return return
else:
builder = buildGJSON
frm = frm or "2000-01-01" frm = frm or "2000-01-01"
to = to or datetime.datetime.now().strftime('%Y-%m-%d') to = to or datetime.datetime.now().strftime('%Y-%m-%d')
@ -233,7 +263,7 @@ def application(env, re):
conn, cur = getDatabase() 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)) cur.execute(sql, (device, frm, to))
data = cur.fetchall() data = cur.fetchall()

View file

@ -25,7 +25,7 @@ config.set('Database', 'user', user)
config.set('Database', 'pass', pwd) config.set('Database', 'pass', pwd)
config.set('Database', 'name', name) 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 );"; 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) );"; sql3 = "CREATE TABLE IF NOT EXISTS device ( device VARCHAR(38) PRIMARY KEY, passkey VARCHAR(128) );";