Skip to content

Instantly share code, notes, and snippets.

@naogify
Last active November 25, 2025 08:14
Show Gist options
  • Select an option

  • Save naogify/73d6c339cf900b53effabda7c7505ac1 to your computer and use it in GitHub Desktop.

Select an option

Save naogify/73d6c339cf900b53effabda7c7505ac1 to your computer and use it in GitHub Desktop.
ShapeをDATA PNGに変換するスクリプト
#!/usr/bin/env bash
set -ex
# 引数のチェック
if [ $# -ne 2 ]; then
echo "エラー: 必要な引数が指定されていません。" >&2
echo "使用方法: $0 <カラーテーブルファイル: 例 color_table.txt> <対象フィールド名: 例 Max_Rank>" >&2
exit 1
fi
# 引数からカラーテーブルファイルとフィールド名を取得
COLOR_TABLE="$1"
FIELD_NAME="$2"
# # 国交省資料に準拠するメッシュサイズ(5メートル: 0.225秒×0.15秒)を基準に変換
# # https://www.mlit.go.jp/river/shishin_guideline/bousai/saigai/tisiki/syozaiti/pdf/e-guideline_5.pdf#page=33
# X_RES=0.225
# Y_RES=0.15
# 国交省資料に準拠するメッシュサイズ(10メートル: 0.45秒×0.3秒)を基準に変換
# https://www.mlit.go.jp/river/shishin_guideline/bousai/saigai/tisiki/syozaiti/pdf/e-guideline_5.pdf#page=33
X_RES=0.45
Y_RES=0.3
# # 静岡県第4次地震被害想定地震動・液状化(4分の1地域メッシュ)
# # https://www.stat.go.jp/data/mesh/m_tuite.html
# X_RES=11.25
# Y_RES=7.5
# 処理対象のズームレベル範囲
ZOOM_MIN=10
ZOOM_MAX=18
# カレントディレクトリ以下の .shp ファイルを処理
find . -type f -iname "*.shp" | while read -r SHP; do
# ベース名(拡張子除去)
BASENAME=$(basename "$SHP" .shp)
# 中間・最終出力ファイル名を定義
REPROJECTED_SHP="${BASENAME}_EPSG3857.shp"
GRAY_TIF="${BASENAME}_gray.tif"
RGBA_VRT="${BASENAME}_rgba.vrt"
RGBA_HR_VRT="${BASENAME}_rgba_high_resolution.vrt"
TILE_DIR="${BASENAME}_tiles"
MBTILES="${BASENAME}.mbtiles"
echo "Processing shapefile: $SHP"
# 1. シェープファイルをEPSG:3857に再投影
ogr2ogr -t_srs EPSG:3857 "$REPROJECTED_SHP" "$SHP"
# 2. 再投影されたシェープファイルからラスタ化 (X_RES, Y_RESを使用)
gdal_rasterize \
-a "$FIELD_NAME" \
-init 0 \
-ot Int32 \
-tr $X_RES $Y_RES \
-tap \
-co COMPRESS=DEFLATE \
"$REPROJECTED_SHP" \
"$GRAY_TIF"
# 3. カラーテーブル適用(RGBA化)
gdaldem color-relief \
"$GRAY_TIF" \
"$COLOR_TABLE" \
"$RGBA_VRT" \
-of VRT \
-alpha \
-exact_color_entry
# 5. タイル生成 (gdal2tiles.py)
gdal2tiles.py --xyz -r near -z $ZOOM_MIN-$ZOOM_MAX --processes=16 "$RGBA_VRT" "$TILE_DIR"
# 6. タイルディレクトリからMBTilesへの変換 (mb-util)
mb-util --image_format=png "$TILE_DIR" "$MBTILES"
echo "Finished processing: $SHP"
echo "Generated MBTiles: $MBTILES"
done
echo "All processing completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment