• Our booking engine at tickets.railforums.co.uk (powered by TrainSplit) helps support the running of the forum with every ticket purchase! Find out more and ask any questions/give us feedback in this thread!

Fares database

Status
Not open for further replies.

soil

Established Member
Joined
28 May 2012
Messages
1,956
I have created a program that imports the ATOC fares feed into a database. (This is the complete national rail fares database, with various supporting data such as supplements, railcards, rovers, etc.)

The program is attached to this message.

To make use of it you will have to download + install Microsoft SQL Server.

You can get it here:

https://www.microsoft.com/betaexperience/pd/SQLEXPWT/enus/

You will need a copy of the fares data from here:

http://data.atoc.org/?q=user/register

(free signup required)

Steps required:
1. Download and unzip the fares data into its own folder.
2. Download and install SQL Server (Express). You can accept the defaults, except that when it says 'Named Instance', I suggest you instead choose 'Default Instance'.
3. Download and unzip the attached 'faresdb.zip' file.
4. Having installed SQL Server, you now need to double click the '1. create database'(.sql) file from wherever you unzipped the attachment here to. This should load SQL Server Management Studio, just click Connect, and then when the file has loaded, click 'Execute'. This should complete with no errors in a few seconds. This creates the fares database.
5. Now you need to import the data. Run 'Load Fares' (again, from whereever you unzipped my attachment to) and click the 'Load Fares' button. You will need to browse to the location of the fares data you downloaded and unzipped the fares database from ATOC, then click ok.

If you did as I said and installed a 'Default Instance', this should work fine. If not, and you get an error, you probably need to edit the config file (wherever you unzipped my attachment to) changing localhost to localhost\SQLEXPRESS.

The data load will take probably 10 minutes or so. The last two stages take the longest - there are around 8 lines of '.'s when loading the 'non-derivable fares' (each dot is 20,000 records out of 17 million total). It will let you know when it's done.
6. Now there are two separate cleanup files. Double click 'cleanup trains part1', and then 'execute'. This may take over 10 minutes. Repeate with 'clean up trains part 2'. Again this may take a few minutes. Do it in the correct order or it might not work.
7. Now run '5. create indexes.sql'. These are not an exhaustive set of indexes, they are more of a base set in order to get the system to do some basic queries without taking 10 minutes each time. You definitely want these ones though.

All done....

All the data are in the database, but I haven't written too many queries for it.

Here are some examples of what you can do:

1. Load SQL Server Management Studio.
2. Hit New/Query Window, paste this:

use trains
go

exec spGetFaresFrom
'WOK'

then hit the execute button

This should give all the available (non-advance) fares from Woking to anywhere.

You can add parameters to this, e.g.,:
exec spGetFaresFrom
'WOK',
@IncludeAdvances = 1

adds in the advances as well

exec spGetFaresFrom
'WOK',
@TicketCode = 'SDS'

finds all SDS fares from Woking

exec spGetFaresFrom
'WOK',
@Seasons = 1 -- default is 0, no seasons

finds all seasons from Woking

(note, this may appear to include duplicates, this is because seasons are reversible, so I have included both directions here, most of the time the fares are the same, but not always, if you can't find the fare specified, try reversing the flows)

exec spGetFaresFrom
'WOK',
@Seasons = 1,
@RouteCode = 734,
@TicketClass = 1 -- 2 is the other option

Finds season tickets in 1st class routed via Virginia Water (note: the route id is given in the output, so you don't need to guess)

You can also do:

exec spGetFaresBetween 'WOK', 'BTN'

to get fares between Woking and Brighton


exec spGetFaresBetween 'WOK', 'YRK',
@IncludeAdvances = 0

Woking - York excluding advances

exec spGetFaresBetween 'LST', 'SNF', @Seasons = 1

Seasons between Liverpool Street and Shenfield

Another procedure:
exec spGetRestrictionDetails 'UT'

will give you the database details on a given restriction code - days the restriction applies (first table), stations and times (second table), exceptions (third table)


There is a database diagrams file, if you know what a database diagram is, you can run this and then have a look, but otherwise don't bother!


Just to add finally, you will need Windows to make this work. The import uses .NET. If you don't have the right version installed, it won't work, but let me know if you have any problems....
 

Attachments

  • FaresDB.zip
    626.6 KB · Views: 221
Last edited:
Sponsor Post - registered members do not see these adverts; click here to register, or click here to log in
R

RailUK Forums

34D

Established Member
Joined
9 Feb 2011
Messages
6,042
Location
Yorkshire
Wow - way over my head, but it is obvious that a lot of work has gone into this to help others. Thanks soil
 

SickyNicky

Verified Rep - FastJP
Joined
8 Sep 2010
Messages
2,772
Location
Ledbury
Hi soil,

First of all, well done on your import mechanism and thanks for all the work you've obviously put into it. I wanted to bench test it against the import I have, but when I try to run it, a message box keeps popping up saying "hello" (and sometimes "hi"), so I can't complete the import.

Any thoughts?
 

soil

Established Member
Joined
28 May 2012
Messages
1,956
SickyNicky: I have uploaded a new version that no longer pops up those message boxes, but instead logs it to the log file (log.txt - this was being created anyway). Also I added a parameter to the config file so that you can alter the effective-from date. For the current ATOC data.atoc.org feed this is, and was, 30/1/2013, and no changes are necessary. But if you have different data/in the future, you will want to change this.

34D: it's not that complex really, there's no programming required!
 

PG

Established Member
Joined
12 Oct 2010
Messages
2,844
Location
at the end of the high and low roads
Again many thanks for this, having downloaded the fares data from ATOC earlier this week but not really having much of a clue how to turn it into something useable this fits the bill cheers!

Just a shame I'll have to install Windows unless you know of a way to get this working on Linux?
 

soil

Established Member
Joined
28 May 2012
Messages
1,956
Shouldn't it just work in Mono?

The app might, but it expects to connect to a MS Sql Server database, which will definitely only run on Windows. There's probably no reason why it couldn't run on MySQL, but I've not used MySQL for over a decade, so I have no idea whether my sprocs would run at all, or if major/minor changes would be needed, and I'm not about to investigate it....
 

SickyNicky

Verified Rep - FastJP
Joined
8 Sep 2010
Messages
2,772
Location
Ledbury
All working now - well done.

Seems like quite an efficient loader - thanks for sharing it.
 

All Line Rover

Established Member
Joined
17 Feb 2011
Messages
5,221
I've had a go at this, but get an error message when I click 'Connect'. I have attached a screenshot of the error message.

Forgive me for my ignorance, but what exactly do you mean by "change localhost to localhost\SQLEXPRESS"?
 

Attachments

  • Capture.PNG
    Capture.PNG
    40.3 KB · Views: 105

soil

Established Member
Joined
28 May 2012
Messages
1,956
I've had a go at this, but get an error message when I click 'Connect'. I have attached a screenshot of the error message.

Forgive me for my ignorance, but what exactly do you mean by "change localhost to localhost\SQLEXPRESS"?
Have you installed SQL server and rebooted your PC?

You can check if the service is running by running services.msc.
 

MrTrain

Member
Joined
2 Dec 2012
Messages
13
Soil, what an excellent utility, I would like to try and get this working with mySQL rather than the microsoft SQL. Would it be Possible to post the source code for loadfares.exe. I assume it is written with visual studio ?
 

soil

Established Member
Joined
28 May 2012
Messages
1,956
Soil, what an excellent utility, I would like to try and get this working with mySQL rather than the microsoft SQL. Would it be Possible to post the source code for loadfares.exe. I assume it is written with visual studio ?

http://pastebin.com/k9zUnkq5

There is also this interface:

public interface IRestriction
{
DateTime StartDate
{
get;

set;
}
DateTime EndDate
{ get; set; }


}

The business objects are all autogenerated using EntityFramework (mySql supported I think?), the only change i made was to make some of the restrictions inherit the above interface, this should be obvious when compiling.
 
Last edited:

Sacro

Member
Joined
20 Jan 2010
Messages
383
It might be an idea to port it to FluentNHibernate, that'll give you access to a lot more database systems
 

MrTrain

Member
Joined
2 Dec 2012
Messages
13
Soil, many thanks, I have just tried to run the origional exe on XP and it is indicating that it is not a valid win32 application.

Was it complied for a 64 bit version of windows by any chance ?
 

soil

Established Member
Joined
28 May 2012
Messages
1,956
Soil, many thanks, I have just tried to run the origional exe on XP and it is indicating that it is not a valid win32 application.

Was it complied for a 64 bit version of windows by any chance ?

I don't think so.

Do you have the .net framework installed?
 

MrTrain

Member
Joined
2 Dec 2012
Messages
13
Have .net framework installed but it is only version 4.0. Version 4.5 won't run on XP.

Have tried it on windows 7 and it runs ok

Have tried to recreate the project in visual studio 2010, (visual studio 2012 won't run on xp).

But it is generating lots of errors, with namespaces etc not found. I have resolved a lot of them, but not all. I think I have got to the limit of my experience of recreating the project.

Would it be possible to zip up the project files / directory and share it wih me ? I can then compile it to run on my configuration.

I really would like to get this working, particularly on xp.

Thanks in advance.
 

Goatboy

Established Member
Joined
23 Jun 2011
Messages
2,274
Quick note to say thanks - got it working perfectly.

Dissapointed to find no Via London tickets priced lower than the return to London Terminals, mind :p
 

soil

Established Member
Joined
28 May 2012
Messages
1,956
Quick note to say thanks - got it working perfectly.

Dissapointed to find no Via London tickets priced lower than the return to London Terminals, mind :p

Here's an updated s-proc

http://pastebin.com/RzJ7UtQV

I've added the TOC name, code, BoJ allowability, and Via London marker (Maltese Cross). Also an @TOCId parameter, e.g.,


EXEC spGetFaresFrom 'BTN', @TocID = 'NSC'

to show only the Southern fares from Brighton.

The Via London marker shows up on some fares not actually routed 'Via London'
 

soil

Established Member
Joined
28 May 2012
Messages
1,956
Would it be possible to zip up the project files / directory and share it wih me ? I can then compile it to run on my configuration.

I really would like to get this working, particularly on xp.

Thanks in advance.

Attached
 
Last edited:

MrTrain

Member
Joined
2 Dec 2012
Messages
13
Many thanks that's very much appreciated. I did not realise. How complex it is.

So that I can open the project could you possibly send me the .sln & .suo files that visual studio uses to open the project. Currently i can only see the individual files one at a time, I can't run it as a project.I am no expert, but I think they usually live in the highest level directory.

Thanks again, what an excellent piece of work, I have learnt lot already about sql.
 

soil

Established Member
Joined
28 May 2012
Messages
1,956
Hi, you don't need a SLN file, you can load the csproj file.
 

MrTrain

Member
Joined
2 Dec 2012
Messages
13
Thanks, I will give it a go ( showing my lack of knowledge here). Are you using visual studio 2010 or 2012 ?
 

MrTrain

Member
Joined
2 Dec 2012
Messages
13
oK, things are looking better, there is however one problem, When I load the project from the csproj, it generates an error complaining that it can't find a file & directory.

RoutingUtilities.csproj(215,3) the imported project C:\NFMProject\.nuget\nuget.targets was not found.

I have tried removing this from the .csproj file, but it seems to get 'upset' about this.
 

soil

Established Member
Joined
28 May 2012
Messages
1,956
not sure. Nuget is a package management system for .NET. It's built into VS.NET 2012.
 

MrTrain

Member
Joined
2 Dec 2012
Messages
13
Managed to get it working, many thanks.

Looking at the database structure, it seems quite complex. Did you have to work it out or is it documented what the exact links are.

I note that the Data feeds interface specification is not that specific.
 

soil

Established Member
Joined
28 May 2012
Messages
1,956
There are primary keys specified in the specification. There are no foreign keys specified as such, but it does make cross references between tables, which are then foreign keys. So the links are mostly documented.

I posted a description of the locations structure here:

http://www.railforums.co.uk/showpost.php?p=1394720&postcount=28

Also, it's not specified, but from what I can see, with the exception of new stations, (almost?) all the normal station-to-station fares are in the flows table, not the non-derivable fares file. Again, not specified, but it appears that where you have a flow with fares between cluster A and cluster B and then a flow from station X (in cluster A) to station Y (in cluster B), then the fares in cluster A and B are not available for sale.

The restrictions in the database use a current/future marker, which from the actual data is useless, and I changed this to use just a simple start and end date, which is much easier to work with.
 

soil

Established Member
Joined
28 May 2012
Messages
1,956
Just found an error with the importer, run this SQL script to fix

update
Flows
set
OriginCluster = OriginLocation, OriginLocation = null
from
Flows
join Clusters on Clusters.ClusterId = Flows.OriginLocation

update
Flows
set
DestinationCluster = DestinationLocation, DestinationLocation = null
from
Flows
join Clusters on Clusters.ClusterId = Flows.DestinationLocation


Basically some of the clusters are also stored as locations for some reason. So it misses some FCC & Metrolink clusters.

I will update the original importer at some point when I can be bothered, but the SQL above will fix things now.
 
Status
Not open for further replies.

Top