Bus position location

Hello
I am working on an app to plot bus locations on a route and have seen a web map (traintimes.org) which plots the position using the arrival time as an estimate of the busses position as the actual bus coordinates aren’t part of the api.

The idea is to make an accessible app with very clear visual representations of the busses on a map with little text and no forms or buttons etc. Easy to use for those with visual impairment. Just open the app and see your busses on route.

I have got as far as plotting the positions using a combination of the next stop arrival time and time last stop was visited and using that to calculate the buses average speed and then the estimated location based on the speed and distance between stops…

I’ve noticed on the traintimes.org website the busses move directly from point to point in a straight line without following roads, so I have used route planning data to have the busses follow the road rather than directly from point to point.

Which seems like a lot of code and at least 3 api calls per vehicle just to get an estimated location, sometimes the estimates are off by a large margin if the bus is stopped in traffic jams etc

I. It iced there is an empty field in the vehicle details referring to its location, So I was wondering if there were any plans to add the location data to the api, or if there was a practical reason why this can’t be added .

1 Like

Hi,

I’ve done similar things with trains, which are easy because they are level and straight!

Here’s some ideas:

  • use the Google Maps directions API to get the actual route between bus stops. This will allow you to know the true path. Grab them from here one by one and then store them…
  • you will need to know the dwell times for the buses at the stops … somehow

  • you will need to look up the standard bus acceleration and deceleration times if you want to get a good handle on the true position.

  • You can get a lot of static info from

const BUSSERVICESFEED = "http://data.tfl.gov.uk/tfl/syndication/feeds/bus-sequences.csv?";

  • use the Tfl Digest to get the live data

    class digestCommon
    {
    const DEBUGMODE = false;
    const LINEID = “UL16,UL51,90”;
    public $isAlreadyRunning = null;

    public function setAlreadyRunning($isAlreadyRunning)
    {
    $this->isAlreadyRunning = $isAlreadyRunning;
    }

    public function startDigest($strURL = “http://countdown.api.tfl.gov.uk/interfaces/ura/stream_V1”,
    $strUsername = “",
    $strPassword = "
    *”,
    $arrPostParameters = [“LineID” => self::LINEID,
    “ReturnList” => “StopCode2,StopPointIndicator,DestinationName,EstimatedTime,ExpireTime,VehicleID,StopID”], $intAuth = CURLAUTH_DIGEST, $strFunctionName = “on_curl_write”)
    {
    if (self::DEBUGMODE) {
    error_reporting(E_ALL);
    ini_set(‘display_errors’, ‘1’);
    }
    if (is_array($arrPostParameters)) {
    $arrPostParameters = http_build_query($arrPostParameters);
    }
    echo PHP_EOL . FUNCTION . " is sending " . strlen(var_export($arrPostParameters, true)) . " bytes to {$strURL}";
    echo PHP_EOL . PHP_EOL . PHP_EOL;
    $arrOptions = [CURLOPT_URL => $strURL,
    CURLOPT_HEADER => false,
    CURLOPT_VERBOSE => self::DEBUGMODE,
    CURLOPT_RETURNTRANSFER => $strFunctionName !== “”,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_SSL_VERIFYPEER => substr($strURL, 0, 5) === “https”, // for https
    CURLOPT_USERPWD => $strUsername . “:” . $strPassword,
    CURLOPT_HTTPAUTH => $intAuth,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $arrPostParameters];
    if ($strFunctionName != “”) {
    $arrOptions = $arrOptions + [CURLOPT_WRITEFUNCTION => array($this, $strFunctionName)];
    }
    $resCurlHandle = curl_init();
    curl_setopt_array($resCurlHandle, $arrOptions);
    try {
    $RawResponce = curl_exec($resCurlHandle);
    // validate CURL status
    if (curl_errno($resCurlHandle)) {
    throw new \Exception(curl_error($resCurlHandle), 500);
    }
    // validate HTTP status code (user/strPassword credential issues)
    $intStatusCode = curl_getinfo($resCurlHandle, CURLINFO_HTTP_CODE);
    if ($intStatusCode != 200) {
    throw new \Exception(“Response with Status Code [” . $intStatusCode . “].”, 500);
    }
    } catch (\Exception $ex) {
    if ($resCurlHandle != null) {
    curl_close($resCurlHandle);
    }
    throw new \Exception($ex);
    }
    if ($resCurlHandle !== null) curl_close($resCurlHandle);
    // echo "raw response: " . $RawResponce;
    return $RawResponce;
    }
    }

It’s quite possible to do this so well you will be able to work out which bus you are on from a couple of GPS readings…!

This is how I worked out the train profiles…

That’s great, I’ll have a play with the different parameters.

Getting the bus i am on is a goal.

I was also thinking I can use text recognition to allow the user to take a picture of the licence plate as they get on but that would not work from a convenience or safety point of view…

It’s you say I can probably get the bus I am on pretty accurately from my phones location coordinates and the best estimate of a busses position. I would probably need the user to type in their bus route number to be doubly sure

1 Like