# postgresql+postgis+pgrouting实现最短路径查询（1）－－－线数据的处理和建立拓扑

2018/04/28 16:00

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;

0
0 收藏

0 评论
0 收藏
0