Saturday 14 May 2016

TrafficControl: Mapping Stations to Tracks from a KML File

The longlat coordinates for several existing and closed tracks and stations in Skåne, Sweden are stored in a KML file from Google Earth (blog post here). That file had some 420 tracks and stations.

The script scans the file for all stations and saves that information to a two-dimensional array that contains station name and coordinates.

After that, the script scans the file for all tracks and calculates the total distance of the tracks using the Haversine formula (Pythagoras' theorem won't do as the coordinate system is on the surface of a sphere). The name of the track, the length and the start and end coordinates are saved to a two-dimensional array. For each track, the station that is closest to the start and end points are found. If those candidates are closer than a predefined distance (in my case 100 meters), the track is connected to that station.

Finally, the script printed out the ADD STATION statements, ADD TRACK statements and CONNECT statements for the found connections.

The result file will look like:
ADD STATION Lund_C COORDINATES 55.70606862405453 13.18645933296118
ADD STATION Stångby COORDINATES 55.75072435969958 13.19996121860929
ADD TRACK dLun-StaN 5159 COORDINATES  55.70646523436889 13.18632710260987 55.71072132192893 13.18584908014066 55.71279148452389 13.18540683116051 55.71523576518295 13.18491978947624 55.71697139241737 13.18451423725925 55.71820550069631 13.18422078540084 55.71863549188491 13.18410509912514 55.72015263051769 13.18387805907402 55.72100614423973 13.18374645589791 55.72250595277901 13.18351438987617 55.72459782468813 13.18323899923316 55.72555794818392 13.18311356441235 55.72702138992866 13.18290315672957 55.72790621390291 13.18281900842842 55.72871601698184 13.18290611634804 55.72962139952958 13.18317702257096 55.7303726493431 13.18355658336061 55.73124798036218 13.18419008179007 55.73260471604188 13.18527763612758 55.73399029217104 13.18644576538502 55.7352346199416 13.18742225161641 55.73714017430663 13.18899079381876 55.73972705840463 13.19107073602017 55.74120631439643 13.19228528053877 55.74469495391181 13.19510449129269 55.75071735799666 13.19995102503562
ADD TRACK dLun-StaS 5159 COORDINATES 55.75071735799666 13.19995102503562 55.74469495391181 13.19510449129269 55.74120631439643 13.19228528053877 55.73972705840463 13.19107073602017 55.73714017430663 13.18899079381876 55.7352346199416 13.18742225161641 55.73399029217104 13.18644576538502 55.73260471604188 13.18527763612758 55.73124798036218 13.18419008179007 55.7303726493431 13.18355658336061 55.72962139952958 13.18317702257096 55.72871601698184 13.18290611634804 55.72790621390291 13.18281900842842 55.72702138992866 13.18290315672957 55.72555794818392 13.18311356441235 55.72459782468813 13.18323899923316 55.72250595277901 13.18351438987617 55.72100614423973 13.18374645589791 55.72015263051769 13.18387805907402 55.71863549188491 13.18410509912514 55.71820550069631 13.18422078540084 55.71697139241737 13.18451423725925 55.71523576518295 13.18491978947624 55.71279148452389 13.18540683116051 55.71072132192893 13.18584908014066 55.70646523436889 13.18632710260987 
CONNECT TRACK dLun-StaN FROM Lund_C TO Stångby DEFAULT
CONNECT TRACK dLun-StaN FROM Stångby TO Lund_C
CONNECT TRACK dLun-StaS FROM Stångby TO Lund_C DEFAULT
CONNECT TRACK dLun-StaS FROM Lund_C TO Stångby

The python scripts are available in the scripts section in my Gtihub repo: