Google maps and ISS location with python

This new project involves the use of two APIs, google maps and open notify (ISS location). The goal of the project to collect real time data of the international space station’s location then be able to pin that location on google maps. I got the idea from this website. But the difference is they pinned it on there on map system.

To start we need to get the API get delivers the ISS data. The API returns a JSON file containing the latitude and longitude of the ISS along with a timestamp. First I tried to use the examples used in on the open notify website which is the API I would be using.

Which looked like this:

import urllib2
import json

req = urllib2.Request("http://api.open-notify.org/iss-now.json")
response = urllib2.urlopen(req)

obj = json.loads(response.read())

print obj['timestamp']
print obj['iss_position']['latitude'], obj['data']['iss_position']['latitude']

# Example prints:
#   1364795862
#   -47.36999493 151.738540034

But the code when I was trying to run was outdated. As this example of code is python 2. And even fixing the syntax errors trying to get the urlib2 library to run correctly was a bit difficult. The solution was to use a third-party library called requests. Which makes dealing with HTTP requests stupidly easy.

To get the JSON file using the requests library I only had to do this:

import requests

r = requests.get('http://api.open-notify.org/iss-now.json')
print(r.json())

And the print statement below:

1pic.png

As we can see the requests library made stuff a lot simpler. Now next task is to print the useful information from the JSON file above. Using the code from the website I mentioned in the beginning of the post. I will extract the longitude and latitude of the ISS.

The code used is below:

result = r.json()

location = result['iss_position']
latitude = location ['latitude']
longitude = location ['longitude']

print('latitude:', latitude)
print('longitude:', longitude)


Printed statements below:

Now we able to extract the latitude and longitude from the JSON file. Now we need to start getting the google maps API ready so we can input this data in.

I created an separate environment before I installed the google maps library for python. After I installed the library I went to the google cloud platform to fetch an API key so I can use the program. Used the example code on the library README file, Tested the google maps one of functions out. It was able to print out this JSON file.

So I just need to replace the example coordinates with my own. So replaced the numbers with the latitude and longitude variables.


Example code

reverse_geocode_result = gmaps.reverse_geocode((40.714224, -73.961452))

my code
reverse_geocode_result = gmaps.reverse_geocode((latitude, longitude))

The replacement worked and it returned this file JSON file.

As we can see in the file the ISS is flying over the Atlantic Ocean at the of running this code. But data is not too useful for are project as we want to plot coordinates over a map. To find the solution I was googling around. Then found the package gmplot which should the trick. The package genrates a html file containing javascript to allow users plot there data on google maps.

So I imported the new gmplots package. Based on the code on the README file. I placeed the map in the area which is based on the latutude and longitude variables.

google_maplot = gmplot.GoogleMapPlotter(latitude, longitude, 13)

Next is the API key for the gmplot package. I used the same API key for the gmap package. Next is to make the marker plot location of the ISS.

google_maplot.apikey = [API_KEY_HERE TypeError: must be real number, not str ]

hidden_gem_lat, hidden_gem_lon = latitudefloat, longitudefloat
google_maplot.marker(hidden_gem_lat, hidden_gem_lon, 'cornflowerblue')

The reason why latitudefloat and longitudefloat exists is because when I was running the orginal latitude and longtuitade varibles. The google_maplot.marker fucntion threw this error:

TypeError: must be real number, not str

Made new variables that turned the variables into floats.

latitudefloat = float(latitude)
longitudefloat = float(longitude)

After that I ran the program I opened the got error saying the map cant be loaded. The page said check the web console for the error message. The error I got is ApiNotActivatedMapError So I had to activate another api for map to load. The API was the Maps JavaScript API.

Now fixing that issue I was able to load the location of ISS. So it gave an general area. But the marker does not appear. Found the solution when googling around. Found a solution on the one of the issue pages on GitHub. I needed to go into the package itself to change the code.

From this:
self.coloricon = os.path.join(os.path.dirname(__file__), 'markers/%s.png')

to this:
self.coloricon = 'file:///' + os.path.dirname(__file__).replace('\\', '/') + '/markers/%s.png'

The most likely reason why the code was failing is that windows dealing with slashes(escape characters) a bit differently. So the windows probably did not open the markers file correctly.

After running the program again we get this result (I’ve zoomed out quite a lot see the location relative to other places in the earth. ):

5.png

Now the goal of the project is now fulfilled, as we can now run the program get the location of the ISS on the map. But I think more features can be added to the program later on like a web interface or giving information which astronauts are in the ISS at the given moment.

You can find the code on Github:

https://github.com/tobiolabode/ISS_Google_maps

$\setCounter{0}$
Previous
Previous

Google maps and the International Space Station with Python part 2

Next
Next

MyFitnessPal and OCR