Open Street Map API
記錄開放街圖 Open Street Map(OSM) API 的基本使用跟文件。
Editing API
RESTful API,data 格式為 XML,目前版本為 v0.6。
文件:
程式化編輯資料要注意的事情:
- http://wiki.openstreetmap.org/wiki/API#Terms_of_use
- http://wiki.openstreetmap.org/wiki/Automated_Edits_code_of_conduct
OSM 有提供測試用 API,測試站跟正式網站的帳號是分開的。
- 測試站:https://master.apis.dev.openstreetmap.org/
- 測試用 API:https://master.apis.dev.openstreetmap.org/
- 正式 API:http://api.openstreetmap.org/
編輯修改 element 必須 reference 到一個 changeset,可以想成修改 log。changeset 跟 element 一樣有 tag,文件建議包含 comment=*
以及 created_by=*
tag,簡述改了什麼以及由誰修改。
API 使用流程
- 生一個 changeset 得到 changeset id
- call API 修改 element,會帶 changeset id
- 關閉 changeset
changeset 如果沒有關掉,OSM 有規則會自動關閉它。
Authentication
editing API 需要 authentication,可以用 HTTP basic authentication,即在 http header 加上 Authorization: Basic xxx
,其中 xxx
是 user:password
的 base64 字串。nodejs 下可以用 new Buffer("Hello World").toString('base64');
得到 base64 字串。
以 curl
送 request:
1 | $ curl https://master.apis.dev.openstreetmap.org/api/capabilities |
除了 HTTP basic authentication 外也可以使用 OAuth,這我沒研究。
剩下 API 使用參考文件定義囉。
Overpass API
read only API
- http://wiki.openstreetmap.org/wiki/Overpass_API
- http://wiki.openstreetmap.org/wiki/Overpass_API/XAPI_Compatibility_Layer
Overpass API 有幾種 query 語法,大致分成 XML query format 跟 Overpass QL,相關文件:
- http://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide
- http://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_QL
我只用 XAPI Compatibility Layer 做簡單的 map 讀取跟 tag query,沒使用上面比較複雜的 query 功能。
XAPI Compatibility Layer 基本 HTTP GET:
1 | http://www.overpass-api.de/api/xapi?map?bbox=left,bottom,right,top |
tag 可以串很多,會被 and 起來,其中 node 也可以改成 way 跟 relation。