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, v.Model AS Model
    FROM Vehicles v, Licences l
    WHERE v.Licence = l.Licence;
    
  2. How many vehicles exist that are passenger cars?

    SELECT COUNT (DISTINCT Licence)
    FROM Vehicles v
    WHERE VehicleType = '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.VehicleId = l.VehicleId 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, v.Licence
    FROM Trips t, Vehicles v, Points p
    WHERE t.VehicleId = v.VehicleId AND t.Trip && stbox(p.Geom) AND
      ST_Intersects(trajectory(t.Trip), p.Geom) 
    ORDER BY p.PointId, v.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.VehicleId = l1.VehicleId AND t2.VehicleId = l2.VehicleId AND
      t1.VehicleId < t2.VehicleId
    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 v1.Licence AS Licence1, v2.Licence AS Licence2
    FROM Trips t1, Vehicles v1, Trips t2, Vehicles v2
    WHERE t1.VehicleId = v1.VehicleId AND t2.VehicleId = v2.VehicleId AND
      t1.VehicleId < t2.VehicleId AND v1.VehicleType = 'truck' AND 
      v2.VehicleType = 'truck' AND t1.Trip && expandSpace(t2.Trip, 10) AND
      eDwithin(t1.Trip, t2.Trip, 10.0)
    ORDER BY v1.Licence, v2.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 v.Licence, p.PointId, p.Geom, 
        MIN(startTimestamp(atValues(t.Trip,p.Geom))) AS Instant
      FROM Trips t, Vehicles v, Points1 p
      WHERE t.VehicleId = v.VehicleId AND v.VehicleType = 'passenger' AND
        t.Trip && stbox(p.Geom) AND ST_Intersects(trajectory(t.Trip), p.Geom)
      GROUP BY v.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(atTime(t.Trip, p.Period))) AS Dist
    FROM Trips t, Licences1 l, Periods1 p
    WHERE t.VehicleId = l.VehicleId 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.VehicleId,
        SUM(length(atTime(t.Trip, p.Period))) AS Dist
      FROM Trips t, Periods p
      WHERE t.Trip && p.Period
      GROUP BY p.PeriodId, p.Period, t.VehicleId )
    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, l2.Licence AS OtherLicence,
        atTime(t1.Trip, getTime(atValues(tdwithin(t1.Trip, t2.Trip, 3.0), TRUE))) AS Pos
      FROM Trips t1, Licences1 l1, Trips t2, Licences2 l2 
      WHERE t1.VehicleId = l1.VehicleId AND t2.VehicleId = l2.VehicleId AND
        t1.VehicleId < t2.VehicleId AND 
        expandSpace(t1.Trip, 3) && expandSpace(t2.Trip, 3) AND
        eDwithin(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, v.Licence
    FROM Trips t, Vehicles v, Points1 p, Instants1 i
    WHERE t.VehicleId = v.VehicleId AND t.Trip @> stbox(p.Geom, i.Instant) AND
      valueAtTimestamp(t.Trip, i.Instant) = p.Geom
    ORDER BY p.PointId, i.InstantId, v.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,
      v1.Licence AS Licence1, v2.Licence AS Licence2
    FROM Trips t1, Vehicles v1, Trips t2, Vehicles v2, Points1 p, Instants1 i
    WHERE t1.VehicleId = v1.VehicleId AND t2.VehicleId = v2.VehicleId AND
      t1.VehicleId < t2.VehicleId 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, v1.Licence, v2.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, v.Licence
    FROM Trips t, Vehicles v, Regions1 r, Periods1 p
    WHERE t.VehicleId = v.VehicleId AND t.trip && stbox(r.Geom, p.Period) AND
      ST_Intersects(trajectory(atTime(t.Trip, p.Period)), r.Geom)
    ORDER BY r.RegionId, p.PeriodId, v.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, v.Licence
    FROM Trips t, Vehicles v, Regions1 r, Instants1 i
    WHERE t.VehicleId = v.VehicleId AND t.Trip && stbox(r.Geom, i.Instant) AND
      ST_Contains(r.Geom, valueAtTimestamp(t.Trip, i.Instant))
    ORDER BY r.RegionId, i.InstantId, v.Licence;
    
  15. Which vehicles passed a point from Points1 during a period from Periods1?

    SELECT DISTINCT pt.PointId, pt.Geom, pr.PeriodId, pr.Period, v.Licence
    FROM Trips t, Vehicles v, Points1 pt, Periods1 pr
    WHERE t.VehicleId = v.VehicleId AND t.Trip && stbox(pt.Geom, pr.Period) AND
      ST_Intersects(trajectory(atTime(t.Trip, pr.Period)), pt.Geom)
    ORDER BY pt.PointId, pr.PeriodId, v.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.VehicleId = l1.VehicleId AND t2.VehicleId = l2.VehicleId 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(atTime(t1.Trip, p.Period)), r.Geom) AND
      ST_Intersects(trajectory(atTime(t2.Trip, p.Period)), r.Geom) AND
      aDisjoint(atTime(t1.Trip, p.Period), atTime(t2.Trip, p.Period))
    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.VehicleId) 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 );