1. 程式人生 > >postgresql+postgis+pgrouting實現最短路徑查詢(1)---線數據的處理和建立拓撲

postgresql+postgis+pgrouting實現最短路徑查詢(1)---線數據的處理和建立拓撲

分享圖片 date table 函數 top pda sql pos ima

1、ALTER TABLE beijing_line ADD COLUMN source integer;

ALTER TABLE beijing_line ADD COLUMN target integer;

ALTER TABLE beijing_line ADD COLUMN length double precision;

UPDATE beijing_line SET length = ST_Length(the_geom);

技術分享圖片

ps:執行createTopology這個函數之前一定得對數據庫執行以下三句sql查詢:

CREATE EXTENSION postgis;

CREATE EXTENSION postgis_topology;

CREATE EXTENSION fuzzystrmatch;

2、select pgr_createTopology(‘beijing_line‘,0.001,source:=‘source‘,id:=‘gid‘,target:=‘target‘,the_geom:=‘the_geom‘);

select pgr_createTopology(‘beijing_line‘,0.1,source:=‘source‘,id:=‘gid‘,target:=‘target‘,the_geom:=‘the_geom’);

把容差值設置的大一點,可能結果會好點

3、 SELECT * FROM pgr_dijkstra(‘

SELECT gid as id,

source::integer,

target::integer,

length::double precision as cost

FROM beijing_line‘,

30, 60, false, false);

無查詢結果就執行下面這幾個試一試,也可能是兩點之間就沒有路徑

select source from beijing_line;

select target from beijing_line;

select length from beijing_line;

4、查詢所經過的所有點:

SELECT st_astext(the_geom) FROM pgr_dijkstra(‘

SELECT gid AS id,

source::integer,

target::integer,

length::double precision AS cost

FROM beijing_line‘,

30, 60, false, false) as di

join beijing_line pt

on di.id2 = pt.gid;

5、將路徑寫入一個幾何文件內:

SELECT seq, id1 AS node, id2 AS edge, cost,the_geom into dijkstra_res FROM pgr_dijkstra(‘

SELECT gid AS id,

source::integer,

target::integer,

length::double precision AS cost

FROM beij hbs90.cn ing_line‘,

30, 60, false, false) as di

join beijing_line pt

on di.id2 = pt. senta77.com gid;

經歷過以上的步驟,如果一切順利,應該能看到結果,這裏不再截圖。這時初步的準備工作已經完成。

參考博文如下():

postgresql+postgis+pgrouting實現最短路徑查詢(1)---線數據的處理和建立拓撲