First and last tube data issue

Hi There,

Was wondering whether someone could help me please.

I’m trying to get the first and last tube data from the following API link (https://api.tfl.gov.uk/Line/district/Timetable/940GZZLUACT/to/940GZZLUERC). I have managed to filter down to the firstJourney and lastJourney elements in the JSON but have double checked this with what’s on the TFL site and the times seem different? Is this just an error in the API or is there another API link to retrieve this?

Example below:

Acton Town (District) → Upminster (District) Mon-Fri
API last journey = 00:28

TFL site shows the last journey = 23:47

Thanks in advance!

@Lausal

Ah yes, the TfL very non-standard way of sometimes using “24” for the hour after midnight…

image

However, I note that that link you provides has an error message at the end

“The stop you selected has now been removed from the route, and therefore we cannot show you a timetable. The route page will be updated shortly to reflect these changes.”"

Checking Acton Town aginst the TfL site Acton Town Underground Station - Transport for London it does show the 00:28

Hi @briantist,

Thanks for your reply.

But when I search for the last service from Acton Town to Upminster on TFL site, it shows it as 11:47. Which is also the two stationID’s i’ve used for the API call.

1 Like

@Lausal

I’m still wondering why the output of the JSON ends with

Which doesn’t make sense if the stations are on the same line.

http://content.tfl.gov.uk/fandl-district-wtt150.pdf The last through train from Acton Town to Upminster is at 2347 with the last service being the 2428 which only runs as far as Hammersmith

Thanks for your reply.

I see - so is there a way to find specifically the first and last service that will actually go to Upminster?

@Lausal

How about using

https://api.tfl.gov.uk/Journey/JourneyResults/940GZZLUACT/to/940GZZLUUPM?time=2300&timeIs=Departing&journeyPreference=LeastInterchange&mode=tube

and looping the times from 2300 to 0100 and seeing what comes back?

I think the problem here is that the call returns far more data than you actually ask for, specifically you get all routes from the starting location in the required direction (in this case, outbound).
What you need to do is iterate through all the interval sets in stationIntervals, to identify which ones contain the destination. In the case of Upminster there’s only one - interval set 0, but if you were looking for Barking, you would need sets 0, 4 and 5 (trains terminating at Upminster, Dagenham East and Barking, respectively).
You then need to go through schedules/knownJourneys picking out all the journeys with the relevant interval sets. In each case the arrival time is the time given in the knownJourney plus the arrivalTime for the stop in the relevant interval set.
What you see in lastJourney is the last trip in that direction to anywhere, in this case interval set 7 - to Hammersmith.
This is further complicated by the fact that there are several sets of schedules, in this case ‘Monday - Friday’, ‘Saturdays and Public Holidays’ and ‘Sunday’. I’ve also seen, albeit with the buses, schedules that differentiate between schooldays and non-schooldays
Curiously, I’m not getting the statusErrorMessage that Brian is seeing, although I’ve got multiple instances of it elsewhere (see the thread on problems with /Line/Timetable)

@nickp

I used the “mode=tube” to try and limit the number of trips. I think, looking at what is returned, the Acton Town to Hammersmith late journey is found, but yes, you need to ensure that all of the parts of the returned journey are of type “tube”.

I’m not really clear what @Lausal is actually after, because there will be different results for Mon to Thur, Friday (night tube), Saturday (night tube) and Sunday if it’s a “general purpose” enquiry.

I might code this us and see what I can get.

OK, I coded up my idea and it seems to work…

22:44 District line to Upminster tube, 84 mins.
23:04 District line to Upminster tube, 83 mins.
23:14 District line to Upminster tube, 83 mins.
23:24 District line to Upminster tube, 84 mins.
23:34 District line to Upminster tube, 84 mins.
23:47 District line to Upminster tube, 83 mins.
23:48 Piccadilly line to Hammersmith (Dist&Picc Line) tube, District line to Upminster tube, 82 mins.
00:00 Piccadilly line to Earl's Court tube, District line to Upminster tube, 85 mins.
00:09 Piccadilly line to Hammersmith (Dist&Picc Line) tube, District line to Upminster tube, 85 mins.

LAST TRAIN FROM 940GZZLUACT to 940GZZLUUPM IS 00:09 Piccadilly line to Hammersmith (Dist&Picc Line) tube, District line to Upminster tube, 85 mins.

The code is …

class TflLastDirectTrain extends tflStationInfo
{
 private $strStart = "940GZZLUACT";
private $strEnd = "940GZZLUUPM";
private $arrOutput = [];
public function scanForLastTrain()
{
    foreach (["2300", "2315", "2330", "2345", "0000", "0015", "0030", "0045", "0100"] as $strStartTime) {
        $this->scanForLastTrainAt($strStartTime);
    }
    ksort($this->arrOutput);
    foreach ($this->arrOutput as $i => $k) {
        echo PHP_EOL . $k["timestamp"] . " " . $k["words"];
    }

    end($this->arrOutput);
    echo PHP_EOL . PHP_EOL . "LAST TRAIN FROM $this->strStart to $this->strEnd IS " . current($this->arrOutput)["timestamp"] . " " . current($this->arrOutput)["words"];
}

 public function scanForLastTrainAt($strStartTime = "2300")
{
    $r = json_decode($this->getSomethingFromTfL($strStartTime));
    foreach ($r->journeys as $journey) {
        $strLegs = "";
        $strMode="";
        foreach ($journey->legs as $leg) {
            $strLegs .= $leg->instruction->summary . " " . $leg->mode->id . ", ";
            $strMode=$leg->mode->id;
        }
        $strLegs .= $journey->duration . " mins.";//. $journey->startDateTime;
        $intDifferenceEnd = From4am::HHMMtoDayMinutes(date("H:i", strtotime($journey->startDateTime)));
        if ($intDifferenceEnd < 240) {
            $intDifferenceEnd += 1440;
        }
        if ($intDifferenceEnd < 1440 and $strMode=="tube") {
            $this->arrOutput[$intDifferenceEnd] = ["words" => $strLegs, "timestamp" => date("H:i", strtotime($journey->startDateTime))];
        }
    }
}

public function getSomethingFromTfL($strStartTime = "2300")
{
    $strURL = "https://api.tfl.gov.uk/Journey/JourneyResults/{$this->strStart}/to/{$this->strEnd}?time={$strStartTime}&timeIs=Departing&journeyPreference=LeastInterchange&mode=tube&" . disruptionViews::TFLAPISUFFIX;
    return $this->cacheTFLcall($strURL, 120);
}

}

Grange Hill to South Ruislip

22:50 Central line to Woodford tube, Central line to South Ruislip tube, 79 mins.
23:10 Central line to Woodford tube, Central line to South Ruislip tube, 79 mins.
23:30 Central line to Woodford tube, Central line to South Ruislip tube, 79 mins.
23:48 Central line to Woodford tube, Central line to South Ruislip tube, 77 mins.

LAST TRAIN FROM 940GZZLUGGH to 940GZZLUSRP IS 23:48 Central line to Woodford tube, Central line to South Ruislip tube, 77 mins

Oakwood to Ickenham

22:38 Piccadilly line to Ickenham tube, 89 mins.
23:11 Piccadilly line to King's Cross St.Pancras tube, Metropolitan line to Ickenham tube, 76 mins.
23:31 Piccadilly line to King's Cross St.Pancras tube, Metropolitan line to Ickenham tube, 78 mins.
23:47 Piccadilly line to King's Cross St.Pancras tube, Metropolitan line to Ickenham tube, 77 mins.
23:57 Piccadilly line to King's Cross St.Pancras tube, Hammersmith & City line or Circle line or Metropolitan line to Baker Street tube, Metropolitan line to Ickenham tube, 79 mins.

LAST TRAIN FROM 940GZZLUOAK to 940GZZLUICK IS 23:57 Piccadilly line to King’s Cross St.Pancras tube, Hammersmith & City line or Circle line or Metropolitan line to Baker Street tube, Metropolitan line to Ickenham tube, 79 mins…

Gunnersbury to Bayswater

22:49 District line to Earl’s Court tube, District line to Bayswater tube, 24 mins.
23:00 District line to Earl’s Court tube, District line to Bayswater tube, 23 mins.
23:10 District line to Earl’s Court tube, District line to Bayswater tube, 23 mins.
23:19 District line to Earl’s Court tube, District line to Bayswater tube, 24 mins.
23:29 District line to Earl’s Court tube, District line to Bayswater tube, 24 mins.
23:40 District line to Earl’s Court tube, District line to Bayswater tube, 23 mins.
23:58 District line to Gloucester Road tube, Circle line to Bayswater tube, 29 mins.
00:09 District line to Gloucester Road tube, Circle line to Bayswater tube, 27 mins.

LAST TRAIN FROM 940GZZLUGBY to 940GZZLUBWT IS 00:09 District line to Gloucester Road tube, Circle line to Bayswater tube, 27 mins.

Notting Hill Gate to Baywater

22:57 Circle line or District line to Bayswater tube, 1 mins.
23:02 District line or Circle line to Bayswater tube, 1 mins.
23:07 Circle line or District line to Bayswater tube, 1 mins.
23:12 District line or Circle line to Bayswater tube, 1 mins.
23:17 Circle line or District line to Bayswater tube, 1 mins.
23:22 District line or Circle line to Bayswater tube, 1 mins.
23:27 Circle line or District line to Bayswater tube, 1 mins.
23:32 District line or Circle line to Bayswater tube, 1 mins.
23:37 Circle line or District line to Bayswater tube, 1 mins.
23:42 District line or Circle line to Bayswater tube, 1 mins.
23:47 Circle line or District line to Bayswater tube, 1 mins.
23:52 District line or Circle line to Bayswater tube, 1 mins.
23:57 Circle line or District line to Bayswater tube, 1 mins.
00:02 District line or Circle line to Bayswater tube, 1 mins.
00:06 Circle line or District line to Bayswater tube, 1 mins.
00:15 Circle line to Bayswater tube, 1 mins.
00:26 Circle line to Bayswater tube, 1 mins.
00:35 Circle line to Bayswater tube, 1 mins.

LAST TRAIN FROM 940GZZLUNHG to 940GZZLUBWT IS 00:35 Circle line to Bayswater tube, 1 mins.

Meda Vale to Lambeth North

22:57 Bakerloo line to Lambeth North tube, 20 mins.
23:01 Bakerloo line to Lambeth North tube, 20 mins.
23:05 Bakerloo line to Lambeth North tube, 20 mins.
23:13 Bakerloo line to Lambeth North tube, 20 mins.
23:17 Bakerloo line to Lambeth North tube, 20 mins.
23:25 Bakerloo line to Lambeth North tube, 20 mins.
23:29 Bakerloo line to Lambeth North tube, 20 mins.
23:37 Bakerloo line to Lambeth North tube, 20 mins.
23:45 Bakerloo line to Lambeth North tube, 20 mins.
23:53 Bakerloo line to Lambeth North tube, 20 mins.
23:58 Bakerloo line to Lambeth North tube, 20 mins.
00:03 Bakerloo line to Lambeth North tube, 20 mins.
00:13 Bakerloo line to Lambeth North tube, 20 mins.
00:19 Bakerloo line to Lambeth North tube, 20 mins.

LAST TRAIN FROM 940GZZLUMVL to 940GZZLULBN IS 00:19 Bakerloo line to Lambeth North tube, 20 mins.

Hi Brian,
I hadn’t really clocked that you were using /Journey, rather than /Line/Timetable. Given that, I think that the error message is probably an artefact caused by ACT not being the intial stop for the timetable concerned. Do you fancy giving it a go from EBY to UPM (or to anywhere actually) and see if the message disappears?
Regards, Nick

@nickp

I guess the issue is what does @Lausal mean by his question. Is there a special need to know when the last direct train runs, or it the more important thing to know when the last available trip is? Especially when the “change at Mile End” trip would be a lot quicker?

Here’s the output of Ealing Broadway to Upminster.

22:58 District line to Upminster tube, 89 mins.
23:08 District line to Upminster tube, 89 mins.
23:18 District line to Upminster tube, 90 mins.
23:28 District line to Upminster tube, 90 mins.
23:41 District line to Upminster tube, 89 mins.
23:45 Central line to Mile End tube, District line to Upminster tube, 77 mins.
23:55 Central line to Mile End tube, District line to Upminster tube, 90 mins.
00:00 District line to Earl's Court tube, District line to Upminster tube, 94 mins.
00:02 Central line to White City tube, Central line to Mile End tube, District line to Upminster tube, 83 mins.

LAST TRAIN FROM 940GZZLUEBY to 940GZZLUUPM IS 00:02 Central line to White City tube, Central line to Mile End tube, District line to Upminster tube, 83 mins.

Hey @briantist,

Thanks for your reply - sorry I should’ve been more clear.

I’m basically trying to get the first and last tube times on a specific line between two stations like they have on the TFL site here:

The below design is what i’m trying to achieve:

So in a nutshell:

  1. The user selects a station (I.e. acton town)
  2. They select whether they want to see it’s corresponding first or last tube
  3. It then displays the corresponding days of week, as the first and last tube times vary depending on the tube line
  4. We then show the list of first/last tube times depending on he users selection

Thanks!

@Lausal

I’m not 100% sure how useful the information you are asking for is to a user of the service: quite a lot of the time there are still trains you can use because there are trains that run on the same tracks but labelled as different lines:
Met and Pic
image
Circle & H&C


Bakerloo & Overground
image
Met & Circle & H&C
image
District & H&C
image
Circle & District
image
District & Overground
image
District & Pic
image

And then you have the “Express Routes” issue where one tube line is the express service and the other fills in

Met Express & Jubliee Local
image
Jubilee Expres & DLR Local
image
Pic Express & District Local
image

There are also the places where there are National Rail services that provide “express tube” services

C2C is the Express District Line: West Ham, Barking , Upminster
Greater Anglia does Express Tfl Rail: Liverpool Street, Stratford, Romford, Shenfield
Chilten is Express Met Line: Harrow-on-the-Hill, Chalfont & Lat, Amersham
London Northwestern from Euston is Express Overground: Euston, Welmbly Central, Harrow & Whetstone, Watford Junction
Chiltern again does South Ruislip, West Ruislip
Heathrow Epress is express Tfl Rail Heathrow to Paddington
HS1 run under the Stratford to Highbury & Islington Overground
Southern runs parallel to some Overground routes.

To this end, I’m not sure what the user gets to know by getting to know the last direct train when there might be indirect one an hour or more later, such as from Oakwood to Ickenham where the last direct train is 22:38, but changing there is a train at 23:57 which is 79 minutes later (and 10 minutes faster trip)

I do get, however, what the people who work for TfL might get out of this information.

Just trying to help…

Gotcha, makes sense - thank you :slight_smile:

1 Like

Phew! I thought I might have over though it…

1 Like