本文是在上一篇文章的基础上的。数据入库和字段设置请参考上篇文章。本文介绍利用pgrouting中其他方法来求取最短路径。
一、添加属性字段
为表格beijingmodified添加x1,y1,x2,y2字段
ALTER TABLE beijingmodified ADD COLUMN x1double precision;
ALTER TABLE beijingmodified ADD COLUMN y1double precision;
ALTER TABLE beijingmodified ADD COLUMN x2double precision;
ALTER TABLE beijingmodified ADD COLUMN y2double precision;
二、为新添加的属性字段赋值
UPDATE beijingmodified SET x1 =ST_x(ST_PointN(geom, 1));
UPDATE beijingmodified SET y1 =ST_y(ST_PointN(geom, 1));
UPDATE beijingmodified SET x2 =ST_x(ST_PointN(geom, ST_NumPoints(geom)));
UPDATE beijingmodified SET y2 =ST_y(ST_PointN(geom, ST_NumPoints(geom)));
三、查询
1.A*算法路径查询
SELECT seq, id1 AS node, id2 AS edge, costFROM pgr_astar(‘
SELECT gid AS id,
source::integer,
target::integer,
length::double precision AS cost,
x1, y1, x2, y2
FROM beijingmodified‘,30, 60, false,false);
2.pgr_kdijkstracost函数查询从出发点到目的地的消耗
SELECT seq, id1 AS source, id2 AS target,cost FROM pgr_kdijkstraCost(‘
SELECT gid AS id,
source::integer,
target::integer,
length::double precision AS cost
FROM beijingmodified‘,
30, array[60,70,100], false, false);
3. pgr_kdijkstraPath函数查询具体路径
SELECT seq, id1 AS path, id2 AS edge, costFROM pgr_kdijkstraPath(‘
SELECT gid AS id,
source::integer,
target::integer,
length::double precision AS cost
FROM beijingmodified‘,
30, array[60,100], false, false);