ST_Locate_Along_Measure() and ST_TrajectoryInterpolatePoint()

20 views
Skip to first unread message

Antonio Valanzano

unread,
Sep 27, 2025, 10:05:09 AM (yesterday) Sep 27
to SpatiaLite Users
Dear list members,
I am trying to understand the different behaviour of the above functions.

I have a line with 2 vertices and a measure value at the start_node and a measure value at the end_node.

If I try to find the point corresponding to a specified value of the measure the two function return partially different results.

The first one "ST_Locate_Along_Measure()" retruns NULL for an intermediate value of the measure, if this value occurs in a position where there is no vertex, while the second one "ST_TrajectoryInterpolatePoint()" never returns NULL values and interpolates between the values of m.

The points returned for the extreme values of m are the same for both the functions.

Here is a simple test

WITH lineam AS
(
  SELECT
    ST_AddMeasure(
      ST_GeomFromText('LINESTRING(0 0, 2 2)'),
      20,   -- start_measure
      80  -- end_measure
    ) as geom
  )
, punti AS
(
  SELECT
    ST_Locate_Along_Measure( geom , 50.0) as pm_i,
    ST_Locate_Along_Measure( geom , 20.0) as pm_e_f,
    ST_Locate_Along_Measure( geom , 80.0) as pm_e_t,
    ST_TrajectoryInterpolatePoint( geom , 50.0) as pi_i,
    ST_TrajectoryInterpolatePoint( geom , 20.0) as pi_e_f,
    ST_TrajectoryInterpolatePoint( geom , 80.0) as pi_e_t
  FROM lineam
)
SELECT
  ST_AsText(pm_i) as pm_i,
  ST_AsText(pm_e_f) as pm_e_f,
  ST_AsText(pm_e_t) as pm_e_t,
  ST_AsText(pi_i) as pi_i,
  ST_AsText(pi_e_f) as pi_e_f,
 ST_AsText(pi_e_t) as pi_e_t
FROM punti;
-- 1 row
NULL POINT M(0 0 20) POINT M(2 2 80) POINT M(1 1 50) POINT M(0 0 20) POINT M(2 2 80)

If I add a new vertex (1, 1) to the line then both functions return the same point for the m value=50.


For me is not clear when to use one function or the other.

Obviosly the interpolation is a linear interpolation and in some cases this is not appropriate depending on what m represents, but I do not see a case for using the first function which is limited to values of m corresponding to only vertices.

Probably I have not completely understood the doc.

Any suggestion will help.

Antonio




sandro furieri

unread,
Sep 27, 2025, 11:25:53 AM (yesterday) Sep 27
to spatiali...@googlegroups.com, Antonio Valanzano
Il 2025-09-27 16:05 Antonio Valanzano ha scritto:
> If I try to find the point corresponding to a specified value of the
> measure the two function return partially different results.
>
> The first one "ST_Locate_Along_Measure()" retruns NULL for an
> intermediate value of the measure, if this value occurs in a position
> where there is no vertex, while the second one
> "ST_TrajectoryInterpolatePoint()" never returns NULL values and
> interpolates between the values of m.
>

Hi Antonio,

the documentation clearly explainds all; please see:
https://www.gaia-gis.it/gaia-sins/spatialite-sql-5.1.0.html#p14-

1. ST_Locate_Along_Measure()
"Return a derived geometry collection value with elements that
match the specified measure. NULL will be returned if any error
is encountered (or when no element corresponding to the given
measure is found)."

this is exectely what happens in your example; your test
LINESTRING has no Point with M=5, so a NULL will be returned.
going in further depth:

SELECT ST_AsText(ST_Locate_Along_Measure(
ST_GeomFromText('LINESTRING M(0 0 1, 1 1 2, 2 2 1, 3 3 2)'),
1));
----------
MULTIPOINT M(0 0 1, 2 2 1)

SELECT ST_AsText(ST_Locate_Along_Measure(
ST_GeomFromText('LINESTRING M(0 0 1, 1 1 2, 2 2 1, 3 3 2)'),
3));
----------
NULL


2. ST_TrajectoryInterpolatePoint()
"Check if a Geometry corresponds to a valid Trajectory.
a Trajectory is assumed to be a LINESTRING supporting
M-values growing from each vertex to the next.
Return a POINT Geometry being interpolated along the
Geometry (that is expected to be a valid Trajectory)
accordingly to the given M-value."

just few practical examples:

SELECT ST_AsText(ST_TrajectoryInterpolatePoint(
ST_GeomFromText('LINESTRING M(0 0 1, 1 1 2, 2 2 1, 3 3 2)'),
2));
----------------------
NULL

this will fail returning a NULL because the Linestring
is not a valid Trajectory; M-Values are not regularly
growing as required.


SELECT ST_AsText(ST_TrajectoryInterpolatePoint(
ST_GeomFromText('LINESTRING M(0 0 1, 1 1 2, 2 2 3, 3 3 5)'),
4));
------------------------
POINT M(2.5 2.5 4)

in this second attempt a Point has been interpolated
that falls halfway between the values ​​M=3 and M=5

----------------------------------------

Short conclusion: the two functions are completely
different and shoukd not be confused with each other.

ST_Locate_Along_Measure() is kind of a filter intended
to extract all Points with the same M-Value

ST_TrajectoryInterpolatePoint() instead only works if
the M-Values are regularly increasing and in this
case will interpolate a new Point.

Just for your curiosity, this funcion was introduced
to support the analysis of GPS tracks, where eech
Waypoint has its own timestamp that is usually stored
ad the M-Value.
GPS loggers register the sequence of Waypoints at
fixex time intervals (e.g. every 1, 5 or 10 seconds),
but under some circumstances extrapolating intemediate
unrecorded Waypoints is really useful.

bye Sandro

Antonio Valanzano

unread,
Sep 27, 2025, 12:18:23 PM (yesterday) Sep 27
to SpatiaLite Users
Sandro,
thanks as usual for your detailed answer.

From your answer:
".. 
ST_Locate_Along_Measure()
"Return a derived geometry collection value with elements that
match the specified measure. NULL will be returned if any error
is encountered (or when no element corresponding to the given
measure is found)."
...

ST_Locate_Along_Measure() is kind of a filter intended
to extract all Points with the same M-Value ..
"

Now is clear the type of output (All Points--> geometry collection) and the scope of the function.

I was confused by the fact that I started from a line and the function doesn't necessarily require as input a linestring.

Thanks again.

Antonio




Reply all
Reply to author
Forward
0 new messages