This video demonstrates how the Raspberry Pi can automate a model railway by interfacing with a DCC controller and Hall-effect switches to provide train positioning feedback.
For further details including python code please see the following:
You may be interested in this project that shows how a ‘Scratch’ program can be used to control a model model railway using DCC++
/