0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PLATEAU CityGML がつんと Download

Posted at

PLATEAU の CityGML がっつりほしいときあるから Script 書いた
これだと展開後はテラバイトになる可能性もあるので、TODO 対応必要なはず

TODO

たぶんこの辺も追加したいけど、bash じゃ厳しそうだから、簡単コマンドツールにしたほうがいいんだろうな

  • "PLT:pref": "東京都" なフィルタリング
  • 複数年度あるのは、一番新しいのだけを
  • unzip も連携しているようなそれ
    • udx/bldg だけとかも指定できるオプション
  • 進捗表示
  • 途中でやめたときは、中途半端なダウンロードのファイルを消す
  • 再開もできるように

bash

#!/bin/bash

# 一時ディレクトリを用意
TMPDIR=$(mktemp -d)

# 1. メイン CKAN パッケージの JSON を取得
echo "Fetching main package information..."
main_json=$(curl -s "https://www.geospatial.jp/ckan/api/3/action/package_show?id=plateau")

# 2. 含まれるサブ JSON URL を取得
echo "Extracting resource URLs..."
resource_urls=$(echo "$main_json" | jq -r '.result.resources[].url')

# 3. 各サブ JSON を処理
for res_url in $resource_urls; do
    echo "Fetching sub JSON from: $res_url"

    # redirect 対応のため -L オプションを使用
    sub_json=$(curl -s -L "$res_url")

    echo "Processing sub JSON..."

    # 4. data_update[].metadata["PLT:url"] に該当する zip URL を抽出
    zip_urls=$(echo "$sub_json" | jq -r \
        '.data_update[].metadata["PLT:url"] | select(test("\\.zip$"))'
    )

    echo "Found ZIP URLs:"
    echo "$zip_urls"

    # 5. 該当 ZIP をダウンロード
    for zip_url in $zip_urls; do
        filename=$(basename "$zip_url")
        echo "Downloading ZIP: $filename"
        curl -L -o "$filename" "$zip_url"
    done
done

# 後処理
rm -rf "$TMPDIR"
echo "All done."

PowerShell

# ===================== 実行ポリシー確認 =====================
$currentPolicy = Get-ExecutionPolicy -Scope Process

if ($currentPolicy -ne 'Bypass') {
    Write-Warning "現在の実行ポリシーは '$currentPolicy' です。スクリプトの実行に失敗する可能性があります。"
    Write-Host "一時的に許可するには、以下のコマンドを実行してください:"
    Write-Host "`nSet-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass`n"
    Pause
    return
}

# ===================== スクリプト本体 =====================

# 一時ディレクトリ(未使用だが必要なら活用可)
$tmpDir = New-TemporaryFile | Split-Path
Write-Output "Using temp directory: $tmpDir"

# 1. メイン CKAN パッケージの JSON を取得
$mainJsonUrl = "https://www.geospatial.jp/ckan/api/3/action/package_show?id=plateau"
Write-Output "Fetching main package information from $mainJsonUrl"
$mainJson = Invoke-RestMethod -Uri $mainJsonUrl

# 2. 含まれるリソースURLを取得
$resourceUrls = $mainJson.result.resources | ForEach-Object { $_.url }

# 3. 各サブ JSON を処理
foreach ($resUrl in $resourceUrls) {
    Write-Output "`nFetching sub JSON from: $resUrl"
    try {
        $subJson = Invoke-RestMethod -Uri $resUrl
    } catch {
        Write-Warning "Failed to fetch or parse JSON from $resUrl"
        continue
    }

    if ($subJson.data_update) {
        foreach ($entry in $subJson.data_update) {
            $metadata = $entry.metadata
            if ($metadata.'PLT:url' -match '\.zip$') {
                $zipUrl = $metadata.'PLT:url'
                $filename = Split-Path $zipUrl -Leaf

                Write-Output "Downloading ZIP: $filename"
                try {
                    Invoke-WebRequest -Uri $zipUrl -OutFile $filename
                } catch {
                    Write-Warning "Failed to download $zipUrl"
                }
            }
        }
    } else {
        Write-Output "No data_update section found in this JSON."
    }
}

Write-Output "`nAll done."
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?