Last active
May 21, 2016 20:02
-
-
Save Sam-Martin/60d4ed2e4d9a2a1b78de83dafcdbf22e to your computer and use it in GitHub Desktop.
A quick PowerShell way to get the lats and longs of all stations in the Greater London area (and a bit more besides)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$hashResult = @{} | |
if(!$TubeStationsRAW){ | |
$TubeStationsRAW =@() | |
$page = 0 | |
while(1){ | |
$page += 1; | |
Write-Verbose "Looking at page $page" | |
$result = $(invoke-webrequest "https://api.tfl.gov.uk/StopPoint/Mode/tube,overground,national-rail?page=$page").content | ConvertFrom-Json | |
if(!$result){ | |
return | |
} | |
$TubeStationsRAW += $result | |
} | |
} | |
$TubeStationsMainEntrances = $TubeStationsRAW | ?{$_.stopType -eq "NaptanMetroStation"} | select @{l="stationName";e={$_.commonname}}, lat, @{l="lng";e={$_.lon}}, @{l="type";e={"tube"}}, @{l="zone";e={($_.additionalProperties | ?{$_.key -eq "zone"}).value -split '\+' -split '/'}} | |
if(!$TrainStationsRaw){ | |
$topLeft = @{ | |
lat = '52.1374073' | |
lng = '-1.0101077'; | |
} | |
$bottomRight = @{ | |
lat = '51.1173673' | |
lng = '0.9008571' | |
} | |
$uri = "https://data.gov.uk/data/api/service/transport/naptan_railway_stations/extent?top-left-lat=$($topLeft.lat)&top-left-lon=$($topLeft.lng)&bottom-right-lat=$($bottomRight.lat)&bottom-right-lon=$($bottomRight.lng)" | |
write-verbose $uri | |
$TrainStationsRaw = $(Invoke-WebRequest $uri).content | ConvertFrom-Json | |
} | |
$TrainStations = $TrainStationsRaw.result | Select stationName, @{l="lat";e={$_.latlong.coordinates[1]}}, @{l="lng";e={$_.latlong.coordinates[0]}}, @{l="type";e={"train"}} | |
# Combine the results into one big array | |
$result = $TubeStationsMainEntrances + $TrainStations | |
# Create a results hash table with the station name as the key (this will deduplicate records with the same station name) | |
$result | %{ | |
$hashResult.($_.stationname) = @{ | |
lat = $_.lat | |
lng = $_.lng | |
type = $_.type | |
zone = $_.zone | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment