Skip to content

Instantly share code, notes, and snippets.

@sonkol
Created August 29, 2025 12:17
Show Gist options
  • Select an option

  • Save sonkol/14b1b15ccf8b5ac40c3f52ef57e2840a to your computer and use it in GitHub Desktop.

Select an option

Save sonkol/14b1b15ccf8b5ac40c3f52ef57e2840a to your computer and use it in GitHub Desktop.
Distances between stops in GTFS
--- Distances between stops
create table stop_sequences
as select
distinct
shape_id,
stop_times.stop_id,
stop_sequence,
stop_name || ' ' || coalesce(platform_code,'') stop_name,
shape_dist_traveled
from stop_times
join stops using (stop_id)
join trips using (trip_id)
where location_type = 0 and zone_id <> ''
order by shape_id, CAST(stop_sequence as integer);
select distinct * from (
select
shape_id,
stop_id,
lead(stop_id) over (partition by shape_id) stop_id2,
stop_name,
lead(stop_name) over (partition by shape_id) stop2,
round(lead(shape_dist_traveled) over (partition by shape_id) - shape_dist_traveled,3) distance
from stop_sequences)
where distance is not null
group by stop_id, stop_id2 and distance -- comment to show all pairs
order by distance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment