ST_LineInterpolatePoint

更新时间:
复制 MD 格式

Returns a point interpolated at a given fraction along a LineString.

Syntax

geometry  ST_LineInterpolatePoint(geometry  aLinestring, float8  aFraction);
geography ST_LineInterpolatePoint(geography aLinestring, float8  aFraction);

Parameters

ParameterDescription
aLinestringThe LineString to interpolate along.
aFractionA floating-point number in the range [0, 1] that specifies the position along the line as a fraction of its total length. 0 returns the start point; 1 returns the end point.

Description

  • Preserves Z coordinates from 3D geometries.

  • Supports M coordinates.

  • When the input is geography, distances are calculated using spherical distance.

To find the point on a line closest to a given point, combine this function with ST_LineLocatePoint: use ST_LineLocatePoint to get the fraction, then pass it to ST_LineInterpolatePoint to get the coordinates.

Examples

Get the midpoint of a line

Pass 0.5 as the fraction to get the point at the halfway mark.

-- geometry: midpoint of LINESTRING(0 0, 2 2)
SELECT ST_AsText(ST_LineInterpolatePoint(geom, 0.5))
FROM (SELECT 'LINESTRING(0 0,2 2)'::geometry AS geom) AS t;
 st_astext
------------
 POINT(1 1)
(1 row)
-- geography: midpoint computed using spherical distance
SELECT ST_AsText(ST_LineInterpolatePoint(geog, 0.5))
FROM (SELECT 'LINESTRING(0 0,2 2)'::geography AS geog) AS t;
------------------------------------------
 POINT(0.99969732796684 1.00015638159834)
The geography result differs from the geometry result because distances are measured along the spherical surface, not on a flat plane.

Get the closest point on a line to a given point

Use ST_LineLocatePoint to find the fraction, then pass it to ST_LineInterpolatePoint to retrieve the coordinates.

-- geometry: closest point on LINESTRING(0 0, 0 2) to POINT(1 1)
SELECT ST_AsText(
    ST_LineInterpolatePoint(geom, ST_LineLocatePoint(geom, 'POINT(1 1)'::geometry))
)
FROM (SELECT 'LINESTRING(0 0,0 2)'::geometry AS geom) AS t;
 st_astext
------------
 POINT(0 1)
(1 row)
-- geography: closest point using spherical distance
SELECT ST_AsText(
    ST_LineInterpolatePoint(geog, ST_LineLocatePoint(geog, 'POINT(1 1)'::geography))
)
FROM (SELECT 'LINESTRING(0 0,0 2)'::geography AS geog) AS t;
---------------------------
 POINT(0 1.00015229710421)