1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

不動産情報ライブラリAPIを使ってpbfタイルデータを取得する(Windows PowerShell)

Last updated at Posted at 2024-06-01

はじめに

不動産情報ライブラリのAPIではタイルデータが公開されています。私もMapLibre GL JSを使って、公開されているタイルを参照したウェブ地図を作ろうとしたのですが、うまくできませんでした。

CORS設定の関係でできない可能性もあり、自分のウェブ地図サーバーでデータを取得してしまう方法ならばできるのではないかと考えました。そこで不動産情報ライブラリAPIを使って、タイルデータを取得する方法についてメモしておくことにしました。

不動産情報ライブラリAPIの提供方針を確認

Webサービスや研究開発に利用してよいということです。今回の作業はウェブ地図のための実験的なものなので趣旨には一致していると考えます。
image.png

また、利用規約も確認しておきます。出典の記載などが必要ですね。

今回の作業対象エリア

以下の 9/454/201 のタイル区画(Z/X/Y)を作業対象にすることにします。
image.png

対象データ

地価公示・地価調査のポイント(点)を対象にすることにします。ズームレベルは13~15です。
https://www.reinfolib.mlit.go.jp/help/apiManual/#titleApi8

方法

Step 0: コマンドの確認

不動産情報ライブラリのAPIで各ZXYにおけるタイル(GeoJSONまたはpbf)をダウンロードするには、WindowsPowerShellで以下のようにやります。なお、API利用申請を行いAPIキーを取得しておくことが必要です。
image.png
なお、地価公示・地価調査のポイント(点)については、本来はpclass(任意クラス)を指定しなければ0と1の両方を含むのですが、APIの不具合のためAPIが修正されるまではどちらかを指定する必要があります。(事務局に問い合わせて確認した)

変数を指定してダウンロードできるか以下の通り実験しておきます。(APIキーは伏せています。zxyはサンプルURLのものです。)

#parameter
$year=2024
$pclass=1

#Tile
$z=13
$x=7312
$y=3008

#test command
curl.exe -H "Ocp-Apim-Subscription-Key:(APIキー)" "https://www.reinfolib.mlit.go.jp/ex-api/external/XPT002?response_format=geojson&z=$z&x=$y&y=$z&year=$year&priceClassification=$pclass" -o price0-$z-$x-$y.geojson

image.png
ダウンロード等はできそうです。

Step1: GeoJSONのダウンロード

まず、GeoJSON形式でデータをダウンロードしてみます。指定したz-x-y(9-454-201)の区画に含まれるzxyタイルについてリストし、それぞれをcurl.exeでダウンロードしていきます。

#parameter
$year=2023
$pclass=1

# 範囲の指定(ダウンロード対象のズームレベルと、対象とするタイル範囲)
$zmin=13
$zmax=13
$z0=9
$x0=454
$y0=201

# ダウンロード
$pclass=0
for($z=$zmin; $z -lt $zmax+1; $z++){
  $width=[Math]::Pow(2, $z-$z0)
  for($x=$x0*$width; $x -lt $x0*$width+$width; $x++){
    for($y=$y0*$width; $y -lt $y0*$width+$width; $y++){
      #Write-Host $z-$x-$y
      curl.exe -H "Ocp-Apim-Subscription-Key:(APIキー)" "https://www.reinfolib.mlit.go.jp/ex-api/external/XPT002?response_format=geojson&z=$z&x=$x&y=$y&year=$year&priceClassification=$pclass" -o price0-$z-$x-$y.geojson
    }
  }
}

$pclass=1
for($z=$zmin; $z -lt $zmax+1; $z++){
  $width=[Math]::Pow(2, $z-$z0)
  for($x=$x0*$width; $x -lt $x0*$width+$width; $x++){
    for($y=$y0*$width; $y -lt $y0*$width+$width; $y++){
      #Write-Host $z-$x-$y
      curl.exe -H "Ocp-Apim-Subscription-Key:(APIキー)" "https://www.reinfolib.mlit.go.jp/ex-api/external/XPT002?response_format=geojson&z=$z&x=$x&y=$y&year=$year&priceClassification=$pclass" -o price0-$z-$x-$y.geojson
    }
  }
}

以下の感じでダウンロードできます。データがないところもサイズは1kbになっていました。
image.png

地物のところは空ですが、ほかのところは記載があります。
image.png

GeoJSONだとダウンロードした後に空かどうかを判断してマージ等をしないといけないので、直接バイナリーのタイル(PBF)をダウンロードすることにします。

Step1’: PBFファイルのダウンロード

PBFファイル形式でダウンロードします。フォルダ構造の中(z/x/y)に入れていく必要があるので、フォルダの作成も併せて行います。

#parameter
$year=2023
$pclass=1

#Extent
$zmin=13
$zmax=15
$z0=9
$x0=454
$y0=201

#PBFのダウンロード
for($z=$zmin; $z -lt $zmax+1; $z++){
  Write-Host $z
  $width=[Math]::Pow(2, $z-$z0)
  for($x=$x0*$width; $x -lt $x0*$width+$width; $x++){
    for($y=$y0*$width; $y -lt $y0*$width+$width; $y++){
      #Write-Host $z-$x-$y
      if(-not (Test-Path $pclass-$year)){mkdir $pclass-$year}
      if(-not (Test-Path $pclass-$year/$z)){mkdir $pclass-$year/$z}
      if(-not (Test-Path $pclass-$year/$z/$x)){mkdir $pclass-$year/$z/$x}
      curl.exe -H "Ocp-Apim-Subscription-Key:(APIKey)" "https://www.reinfolib.mlit.go.jp/ex-api/external/XPT002?response_format=pbf&z=$z&x=$x&y=$y&year=$year&priceClassification=$pclass" -o $pclass-$year/$z/$x/$y.pbf
    }
  }
}

$pclass=0
for($z=$zmin; $z -lt $zmax+1; $z++){
  Write-Host $z
  $width=[Math]::Pow(2, $z-$z0)
  for($x=$x0*$width; $x -lt $x0*$width+$width; $x++){
    for($y=$y0*$width; $y -lt $y0*$width+$width; $y++){
      #Write-Host $z-$x-$y
      if(-not (Test-Path $pclass-$year)){mkdir $pclass-$year}
      if(-not (Test-Path $pclass-$year/$z)){mkdir $pclass-$year/$z}
      if(-not (Test-Path $pclass-$year/$z/$x)){mkdir $pclass-$year/$z/$x}
      curl.exe -H "Ocp-Apim-Subscription-Key:(APIKey)" "https://www.reinfolib.mlit.go.jp/ex-api/external/XPT002?response_format=pbf&z=$z&x=$x&y=$y&year=$year&priceClassification=$pclass" -o $pclass-$year/$z/$x/$y.pbf
    }
  }
}

#長さ0のタイルを削除する
$tiles = Get-Childitem -Path */*/*/*.pbf
foreach($tile in $tiles){
  if( $tile.length -eq 0){ rm $tile.PSPath} 
}

実行するとダウンロードが始まります。PowerShellなので同期処理となっておりすこし時間がかかります。先方サーバーへの負荷を考えると、同期処理のままでよいと思います。

結果(PBF)

pclass(0と1)ごとに2つのフォルダを作り、その中にpbfファイルが入っています。
image.png
image.png

作業しての感想ですが、ZL15のタイルまでとるとタイル量がおおくなるので、ZLは13だけとか14までとかでもいいかもしれません。

まとめ

今回は不動産情報ライブラリAPIを使ってデータを取得する方法を試しました。無事にPBFファイルをダウンロードすることができました。

今後、ダウンロードしたPBFファイルを、tile-joinなどを使ってPMTilesにすれば容量も多少減るでしょうし、ファイル数も減らすことができると思います。ウェブ上でのホスト方法やウェブ地図の作成も順次進めていきたいと思います。

なお、今回の作業で利用したデータの出典は「国土交通省 不動産情報ライブラリ」です。

参考

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?