Querying the Data

We discuss next four categories of queries: range queries, distance queries, temporal aggregate queries, and nearest-neighbor queries.

Range Queries

The queries in this category restrict Trips with respect to a spatial, temporal, or spatio-temporal point or range. In the examples, the spatial points and ranges are given, respectively, in tables Points and Regions, while temporal points and ranges are given, respectively, in tables Instants and Periods.

  1. List the vehicles that have passed at a region from Regions.

    SELECT DISTINCT R.RegionId, T.VehId
    FROM Trips T, Regions R
    WHERE ST_Intersects(trajectory(T.Trip), R.Geom)
    ORDER BY R.RegionId, T.VehId;
    

    This is a spatial range query. The query verifies that the trajectory of the vehicle intersects the region. PostGIS performs an implicit bounding box comparison trajectory(T.Trip) && R.Geom using the spatial index on table Regions when executing the predicate ST_Intersects.

  2. List the vehicles that were within a region from Regions during a period from Periods.

    SELECT R.RegionId, P.PeriodId, T.VehId
    FROM Trips T, Regions R, Periods P
    WHERE T.Trip && stbox(R.Geom, P.Period) AND 
      intersects(atPeriod(T.Trip, P.Period), R.Geom)
    ORDER BY R.RegionId, P.PeriodId, T.VehId;
    

    This is a spatio-temporal range query. The query performs a bounding box comparison with the && operator using the spatio-temporal index on table Trips. After that, the query verifies that the location of the vehicle during the period intersects the region. Notice that the predicate _intersects is used instead of intersects to avoid an implicit index test with the bounding box comparison atPeriod(Trip, P.Period) && R.Geom is performed using the spatio-temporal index.

  3. List the pairs of vehicles that were both located within a region from Regions during a period from Periods.

    SELECT DISTINCT T1.VehId AS VehId1, T2.VehId AS VehId2, R.RegionId, P.PeriodId
    FROM Trips T1, Trips T2, Regions R, Periods P
    WHERE T1.VehId < T2.VehId AND T1.Trip && stbox(R.Geom, P.Period) AND
      T2.Trip && stbox(R.Geom, P.Period) AND 
      intersects(atPeriod(T1.Trip, P.Period), R.Geom) AND
      intersects(atPeriod(T2.Trip, P.Period), R.Geom)
    ORDER BY T1.VehId, T2.VehId, R.RegionId, P.PeriodId;
    

    This is a spatio-temporal range join query. The query selects two trips of different vehicles and performs bounding box comparisons of each trip with a region and a period using the spatio-temporal index of the Trips table. The query then verifies that both vehicles were located within the region during the period.

  4. List the first time at which a vehicle visited a point in Points.

    SELECT T.VehId, P.PointId, MIN(startTimestamp(atValue(T.Trip,P.Geom))) AS Instant
    FROM Trips T, Points P
    WHERE ST_Contains(trajectory(T.Trip), P.Geom)
    GROUP BY T.VehId, P.PointId;
    

    The query selects a trip and a point and verifies that the vehicle passed by the point by testing that the trajectory of the trip contains the point. Notice that PostGIS will perform the bounding box containment trajectory(T.Trip) ~ P.Geom using the spatial index on table Points before executing ST_Contains. Then, the query projects the trip to the point with the atValue function, get the first timestamp of the projected trip with the startTimestamp function, and applies the traditional MIN aggregate function for all trips of the vehicle and the point.

Temporal Aggregate Queries

There are three common types of temporal aggregate queries.

  • Instant temporal aggregate queries in which, from a conceptual perspective, the traditional aggregate function is applied at each instant.

  • Window temporal aggregate queries (also known as cumulative queries), which, given a time interval w, compute the value of the aggregate at a time instant t from the values during the time period [t-w, t].

  • Span temporal aggregate queries, which, first, split the time line into predefined intervals independently of the target data, and then, for each of these intervals, aggregate the data that overlap the interval.

  1. Compute how many vehicles were active at each period in Periods.

    SELECT P.PeriodID, COUNT(*), TCOUNT(atPeriod(T.Trip, P.Period))
    FROM Trips T, Periods P
    WHERE T.Trip && P.Period
    GROUP BY P.PeriodID
    ORDER BY P.PeriodID;
    

    This an instant temporal aggregate query. For each period, the query projects the trips to the given period and applies the temporal count to the projected trips. The condition in the WHERE clause is used for filtering the trips with the spatio-temporal index on table Trips.

  2. For each region in Regions, give the window temporal count of trips with a 10-minute interval.

    SELECT R.RegionID, WCOUNT(atGeometry(T.Trip, R.Geom), interval '10 min')
    FROM Trips T, Regions R
    WHERE T.Trip && R.Geom
    GROUP BY R.RegionID
    HAVING WCOUNT(atGeometry(T.Trip, R.Geom), interval '10 min') IS NOT NULL
    ORDER BY R.RegionID;
    

    This is a window temporal aggregate query. Suppose that we are computing pollution levels by region. Since the effect of a vehicle passing at a location lasts some time interval, this is a typical case for window aggregates. For each region, the query computes the spatial projection of the trips to the given region and apply the window temporal count to the projected trips. The condition in the WHERE clause is used for filtering the trips with the spatio-temporal index. The condition in the HAVING clause is used for removing regions that do not intersect with any trip.

  3. Count the number of trips that were active during each hour in May 29, 2007.

    WITH TimeSplit(Period) AS (
      SELECT period(H, H + interval '1 hour')
      FROM generate_series(timestamptz '2007-05-29 00:00:00', 
        timestamptz '2007-05-29 23:00:00', interval '1 hour') AS H )
    SELECT Period, COUNT(*)
    FROM TimeSplit S, Trips T
    WHERE S.Period && T.Trip AND atPeriod(Trip, Period) IS NOT NULL
    GROUP BY S.Period
    ORDER BY S.Period;
    

    This is a span temporal aggregate query. The query defines the intervals to consider in the TimeSplit temporary table. For each of these intervals, the main query applies the traditional count function for counting the trips that overlap the interval.

Distance Queries

The queries in this category deal with either the distance travelled by a single object or the distance between two objects. The complexity of the latter queries depend, on the one hand, on whether the reference objects are static or moving, and on the other, on whether the operation required is either the minimum distance ever or the temporal distance computed at each instant.

  1. List the overall traveled distances of the vehicles during the periods from Periods.

    SELECT T.VehId, P.PeriodId, P.Period,
      SUM(length(atPeriod(T.Trip, P.Period))) AS Distance
    FROM Trips T, Periods P
    WHERE T.Trip && P.Period
    GROUP BY T.VehId, P.PeriodId, P.Period
    ORDER BY T.VehId, P.PeriodId;
    

    The query performs a bounding box comparison with the && operator using the spatio-temporal index on the Trips table. It then projects the trip to the period, computes the length of the projected trip, and sum the lengths of all the trips of the same vehicle during the period.

  2. List the minimum distance ever between each vehicle and each point from Points.

    SELECT T.VehId, P.PointId, MIN(trajectory(T.Trip) <-> P.Geom) AS MinDistance
    FROM Trips T, Points P
    GROUP BY T.VehId, P.PointId
    ORDER BY T.VehId, P.PointId;
    

    The query projects the trip to the spatial dimension with the trajectory function and computes the traditional distance between the trajectory of the trip and the point. The traditional minimum function is then applied for computing the minimum distance between all trips of the vehicle and the point.

  3. List the minimum temporal distance between each pair of vehicles.

    SELECT T1.VehId AS Car1Id, T2.VehId AS Car2Id, tmin(T1.Trip <-> T2.Trip) AS MinDistance
    FROM Trips T1, Trips T2
    WHERE T1.VehId < T2.VehId AND period(T1.Trip) && period(T2.Trip)
    GROUP BY T1.VehId, T2.VehId
    ORDER BY T1.VehId, T2.VehId;
    

    The query selects two trips T1 and T2 from different vehicles that were both traveling during a common period of time, computes the temporal distance between the trips, and then computes the temporal minimum distance between all trips of the two vehicles. The query uses the spatio-temporal index to filter the pairs of trips that were both traveling during a common period of time.

  4. List the nearest approach time, distance, and shortest line between each pair of trips.

    SELECT T1.VehId AS Car1Id, T1.TripId AS Trip1Id, T2.VehId AS Car2Id, 
      T2.TripId AS Trip2Id, period(NearestApproachInstant(T1.Trip, T2.Trip)) AS Time,
      NearestApproachDistance(T1.Trip, T2.Trip) AS Distance, 
      ShortestLine(T1.Trip, T2.Trip) AS Line
    FROM Trips T1, Trips T2
    WHERE T1.VehId < T2.VehId AND period(T1.Trip) && period(T2.Trip)
    ORDER BY T1.VehId, T1.TripId, T2.VehId, T2.TripId;
    

    This query shows similar functionality as that provided by the PostGIS functions ST_ClosestPointOfApproach and ST_DistanceCPA. The query selects two trips T1 and T2 from different vehicles that were both traveling during a common period of time and computes the required results.

  5. List when and where a pairs of vehicles have been at 10 m or less from each other.

    SELECT T1.VehId AS VehId1, T2.VehId AS VehId2, atPeriodSet(T1.Trip,
      getTime(tdwithin(T1.Trip, T2.Trip, 10.0, TRUE))) AS Position
    FROM Trips T1, Trips T2
    WHERE T1.VehId < T2.VehId AND T1.Trip && expandSpatial(T2.Trip, 10) AND
      tdwithin(T1.Trip, T2.Trip, 10.0, TRUE) IS NOT NULL
    ORDER BY T1.VehId, T2.VehId, Position;
    

    The query performs for each pair of trips T1 and T2 of different vehicles a bounding box comparison with the && operator using the spatio-temporal index on the Trips table, where the bounding box of T2 is expanded by 10 m. Then, the period expression computes the periods during which the vehicles were within 10 m. from each other and the atPeriodSet function projects the trips to those periods. Notice that the expression tdwithin(T1.Trip, T2.Trip, 10.0) is conceptually equivalent to dwithin(T1.Trip, T2.Trip) #<= 10.0. However, in this case the spatio-temporal index cannot be used for filtering values.

Nearest-Neighbor Queries

There are three common types of nearest-neighbor queries in spatial databases.

  • k-nearest-neighbor (kNN) queries find the k nearest points to a given point.

  • Reverse k-nearest-neighbor (RkNN) queries find the points that have a given point among their k nearest-neighbors.

  • Given two sets of points P and Q, aggregate nearest-neighbor (ANN) queries find the points from P that have minimum aggregated distance to all points from Q.

The above types of queries are generalized to temporal points. However, the complexity of these queries depend on whether the reference object and the candidate objects are static or moving. In the examples that follow we only consider the nontemporal version of the nearest-neighbor queries, that is, the one in which the calculation is performed on the projection of temporal points on the spatial dimension. The temporal version of the nearest-neighbor queries remains to be done.

  1. For each trip from Trips, list the three points from Points that have been closest to that vehicle.

    WITH TripsTraj AS (
      SELECT TripId, VehId, trajectory(Trip) AS Trajectory FROM Trips )
    SELECT T.VehId, P1.PointId, P1.Distance
    FROM TripsTraj T CROSS JOIN LATERAL (
      SELECT P.PointId, T.Trajectory <-> P.Geom AS Distance
      FROM Points P
      ORDER BY Distance LIMIT 3 ) AS P1
    ORDER BY T.TripId, T.VehId, P1.Distance;
    

    This is a nearest-neighbor query with moving reference objects and static candidate objects. The query above uses PostgreSQL's lateral join, which intuitively iterates over each row in a result set and evaluates a subquery using that row as a parameter. The query starts by computing the trajectory of the trips in the temporary table TripsTraj. Then, given a trip T in the outer query, the subquery computes the traditional distance between the trajectory of T and each point P. The ORDER BY and LIMIT clauses in the inner query select the three closest points. PostGIS will use the spatial index on the Points table for selecting the three closest points.

  2. For each trip from Trips, list the three vehicles that are closest to that vehicle

    SELECT T1.VehId AS VehId1, C2.VehId AS VehId2, C2.Distance
    FROM Trips T1 CROSS JOIN LATERAL (
      SELECT T2.VehId, minValue(T1.Trip <-> T2.Trip) AS Distance
      FROM Trips T2
      WHERE T1.VehId < T2.VehId AND period(T1.Trip) && period(T2.Trip)
      ORDER BY Distance LIMIT 3 ) AS C2
    ORDER BY T1.VehId, C2.VehId;
    

    This is a nearest-neighbor query where both the reference and the candidate objects are moving. Therefore, it is not possible to proceed as in the previous query to first project the moving points to the spatial dimension and then compute the traditional distance. Given a trip T1 in the outer query, the subquery computes the temporal distance between T1 and a trip T2 of another vehicle different from the vehicle from T1 and then computes the minimum value in the temporal distance. Finally, the ORDER BY and LIMIT clauses in the inner query select the three closest vehicles.

  3. For each trip from Trips, list the points from Points that have that vehicle among their three nearest neighbors.

    WITH TripsTraj AS (
      SELECT TripId, VehId, trajectory(Trip) AS Trajectory FROM Trips ),
    PointTrips AS (
      SELECT P.PointId, T2.VehId, T2.TripId, T2.Distance
      FROM Points P CROSS JOIN LATERAL (
        SELECT T1.VehId, T1.TripId, P.Geom <-> T1.Trajectory AS Distance
        FROM TripsTraj T1
        ORDER BY Distance LIMIT 3 ) AS T2 )
    SELECT T.VehId, T.TripId, P.PointId, PT.Distance
    FROM Trips T CROSS JOIN Points P JOIN PointTrips PT
      ON T.VehId = PT.VehId AND T.TripId = PT.TripId AND P.PointId = PT.PointId
    ORDER BY T.VehId, T.TripId, P.PointId;
    

    This is a reverse nearest-neighbor query with moving reference objects and static candidate objects. The query starts by computing the corresponding nearest-neighbor query in the temporary table PointTrips as it is done in Query 13. Then, in the main query it verifies for each trip T and point P that both belong to the PointTrips table.

  4. For each trip from Trips, list the vehicles having the vehicle of the trip among the three nearest neighbors.

    WITH TripDistances AS (
      SELECT T1.VehId AS VehId1, T1.TripId AS TripId1, T3.VehId AS VehId2, 
        T3.TripId AS TripId2, T3.Distance
      FROM Trips T1 CROSS JOIN LATERAL (
        SELECT T2.VehId, T2.TripId, minValue(T1.Trip <-> T2.Trip) AS Distance
        FROM Trips T2
        WHERE T1.VehId < T2.VehId AND period(T1.Trip) && period(T2.Trip)
        ORDER BY Distance LIMIT 3 ) AS T3 )
    SELECT T1.VehId, T1.TripId, T2.VehId, T2.TripId, TD.Distance
    FROM Trips T1 JOIN Trips T2 ON T1.VehId < T2.VehId
      JOIN TripDistances TD ON T1.VehId = TD.VehId1 AND T1.TripId = TD.TripId1 AND
      T2.VehId = TD.VehId2 AND T2.TripId = TD.TripId2
    ORDER BY T1.VehId, T1.TripId, T2.VehId, T2.TripId;
    

    This is a reverse nearest-neighbor query where both the reference and the candidate objects are moving. The query starts by computing the corresponding nearest-neighbor query in the temporary table TripDistances as it is done in Query 14. Then, in the main query it verifies for each pair of trips T1 and T2 that both belong to the TripDistances table.

  5. For each group of ten disjoint vehicles, list the point(s) from Points, having the minimum aggregated distance from the given group of ten vehicles during the given period.

    WITH Groups AS (
      SELECT ((ROW_NUMBER() OVER (ORDER BY V.VehId))-1)/10 + 1 AS GroupId, V.VehId
      FROM Vehicles V ),
    SumDistances AS (
      SELECT G.GroupId, P.PointId,
        SUM(ST_Distance(trajectory(T.Trip), P.Geom)) AS SumDist
      FROM Groups G, Points P, Trips T
      WHERE T.VehId = G.VehId
      GROUP BY G.GroupId, P.PointId )
    SELECT S1.GroupId, S1.PointId, S1.SumDist
    FROM SumDistances S1
    WHERE S1.SumDist <= ALL (
      SELECT SumDist
      FROM SumDistances S2
      WHERE S1.GroupId = S2.GroupId )
    ORDER BY S1.GroupId, S1.PointId;
    

    This is an aggregate nearest-neighbor query. The temporary table Groups splits the vehicles in groups where the GroupId column takes the values from 1 to total number of groups. The temporary table SumDistances computes for each group G and point P the sum of the distances between a trip of a vehicle in the group and the point. The main query then selects for each group in table SumDistances the points(s) that have the minimum aggregated distance.