NextBus vehicle locations

So, the Mainline route ID stored in the tag attribute is simply 1 according to these results. Now, we have all the information that we need to track buses along the LA Metro route 2.

There is only one more required parameter called t, that is, milliseconds since the 1970 Epoch date (January 1, 1970, at midnight UTC). The epoch date is simply a computer standard used by machines to track time. The easiest thing to do in the NextBus API is to specify 0 for this value that returns data for the last 15 minutes.

There is an optional direction tag that allows you to specify a terminating bus stop in case a route has multiple buses running the route in opposite directions. If we don't specify this, the API will return the first one, which suits our needs. The REST URL to get the Mainline route for the LA Metro looks as follows: http://webservices.nextbus.com/service/publicXMLFeed?command=vehicleLocations&a=lametro&r=2&t=0.

Calling this REST URL in a browser returns the following XML document:

<?xml version="1.0" encoding="utf-8" ?>
<body copyright="All data copyright Los Angeles Metro 2015.">
<vehicle id="7582" routeTag="2" dirTag="2_758_0" lat="34.097992" lon="-118.350365" secsSinceReport="44" predictable="true" heading="90" speedKmHr="0"/>
<vehicle id="7583" routeTag="2" dirTag="2_779_0" lat="34.098076" lon="-118.301399" secsSinceReport="104" predictable="true" heading="90" speedKmHr="37"/>
. . .
</body>

Each vehicle tag represents a location for the last 15 minutes. The last tag is the most recent location (even though XML is technically unordered).

Tip

These public transportation systems do not run all the time. Many close down at 10:00 pm (22:00) local time. If you encounter an error in the script, use the Nextbus.com website to locate a system that is running, and change the agency and route variables to this system.

We can now write a Python script that returns the locations for a bus on a given route. If we don't specify the direction tag, NextBus returns the first one. In this example, we will poll the NextBus tracking API by calling the REST URL using the built-in Python urllib module demonstrated in the previous chapters. We'll parse the returned XML document using the simple built-in minidom module, covered in the The minidom module section, in Chapter 4, Geospatial Python Toolbox. This script simply outputs the latest latitude and longitude of the route 2 bus. You will see the agency and route variables near the top:

import urllib.request
import urllib.parse
import urllib.error
from xml.dom import minidom

# Nextbus API command mode
command = "vehicleLocations"

# Nextbus customer to query
agency = "lametro"

# Bus we want to query
route = "2"

# Time in milliseconds since the

# 1970 epoch time.  All tracks
# after this time will be returned.
# 0 only returns data for the last
# 15 minutes
epoch = "0"

# Build our query url
#
# webservices base url
url = "http://webservices.nextbus.com"
# web service path
url += "/service/publicXMLFeed?"
# service command/mode
url += "command=" + command
# agency

url += "&a=" + agency
url += "&r=" + route
url += "&t=" + epoch

# Access the REST URL
feed = urllib.request.urlopen(url)

if feed:
    # Parse the xml feed
    xml = minidom.parse(feed)
    # Get the vehicle tags
    vehicles = xml.getElementsByTagName("vehicle")
    # Get the most recent one. Normally there will
    # be only one.
    if vehicles:
        bus = vehicles.pop()
        # Print the bus latitude and longitude
        att = bus.attributes
        print(att["lon"].value, ",", att["lat"].value)
    else:
        print("No vehicles found.")

The output of this script is simply a latitude and longitude value that implies that we now have control of the API and understand it. The output should be a coordinate value for the latitude and longitude.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset