Skip to content

Commit 1b4f8a2

Browse files
committed
add guide: distance between stops along trip shape 📝
1 parent a6045f8 commit 1b4f8a2

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# calculating the geographic distance of a trip's shape between two stops
2+
3+
1. For each stop, respectively, find the point that's closest to the stop (using `ST_LineLocatePoint()`), and then
4+
2. measure the length between those points (using `ST_LineSubstring()` & `ST_Length()`).
5+
6+
```sql
7+
WITH
8+
stop_a AS (
9+
SELECT *
10+
FROM stops
11+
WHERE stop_id = 'stop A ID'
12+
),
13+
stop_b AS (
14+
SELECT *
15+
FROM stops
16+
WHERE stop_id = 'stop B ID'
17+
)
18+
SELECT
19+
ST_Length(ST_LineSubstring(
20+
shape::geography,
21+
ST_LineLocatePoint(shape::geography, stop_a.stop_loc),
22+
ST_LineLocatePoint(shape::geography, stop_b.stop_loc)
23+
)) AS segment_length
24+
FROM stop_a, stop_b, trips
25+
JOIN shapes_aggregated ON shapes_aggregated.shape_id = trips.shape_id
26+
WHERE trip_id = 'some trip ID'
27+
```

0 commit comments

Comments
 (0)