Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanzimanyi committed Jul 3, 2024
1 parent 444ff2c commit 8f863ab
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
3 changes: 2 additions & 1 deletion develop/html/ch04.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
shape_id text NOT NULL,
shape_pt_lat double precision NOT NULL,
shape_pt_lon double precision NOT NULL,
shape_pt_sequence int NOT NULL
shape_pt_sequence int NOT NULL,
shape_dist_traveled float NOT NULL
);
CREATE INDEX shapes_shape_id_idx ON shapes (shape_id);

Expand Down
45 changes: 21 additions & 24 deletions develop/html/ch04s02.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(friday = 1 AND extract(isodow FROM d) = 5) OR
(saturday = 1 AND extract(isodow FROM d) = 6) OR
(sunday = 1 AND extract(isodow FROM d) = 7)
)
)
EXCEPT
SELECT service_id, date
FROM calendar_dates WHERE exception_type = 2
Expand Down Expand Up @@ -99,7 +99,7 @@
</pre><p>
We use twice the <code class="varname">LEAD</code> window function for obtaning the next stop and the next percentage of a given stop and the <code class="varname">MAX</code> window function for obtaining the total number of stops in a trip. Then, we generate the geometry of the segment betwen two stops using the function <code class="varname">ST_LineSubstring</code> and compute the length and the number of points in the segment with functions <code class="varname">ST_Length</code> and <code class="varname">ST_NumPoints</code>.
</p><p>
The geometry of a segment is a linestring containing multiple points. From the previous table we know at which time the trip arrived at the first point and at the last point of the segment. To determine at which time the trip arrived at the intermediate points of the segments, we create a table <code class="varname">trip_points</code> that contains all the points composing the geometry of a segment.
The geometry of a segment is a linestring containing multiple points. From table <code class="varname">trip_stops</code> we know at which time the trip arrived at the first point and at the last point of the segment. To determine at which time the trip arrived at the intermediate points of the segments, we create a table <code class="varname">trip_points</code> that contains all the points composing the geometry of a segment.
</p><pre class="programlisting">
DROP TABLE IF EXISTS trip_points;
CREATE TABLE trip_points (
Expand All @@ -117,29 +117,26 @@
point_sequence, point_geom, point_arrival_time)
WITH temp1 AS (
SELECT trip_id, route_id, service_id, stop1_sequence, stop2_sequence,
no_stops, stop1_arrival_time, stop2_arrival_time, seg_length,
(dp).path[1] AS point_sequence, no_points, (dp).geom as point_geom
FROM trip_segs, ST_DumpPoints(seg_geom) AS dp
),
no_stops, stop1_arrival_time, stop2_arrival_time, seg_length,
(dp).path[1] AS point_sequence, no_points, (dp).geom as point_geom
FROM trip_segs, ST_DumpPoints(seg_geom) AS dp ),
temp2 AS (
SELECT trip_id, route_id, service_id, stop1_sequence, stop1_arrival_time,
stop2_arrival_time, seg_length, point_sequence, no_points, point_geom
FROM temp1
WHERE point_sequence &lt;&gt; no_points OR stop2_sequence = no_stops
),
SELECT trip_id, route_id, service_id, stop1_sequence, stop1_arrival_time,
stop2_arrival_time, seg_length, point_sequence, no_points, point_geom
FROM temp1
WHERE point_sequence != no_points OR stop2_sequence = no_stops ),
temp3 AS (
SELECT trip_id, route_id, service_id, stop1_sequence, stop1_arrival_time,
stop2_arrival_time, point_sequence, no_points, point_geom,
ST_Length(ST_MakeLine(array_agg(point_geom) OVER w)) / seg_length AS perc
FROM temp2 WINDOW w AS (PARTITION BY trip_id, service_id, stop1_sequence
ORDER BY point_sequence)
)
SELECT trip_id, route_id, service_id, stop1_sequence, stop1_arrival_time,
stop2_arrival_time, point_sequence, no_points, point_geom,
ST_Length(ST_MakeLine(array_agg(point_geom) OVER w)) / seg_length AS perc
FROM temp2 WINDOW w AS (PARTITION BY trip_id, service_id, stop1_sequence
ORDER BY point_sequence) )
SELECT trip_id, route_id, service_id, stop1_sequence, point_sequence, point_geom,
CASE
WHEN point_sequence = 1 then stop1_arrival_time
WHEN point_sequence = no_points then stop2_arrival_time
ELSE stop1_arrival_time + ((stop2_arrival_time - stop1_arrival_time) * perc)
END AS point_arrival_time
CASE
WHEN point_sequence = 1 THEN stop1_arrival_time
WHEN point_sequence = no_points THEN stop2_arrival_time
ELSE stop1_arrival_time + ((stop2_arrival_time - stop1_arrival_time) * perc)
END AS point_arrival_time
FROM temp3;
</pre><p>
In the temporary table <code class="varname">temp1</code> we use the function <code class="varname">ST_DumpPoints</code> to obtain the points composing the geometry of a segment. Nevertheless, this table contains duplicate points, that is, the last point of a segment is equal to the first point of the next one. In the temporary table <code class="varname">temp2</code> we filter out the last point of a segment unless it is the last segment of the trip. In the temporary table <code class="varname">temp3</code> we compute in the attribute <code class="varname">perc</code> the relative position of a point within a trip segment with window functions. For this we use the function <code class="varname">ST_MakeLine</code> to construct the subsegment from the first point of the segment to the current one, determine the length of the subsegment with function <code class="varname">ST_Length</code> and divide this length by the overall segment length. Finally, in the outer query we use the computed percentage to determine the arrival time to that point.
Expand Down Expand Up @@ -184,8 +181,8 @@

INSERT INTO trips_mdb(trip_id, service_id, route_id, date, trip)
SELECT trip_id, route_id, t.service_id, d.date,
shift(trip, make_interval(days =&gt; d.date - t.date))
FROM trips_mdb t JOIN service_dates d ON t.service_id = d.service_id AND t.date &lt;&gt; d.date;
shiftTime(trip, make_interval(days =&gt; d.date - t.date))
FROM trips_mdb t JOIN service_dates d ON t.service_id = d.service_id AND t.date != d.date;
</pre><p>
In the first <code class="varname">INSERT</code> statement we group the rows in the <code class="varname">trips_input</code> table by <code class="varname">trip_id</code> and <code class="varname">date</code> while keeping the <code class="varname">route_id</code> atribute, use the <code class="varname">array_agg</code> function to construct an array containing the temporal points composing the trip ordered by time, and compute the trip from this array using the function <code class="varname">tgeompointseq</code>. As explained above, table <code class="varname">trips_input</code> only contains the first date of a trip. In the second <code class="varname">INSERT</code> statement we add the trips for all the other dates with the function <code class="varname">shift</code>.
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch04.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch04.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch05.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 4. Managing GTFS Data </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 5. Managing Google Location History</td></tr></table></div></body></html>
Binary file modified develop/mobilitydb-workshop.epub
Binary file not shown.
Binary file modified develop/mobilitydb-workshop.pdf
Binary file not shown.

0 comments on commit 8f863ab

Please sign in to comment.