OpenStreetMap (OSM)のデータは日々更新されているため、OSMデータを一度自サーバにインポートした後も、定期的に新しいデータを再取り込みして最新の状態を保ちたいという場合があります。
OSMのデータを更新する方法は様々あるようですが、ここでは、GeofabrikからダウンロードしたOSMデータを利用している場合のデータ更新方法を記載します。
方法は、OpenStreetMap Carto Tutorials の Keeping the local database in sync with OSM の下の方に書かれている「Exploiting Osmosis with an extract from Geofabrik」の項をベースにしています。
目次
osmosis と osmium をインストール
$ sudo apt-get -y install osmosis osmium-tool
osmiumはPBFファイルを分析して、例えば以下のような情報を抽出できます。
$ PBF_FILE=~/data/japan-latest.osm.pbf $ osmium fileinfo -e $PBF_FILE File: Name: data/japan-latest.osm.pbf Format: PBF Compression: none Size: 1498025738 Header: Bounding boxes: (122.5607,20.08228,154.4709,45.80245) With history: no Options: generator=osmium/1.8.0 osmosis_replication_base_url=http://download.geofabrik.de/asia/japan-updates osmosis_replication_sequence_number=2598 osmosis_replication_timestamp=2020-05-01T20:59:03Z pbf_dense_nodes=true timestamp=2020-05-01T20:59:03Z [======================================================================] 100% Data: Bounding box: (121.4932442,10.9583333,161.9498554,61.9416164) Timestamps: First: 2007-07-02T22:46:56Z Last: 2020-05-01T20:39:13Z Objects ordered (by type and id): yes Multiple versions of same object: no CRC32: not calculated (use --crc/-c to enable) Number of changesets: 0 Number of nodes: 184075264 Number of ways: 23658371 Number of relations: 83290 Smallest changeset ID: 0 Smallest node ID: 31236558 Smallest way ID: 4847506 Smallest relation ID: 12578 Largest changeset ID: 0 Largest node ID: 7472231102 Largest way ID: 798953401 Largest relation ID: 11057963 All objects have following metadata attributes: version+timestamp Some objects have following metadata attributes: version+timestamp Number of buffers: 260551 (avg 797 objects per buffer) Sum of buffer sizes: 16611412744 (16.611 GB) Sum of buffer capacities: 17088053248 (17.088 GB, 97% full)
configuration.txt を生成
OSMデータ更新のためのconfiguration.txt
を生成します。
必要な情報は、OSMのPBFファイル(ここでは前回インポートしたjapan-latest.osm.pbf)からosumiumで抽出してます。
$ PBF_FILE=~/data/japan-latest.osm.pbf $ WORKOSM_DIR=/var/lib/mod_tile/.osmosis/ $ REPLICATION_BASE_URL="$(osmium fileinfo -g 'header.option.osmosis_replication_base_url' "${PBF_FILE}")" $ echo -e "baseUrl=${REPLICATION_BASE_URL}\nmaxInterval=3600" > "${WORKOSM_DIR}/configuration.txt"
configuration.txt
baseUrl=http://download.geofabrik.de/asia/japan-updates maxInterval=3600
state.txt を生成
OSMデータ更新のためのstate.txt
を生成します。
$ WORKOSM_DIR=/var/lib/mod_tile/.osmosis/ $ curl -s -L -o "${WORKOSM_DIR}/state.txt" "${REPLICATION_BASE_URL}/${REPLICATION_SEQUENCE_NUMBER}.state.txt"
state.txt
# original OSM minutely replication sequence number 4002294 timestamp=2020-05-01T20\:59\:03Z sequenceNumber=2598
OSMデータを更新
OSMのデータの更新を行います。
osmosisの出力をパイプでosm2pgsqlに渡しています。
$ osmosis --read-replication-interval workingDirectory=/var/lib/mod_tile/.osmosis/ --simplify-change --write-xml-change - | osm2pgsql -d gis --append --slim -G --hstore --tag-transform-script ~/src/openstreetmap-carto/openstreetmap-carto.lua -C 4000 --number-processes 1 -S ~/src/openstreetmap-carto/openstreetmap-carto.style -r xml - osm2pgsql version 1.2.0 (1.2.0-396-g5caa410) Allocating memory for dense node cache Allocating dense node cache in one big chunk Allocating memory for sparse node cache Sharing dense sparse Node-cache: cache=4000MB, maxblocks=64000*65536, allocation method=11 Mid: pgsql, cache=4000 Using lua based tag processing pipeline with script /home/renderaccount/src/openstreetmap-carto/openstreetmap-carto.lua Using projection SRS 3857 (Spherical Mercator) Setting up table: planet_osm_point Setting up table: planet_osm_line Setting up table: planet_osm_polygon Setting up table: planet_osm_roads Reading in file: - Using XML parser. 5月 04, 2020 11:35:44 午後 org.openstreetmap.osmosis.core.Osmosis run 情報: Osmosis Version 0.47 5月 04, 2020 11:35:44 午後 org.java.plugin.registry.xml.ManifestParser <init> 情報: got SAX parser factory - org.apache.xerces.jaxp.SAXParserFactoryImpl@3fee9989 5月 04, 2020 11:35:44 午後 org.java.plugin.registry.xml.PluginRegistryImpl configure 情報: configured, stopOnError=false, isValidating=true 5月 04, 2020 11:35:44 午後 org.java.plugin.registry.xml.PluginRegistryImpl register 情報: plug-in and fragment descriptors registered - 1 5月 04, 2020 11:35:44 午後 org.java.plugin.standard.StandardPluginManager activatePlugin 情報: plug-in started - org.openstreetmap.osmosis.core.plugin.Core@0.47.0 (active/total: 1 of 1) 5月 04, 2020 11:35:44 午後 org.openstreetmap.osmosis.core.Osmosis run 情報: Preparing pipeline. 5月 04, 2020 11:35:44 午後 org.openstreetmap.osmosis.core.Osmosis run 情報: Launching pipeline execution. 5月 04, 2020 11:35:44 午後 org.openstreetmap.osmosis.core.Osmosis run 情報: Pipeline executing, waiting for completion. 5月 04, 2020 11:36:20 午後 org.openstreetmap.osmosis.core.Osmosis run 情報: Pipeline complete. 5月 04, 2020 11:36:20 午後 org.openstreetmap.osmosis.core.Osmosis run Processing: Node(75k 0.0k/s) Way(13k 0.01k/s) Relation(232 0.3/s) parse time: 4058s Node stats: total(75425), max(7474463608) in 2475s Way stats: total(13189), max(799222089) in 884s Relation stats: total(232), max(11061495) in 665s Going over pending ways... 32166 ways are pending Using 1 helper-processes Finished processing 32166 ways in 190 s 32166 Pending ways took 190s at a rate of 169.29/s Going over pending relations... 7152 relations are pending Using 1 helper-processes Finished processing 7152 relations in 1705 s 7152 Pending relations took 1705s at a rate of 4.19/s Completed planet_osm_point Completed planet_osm_line Completed planet_osm_polygon Completed planet_osm_roads Stopping table: planet_osm_nodes Stopped table: planet_osm_nodes in 0s Stopping table: planet_osm_ways Stopped table: planet_osm_ways in 0s Stopping table: planet_osm_rels Stopped table: planet_osm_rels in 0s Osm2pgsql took 6427s overall node cache: stored: 75425(100.00%), storage efficiency: 47.44% (dense blocks: 1, sparse nodes: 75403), hit rate: 3.76%
更新結果
今回の実行環境は前回と同じく以下のとおりです。
- ホストマシン: Core i7-8700 @ 3.20GHz、DDR4 32GB、HDD 5400RPM SATA/600、Windows 10
- ゲストマシン: VirtualBox 6.1、CPU×2、メモリ 8GB、Ubuntu 20.04、PostgreSQL 12
- 地図データ: 日本、アゼルバイジャン(アゼルバイジャンはタイルサーバ構築当初の動作確認用にインポートしていた)
所要時間
トータル6427秒ということで、約1時間50分かかりました。
ログの最後を前回と今回で比べてみると、
前回日本域データを初回インポートした時 | 今回の差分アップデート時 |
---|---|
Osm2pgsql took 64428s overall | Osm2pgsql took 6427s overall |
node cache: stored: 184075264(100.00%), storage efficiency: 52.70% (dense blocks: 6626, sparse nodes: 147501249), hit rate: 99.98% | node cache: stored: 75425(100.00%), storage efficiency: 47.44% (dense blocks: 1, sparse nodes: 75403), hit rate: 3.76% |
⇒ 2857 [node cache: stored] / s | ⇒ 11.74 [node cache: stored] / s |
という結果なので、Osm2pgsqlにかかった時間だけ見るとだいぶ遅くなっています。
前回は当初アゼルバイジャンのデータしか入っておらずDB内のレコード件数が少ない状態から始めており、一方今回は日本データが入っていて件数が多い状態から始めたため、レコードの挿入に時間がかかったため、という可能性があります。もしくは、単に差分アップデート時には時間がかかるという性質がある可能性もあります。
いずれにせよ、差分アップデートは全インポートよりは十分に速いですが、単位時間あたりの登録速度は全インポートの時より遅くなるケースもあるようです。
レコード数
$ sudo su - postgres postgres@ubu-VirtualBox:~$ psql gis psql (12.2 (Ubuntu 12.2-4)) Type "help" for help. gis=# select relname, n_live_tup from pg_stat_user_tables where schemaname='public'; relname | n_live_tup --------------------+------------ planet_osm_roads | 610872 planet_osm_nodes | 186468823 planet_osm_ways | 23732835 spatial_ref_sys | 8500 planet_osm_polygon | 14443260 planet_osm_point | 1997131 planet_osm_line | 9169679 planet_osm_rels | 86215 (8 rows)
テーブル名 | 初回インポート後 | 更新1回目後 | 増加 |
---|---|---|---|
planet_osm_roads | 610,310 | 610,872 | 562 |
planet_osm_nodes | 186,422,289 | 186,468,823 | 46,534 |
planet_osm_ways | 23,726,279 | 23,732,835 | 6,556 |
spatial_ref_sys | 8,500 | 8,500 | 0 |
planet_osm_polygon | 14,438,037 | 14,443,260 | 5,223 |
planet_osm_point | 1,996,382 | 1,997,131 | 749 |
planet_osm_line | 9,038,265 | 9,169,679 | 131,414 |
planet_osm_rels | 86,139 | 86,215 | 76 |
合計 | 236,326,201 | 236,517,315 | 191,114 |
state.txt
初回インポート後 | 更新1回目後 |
---|---|
# original OSM minutely replication sequence number 4002294 timestamp=2020-05-01T20\:59\:03Z sequenceNumber=2598 | # Mon May 04 23:36:20 JST 2020 sequenceNumber=2599 timestamp=2020-05-02T20\:59\:02Z |
今回更新コマンドを打ったのは5/4 23:35でしたが、更新後のstate.txtは5/2 20:59となっており、5/4に追いついていません。configuration.txt
でmaxInterval=3600
としていたので、一度の実行で更新されるのは3600秒=1時間分ということのようです。よって、現時点へ追いつくまで複数回実行したり、maxIntervalを大きくしたりする必要があります。
更新2回目
所要時間
$ osmosis --read-replication-interval workingDirectory=/var/lib/mod_tile/.osmosis/ --simplify-change --write-xml-change - | osm2pgsql -d gis --append --slim -G --hstore --tag-transform-script ~/src/openstreetmap-carto/openstreetmap-carto.lua -C 4000 --number-processes 1 -S ~/src/openstreetmap-carto/openstreetmap-carto.style -r xml - osm2pgsql version 1.2.0 (1.2.0-396-g5caa410) Allocating memory for dense node cache Allocating dense node cache in one big chunk Allocating memory for sparse node cache Sharing dense sparse Node-cache: cache=4000MB, maxblocks=64000*65536, allocation method=11 Mid: pgsql, cache=4000 Using lua based tag processing pipeline with script /home/renderaccount/src/openstreetmap-carto/openstreetmap-carto.lua Using projection SRS 3857 (Spherical Mercator) Setting up table: planet_osm_point Setting up table: planet_osm_line Setting up table: planet_osm_polygon Setting up table: planet_osm_roads Reading in file: - Using XML parser. 5月 05, 2020 11:51:01 午前 org.openstreetmap.osmosis.core.Osmosis run 情報: Osmosis Version 0.47 5月 05, 2020 11:51:01 午前 org.openstreetmap.osmosis.core.Osmosis run 情報: Preparing pipeline. 5月 05, 2020 11:51:01 午前 org.openstreetmap.osmosis.core.Osmosis run 情報: Launching pipeline execution. 5月 05, 2020 11:51:01 午前 org.openstreetmap.osmosis.core.Osmosis run 情報: Pipeline executing, waiting for completion. 5月 05, 2020 11:51:12 午前 org.openstreetmap.osmosis.core.Osmosis run 情報: Pipeline complete. 5月 05, 2020 11:51:12 午前 org.openstreetmap.osmosis.core.Osmosis run 情報: Total execution time: 11462 milliseconds. Processing: Node(97k 0.0k/s) Way(16k 0.05k/s) Relation(198 1.0/s) parse time: 3577s Node stats: total(97390), max(7477039024) in 3058s Way stats: total(16872), max(799483339) in 324s Relation stats: total(198), max(11064694) in 189s Going over pending ways... 21422 ways are pending Using 1 helper-processes Finished processing 21422 ways in 95 s 21422 Pending ways took 95s at a rate of 225.49/s Going over pending relations... 8372 relations are pending Using 1 helper-processes Finished processing 8372 relations in 481 s 8372 Pending relations took 481s at a rate of 17.41/s Completed planet_osm_point Completed planet_osm_line Completed planet_osm_polygon Completed planet_osm_roads Stopping table: planet_osm_nodes Stopped table: planet_osm_nodes in 0s Stopping table: planet_osm_ways Stopped table: planet_osm_ways in 0s Stopping table: planet_osm_rels Stopped table: planet_osm_rels in 0s Osm2pgsql took 4316s overall node cache: stored: 97390(100.00%), storage efficiency: 48.43% (dense blocks: 2, sparse nodes: 92359), hit rate: 5.36%
更新1回目 | 更新2回目 |
---|---|
Osm2pgsql took 6427s overall | Osm2pgsql took 4316s overall |
node cache: stored: 75425(100.00%), storage efficiency: 47.44% (dense blocks: 1, sparse nodes: 75403), hit rate: 3.76% | node cache: stored: 97390(100.00%), storage efficiency: 48.43% (dense blocks: 2, sparse nodes: 92359), hit rate: 5.36% |
⇒ 11.74 [node cache: stored] / s | ⇒ 22.56 [node cache: stored] / s |
レコード数
テーブル名 | 初回インポート後 | 更新1回目後 | 更新2回目後 |
---|---|---|---|
planet_osm_roads | 610,310 | 610,872 | 610,974 |
planet_osm_nodes | 186,422,289 | 186,468,823 | 186,530,219 |
planet_osm_ways | 23,726,279 | 23,732,835 | 23,743,144 |
spatial_ref_sys | 8,500 | 8,500 | 8,500 |
planet_osm_polygon | 14,438,037 | 14,443,260 | 14,451,425 |
planet_osm_point | 1,996,382 | 1,997,131 | 1,998,355 |
planet_osm_line | 9,038,265 | 9,169,679 | 9,171,960 |
planet_osm_rels | 86,139 | 86,215 | 86,316 |
合計 | 236,326,201 | 236,517,315 | 236,600,893 |
テーブル名 | 1回目での増加 | 2回目での増加 |
---|---|---|
planet_osm_roads | 562 | 102 |
planet_osm_nodes | 46,534 | 61,396 |
planet_osm_ways | 6,556 | 10,309 |
spatial_ref_sys | 0 | 0 |
planet_osm_polygon | 5,223 | 8,165 |
planet_osm_point | 749 | 1,224 |
planet_osm_line | 131,414 | 2,281 |
planet_osm_rels | 76 | 101 |
合計 | 191,114 | 83,578 |
state.txt
更新1回目後 | 更新2回目後 |
---|---|
# Mon May 04 23:36:20 JST 2020 sequenceNumber=2599 timestamp=2020-05-02T20\:59\:02Z | # Tue May 05 11:51:12 JST 2020 sequenceNumber=2600 timestamp=2020-05-03T20\:59\:02Z |
更新3回目
所要時間
$ osmosis --read-replication-interval workingDirectory=/var/lib/mod_tile/.osmosis/ --simplify-change --write-xml-change - | osm2pgsql -d gis --append --slim -G --hstore --tag-transform-script ~/src/openstreetmap-carto/openstreetmap-carto.lua -C 4000 --number-processes 1 -S ~/src/openstreetmap-carto/openstreetmap-carto.style -r xml - osm2pgsql version 1.2.0 (1.2.0-396-g5caa410) Allocating memory for dense node cache Allocating dense node cache in one big chunk Allocating memory for sparse node cache Sharing dense sparse Node-cache: cache=4000MB, maxblocks=64000*65536, allocation method=11 Mid: pgsql, cache=4000 Using lua based tag processing pipeline with script /home/renderaccount/src/openstreetmap-carto/openstreetmap-carto.lua Using projection SRS 3857 (Spherical Mercator) Setting up table: planet_osm_point Setting up table: planet_osm_line Setting up table: planet_osm_polygon Setting up table: planet_osm_roads Reading in file: - Using XML parser. 5月 05, 2020 1:41:33 午後 org.openstreetmap.osmosis.core.Osmosis run 情報: Osmosis Version 0.47 5月 05, 2020 1:41:33 午後 org.openstreetmap.osmosis.core.Osmosis run 情報: Preparing pipeline. 5月 05, 2020 1:41:33 午後 org.openstreetmap.osmosis.core.Osmosis run 情報: Launching pipeline execution. 5月 05, 2020 1:41:33 午後 org.openstreetmap.osmosis.core.Osmosis run 情報: Pipeline executing, waiting for completion. 5月 05, 2020 1:42:25 午後 org.openstreetmap.osmosis.core.Osmosis run 情報: Pipeline complete. 5月 05, 2020 1:42:25 午後 org.openstreetmap.osmosis.core.Osmosis run 情報: Total execution time: 52618 milliseconds. Processing: Node(111k 0.0k/s) Way(20k 0.22k/s) Relation(143 4.2/s) parse time: 3810s Node stats: total(111675), max(7482819541) in 3635s Way stats: total(20236), max(800111922) in 93s Relation stats: total(143), max(11068583) in 34s Going over pending ways... 10078 ways are pending Using 1 helper-processes Finished processing 10078 ways in 28 s 10078 Pending ways took 28s at a rate of 359.93/s Going over pending relations... 6676 relations are pending Using 1 helper-processes Finished processing 6676 relations in 348 s 6676 Pending relations took 348s at a rate of 19.18/s Completed planet_osm_point Completed planet_osm_line Completed planet_osm_polygon Completed planet_osm_roads Stopping table: planet_osm_nodes Stopped table: planet_osm_nodes in 0s Stopping table: planet_osm_ways Stopped table: planet_osm_ways in 0s Stopping table: planet_osm_rels Stopped table: planet_osm_rels in 0s Osm2pgsql took 4442s overall node cache: stored: 111675(100.00%), storage efficiency: 48.25% (dense blocks: 1, sparse nodes: 111618), hit rate: 7.76%
更新1回目 | 更新2回目 | 更新3回目 |
---|---|---|
Osm2pgsql took 6427s overall | Osm2pgsql took 4316s overall | Osm2pgsql took 4442s overall |
node cache: stored: 75425(100.00%), storage efficiency: 47.44% (dense blocks: 1, sparse nodes: 75403), hit rate: 3.76% | node cache: stored: 97390(100.00%), storage efficiency: 48.43% (dense blocks: 2, sparse nodes: 92359), hit rate: 5.36% | node cache: stored: 111675(100.00%), storage efficiency: 48.25% (dense blocks: 1, sparse nodes: 111618), hit rate: 7.76% |
⇒ 11.74 [node cache: stored] / s | ⇒ 22.56 [node cache: stored] / s | ⇒ 25.14 [node cache: stored] / s |
レコード数
テーブル名 | 初回インポート後 | 更新1回目後 | 更新2回目後 | 更新3回目後 |
---|---|---|---|---|
planet_osm_roads | 610,310 | 610,872 | 610,974 | 611,004 |
planet_osm_nodes | 186,422,289 | 186,468,823 | 186,530,219 | 186,614,409 |
planet_osm_ways | 23,726,279 | 23,732,835 | 23,743,144 | 23,757,416 |
spatial_ref_sys | 8,500 | 8,500 | 8,500 | 8,500 |
planet_osm_polygon | 14,438,037 | 14,443,260 | 14,451,425 | 14,464,037 |
planet_osm_point | 1,996,382 | 1,997,131 | 1,998,355 | 1,999,030 |
planet_osm_line | 9,038,265 | 9,169,679 | 9,171,960 | 9,173,659 |
planet_osm_rels | 86,139 | 86,215 | 86,316 | 86,384 |
合計 | 236,326,201 | 236,517,315 | 236,600,893 | 236,714,439 |
テーブル名 | 1回目での増加 | 2回目での増加 | 3回目での増加 |
---|---|---|---|
planet_osm_roads | 562 | 102 | 30 |
planet_osm_nodes | 46,534 | 61,396 | 84,190 |
planet_osm_ways | 6,556 | 10,309 | 14,272 |
spatial_ref_sys | 0 | 0 | 0 |
planet_osm_polygon | 5,223 | 8,165 | 12,612 |
planet_osm_point | 749 | 1,224 | 675 |
planet_osm_line | 131,414 | 2,281 | 1,699 |
planet_osm_rels | 76 | 101 | 68 |
合計 | 191,114 | 83,578 | 113,546 |
state.txt
更新1回目後 | 更新2回目後 | 更新3回目後 |
---|---|---|
# Mon May 04 23:36:20 JST 2020 sequenceNumber=2599 timestamp=2020-05-02T20\:59\:02Z | # Tue May 05 11:51:12 JST 2020 sequenceNumber=2600 timestamp=2020-05-03T20\:59\:02Z | Tue May 05 13:42:25 JST 2020 sequenceNumber=2601 timestamp=2020-05-04T20\:59\:02Z |
他の方法
今回参考にした OpenStreetMap Carto Tutorials の Keeping the local database in sync with OSM には、Geofabrik のOSMデータを前提とした方法以外も記載されています。
mod_tileに付属のopenstreetmap-tiles-update-expireスクリプトを使用する方法も記載されており、これについては日本語の記事もありました。
OpenStreetMap のタイルサーバーの更新設定
この記事ではGeofabrikのデータを使いつつもopenstreetmap-tiles-update-expireスクリプトと日本域ポリゴンを使って更新を行っているようです。