Skip to main content

Call the endpoint and generate a CSV

In this tutorial, we are gonna look at how to call the Tracking History endpoint, retrieve the desired dataset, and export the results in a CSV file.

Source code#

You can find and download the source code for this example here.

Geo filtering#

First, let's look at a particular area, such as a larger Paris area, represented by this GeoJSON:

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
0.9008789062499999,
48.122101028190805
],
[
3.8452148437499996,
48.122101028190805
],
[
3.8452148437499996,
49.5822260446217
],
[
0.9008789062499999,
49.5822260446217
],
[
0.9008789062499999,
48.122101028190805
]
]
]
}
}
]
}

Which is represented like that on a map:

Calling the Tracking History API#

Using the coordinates previously shown, we can filter the API results for the area we want:

  • Latitude range: 48.122101028190805,49.5822260446217 need to be South to North, so values can vary from -90 to 90.
  • Longitude range: 0.9008789062499999,3.8452148437499996 need to be West to East, so values can vary from -170 to 170.
import requests
if __name__ == "__main__":
response = requests.get(
"https://api.airsafe.spire.com/v2/targets/history",
params={
"longitude_between": "0.9008789062499999,3.8452148437499996",
"latitude_between": "48.122101028190805,49.5822260446217",
"start": "2021-05-21T12:00:00Z",
"end": "2021-05-21T15:59:59Z",
},
headers={"Authorization": "Bearer <your_token>"},
)

Parsing results#

We can now parse the response, which is a set of JSON objects each separated by a new line. We can then just simply iterate through those lines and parse the JSON using response.iter_lines object in each of them.

import json
import requests
if __name__ == "__main__":
response = requests.get(
"https://api.airsafe.spire.com/v2/targets/history",
params={
"longitude_between": "0.9008789062499999,3.8452148437499996",
"latitude_between": "48.122101028190805,49.5822260446217",
"start": "2021-05-21T12:00:00Z",
"end": "2021-05-21T15:59:59Z",
},
headers={"Authorization": "Bearer <your_token>"},
)
data = []
for line in response.iter_lines(decode_unicode=True):
if line:
data.append(json.loads(line)["target"])

Writing CSV output#

Finally, we can now create a new output file data.csv.

We need to find the JSON item with the most keys in it, to have all the necessary CSV columns created. For that we can use max(data, key=lambda item: len(item.keys())), which is gonna loop through the JSON objects and count their respective entries, returning the element with the biggest number.

With this newly obtained element with the most entries, we write our first row containing the CSV's headers: csv_writer.writerow(most_keys.keys()).

We are now all ready to write all the contents of the CSV by looping through the entries again, and getting the associated values for each key, or "" if that key is not present in the object.

import json
import requests
import csv
if __name__ == "__main__":
try:
response = requests.get(
"https://api.airsafe.spire.com/v2/targets/history",
params={
"longitude_between": "0.9008789062499999,3.8452148437499996",
"latitude_between": "48.122101028190805,49.5822260446217",
"start": "2021-05-21T12:00:00Z",
"end": "2021-05-21T15:59:59Z",
},
headers={"Authorization": f"Bearer <your_token>"},
)
except Exception as e:
print("Failed to query API")
sys.exit()
if response.status_code == 401:
print("Unauthorized, token might be invalid")
sys.exit()
data_file = open("data.csv", "w")
# create the csv writer object
csv_writer = csv.writer(data_file)
data = []
for line in response.iter_lines(decode_unicode=True):
if line:
data.append(json.loads(line)["target"])
# To generate the right number of columns for the CSV, we find the row with the biggest number of items
most_keys = max(data, key=lambda item: len(item.keys()))
try:
csv_writer.writerow(most_keys.keys())
for elem in data:
csv_writer.writerow(map(lambda key: elem.get(key, ""), most_keys.keys()))
data_file.close()
print("CSV file generated successfully")
except Exception as e:
print("Something went wrong", e)