diff --git a/lambda/query_ham/__init__.py b/lambda/query_ham/__init__.py index 955864d..8f62bc3 100644 --- a/lambda/query_ham/__init__.py +++ b/lambda/query_ham/__init__.py @@ -274,7 +274,7 @@ def get_telem_full(event, context): lt = requested_time + timedelta(0, 1) gte = requested_time - timedelta(0, last) - path = f"ham-telm-{lt.year:2}-{lt.month:02},telm-{gte.year:2}-{gte.month:02}/_search" + path = f"ham-telm-*/_search" payload = { "timeout": "30s", "size": 10000, @@ -370,6 +370,70 @@ def get_telem_full(event, context): fc.writeheader() fc.writerows(data) data = csv_output.getvalue() + elif ( + "queryStringParameters" in event + and "format" in event["queryStringParameters"] + and event["queryStringParameters"]['format'] == "kml" + ): + content_type = "application/vnd.google-earth.kml+xml" + + # Extract some basic flight info for use in KML Metadata + callsign = str(event["pathParameters"]["payload_callsign"]) + start_datetime = gte.isoformat() + + # Only get unique date/time data. + # This results in a much shorter dictionary. + _temp_data = {} + for _telem in data: + _temp_data[_telem['datetime']] = f"{float(_telem['lon']):.6f},{float(_telem['lat']):.6f},{float(_telem['alt']):.1f}\n" + + # For a KML output, the data *must* be sorted, else the KML LineString becomes a complete mess. + # Get the keys from the dictionary generated above, and sort them. + data_keys = list(_temp_data.keys()) + data_keys.sort() + + # Now generate the LineString data (lon,lat,alt) + # This could possibly done with a .join, but I suspect that wont be much more efficient. + kml_coords = "" + for _key in data_keys: + kml_coords += _temp_data[_key] + + # Generate the output KML. + # This is probably the simplest way of handling this without bringing in + # any other libraries. + kml_out = f""" + + 1 + + {start_datetime} {callsign} + Flight Path + 1 + + {start_datetime} {callsign} + 1 + + aaffffff + 2.0 + + + 20000000 + 1 + 1 + + + 1 + absolute + + {kml_coords} + + + + + + """ + + # Finally replace the data with the kml data + data = kml_out else: data = json.dumps(data) diff --git a/lambda/query_ham/__main__.py b/lambda/query_ham/__main__.py index 7d9c4c4..2b4fc72 100644 --- a/lambda/query_ham/__main__.py +++ b/lambda/query_ham/__main__.py @@ -6,12 +6,12 @@ import zlib response = get_telem_full( { "pathParameters": { - "payload_callsign" : "HORUS-V2" + "payload_callsign" : "HORUSBINARY" }, "queryStringParameters":{ - "last": "3600", - "datetime": "2022-05-07T04:18:10.000000Z", - "format": "csv" + "last": "10800", + "datetime": "2022-06-26T08:30:00.000001Z", + "format": "kml" } }, {}) print(len(response['body'])) @@ -19,4 +19,5 @@ compressed = base64.b64decode(response['body']) decompressed = (zlib.decompress(compressed, 16 + zlib.MAX_WBITS)) #print(json.loads(decompressed)) -print(decompressed.decode().splitlines()[:5]) \ No newline at end of file +#print(decompressed.decode().splitlines()[:5]) +print(decompressed.decode()) \ No newline at end of file diff --git a/swagger.yaml b/swagger.yaml index 40407ae..fe4c435 100644 --- a/swagger.yaml +++ b/swagger.yaml @@ -102,7 +102,7 @@ paths: type: string - in: query name: format - description: "If set to csv will provide a CSV of the results rather than JSON" + description: "Valid options are csv, kml or json" required: false type: string format: string