ST_LineFromMultiPoint
What is ST_LineFromMultiPoint?
ST_LineFromMultiPoint is a PostGIS function that constructs a LINESTRING from a MULTIPOINT by connecting the points in the order in which they appear. It is a convenience constructor for turning an ordered collection of coordinates — often produced by an aggregation or an ETL pipeline — into a connected linear geometry.
ST_LineFromMultiPoint(geometry multipoint) → geometryInput must be a MULTIPOINT; any other geometry type raises an error. The resulting LINESTRING inherits the SRID of the input.
When would you use ST_LineFromMultiPoint?
Use ST_LineFromMultiPoint when you have already aggregated an ordered set of points into a MULTIPOINT (for example via ST_Collect over a sorted subquery) and need to convert that container into a true line. Common cases include reconstructing GPS tracks from logged waypoints, building polyline representations of ordered survey points, or constructing centerline approximations from ordered midpoints.
1SELECT ST_LineFromMultiPoint(
2 ST_Collect(pt_geom ORDER BY recorded_at)
3) AS track
4FROM gps_points
5WHERE vehicle_id = 42;For truly ordered construction, prefer ST_MakeLine with an ORDER BY clause — it avoids the MULTIPOINT round-trip and is more idiomatic.
FAQs
How is the order of points determined?
The order is the internal vertex order of the MULTIPOINT. If you constructed it with ST_Collect(... ORDER BY ...), that order is preserved; otherwise it follows row order, which is not guaranteed without an explicit ORDER BY.
When should I use ST_MakeLine instead?
ST_MakeLine is the more direct constructor: it takes an array, aggregate, or two-point form and produces a LINESTRING without the intermediate MULTIPOINT. Reserve ST_LineFromMultiPoint for cases where you already have a MULTIPOINT value.
What happens if the MultiPoint contains fewer than two points?
A LINESTRING requires at least two vertices. Passing a single-point or empty MULTIPOINT raises an error. Guard with ST_NumGeometries(mp) >= 2 in the WHERE clause.
Does ST_LineFromMultiPoint remove duplicate vertices?
No. Consecutive identical points are preserved, which may produce zero-length segments. Use ST_RemoveRepeatedPoints afterwards if your downstream logic (like length calculation or routing) depends on distinct vertices.