-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed
More file actions
84 lines (71 loc) · 2.88 KB
/
Copy pathembed
File metadata and controls
84 lines (71 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
param(
[string]$AreasPath = (Join-Path $PSScriptRoot 'areas.geojson')
)
Add-Type -AssemblyName System.IO.Compression.FileSystem
$gtfsUrl = 'https://storage.googleapis.com/marduk-production/outbound/gtfs/rb_sky-aggregated-gtfs.zip'
$zipPath = Join-Path $env:TEMP 'skyss-gtfs.zip'
Invoke-WebRequest -Uri $gtfsUrl -OutFile $zipPath
$archive = [System.IO.Compression.ZipFile]::OpenRead($zipPath)
function Read-GtfsCsv([string]$name) {
$entry = $archive.GetEntry($name)
if (-not $entry) { throw "Mangler $name i GTFS-filen" }
$reader = [System.IO.StreamReader]::new($entry.Open())
try { @($reader.ReadToEnd() | ConvertFrom-Csv) }
finally { $reader.Dispose() }
}
try {
$areas = Get-Content -Raw $AreasPath | ConvertFrom-Json
$routes = Read-GtfsCsv 'routes.txt'
$trips = Read-GtfsCsv 'trips.txt'
$shapes = Read-GtfsCsv 'shapes.txt'
$stops = Read-GtfsCsv 'stops.txt'
$routeIndex = @{}
$routes | Where-Object { $_.route_type -in @('0', '3') } |
ForEach-Object { $routeIndex[$_.route_id] = $_ }
$shapeRoutes = @{}
$trips | Where-Object { $routeIndex.ContainsKey($_.route_id) -and $_.shape_id } |
ForEach-Object {
if (-not $shapeRoutes.ContainsKey($_.shape_id)) {
$shapeRoutes[$_.shape_id] = $_.route_id
}
}
$shapePoints = @{}
$shapes | ForEach-Object {
if ($shapeRoutes.ContainsKey($_.shape_id)) {
if (-not $shapePoints.ContainsKey($_.shape_id)) {
$shapePoints[$_.shape_id] = [System.Collections.Generic.List[object]]::new()
}
$shapePoints[$_.shape_id].Add($_)
}
}
$network = foreach ($shapeId in $shapePoints.Keys) {
$route = $routeIndex[$shapeRoutes[$shapeId]]
$points = @($shapePoints[$shapeId] |
Sort-Object { [int]$_.shape_pt_sequence } |
ForEach-Object {
@([math]::Round([double]$_.shape_pt_lon, 5), [math]::Round([double]$_.shape_pt_lat, 5))
})
if ($points.Count -ge 2) {
[ordered]@{
routeId = $route.route_id
transportType = if ($route.route_type -eq '0') { 'tram' } else { 'localBus' }
routeShortName = $route.route_short_name
routeLongName = $route.route_long_name
coordinates = $points
}
}
}
$areas | Add-Member -Force -NotePropertyName staticData -NotePropertyValue ([ordered]@{
generated = (Get-Date).ToUniversalTime().ToString('o')
network = @($network)
stops = @($stops | ForEach-Object {
@([math]::Round([double]$_.stop_lat, 5), [math]::Round([double]$_.stop_lon, 5))
})
})
$areas | ConvertTo-Json -Depth 20 -Compress |
Set-Content -Encoding utf8NoBOM $AreasPath
}
finally {
$archive.Dispose()
Remove-Item $zipPath -Force -ErrorAction SilentlyContinue
}