The problem with a compass is that it only tells you which direction the carriage is facing. It doesn't tell you which direction it is going in, or which end of the train it is at. You need additional information to do that.
Take the London Kings Cross to Glasgow Central route. In both locations the train will be pointing virtually North/South, and the trains simply reverse back down the line - I don't recall the last time first class wasn't at the near end of the platform at Kings Cross. The compass doesn't help you here without additional information, namely a timetable or where the train has just come from.
The reason I say this is that all announcements I have heard on the platform or train all refer to front/rear. It seems far easier to just know front/rear than infer it from additional information such as a timetable (which can't be relied on in the UK) or live scheduling information (which seems to vary from TOC to TOC - SouthWestTrains is always correct, FGW have to override automated announcements manually all of the time).
You need to know which direction the train is moving in, which is very different. The complex solution to this is GPS. You could use one unit (only useful once the train is moving) or two units (useful when stationary) to do this. It seems like a lot of work.
Easier than this would be an accelerometer, but again, this will only work once the train is moving.
How would I suggest doing it? A small box with a GPRS modem in each driving cab. The driver presses a button during his pre-departure checks. Depending on which one is pressed, you know which end is the front. The issue with it is that you have to rely on the driver, and implementing a fleet-wide additional step to checks may be hard. You could try detecting the presence of the driver in other ways - PIR might work (fails if the guard sits in the non-driving cab). I doubt you'd be able to link into the trains systems to detect where the driver was.