コンテンツへスキップ

インポートした OpenStreetMap データの更新方法

OpenStreetMap (OSM)のデータは日々更新されているため、OSMデータを一度自サーバにインポートした後も、定期的に新しいデータを再取り込みして最新の状態を保ちたいという場合があります。

OSMのデータを更新する方法は様々あるようですが、ここでは、GeofabrikからダウンロードしたOSMデータを利用している場合のデータ更新方法を記載します。

方法は、OpenStreetMap Carto TutorialsKeeping 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 overallOsm2pgsql 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_roads610,310610,872562
planet_osm_nodes186,422,289186,468,82346,534
planet_osm_ways23,726,27923,732,8356,556
spatial_ref_sys8,5008,5000
planet_osm_polygon14,438,03714,443,2605,223
planet_osm_point1,996,3821,997,131749
planet_osm_line9,038,2659,169,679131,414
planet_osm_rels86,13986,21576
合計236,326,201236,517,315191,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.txtmaxInterval=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 overallOsm2pgsql 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_roads610,310610,872610,974
planet_osm_nodes186,422,289186,468,823186,530,219
planet_osm_ways23,726,27923,732,83523,743,144
spatial_ref_sys8,5008,5008,500
planet_osm_polygon14,438,03714,443,26014,451,425
planet_osm_point1,996,3821,997,1311,998,355
planet_osm_line9,038,2659,169,6799,171,960
planet_osm_rels86,13986,21586,316
合計236,326,201236,517,315236,600,893
テーブル名1回目での増加2回目での増加
planet_osm_roads562102
planet_osm_nodes46,53461,396
planet_osm_ways6,55610,309
spatial_ref_sys00
planet_osm_polygon5,2238,165
planet_osm_point7491,224
planet_osm_line131,4142,281
planet_osm_rels76101
合計191,11483,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 overallOsm2pgsql took 4316s overallOsm2pgsql 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_roads610,310610,872610,974611,004
planet_osm_nodes186,422,289186,468,823186,530,219186,614,409
planet_osm_ways23,726,27923,732,83523,743,14423,757,416
spatial_ref_sys8,5008,5008,5008,500
planet_osm_polygon14,438,03714,443,26014,451,42514,464,037
planet_osm_point1,996,3821,997,1311,998,3551,999,030
planet_osm_line9,038,2659,169,6799,171,9609,173,659
planet_osm_rels86,13986,21586,31686,384
合計236,326,201236,517,315236,600,893236,714,439
テーブル名1回目での増加2回目での増加3回目での増加
planet_osm_roads56210230
planet_osm_nodes46,53461,39684,190
planet_osm_ways6,55610,30914,272
spatial_ref_sys000
planet_osm_polygon5,2238,16512,612
planet_osm_point7491,224675
planet_osm_line131,4142,2811,699
planet_osm_rels7610168
合計191,11483,578113,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 TutorialsKeeping the local database in sync with OSM には、Geofabrik のOSMデータを前提とした方法以外も記載されています。

mod_tileに付属のopenstreetmap-tiles-update-expireスクリプトを使用する方法も記載されており、これについては日本語の記事もありました。
OpenStreetMap のタイルサーバーの更新設定
この記事ではGeofabrikのデータを使いつつもopenstreetmap-tiles-update-expireスクリプトと日本域ポリゴンを使って更新を行っているようです。

コメントを残す

%d人のブロガーが「いいね」をつけました。