Find Y value of an interpolated point in a Linestr

2019-08-06 12:00发布

问题:

I would like to know what's the best way to find the Y value of an interpolated point inside the Linestring, when given the X.

The X coordinates of my input Linestring will always be incremental and non sequential (as in the example below). The Y values could be any real number.

LINESTRING(223 -59,228 -59.3,233 -59.7,242 -60,263 -60.4,
           268 -61.7,275 -62.1,280 -62.5)

Given an X value (let's say 270 for example), the query would output the interpolated value inside the Linestring (in this case it would be -61.81428571, using points [268 -61.7] and [275 -62.1] for the interpolation)

If the X value already belongs to the Linestring, the it would just output its corresponding Y value. 268: -61.7

The input table will just have a single column with a single row with the linestring. The X value would be part of the query.

I'm using PostGIS.

回答1:

Probably the easiest way will be to generate a vertical line and check intersection between it and the geom. It will be good to make sure the line is as long as the bounding box of the linestring.

SELECT ST_INTERSECTION(geom, ST_MakeLine(ST_MakePoint(268, ST_YMin(geom)), ST_MakePoint(268,ST_YMax(geom)))) 
FROM
    linestrings;