Skip to content

Instantly share code, notes, and snippets.

@scruss
Last active November 1, 2025 17:48
Show Gist options
  • Save scruss/cebe194f384910770dc38de466ff22ff to your computer and use it in GitHub Desktop.
Save scruss/cebe194f384910770dc38de466ff22ff to your computer and use it in GitHub Desktop.
average velocity of all moving TTC vehicles from GTFS feed
#!/usr/bin/gawk -f
# average velocity of all moving TTC vehicles - scruss, 2025-09
# get GTFS (text) from https://bustime.ttc.ca/gtfsrt/vehicles?debug
# *** THIS IS NEITHER A SENSIBLE NOR A USEFUL TOOL ***
# needs gawk or mawk for strftime, which Kernighan awk does not have
BEGIN {
d2r=4*atan2(1,1)/180;
kmh2mms=2500/9;
}
/header {/ {
inheader=1;
}
/entity {/ {
inheader=0;
}
(inheader>0 && /timestamp:/) {
timestamp=$2;
}
/bearing: / {
b=$2;
}
/speed: / {
s=$2;
}
(b>-1 && s>0.0) {
# vector sum components for all moving vehicles
tx+=s*cos(b*d2r);
ty+=s*sin(b*d2r);
n++;
b=-999;
}
END {
x=tx/n;
y=ty/n;
v=kmh2mms*sqrt(x*x+y*y);
b=atan2(y, x)/d2r;
if (b<0.0) {
b+=360.0;
}
reported=strftime("%X %Z", timestamp);
printf("TTC fleet mean velocity %.1f mm/s, bearing %.1f° for %'d moving vehicles: reported at %s.\n", v, b, n, reported);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment