惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

博客园 - StinJia

浙江省地理信息中心招GIS研发工程师 6 bytes判定IE浏览器 重回单身生活 really 切尔西英超亚军 用javascript实现较为通用的客户端分页组件(兼容firefox) 今天被ppmm拦下来了 A*寻路初探 GameDev.net(转) 用apache代理解决ajax跨域问题 postgresql 数据库远程连接问题 新同事离职了 4月5日,jia的出行 Open Source Gis(开源Gis大全) GPS Track Solution 1月29日以后需要做的事情 人渐渐而知天命,而非天命不可为也 好冷,nnd 回掉高德公司 IIS和APACHE共享端口
pgrouting示例
StinJia · 2008-04-22 · via 博客园 - StinJia

今天搞定pgrouting,记录
感谢 网友 前进
以前用pgdijkstra,遇到一些问题,后来出了pgrouting,使用还是有些问题
主要问题是

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);

得到结果

vertex_id edge_id cost
248 82102 110.559379577637
84 57984 141.242736816406
1252 193310 52.5332260131836
1970 207837 80.3645401000977
100 180746 284.944580078125
1720 176290 143.326675415039
910 35578 139.427536010742
911 143636 140.143005371094
1495 204813 139.011123657227
430 143222 123.869613647461
1594 155785 253.544082641602
684 26341 243.902267456055
685 56973 141.959945678711
1239 195168 48.8763236999512
1147 112572 161.616928100586
1839 119499 303.865966796875
1759 188892 56.5326347351074
1575 143214 77.3971405029297
2050 140591 158.934661865234
2051 216067 115.035369873047
1950 122139 11.0751705169678
1060 44613 182.172515869141
1061 148709 111.463470458984
908 211592 12.3400468826294
771 29588 126.651756286621
774 63990 175.560150146484
1338 142395 101.610443115234
1978 125346 79.8519439697266
1268 58800 91.4863662719727
1269 83787 87.1205139160156
1613 96196 98.0832748413086
586 22142 275.695068359375
33 108489 152.655151367188
1455 -1 0

#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表来看最短路径的结果了