Nat. Rail Enq. LDB

HOWTO:  Access National Rail Enquiries Live Departure Boards API

Introduction

This page is a quick introduction to getting data from the National Rail Enquiries (NRE) Live Departure Boards (LDB) Web Service.

The National Rail Enquiries API provides more information than shown in this example, which is focussed on showing step-by-step how to build a very basic client.

Register for the NRE LDB API

Register for the NRE LDB API:

  1. Go to:  http://realtime.nationalrail.co.uk/OpenLDBWSRegistration
  2. Complete the process for registering.
  3. At the end of the process, you should have an API token (this may be emailed to you) that looks something like:
    2dea23a4-a3b2-1c84-ac43-1c4a51247c32
    Note, the value above is not a real token.  You token should be kept private and not shared with anyone.

Find the current Web Service WSDL URL

  1. Go to:  https://lite.realtime.nationalrail.co.uk/OpenLDBWS/
  2. This page will state the current URL for the Live Departures Web Service WSDL.  At the time of writing, this is:
    https://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2014-02-20
  3. Keep the current URL to hand as it will be needed later.

Get Visual Studio 2013 Community Edition (Free)

  1. Go to:  https://www.visualstudio.com/en-us/news/vs2013-community-vs.aspx
  2. Click “Download Visual Studio Community 2013” (near the top of the page)
  3. This will download the “vs_community.exe” file.  Run this and follow the instructions to install Visual Studio.  A detailed step-by-step guide for installing Visual Studio can be found here.  For using the Live Departure Boards API, none of the optional components shown in step 4 of the step-by-step install guide are needed.

Create The Test Project

This quick tutorial creates a new Console Application, which is the most basic type of program.  After completing this tutorial you could of course create a different type of project (e.g. for a phone app, desktop app, etc).

  1. Open Visual Studio Community Edition (e.g. from the Start Menu).
  2. Go to the File Menu >> New >> Project.
  3. Specify the details of the project as follows (you can use a different location if you wish):
    2015-02-24-01
  4. Click “OK” at the bottom right of the window to create the project.  This will create an empty console application.
  5. We now need to add a service reference to the Live Departure Boards web service.  To do this, go to the Project Menu and click “Add Service Reference”:
    2015-02-24-02
  6. This will open a window where you can specify the location and details of the web service you wish to connect to.  Into the address box, paste the Web Service WSDL URL:
    2015-02-24-03
  7. Then click the “Go” button.  Visual Studio will then attempt to download the details of the web service, which are specified in the WSDL file.  If this is successful, the service should appear in the “Services” box.
  8. You can see the basic details of the service by double clicking on “ldb” in the services box, then “LDBSoapService” underneath that.
    2015-02-24-04
  9. Into the “Namespace” box, type “OpenLDB”.  Note, make sure you get the capitalisation correct, otherwise the code later will not work.  Then click “OK”.
  10. Next, add the following line of code as shown in the image below:
    using LiveDepartureBoardExample.OpenLDB;
    2015-02-24-05
  11. Next, inside the “Main” method, paste the code from the code listing at the end of this post.  So, in the screenshot above, the code should be pasted into line 14.
  12. Next, edit the token value line, to replace the dummy token value in the code with your actual token.
    2015-02-24-06
  13. Finally, check the code compiles.  From the Build menu, select “Build Solution”.

Run the Example

  1. To run the example, click the “Start” button or press the F5 key.
    2015-02-24-07
  2. Enter the three letter CRS code for a station and press enter.  These are the same three letter codes that appear in the Live Departure station list on the National Rail Enquiries website.  For example, EUS is London Euston, KGX is London Kings Cross, BHM is Birmingham New Street, etc.
    2015-02-24-08

Example Project

The example project created above can be downloaded here.  Note, you will need to change the access token in the code to use your own access token and then build the solution in Visual Studio.  The token value in the code in this example is a dummy value.

Code Listing

The code listing used in step 11 above is:

const int ciMaxTrainsDisplayed = 15;
try
{
 AccessToken oAccessToken = new AccessToken();
 oAccessToken.TokenValue = "2dea23a4-a3b2-1c84-ac43-1c4a51247c32";
Console.Clear();
 Console.WriteLine("NATIONAL RAIL ENQUIRIES LIVE DEPARTURE BOARDS SAMPLE");
 Console.Write("Enter Location CRS Code (e.g. EUS, BHM, DBY or EDB): ");
 string sLocation = Console.ReadLine();
LDBServiceSoapClient oClient = new LDBServiceSoapClient();
 StationBoard oStationBoard = oClient.GetArrivalDepartureBoard(oAccessToken, 200, sLocation, null, FilterType.from, 0, 120);
if (oStationBoard.trainServices == null)
 {
 Console.WriteLine("There are no train services available.");
 Console.WriteLine("Press any key to finish.");
 Console.ReadKey();
 }
 else
 {
 Console.WriteLine(oStationBoard.trainServices.Length.ToString() + " train services retrieved, up to " + ciMaxTrainsDisplayed.ToString() + " displayed:");
 Console.WriteLine("-----------------------------------------------------------------------");
 Console.WriteLine("SchArr ExpArr Origin SchDep ExpDep Destination ");
 Console.WriteLine("------ ------- ----------------- ------ ------- ------------------");
 int i = 0;
 foreach (ServiceItem oServiceItem in oStationBoard.trainServices)
 {
 string sDetails = "";
string sSta = (oServiceItem.sta == null) ? "" : oServiceItem.sta;
 string sEta = (oServiceItem.eta == null) ? "" : oServiceItem.eta;
 string sOrigin = ((oServiceItem.origin == null) || (oServiceItem.origin.Length == 0)) ? " " : oServiceItem.origin[0].locationName;
while (sSta.Length < 8) sSta += " ";
 while (sEta.Length < 9) sEta += " ";
 if (sOrigin.Length > 16) sOrigin = sOrigin.Substring(0, 16) + "…";
 while (sOrigin.Length < 19) sOrigin += " ";
 sDetails += sSta + sEta + sOrigin;
string sStd = (oServiceItem.std == null) ? "" : oServiceItem.std;
 string sEtd = (oServiceItem.etd == null) ? "" : oServiceItem.etd;
 string sDestination = ((oServiceItem.destination == null) || (oServiceItem.destination.Length == 0)) ? " " : oServiceItem.destination[0].locationName;
while (sStd.Length < 8) sStd += " ";
 while (sEtd.Length < 9) sEtd += " ";
 if (sDestination.Length > 16) sDestination = sDestination.Substring(0, 16) + "…";
 while (sDestination.Length < 19) sDestination += " ";
 sDetails += sStd + sEtd + sDestination;
Console.WriteLine(sDetails);
 i++;
 if (i == ciMaxTrainsDisplayed) break;
 }
 Console.WriteLine("-----------------------------------------------------------------------");
 Console.WriteLine("Press any key to finish.");
 Console.ReadKey();
 }
}
catch (Exception oException)
{
 Console.WriteLine("An error has occurred:");
 Console.WriteLine(oException.ToString());
 Console.WriteLine("Press any key to finish.");
 Console.ReadKey();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s