我正在尝试找到一种从线串上最近点到我当前位置(lat,long)的路径。到目前为止,我能够获得最短的路径,但它从线串(也称为源)的最开始。我正在使用prg_trsp http://docs.pgrouting.org/2.0/en/src/trsp/doc/index.html 因为它有一个功能来指定沿线串的起始位置。我能够正确计算沿线串的距离并将值传递给函数,但无法弄清楚如何使用函数的结果(pgr_costResult [])来指定路径应该从哪里开始(部分沿着最近的线串)。
当我将路由算法的结果连接到边缘表以获取几何体时,我感觉我在连接时出错了,因为当我加入它时使用边缘表的完整几何体和不是段。虽然,查看文档,但我不知道从路由功能中获取返回段的位置。
以下是我要做的事情(红线)和我所拥有的(蓝线)截图,该点是当前位置。红线来自于使用qgis中的pgrouting插件和trsp(边缘)选择。
见下面的代码:
非常感谢任何帮助!
SELECT st_linemerge(edgeTable.geom_way) FROM pgr_trsp('SELECT id, source, target, cost FROM edgeTable',
(SELECT id FROM origin),
(SELECT * FROM sourcePos),
(SELECT id FROM destination),
(SELECT * FROM destPos),
false, false) AS shortestPath
JOIN edgeTable ON shortestPath.id2 = edgeTable.id;
origin是起始路线的ID
sourcePos是沿线串偏移
的距离destination是结束线串的id
destPos是结束线串的分数
此处指定的全部内容:http://docs.pgrouting.org/2.0/en/src/trsp/doc/index.html
答案 0 :(得分:0)
这是因为pgr_trsp()函数没有给出您所例外的输出。 QGIS中的Pg_routing插件对从pgr_trsp()生成的路由进行捕捉。因此,您将红线的输出贴近了您的点。因此,仅pgr_trsp()不会为您提供所需的输出。您正在尝试做的事情有些复杂,但可能。这是我解决此问题的方法
WITH
--Make a start point
start_pt as (
select st_setsrid(st_makepoint(204845.95, 2410097.47), 32643) as starting),
--Make a End Point
end_pt as (
select st_setsrid(st_makepoint(204937.15, 2409430.86), 32643) as ending),
--Select Closest source node and its geom for start point
source_code AS (
select source, geom from edgeTable order by st_distance(geom, (select starting from start_pt)) limit 1),
--Select closest target node and its geom for end point
target_code AS (
select target, geom from edgeTable order by st_distance(geom, (select ending from end_pt)) limit 1),
--Route Union from pgr_trsp()
route as (
SELECT ST_LineMerge(ST_union(geom)) as geom, round( CAST(float8 (st_length(ST_union(geom))/1000) as numeric), 2) as length from (
SELECT geom FROM pgr_trsp(
'SELECT feat_id as id, source, target, cost_len as cost, geom FROM edgeTable',
(select source from source_code), (select target from target_code), false, false
) as di JOIN edgeTable
ON di.id2 = edgeTable.id) as foo)
--Finaly snap the route to precisely matach our start and end point
select ST_Line_Substring(geom,
ST_LineLocatePoint(geom, (select starting from start_pt)),
ST_LineLocatePoint(geom, (select ending from end_pt)))
from route
我唯一的问题是我必须在最后一个选择语句中切换起点和终点。这可以通过编写函数来处理。这是我的输出
希望此帮助...