LoginSignup
3
4

More than 1 year has passed since last update.

Windowsでxyzタイルを作るよ(WSL2でgdal2tiles.pyを動かす)

Last updated at Posted at 2022-01-27

はじめに

Windowsでオルソ画像のxyzタイルを作りたい。

@Kanahiro さんの記事が参考になるとの情報を得た。
参考:https://qiita.com/Kanahiro/items/4b5f8db5743c69741220

gdal2tiles.pyをxyzオプション付きで動かせばいいそうだ。
※GDAL3.1以上のバージョンが必要

この記事によるとWindowsの環境構築がややこしそうだけれど、
今ならWSLでいけんじゃね?と思ったのでやってみた。

環境構築

WSLを有効化

既に動かしている人はパス
入ってない人はコマンドプロンプトから以下のコマンドでインストール(Ubuntuが入る)

> wsl --install

GDALインストール

以下、WSLのコンソールで作業する。
ワンチャン入っているかもしれないのでバージョン確認

$ gdalinfo --version

Command 'gdalinfo' not found, but can be installed with:

sudo apt install gdal-bin

入ってないからこれで入れろって出てきたのでそのままやる

$ sudo apt install gdal-bin
[sudo] password for takeyabu:

パスワードを入れたらインストールされた。
バージョン確認

$ gdalinfo --version
GDAL 3.0.4, released 2020/01/28

古い。残念。

では以下のサイトを参考にUbuntuGISのリポジトリを追加して
https://launchpad.net/~ubuntugis/+archive/ubuntu/ubuntugis-unstable

$ sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable
 Unstable releases of Ubuntu GIS packages. These releases are more bleeding edge and while generally they should work well, they dont receive the same amount of quality assurance as our stable releases do.
 More info: https://launchpad.net/~ubuntugis/+archive/ubuntu/ubuntugis-unstable
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Get:1 http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu focal InRelease [23.8 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:3 http://archive.ubuntu.com/ubuntu focal InRelease
Get:4 http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu focal/main amd64 Packages [56.8 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:6 http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu focal/main Translation-en [24.7 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Fetched 441 kB in 3s (128 kB/s)
Reading package lists... Done

アップデート

$ sudo apt-get update
Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:2 http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu focal InRelease
Hit:3 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Reading package lists... Done

アップグレードできるパッケージを確認

$ sudo apt list --upgradable
Listing... Done
gdal-bin/focal 3.4.0+dfsg-1~focal0 amd64 [upgradable from: 3.0.4+dfsg-1build3]
gdal-data/focal 3.4.0+dfsg-1~focal0 all [upgradable from: 3.0.4+dfsg-1build3]
libgeos-c1v5/focal 3.10.1-1~focal0 amd64 [upgradable from: 3.8.0-1build1]
libgeotiff5/focal 1.7.0-2~focal1 amd64 [upgradable from: 1.5.1-2]
libspatialite7/focal 5.0.1-2~focal0 amd64 [upgradable from: 4.3.0a-6build1]
proj-bin/focal 8.2.0-1~focal0 amd64 [upgradable from: 6.3.1-1]
proj-data/focal 8.2.0-1~focal0 all [upgradable from: 6.3.1-1]
python3-gdal/focal 3.4.0+dfsg-1~focal0 amd64 [upgradable from: 3.0.4+dfsg-1build3]

GDAL3.4にあげられるっぽい
なんか全部上げちゃった方が良さそうなものな気がするのでパッケージ指定せずアップグレード

$ sudo apt upgrade

入った
バージョン確認

$ gdalinfo --version
GDAL 3.4.0, released 2021/11/04

上がった。成功。

Python確認

Pythonのバージョン確認

$ python3 -V
Python 3.8.10

問題なさそう

gdal2tiles.pyを実行

とりあえず動かしてみる

$ gdal2tiles.py
Usage: gdal2tiles.py [options] input_file [output]

gdal2tiles.py: error: You need to specify at least an input file as argument to the script

ちゃんとPATHが通ってて使い方が間違ってるよってエラーが出ていますね。

では変換対象のオルソ画像を配置する。

WSLのファイルには\\wsl$でアクセスできる。
WindowsのExplorerで\\wsl$\Ubuntu\home\<アカウント名>を開いてファイルを貼り付ける。

変換パラメータを付けてgdal2tiles.pyを実行

$ cd
$ gdal2tiles.py source.tif target_dir -z14-18 --xyz
Generating Base Tiles:
0...10...20...30...40...50...60...70...80...90...100
Generating Overview Tiles:
0...10...20...30...40...50...60...70...80...90...100

出来た

バーチャルラスタでもできました。

$ gdal2tiles.py source.vrt target_dir -z14-18 --xyz
Generating Base Tiles:
0...10...20...30...40...50...60...70...80...90...100
Generating Overview Tiles:
0...10...20...30...40...50...60...70...80...90...100

gdal2tilesのリファレンスはこちら
https://gdal.org/programs/gdal2tiles.html

コマンドプロンプトから実行

wslのコンソールを使って実行するのに抵抗があればコマンドプロンプトから実行しましょう。
変換対象や出力先もWindowsディレクトリが扱えます。

今回は対象ファイルをC:\tmp\ortho\source.tifに配置
対象フォルダに移動して実行

> cd C:\tmp\ortho\
> wsl gdal2tiles.py source.tif target_dir -z14-18 --xyz
Generating Base Tiles:
0...10...20...30...40...50...60...70...80...90...100
Generating Overview Tiles:
0...10...20...30...40...50...60...70...80...90...100

できました。

アクティブディレクトリではなくPATH指定する場合はちょっと書き換える必要があります。
例えば、D:\tmp\Sample\source.tifを処理したい場合、
どのように表記すればいいのかはwslpathコマンドを使って調べます。

> wsl wslpath -u 'D:\tmp\Sample\source.tif'
/mnt/d/tmp/Sample/source.tif

/mnt/d/tmp/Sample/source.tifで実行してみます

> wsl gdal2tiles.py /mnt/d/tmp/Sample/source.tif /mnt/d/tmp/Sample/target_dir -z14-18 --xyz
Generating Base Tiles:
0...10...20...30...40...50...60...70...80...90...100
Generating Overview Tiles:
0...10...20...30...40...50...60...70...80...90...100

できました。
繰り返し実行するならバッチファイルにしておくと便利ですね。

さいごに

今は知らないけど数年前WSLはI/Oが遅いことがよく話題になっていたので、今回のようなファイル読み書きメインの処理はパフォーマンスが期待できないかもしれない。
UbuntuのGDAL、WindowsのDockerでGDAL、Windows版QGISバンドルのGDAL、WSLのGDALとかでベンチマークしてみたいなぁ。

本記事のライセンス

クリエイティブ・コモンズ・ライセンス
この記事は クリエイティブ・コモンズ 表示 4.0 国際 ライセンスの下に提供されています。

3
4
0

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
3
4