Step 5Interpret and display your data
Your InstaMapper data can be viewed in the 'IM API' by visiting a site with your device's key; read more about how to access this page here (requires free registration). A screenshot of the site is below. This is the site we will poll to get the current location of a bus.
The Python script is attached below as bus_tracker_v8.txt; you can download it and change the extension to .py. The code does a little bit of math and a good deal of clumsy data-parsing. To run the script for the first time, you need the route CSV file you created earlier and a bus data text file like the one below. The bus data file is accessed every time the script runs and writes the time and position info about your buses. You also need Python.. I used 2.6.4.
IM requests that you limit your API access to once every ten seconds so the script checks this first. If the data file is less than ten seconds old, the existing data file is used as the output. If the data's older than ten seconds, the script seeks new data from the API page using any API keys you provide.
The script then imports your route file of waypoints and calculates the distance between the current bus location (its latitude and longitude) and each waypoint. The index of the smallest distance is stored, establishing a position for each bus. Finally the data for each bus is written to the screen and logged in a separate text file.
This Python script is used as a CGI, a protocol which allows Python to create a webpage after performing the functions described above. The user goes to a page which links to the script, the script runs on the server, and the output is formatted into HTML so the browser can view it. CGI is commonly used with Perl and parsing data from HTML forms. A very readable tutorial on CGI scripting with Python is available at Penzilla.net.
The host of your site will have specific guidelines about where CGI scripts can live and how they must be formatted. Here are two of the CGI-troubleshooting FAQs from my host that generally apply no matter what language you're scripting in: CGI troubleshooting, Python CGI.
All of this outputs to something like the final image on this step. The final presentation can be more cleverly arranged in a template or beautified with CSS. You can also have Python print a meta tag so the page is automatically refreshed (and the script is called again).
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|














































