I have a linestring:
LINESTRING(-1.3326397 50.9174932,-1.3319842 50.9166939)
and I select two points that lay on that linestring.
How do I get the distance between these two points (in meters)?
distancelinestringpostgis
I have a linestring:
LINESTRING(-1.3326397 50.9174932,-1.3319842 50.9166939)
and I select two points that lay on that linestring.
How do I get the distance between these two points (in meters)?
Best Answer
There are several linear referencing functions that can be used to reference subsets of LineStrings, which can be converted to a
geography
and get the length of the geodesic with ST_Length.For example, get the distance along
line
that are near pointspta
andptb
:Returning a distance of 83.315 m (blue line; red line is original).
Note in older versions of PostGIS, the functions were called
ST_Line_Locate_Point
andST_Line_Substring
, so you many need to adjust the example to work.In the event of "ERROR: 2nd arg must be smaller then 3rd arg", the query can be made more robust to find the lowest and highest values for A and B with the
least
andgreatest
SQL functions, e.g.:which will now work if
pta
andptb
are swapped.