Last active
February 22, 2017 16:26
-
-
Save kgorman/781793c8aa69fefd5818f8513bd77427 to your computer and use it in GitHub Desktop.
georoutes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def build_routes(): | |
""" return a geojson object with flight paths over last day """ | |
conn = database_connect() | |
cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) | |
output = cur.execute( | |
""" | |
SELECT DISTINCT(tail) | |
AS tail | |
FROM planedemo_routes | |
WHERE lat != '' | |
AND tail != 'None' | |
AND arrival_timestamp > (clock_timestamp() - '1 hour'::interval) | |
""" | |
) | |
results = cur.fetchall() | |
geojson = [] | |
for i in results: | |
sql = """SELECT lat,long FROM planedemo_routes | |
WHERE tail = '{}' | |
AND lat != '' | |
AND arrival_timestamp > | |
(clock_timestamp() - '1 hour'::interval) | |
""".format(i['tail']) | |
coords = cur.execute(sql) | |
nested_output = cur.fetchall() | |
coordinates = [] | |
for ii in nested_output: | |
latlong = [ii['long'], ii['lat']] | |
coordinates.append(latlong) | |
georow = {"type": "Feature", | |
"properties": { | |
"stroke": "#008e00", | |
"stroke-width": "2", | |
"stroke-opacity": ".2" | |
}, | |
"geometry": { | |
"type": "LineString", | |
"coordinates": coordinates | |
} | |
} | |
geojson.append(georow) | |
return json.dumps(geojson) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment