標籤:
準備一個線shp資料,並將其匯入postgres裡面,postgres安裝postgis和pgrouting兩個外掛程式(方法見http://www.cnblogs.com/nidaye/p/4553522.html)。線資料的欄位如下:注意欄位的名稱,省的出現不必要的麻煩。
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 beijing_line‘,
30, 60, false, false) as di
join beijing_line pt
on di.id2 = pt.gid;
經曆過以上的步驟,如果一切順利,應該能看到結果,這裡不再。這時初步的準備工作已經完成。
參考博文如下():
1、基於pgrouting的路徑規劃之一
2、pgrouting進行路徑規劃之入門二(1和2是一個求最短路徑博文系列,挺不錯的可以看看,但可能是因為機器配置環境的不同,原封不動的可能無法運行別人的代碼,一定搞懂原理,然後可對前人的代碼改進)
3、Pgrouting運用基礎
4、使用pgrouting2.0求最短路徑
postgresql+postgis+pgrouting實現最短路徑查詢(1)---線資料的處理和建立拓撲