? ? 最近在學(xué)習(xí)使用postgis查詢兩點(diǎn)之間最近路徑,將整理的問題記錄提供需要的人參考使用,接下來我以北京為例。
1.數(shù)據(jù)下載
https://www.openstreetmap.org,這個(gè)網(wǎng)站下載的數(shù)據(jù)沒有特定某個(gè)城市,是全國的數(shù)據(jù)文件,需要某一個(gè)的只能去網(wǎng)上搜索(度娘);
2.數(shù)據(jù)導(dǎo)入
需要的數(shù)據(jù)下載成功后,有多個(gè)格式文件,dbf、shp.....其他省略號代替;
可以用命令將shp文件轉(zhuǎn)換成sql文件直接使用navicat導(dǎo)入sql文件
轉(zhuǎn)換文件命令:cmd進(jìn)入到PostgreSQL\10目錄,
shp2pgsql -W UTF-8 -s 4326 -g geom_data C:/Users/vis/Desktop/lw/bjluw/市區(qū)道路_polyline? 改成你的文件名>C:/Users/vis/Desktop/你的文件名.sql
到此sql文件轉(zhuǎn)換完成。
需要注意:
使用PostGIS PostGIS Bundle 3 for PostgreSQL x64 10 Shapefile and DBF Loader Exporter 導(dǎo)入的時(shí)候需要改一下編碼格式,文件可能是utf8或者gbk。(navicat 可以直接導(dǎo)入多種格式,但是不支持shp文件,需要用上述插件來導(dǎo)入shp文件);至此數(shù)據(jù)文件導(dǎo)入完成。
3.添加字段
https://github.com/cytggit/Map-openlayers/wiki/%E8%B7%AF%E5%BE%84%E8%A7%84%E5%88%92%E7%9A%84%E5%BA%94%E7%94%A8 可以跟著網(wǎng)址給的步驟操作。
需要注意:
1、道路權(quán)重值字段可能在你導(dǎo)入的文件中存在了,如果存在可以忽略;
2、為source和target字段創(chuàng)建索引,和上一步一樣,可能會(huì)存在索引不需要再次創(chuàng)建;
3.為length賦值,同上;
字段添加結(jié)束.
4.使用pgr_dijkstra算法查詢
SELECT seq, node, edge, cost FROM pgr_dijkstra('
SELECT gid as id,
source::integer,
target::integer,
length::double precision AS cost,
x1,y1,x2,y2
FROM _polyline',
103, 128,directed:=false)
5.將查詢結(jié)果存表
insert into line2 (the_geom) select ST_MakeLine(ARRAY (select the_geom
from (SELECT seq,? node FROM pgr_dijkstra(
'SELECT? gid AS id,
source::integer,
target::integer,
length::double precision AS cost
FROM _polyline', 103, 128,directed:=false))path, _polyline_vertices_pgr p
where path.node = p.id order by seq));
6.使用qgis展示
打開QGIS Desktop 3.12.3 左側(cè)postgis連接自己的庫可以看自己的查詢數(shù)據(jù)了。
到此全部完成。