BerlinMOD/R Queries

The script for querying BerlinMOD data loaded in MobilityDB with the BerlinMOD/R queries is available here.

  1. What are the models of the vehicles with licence plate numbers from Licences?

    SELECT DISTINCT L.Licence, C.Model AS Model
    FROM Vehicles C, Licences L
    WHERE C.Licence = L.Licence;
    
  2. How many vehicles exist that are passenger cars?

    SELECT COUNT (Licence)
    FROM Vehicles C
    WHERE Type = 'passenger';
    
  3. Where have the vehicles with licences from Licences1 been at each of the instants from Instants1?

    SELECT DISTINCT L.Licence, I.InstantId, I.Instant AS Instant,
      valueAtTimestamp(T.Trip, I.Instant) AS Pos
    FROM Trips T, Licences1 L, Instants1 I
    WHERE T.VehId = L.VehId AND valueAtTimestamp(T.Trip, I.Instant) IS NOT NULL
    ORDER BY L.Licence, I.InstantId;
    
  4. Which vehicles have passed the points from Points?

    SELECT DISTINCT P.PointId, P.Geom, C.Licence
    FROM Trips T, Vehicles C, Points P
    WHERE T.VehId = C.VehId AND T.Trip && P.Geom AND
      ST_Intersects(trajectory(T.Trip), P.Geom) 
    ORDER BY P.PointId, C.Licence;
    
  5. What is the minimum distance between places, where a vehicle with a licence from Licences1 and a vehicle with a licence from Licences2 have been?

    SELECT L1.Licence AS Licence1, L2.Licence AS Licence2,
      MIN(ST_Distance(trajectory(T1.Trip), trajectory(T2.Trip))) AS MinDist
    FROM Trips T1, Licences1 L1, Trips T2, Licences2 L2
    WHERE T1.VehId = L1.VehId AND T2.VehId = L2.VehId AND T1.VehId < T2.VehId
    GROUP BY L1.Licence, L2.Licence 
    ORDER BY L1.Licence, L2.Licence;
    
  6. What are the pairs of trucks that have ever been as close as 10m or less to each other?

    SELECT DISTINCT C1.Licence AS Licence1, C2.Licence AS Licence2
    FROM Trips T1, Vehicles C1, Trips T2, Vehicles C2
    WHERE T1.VehId = C1.VehId AND T2.VehId = C2.VehId AND
      T1.VehId < T2.VehId AND C1.Type = 'truck' AND C2.Type = 'truck' AND
      T1.Trip && expandSpatial(T2.Trip, 10) AND
      tdwithin(T1.Trip, T2.Trip, 10.0) ?= true
    ORDER BY C1.Licence, C2.Licence;
    
  7. What are the licence plate numbers of the passenger cars that have reached the points from Points first of all passenger cars during the complete observation period?

    WITH Timestamps AS (
      SELECT DISTINCT C.Licence, P.PointId, P.Geom, 
        MIN(startTimestamp(atValue(T.Trip,P.Geom))) AS Instant
      FROM Trips T, Vehicles C, Points1 P
      WHERE T.VehId = C.VehId AND C.Type = 'passenger' AND
        T.Trip && P.Geom AND ST_Intersects(trajectory(T.Trip), P.Geom)
      GROUP BY C.Licence, P.PointId, P.Geom )
    SELECT T1.Licence, T1.PointId, T1.Geom, T1.Instant
    FROM Timestamps T1
    WHERE T1.Instant <= ALL (
      SELECT T2.Instant
      FROM Timestamps T2
      WHERE T1.PointId = T2.PointId )
    ORDER BY T1.PointId, T1.Licence;
    
  8. What are the overall travelled distances of the vehicles with licence plate numbers from Licences1 during the periods from Periods1?

    SELECT L.Licence, P.PeriodId, P.Period, SUM(length(atPeriod(T.Trip, P.Period))) AS Dist
    FROM Trips T, Licences1 L, Periods1 P
    WHERE T.VehId = L.VehId AND T.Trip && P.Period
    GROUP BY L.Licence, P.PeriodId, P.Period 
    ORDER BY L.Licence, P.PeriodId;
    
  9. What is the longest distance that was travelled by a vehicle during each of the periods from Periods?

    WITH Distances AS (
      SELECT P.PeriodId, P.Period, T.VehId, SUM(length(atPeriod(T.Trip, P.Period))) AS Dist
      FROM Trips T, Periods P
      WHERE T.Trip && P.Period
      GROUP BY P.PeriodId, P.Period, T.VehId )
    SELECT PeriodId, Period, MAX(Dist) AS MaxDist
    FROM Distances
    GROUP BY PeriodId, Period
    ORDER BY PeriodId;
    
  10. When and where did the vehicles with licence plate numbers from Licences1 meet other vehicles (distance < 3m) and what are the latter licences?

    WITH Values AS (
      SELECT DISTINCT L1.Licence AS QueryLicence, C2.Licence AS OtherLicence,
        atPeriodSet(T1.Trip, getTime(atValue(tdwithin(T1.Trip, T2.Trip, 3.0), TRUE))) AS Pos
      FROM Trips T1, Licences1 L1, Trips T2, Licences2 C2 
      WHERE T1.VehId = L1.VehId AND T2.VehId = C2.VehId AND T1.VehId < T2.VehId AND
        expandSpatial(T1.Trip, 3) && expandSpatial(T2.Trip, 3) 
        dwithin(T1.Trip, T2.Trip, 3.0) )
    SELECT QueryLicence, OtherLicence, array_agg(Pos ORDER BY startTimestamp(Pos)) AS Pos
    FROM Values 
    GROUP BY QueryLicence, OtherLicence
    ORDER BY QueryLicence, OtherLicence;
    
  11. Which vehicles passed a point from Points1 at one of the instants from Instants1?

    SELECT P.PointId, P.Geom, I.InstantId, I.Instant, C.Licence
    FROM Trips T, Vehicles C, Points1 P, Instants1 I
    WHERE T.VehId = C.VehId AND T.Trip @> STBOX(P.Geom, I.Instant) AND
      valueAtTimestamp(T.Trip, I.Instant) = P.Geom
    ORDER BY P.PointId, I.InstantId, C.Licence;
    
  12. Which vehicles met at a point from Points1 at an instant from Instants1?

    SELECT DISTINCT P.PointId, P.Geom, I.InstantId, I.Instant,
      C1.Licence AS Licence1, C2.Licence AS Licence2
    FROM Trips T1, Vehicles C1, Trips T2, Vehicles C2, Points1 P, Instants1 I
    WHERE T1.VehId = C1.VehId AND T2.VehId = C2.VehId AND T1.VehId < T2.VehId AND
      T1.Trip @> STBOX(P.Geom, I.Instant) AND T2.Trip @> STBOX(P.Geom, I.Instant) AND
      valueAtTimestamp(T1.Trip, I.Instant) = P.Geom AND
      valueAtTimestamp(T2.Trip, I.Instant) = P.Geom
    ORDER BY P.PointId, I.InstantId, C1.Licence, C2.Licence;
    
  13. Which vehicles travelled within one of the regions from Regions1 during the periods from Periods1?

    SELECT DISTINCT R.RegionId, P.PeriodId, P.Period, C.Licence
    FROM Trips T, Vehicles C, Regions1 R, Periods1 P
    WHERE T.VehId = C.VehId AND T.trip && STBOX(R.Geom, P.Period) AND
      ST_Intersects(trajectory(atPeriod(T.Trip, P.Period)), R.Geom)
    ORDER BY R.RegionId, P.PeriodId, C.Licence;
    
  14. Which vehicles travelled within one of the regions from Regions1 at one of the instants from Instants1?

    SELECT DISTINCT R.RegionId, I.InstantId, I.Instant, C.Licence
    FROM Trips T, Vehicles C, Regions1 R, Instants1 I
    WHERE T.VehId = C.VehId AND T.Trip && STBOX(R.Geom, I.Instant) AND
      ST_Contains(R.Geom, valueAtTimestamp(T.Trip, I.Instant))
    ORDER BY R.RegionId, I.InstantId, C.Licence;
    
  15. Which vehicles passed a point from Points1 during a period from Periods1?

    SELECT DISTINCT PO.PointId, PO.Geom, PR.PeriodId, PR.Period, C.Licence
    FROM Trips T, Vehicles C, Points1 PO, Periods1 PR
    WHERE T.VehId = C.VehId AND T.Trip && STBOX(PO.Geom, PR.Period) AND
      ST_Intersects(trajectory(atPeriod(T.Trip, PR.Period)),PO.Geom)
    ORDER BY PO.PointId, PR.PeriodId, C.Licence;
    
  16. List the pairs of licences for vehicles, the first from Licences1, the second from Licences2, where the corresponding vehicles are both present within a region from Regions1 during a period from QueryPeriod1, but do not meet each other there and then.

    SELECT P.PeriodId, P.Period, R.RegionId, L1.Licence AS Licence1, L2.Licence AS Licence2
    FROM Trips T1, Licences1 L1, Trips T2, Licences2 L2, Periods1 P, Regions1 R
    WHERE T1.VehId = L1.VehId AND T2.VehId = L2.VehId AND L1.Licence < L2.Licence AND
      T1.Trip && STBOX(R.Geom, P.Period) AND T2.Trip && STBOX(R.Geom, P.Period) AND
      ST_Intersects(trajectory(atPeriod(T1.Trip, P.Period)), R.Geom) AND
      ST_Intersects(trajectory(atPeriod(T2.Trip, P.Period)), R.Geom) AND
      tintersects(atPeriod(T1.Trip, P.Period), atPeriod(T2.Trip, P.Period)) %= FALSE
    ORDER BY PeriodId, RegionId, Licence1, Licence2;
    
  17. Which point(s) from Points have been visited by a maximum number of different vehicles?

    WITH PointCount AS (
      SELECT P.PointId, COUNT(DISTINCT T.VehId) AS Hits
      FROM Trips T, Points P
      WHERE ST_Intersects(trajectory(T.Trip), P.Geom)
      GROUP BY P.PointId )
    SELECT PointId, Hits
    FROM PointCount AS P
    WHERE P.Hits = ( SELECT MAX(Hits) FROM PointCount );