diff --git a/develop/html/ch04.html b/develop/html/ch04.html index 6413838..e4edc0f 100644 --- a/develop/html/ch04.html +++ b/develop/html/ch04.html @@ -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); diff --git a/develop/html/ch04s02.html b/develop/html/ch04s02.html index 0efedc1..3381c1d 100644 --- a/develop/html/ch04s02.html +++ b/develop/html/ch04s02.html @@ -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 @@ -99,7 +99,7 @@

We use twice the LEAD window function for obtaning the next stop and the next percentage of a given stop and the MAX 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 ST_LineSubstring and compute the length and the number of points in the segment with functions ST_Length and ST_NumPoints.

- 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 trip_points that contains all the points composing the geometry of a segment. + The geometry of a segment is a linestring containing multiple points. From table trip_stops 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 trip_points that contains all the points composing the geometry of a segment.

 DROP TABLE IF EXISTS trip_points;
 CREATE TABLE trip_points (
@@ -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 <> 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;
 			

In the temporary table temp1 we use the function ST_DumpPoints 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 temp2 we filter out the last point of a segment unless it is the last segment of the trip. In the temporary table temp3 we compute in the attribute perc the relative position of a point within a trip segment with window functions. For this we use the function ST_MakeLine to construct the subsegment from the first point of the segment to the current one, determine the length of the subsegment with function ST_Length 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. @@ -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 => d.date - t.date)) -FROM trips_mdb t JOIN service_dates d ON t.service_id = d.service_id AND t.date <> d.date; + shiftTime(trip, make_interval(days => d.date - t.date)) +FROM trips_mdb t JOIN service_dates d ON t.service_id = d.service_id AND t.date != d.date;

In the first INSERT statement we group the rows in the trips_input table by trip_id and date while keeping the route_id atribute, use the array_agg 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 tgeompointseq. As explained above, table trips_input only contains the first date of a trip. In the second INSERT statement we add the trips for all the other dates with the function shift.

diff --git a/develop/mobilitydb-workshop.epub b/develop/mobilitydb-workshop.epub index b967880..f256e21 100644 Binary files a/develop/mobilitydb-workshop.epub and b/develop/mobilitydb-workshop.epub differ diff --git a/develop/mobilitydb-workshop.pdf b/develop/mobilitydb-workshop.pdf index 7dfc3c4..dad3dc8 100644 Binary files a/develop/mobilitydb-workshop.pdf and b/develop/mobilitydb-workshop.pdf differ