一条线上两点之间的postgis距离

时间:2014-04-30 10:26:14

标签: line distance postgis point

我有一条路线(作为LINESTRING)和两条有位置的车辆(作为一个点)。我需要计算两个点 over 路线之间的距离。

作为一个额外的困难,我认为我还需要测量当前车辆不在路线上时从点到线上最近点的距离。

我用这个来找到路线上最近的点:

SELECT ST_AsText(ST_ClosestPoint(pt,line)) AS cp_pt_line, 
    ST_AsText(ST_ClosestPoint(line,pt)) As cp_line_pt
FROM (SELECT 'POINT(100 100)'::geometry As pt, 
    'LINESTRING (20 80, 98 190, 110 180, 50 75 )'::geometry As line
) As foo;

在查询中可以吗?

1 个答案:

答案 0 :(得分:4)

SELECT     ST_Length(ST_GeographyFromText(ST_AsText(ST_Line_Substring(line,ST_Line_Locate_Point(line,ST_ClosestPoint(line,fpt)),ST_Line_Locate_Point(line,ST_ClosestPoint(line,tpt)))))) As length_m,
                                                                   ST_Distance(ST_ClosestPoint(line,tpt)::geography, tpt::geography) as to_point_to_line_m,
                                                                   ST_Distance(ST_ClosestPoint(line,fpt)::geography, fpt::geography) as from_point_to_line_m,
                                                                   ST_AsText(ST_ClosestPoint(line,tpt)) as to_point_on_line,
                                                                   ST_AsText(ST_ClosestPoint(line,fpt)) as from_point_on_line,
                                                                   ST_AsText(tpt) as to_point,
                                                                   ST_AsText(fpt) as from_point
                                           FROM ( SELECT 'SRID=4326;POINT(1)'::geometry As tpt,
                                                                                          'SRID=4326;POINT(2)'::geometry As fpt,
                                                                                          ST_Segmentize('SRID=4326;LINESTRING(123)'::geometry,  0.00001) As line
                                                       ) As foo;

距离length_m,距离to_point_on_line和from_point_on_line。 :)