postgresql+postgis+pgrouting实现最短路径查询---线数据的处理和建立拓扑
Posted eryaketang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postgresql+postgis+pgrouting实现最短路径查询---线数据的处理和建立拓扑相关的知识,希望对你有一定的参考价值。
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实现最短路径查询---线数据的处理和建立拓扑的主要内容,如果未能解决你的问题,请参考以下文章
整理一下postgresql的扩展功能postgis和pgrouting的使用
postgresql+postgis+pgrouting实现最短路径查询---线数据的处理和建立拓扑