When placing buildings and to some extent, tracks, we can still build them outside the map as long the cursor is inside the map.

The depot connection to the track is inside the map boundary. The track is half outside the map because the build cursor was exactly on the edge.

The station was built exactly on the map edge. There was no problem to connect the tracks outside the meap, even if the end points was outside the map (the station). Start point was on the map.
I guess the easiest way to fix this is to perform a collision test with the map border.