該文章純粹自學積累,如有雷同,十有八九是巧合 :D
首先需要在 http://metro.teczno.com/ 下載所需地區的完整地圖信息 .pbf 格式,然后用osmconvert 轉化為 .osm 格式,再使用 osmfilter 抓取所需要的信息。這兩個工具只支持在windows OS運行,具體操作如下。
osmconvert
C:\Windows\system32>osmconvert C:\Users\caochu\Downloads\singapore.pbf >C:\Users\caochu\Downloads\ccc.osm
annotation
osmconvert 表示調用轉換的命令
其后是需要轉換的源文件詳細位置
space> 之后跟生成的文件位置和filename
osmfilter
C:\Windows\system32>osmfilter C:\Users\caochu\Downloads\ccc.osm
--keep="highway=" -o=ddd.osm
annotation
osmfilter 表示調用過濾的命令
其后是即將進行過濾的詳細文件地址
--keep="highway=" 表示在過濾過程中保存所有的以highway為標簽的元素,這樣就把所有的道路保存了下來
-o= 表示需要輸出的內容
該命令可以成功執行,但是希望其保存的ddd.osm文件并沒有保存,而且不會報錯,主要是因為沒有規定新生成的文件地址。改為如下命令即可。
只要在 -o= 后面添加詳細地輸入地址即可完成過濾,生成的文件在填寫的地址中。
C:\Windows\system32>osmfilter C:\Users\caochu\Downloads\ccc.osm
--keep="highway=" -o=C:\Users\caochu\Downloads\ddd.osm
之后需要將.osm文件導入至postgreSQL中,可惜windows平臺我始終沒有調試好python與postgreSQL之間的聯系,只能轉戰Mac平臺,詳細的操作過程如下。
postgreSQL configureation
psotgres APP side: open psql terminal
error:
CCMac:~caochu$'/Applications/Postgres.app/Contents/Versions/9.5/bin'/psql -p5432
psql: FATAL: database "caochu" does not exist
solution:
CC:~ caochu$ sudo -u postgres createuser owning_user
next step:
CC:~ caochu$ psql osm
import OSM into psql:
osm=> osm2pgsql -s -U osm -d osm/Users/caochu/Downloads/map.osm
iTerm terminal:
CC:~ caochu$ osm2pgsql -s -U osm -d osm/Users/caochu/Downloads/map.osm.xml
error:
osm2pgsql SVN version 0.88.1 (64bit id space)
Using built-in tag processing pipeline
Osm2pgsql failed due to ERROR: Error: Connection to database failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
solution:
CC:~ caochu$ mkdir /var/pgsql_socket/
error:
mkdir: /var/pgsql_socket/: Permission denied
solution: add sudo in front of your commond
CC:~ caochu$ sudo mkdir /var/pgsql_socket/
mkdir: /var/pgsql_socket/: File exists
next step
CC:~ caochu$ ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/
error
ln: /var/pgsql_socket//.s.PGSQL.5432: Permission denied
solution
CC:~ caochu$ "sudo" ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/
import osm map into postgreSQL
CC:~ caochu$ osm2pgsql -s -U postgres -d osm /Users/caochu/Downloads/map.osm
osm2pgsql SVN version 0.88.1 (64bit id space)
error:
Reading in file: /Users/caochu/Downloads/map.osm
error while opening file /Users/caochu/Downloads/map.osm
solution: add .xml at the end of your map like map.osm.xml
CC:~ caochu$ osm2pgsql -s -U postgres -d osm /Users/caochu/Downloads/map.osm.xml
successfult indicator:
Osm2pgsql took 1s overall
pgAdmin 3 side
open and connect to the server
create the extension sql file which can be found in the folder on desktop
到這里基本上就結束了,把這些東西存放在一個地方,等到使用的時候再來看。