2

I want to use LINESTRING to record the path of car driving, which has both spatial information and time information.

In PostGIS, is there a way to assign timestamps to each POINT in a LINESTRING?

2 Answers 2

1

No, this is not possible because linestrings are stored in a binary format, the so-called Enhanced Well-Known Binary (EWKB) format. In that format, you cannot associate any information with the individual co-ordinates.

What you can do is to store the points along the path of the car in (a) separate table(s) and then associate those points with linestrings - presumably a road network - using the standard PostGIS functions such as ST_Distance(), ST_ClosestPoint() or any of those for linear referencing.

Your model would be something like this:

table: car
  id   serial PRIMARY KEY
  ...

table: ride
  id   serial PRIMARY KEY
  car  integer NOT NULL REFERENCES car
  ...

table: ride_path
  id   serial PRIMARY KEY
  ride integer NOT NULL REFERENCES ride
  loc  geometry(4326, 'POINT') NOT NULL
  tim  timestamp
  ...

With this structure you can use standard SQL queries to associate every location of a car to a specific linestring, or to some position along a linestring (e.g. ST_LineLocatePoint().

1

I've been storing GPS tracks by using the LINESTRINGM format, storing the timestamps as "unix time_t" values (i.e, seconds since 1970-01-01 00:00:00) in the M coordinate. It requires some conversions to get to and from a more SQL-friendly time format, but it keeps things neatly together.

1
  • In retrospect, it might be better to store the start time of the track as a separate column, and offsets from the start in the M values. This might save some headaches with time_t:s and leap seconds, for example. Jul 7, 2021 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.