ERROR: relation with OID 51307 does not exist SQL state: 42P01 Context: SQL statement "SELECT id, the_geom FROM vertices_tmp WHERE distance(the_geom, $1 ) < $2 " PL/pgSQL function "point_to_id" line 7 at select into variables PL/pgSQL function "assign_vertex_id" line 50 at assignment
网上别人的办法
Ok found out that the problem was due to that the function gets compiled only once per connection, so I had to reconnect
而我的情况是,我直接在pgadmin里执行assign_vertex_id函数,会报上面的错,而从上面方法启示,在psql的命令行方式下执行sql语句,成功了,呵呵,相信如果我直接用php连pgrouting就不会出现这么多麻烦了
下面是pgrouting完整的解决方案:
这里用的版本是postgresql8.2 ,pgrouting_1.0.0_win32
1.postgresql和postgis安装略过
2.安装pgrouting,将pgrouting下doc lib share三个文件夹靠到postgresql文件夹下
3.执行routing_core.sql,routing_core_wrappers.sql,安装pgrouting,这里安装了dijkstra算法包,另外还有driving distance,travling sales man两个算法包,可以选择安装(routing_dd.sql,routing_dd_wrappers.sql,routing_tsp.sql,routing_tsp_wrappers.sql)
4.导入示例数据 psql -d pgrouting -U postgres -f d:\data\victoria.sql 示例数据在官网可下载
5.为victoria表添加the_geom字段
ALTER TABLE victoria RENAME COLUMN the_geom TO geom;
SELECT AddGeometryColumn('victoria','the_geom',54004,'MULTILINESTRING',2);
UPDATE victoria SET the_geom=geom;
ALTER TABLE victoria DROP COLUMN geom;
6.为victoria赋 source,target值,执行
SELECT assign_vertex_id('victoria', 0.001, 'the_geom', 'gid');
0.001为阈值范围,可视具体情形设定,计算好后,会生成一张vertex_tmp的临时表(psql执行sql命令哦)
7.一切准备好后,就可以计算最短路径了,以dijkstra算法为例,这里用长度当作权重,求起点248到终点1455的最短路径
SELECT * FROM shortest_path('
SELECT gid as id,
source::integer,
target::integer,
length::double precision as cost
FROM victoria',
248, 1455, true, false);
#drop table tmp;
select * into tmp from vertices_tmp as vt where vt.id in (SELECT vertex_id FROM shortest_path('SELECT gid as id,source::integer,target::integer,length::double precision as cost FROM road',248, 400, true, false));
或者
SELECT sp.vertex_id,vt.the_geom into tmp FROM shortest_path('SELECT gid as id,source::integer,target::integer,length::double precision as cost FROM road',123, 334, true, false) as sp,vertices_tmp as vt where sp.vertex_id=vt.id
ALTER TABLE tmp ADD CONSTRAINT tmp_pk PRIMARY KEY (id);
这样可以直接在qgis里打开tmp表来看最短路径的结果了