2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

osrmのインストールからベルリンで経路探索するまで

Last updated at Posted at 2019-09-13

前々回記事 stravaからglobal heat mapのデータを頂く
前回記事 Strava heat map の GeoTIFF化

動機

ルートラボが終わるから、自分でルート検索サービスを作ってみる
詳しくは前々回記事を参照

インストール

オープンソースのルート検索エンジンの定番 osrm をインストールします
やっぱり頼りになるQiitaの記事
GoogleMapのオープンソース版地図アプリ(OSRM)のインストール

自前でビルドしている...
ちなみにWindowsにはバイナリリリースがある様子
WindowsでOSRMを起動する方法

osrmの公式ページ
https://github.com/Project-OSRM/osrm-backend
https://github.com/Project-OSRM/osrm-backend#building-from-source
の手順と
GoogleMapのオープンソース版地図アプリ(OSRM)のインストール
で手順をあわせて...

(1) 必要なライブラリそろえる

$ sudo apt install build-essential git cmake pkg-config libbz2-dev libxml2-dev libzip-dev libboost-all-dev lua5.2 liblua5.2-dev libtbb-dev

(2) ソースコードをとってくる

$ mkdir ~/osrm
$ cd osrm
$ git clone https://github.com/Project-OSRM/osrm-backend.git

(3) ビルド
$ cd osrm-backend
$ mkdir -p build
$ cd build
$ cmake ..
$ make

問題なくビルドできました。

(おまけ) osrmのテスト用サーバを使って...

ビルドの待ち時間を利用して
osrmのテストサーバを使ってルート検索をしてみます。

$ curl 'https://router.project-osrm.org/route/v1/driving/13.388860,52.517037;13.385983,52.496891?steps=true&alternatives=true&alternatives=3&geometries=geojson' | jq 'def hexdec(i): "0123456789abcdef"[i:i+1]; {"type": "FeatureCollection", "features": [[.routes[].geometry] | [., keys] | transpose[] | {"geometry": .[0], "type": "Feature", "properties": {"stroke-width": 2, "stroke": ("#" + hexdec(15-.[1]*2) + hexdec(.[1]*2) + hexdec(.[1]*2))}}]}' > routes.json

この routes.json を
http://geojson.io/ に食べさせます。
Open --> File --> routes.json を選択でOK
すると検索したルートを表示してくれます。

GeoJson形式のファイルは GitHub にコミットすると
GitHub上でもルートとして見られるようです。
便利...

地図のダウンロードから経路検索まで

osrmで利用する地図をダウンロードします
osm(Open Street Map)のものを利用します

ベルリンなのは、公式の手順書がベルリンをダウンロードしているから。
日本全国 (japan-latest.osm.pbf) などもありますが、
サイズが大きく、ダウンロードにも地図構築にも時間がかかるので要注意。

$ pwd
/home/tomonobu/work/osr/berlin
$ wget http://download.geofabrik.de/europe/germany/berlin-latest.osm.pbf

次にosrmが食べる地図を構築します

cd ../../osrm-backend

$ pwd
/home/tomonobu/work/osrm-backend

$ osrm-extract   ../osm/berlin/berlin-latest.osm.pbf
$ osrm-partition ../osm/berlin/berlin-latest.osrm
$ osrm-customize ../osm/berlin/berlin-latest.osrm

次にサーバを起動してアクセスしてみます

↓ サーバをIP/Port指定で立てる(念のため)

$ osrm-routed --ip 127.0.0.1 --port 5000 --algorithm mld ../osm/berlin/berlin-latest.osrm

↓ curlのproxyを無効化してアクセス(念のため)

$ curl --noproxy 127.0.0.1 'http://127.0.0.1:5000/route/v1/driving/13.246353470716485,52.43405136831916;13.39309242380466,52.56433207398476?alternatives=3&geometries=geojson'

これで json 形式の出力がコンソールに表示されれば成功です。
次にjqで形を整えてファイルに出力

$ curl --noproxy 127.0.0.1 'http://127.0.0.1:5000/route/v1/driving/13.246353470716485,52.43405136831916;13.39309242380466,52.56433207398476?alternatives=3&geometries=geojson' | jq 'def hexdec(i): "0123456789abcdef"[i:i+1]; {"type": "FeatureCollection", "features": [[.routes[].geometry] | [., keys] | transpose[] | {"geometry": .[0], "type": "Feature", "properties": {"stroke-width": 2, "stroke": ("#" + hexdec(15-.[1]*2) + hexdec(.[1]*2) + hexdec(.[1]*2))}}]}' > routes.json

最後に出来上がった routes.json を
http://geojson.io/ に食べさせて確認

おー、自分のマシンで経路検索ができるのはなかなかの感動
今回はここまで

2
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?