基于pgrouting的路径规划之一

前端之家收集整理的这篇文章主要介绍了基于pgrouting的路径规划之一前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近接触pgrouting。把学习的结果记录下来。@H_404_1@

利用pgrouting进行路径规划只能导入line数据,mutiline会出错。这一点在进行数据导入的时候需要注意。@H_404_1@

一、创建数据库

有两种方法:@H_404_1@

1.pgadmin可视化工具创建

直接把数据道路postgis模版数据库或以postgis模版数据库为模版创建数据库,这样创建的数据库直接支持空间查询和空间分析。@H_404_1@

2.命令行创建数据库

创建数据库@H_404_1@

createdb -U postgres routing@H_404_1@

数据库支持PostGIS和pgRouting的函数和基础表@H_404_1@

psql –U postgres –d routing –f postgis.sql@H_404_1@

psql –U postgres –d routing –fspatial_ref_sys.sql@H_404_1@

psql –U postgres –d routing –frouting_core.sql@H_404_1@

psql –U postgres –d routing –frouting_core_wrappers.sql@H_404_1@

psql –U postgres –d routing –frouting_topology.sql@H_404_1@

在windows环境下可以粘贴相应的sql文件代码在对应表中直接执行@H_404_1@

二、把shp数据导入空间数据库

两种方式:@H_404_1@

1.是用可视化工具

(a)打开postgis工具@H_404_1@

postgis安装目录下的PostGISShapefile Import/Export Manager@H_404_1@


@H_404_1@

会弹出对话框@H_404_1@


@H_404_1@

(b)设置数据库连接@H_404_1@

单击view connection details,设置数据库的连接@H_404_1@


@H_404_1@


@H_404_1@

(c)添加shp数据@H_404_1@

添加需要导入的shp文件@H_404_1@

单击Add File ,会弹出文件选中对话框@H_404_1@


@H_404_1@

选择需要导入是shp文件,注意此处路径一定要是英文,否则会导入失败@H_404_1@

(d)编码设置@H_404_1@

单击options,设备编码格式为GBK,选中generate simple geometries instead of multi geometries。@H_404_1@

此处导入的shp数据一定要是单线的,否则无法完成路径计算。@H_404_1@

(e)查看导入数据@H_404_1@

成功导入数据之后就能在postgresql中看到导入的数据表@H_404_1@

2.命令行导入数据

先用shp2psql.exe程序把shp文件装换成sql脚本,@H_404_1@

格式为:shp2pgsql 路径\shp数据文件名 新建的数据表名 > 路径\sql文件名.sql@H_404_1@

shp2pgsql -s 4326 beijingmodified public.beijingmodified > beijingmodified.sql@H_404_1@

执行得到的sql文件@H_404_1@

psql -U postgres -d routing -f beijingmodified.sql@H_404_1@

三、创建路网拓扑结构

//添加起点id@H_404_1@

ALTER TABLE public.beijing ADD COLUMN source integer;@H_404_1@

//添加终点id@H_404_1@

ALTER TABLE public.beijing ADD COLUMN target integer;@H_404_1@

//添加道路权重值@H_404_1@

ALTER TABLE public.beijing ADD COLUMN length double precision;@H_404_1@

//为sampledata表创建拓扑布局,即为source和target字段赋值@H_404_1@

SELECT pgr_createTopology('public.beijing',0.00001,'geom','gid');@H_404_1@

//为source和target字段创建索引@H_404_1@

CREATE INDEX source_idx ON beijingmodified("source");@H_404_1@

CREATE INDEX target_idx ON beijingmodified("target");@H_404_1@

//为length赋值@H_404_1@

update beijingmodified set length =st_length(geom);@H_404_1@

//或者用已有的字段长度赋值,下面shape_length为shp中已有的长度属性@H_404_1@

UPDATE beijingmodified SET length = shape_length;@H_404_1@

//为beijingmodified表添加reverse_cost字段并用length的值赋值@H_404_1@

ALTER TABLE beijingmodified ADD COLUMN reverse_cost double precision;@H_404_1@

UPDATE beijingmodified SET reverse_cost =length;@H_404_1@

四、尝试查询

使用pgr_dijkstra算法查询@H_404_1@

SELECT seq,id1 AS node,id2 AS edge,costFROM pgr_dijkstra('@H_404_1@

SELECT gid AS id,@H_404_1@

source::integer,@H_404_1@

target::integer,@H_404_1@

length::double precision AS cost@H_404_1@

FROM beijingmodified',@H_404_1@

30,60,false,false);@H_404_1@

//查询每个路段经过的点@H_404_1@

SELECT st_astext(geom) FROM pgr_dijkstra('@H_404_1@

SELECT gid AS id,false) as di@H_404_1@

join beijingmodified pt@H_404_1@

on di.id2 = pt.gid;@H_404_1@


@H_404_1@

//把查询结果放在新建的表格中@H_404_1@

SELECT seq,cost,geom into dijkstra_res FROM pgr_dijkstra('@H_404_1@

SELECT gid AS id,false) as di@H_404_1@

join beijingmodified pt@H_404_1@

on di.id2 = pt.gid;@H_404_1@

/*@H_404_1@

pgr_dijkstra的定义是pgr_costResult[]
pgr_dijkstra(textsql,integer source,integer target,boolean directed,boolean has_rcost);@H_404_1@

directed是否限制方向,has_ rcost作用未知,返回值为pgr_costResult。@H_404_1@

pgr_costResult在workshop中的解释是@H_404_1@

A set of records to describe a path resultwith cost attribute@H_404_1@

一组带有消耗属性的用于描述路径结果的记录的集合。@H_404_1@

其定义如下@H_404_1@

CREATE TYPE pgr_costResult AS@H_404_1@

(@H_404_1@

seq integer,-- rowsequence@H_404_1@

id1 integer,-- node ID@H_404_1@

id2 integer,-- edge ID(-1 for the last row)@H_404_1@

cost float8 -- cost totraverse from id1 using id2@H_404_1@

);@H_404_1@

*/@H_404_1@

将这个表导出为shp,再在arcmap中定义坐标系打开,可以看到上面的结果如下图所示@H_404_1@

猜你在找的Postgre SQL相关文章