From what I've gathered when speaking to Transit Police, Railroad Police, DHS guys and the TSA, in most circumstances it's not illegal to take pics of infrastructure like railroads, although there's always the chance that you might have a poorly trained security guard or overzealous and inexperienced officer try to tell you it's illegal. Also, despite what some people might tell you, Railroad Police Special Agents do have police authority beyond railroad property. In most states, they have full police authority throughout any state in which their railroad owns property.
They are trained to keep an eye out for people who are taking particular notice and/or photographs of railroad infrastructure. There has been legit intel over the past decade or so, showing Al Qaeda and some domestic terrorist groups have been surveilling the various parts of our transit system as potential targets.
Standing back and taking a picture of a train will probably not raise any red flags. However, due to the fact that there is a potential threat to our national infrastructure, if you're taking detailed pictures or observations of the nitty gritty mechanics of how the rail system runs, taking closeup shots of the power transmission lines, junctions, switch boxes, locks, train wheels and undercarriages, etc you will usually get stopped briefly and questioned while they check your ID (if they're in the area and see you do it).
Some people argue that you do not have to give your name to police if you are stopped. If you are stopped and you are uncertain if you are legally required to comply, simply ask them if you are being detained or if you are free to go. If they say you are being detained and if they have a reasonable suspicion to stop you, in most states you are legally required to give them your (real) name and DOB for identification purposes. They'll most likely call it in and verify if the name you give is a real person and matches your physical description. Giving false information in such a circumstance is a misdemeanor in most states and can get you arrested.
As long as you aren't operating a motor vehicle, in most jurisdictions you generally aren't required to give them your actual ID card if all they're doing is questioning you about your activities. However giving them your ID will usually speed up the process and get you out of there quicker (assuming you don't have any outstanding warrants).
Be polite, keep your hands visible and out of your pockets during any such encounter. Assuming you're stopped by one of the majority of officers who are reasonable human beings, it shouldn't take too long to resolve their suspicions and send you on your way. However if you're obstinate, or you get one of the few who are badge-heavy jerks, it might take a little longer.
Here is a link from a law firm who has experience with cases like this, and they offer a free PDF giving you some legal advice on the subject. I haven't read it in a while, so if they give advice in that pdf which is contrary to what I've said here, I'd go with what they say as they're the legal experts.
Just right click the link and save as a pdf.
http://www.krages.com/ThePhotographersRight.pdfIf you do a lot of photography, it might be a good idea to print out a copy and keep it in your camera bag.