GeoTIFF ファイル処理

概要

GeoTIFF ファイルの拡大(縦横の画素数を増やす)、GeoTIFF と PNG の相互変換、EPSG コードを用いた座標系の変換の手順を示す。

キーワード: GeoTIFF ファイルの拡大, GeoTIFF から PNG へ変換, PNG から GeoTIFF へ変換, EPSG コードを用いた座標系の変換

目次

前準備

GDAL Windows 版のインストール

GDAL(地理空間データを扱うためのライブラリ)の Windows 版をインストールする。conda-forge を利用する場合は、次の手順でインストールする。

管理者権限コマンドプロンプトを起動する (手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。

conda install -c conda-forge gdal

OSGeo4W を用いてインストールすることもできる。

GeoTIFF サンプルデータファイルの準備

次の Web ページから cea.tif をダウンロードする。

https://download.osgeo.org/geotiff/samples/gdal_eg/

*(⚠️ link removed: this download link couldn't be verified — ask me to re-send this file)*

ダウンロードした .tif ファイルを、分かりやすいディレクトリ(例えば d:\)に保存する。

GeoTIFF から PNG へ変換

  1. GeoTIFF ファイルの画素の値の範囲を調べる。
    gdalinfo -mm cea.tif

    下の実行例では、画素の範囲は 0 から 255 である。

    *(⚠️ link removed: this download link couldn't be verified — ask me to re-send this file)*
  2. PNG に変換する前に決めておくこと

    PNG ファイルは 1 画素 8 ビットなので、0 から 255 の値をとる。

    PNG に変換するとき、次のことを決めておく。

    • ファイル名: ここでは cea.png とする。
    • スケール: ここでは、GeoTIFF での 0 から 255 の値を、PNG での 0 から 255 の値に変換する。
  3. GeoTIFF から PNG への変換

    PNG は 8 ビットなので、出力のデータ型は Byte を指定する。

    gdal_translate -scale 0 255 0 255 -ot Byte -of PNG cea.tif cea.png
    *(⚠️ link removed: this download link couldn't be verified — ask me to re-send this file)*

    結果は、画像ビューワで確認できる。

    *(⚠️ link removed: this download link couldn't be verified — ask me to re-send this file)*

PNG から GeoTIFF へ変換

PNG ファイルは 1 画素 8 ビットなので、0 から 255 の値をとる。

GeoTIFF に変換するとき、次のことを決めておく。

PNG から GeoTIFF への変換例

gdal_translate -scale 0 255 0 24700 -ot Int16 -of GTiff cea.png cea3.tif
*(⚠️ link removed: this download link couldn't be verified — ask me to re-send this file)*

結果は、画像ビューワで確認できる。

*(⚠️ link removed: this download link couldn't be verified — ask me to re-send this file)*

PNG ファイルは座標系や位置の情報を持たないため、この変換だけでは位置情報のない GeoTIFF ができる。位置情報を与えるには、gdal_translate の -a_srs(座標系)と -a_ullr(左上と右下の座標)を指定する。

GeoTIFF ファイルの拡大

拡大率 200% にするときの操作手順例を示す。gdal_translate コマンドを用いる。

gdal_translate -outsize 200% 200% d:\cea.tif d:\cea2.tif
*(⚠️ link removed: this download link couldn't be verified — ask me to re-send this file)*

gdalinfo コマンドを用いて確認する。

*(⚠️ link removed: this download link couldn't be verified — ask me to re-send this file)*

GeoTIFF ファイルは、多くの場合、画像ビューワで表示できる。

拡大前

*(⚠️ link removed: this download link couldn't be verified — ask me to re-send this file)*

拡大

*(⚠️ link removed: this download link couldn't be verified — ask me to re-send this file)*

EPSG コードを用いた座標系の変換

座標系を変換するには gdalwarp コマンドを用いる。変換元の EPSG コードを -s_srs、変換先の EPSG コードを -t_srs で指定する。入力ファイルが座標系の情報を持つ場合は -s_srs を省略できる。

gdalwarp -s_srs EPSG:<変換元の EPSG コード> -t_srs EPSG:<変換先の EPSG コード> <入力ラスタファイル名> <出力ラスタファイル名>

実行例(WGS 84 の緯度経度 EPSG:4326 から Web メルカトル EPSG:3857 への変換)

gdalwarp -t_srs EPSG:3857 cea.tif cea_3857.tif

EPSG コードは https://epsg.io/ で検索できる。

関連する外部ページgdalwarp の公式ドキュメント(GDAL)

関連する外部ページgdal_translate の公式ドキュメント(GDAL)