Skip to content
Advertisement

PostGIS: Transform Linestring to LineString ZM

Problem:

I have a series of geometries (LineString) saved in a Postgres database with additional information saved next to the geometry (a column for speed and time). I want to combine the three columns into a LineString ZM type so the geometry goes from a 2d to a 4d vector.

id geometry speed time
1 LineString_A Int[] Float[]
2 LineString_B Int[] Float[]

Question:

How do I most easily combine the geometry (x, y), speed, and time into a LineString ZM?

Advertisement

Answer

You’re looking for ST_Force4D.

All you need to do is to ALTER TABLE using the values you already have in the columns speed and time, e.g.

Demo (PostGIS 3.1)

Different scenario (see comments): speed and time are arrays that overlap 1:1 with the points in the LineString.

Since you cannot unnest an array in a ALTER TABLE statement, you could just change the data type from LineString to LineStringZM and set the extra dimensions temporarily to 0:

The following CTE dumps all points from your LineStrings, create new ones with Z and M dimensions, and finally creates the LineStringZM for the UPDATE query.

Further reading:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement