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?

フォルダ内のLASファイル(点群データ)をすべて軽量化する

Posted at

はじめに

 点群データをオープンデータとして公開する際に一番問題となるのでデータのファイルサイズです。
特にオリジナルデータ(建物や樹木などを含んだデータ)は点数も多く1ファイルで数GBになるファイルもたくさんあります。それが数百ファイルもあるので、ZIPに圧縮してもとてもファイルサーバーに置くことはできません。
 そこで、一つの解決策として、点の数を減らして、ファイルサイズを小さくする方法があります。当然元のデータよりも精度は落ちますが、データが公開されないよりは、精度が多少悪くても公開されている方が嬉しい方も多いと思います。本当の制度のファイルがほしければ、取得した部署に問い合わせて入手するばいいのですから。
 この記事では、点群データ処理ツールであるPDALとPythonを使用して、フォルダ内に保存されている複数のLASファイルを一括で処理する方法を記載します。
 なお、この方法はChatGPTの助けも借りています。
 著者のPC環境はWindows11です。

【準備】PDALのインストール

PDALのインストールは次の記事で説明していますので、そちらを参照してインストールしてください。
Windows環境で点群処理ツールPDALをインストールする

一括変換処理を行うPythonファイルを作成する

 はじめに一括処理するPythonファイルと設定用のJSONファイルを作成します。
 設定用のJSONファイルは次のように作成します。
テキストエディタに下のコードをコピペして、「pipeline.json」というファイル名で保存します。

pipeline.json
{
   "pipeline": [
       {
           "type": "readers.las",
           "filename": "INPUT_FILE"
       },
       {
           "type": "filters.decimation",
           "step": 2
       },
       {
           "type": "writers.las",
           "filename": "OUTPUT_FILE"
       }
   ]
}

このJSONファイルは、"type": "readers.las"でファイルを読み込み、"type": "filters.decimation"で点群を間引きして、"type": "writers.las"で保存します。
"filename""step"は次に作成するPythonファイルを実行すると、ファイルごとに書き換わります。

 次に、一括変換処理を行うPythonファイルを作成します。
 テキストエディタに下のコードをコピペして、「process_las.py」というファイル名で保存します。

process_las.py
import os
import subprocess
import json

def process_las_files_with_decimation(step):
   """
   スクリプトと同じフォルダ内のLASファイルにfilters.decimationを適用。
   出力は "output" フォルダに保存し、ファイル名に間引き間隔を付加。

   :param step: decimationの間引き間隔
   """
   # スクリプトのあるディレクトリを取得
   script_dir = os.path.dirname(os.path.abspath(__file__))
   input_folder = script_dir  # 入力フォルダはスクリプトのある場所
   output_folder = os.path.join(script_dir, "output")  # 出力フォルダは "output"
   os.makedirs(output_folder, exist_ok=True)  # 出力フォルダを作成

   # 入力フォルダ内のすべてのLASファイルを取得
   las_files = [f for f in os.listdir(input_folder) if f.lower().endswith('.las')]

   for las_file in las_files:
       input_path = os.path.join(input_folder, las_file)
       # ファイル名に間引き間隔を付加した出力ファイル名を作成
       base_name, ext = os.path.splitext(las_file)
       output_file_name = f"{base_name}_{step}{ext}"
       output_path = os.path.join(output_folder, output_file_name)

       # JSON構成の作成
       pipeline = {
           "pipeline": [
               {"type": "readers.las", "filename": input_path},
               {"type": "filters.decimation", "step": step},
               {"type": "writers.las", "filename": output_path}
           ]
       }
       json_file = os.path.join(script_dir, "pipeline.json")
       with open(json_file, "w") as f:
           json.dump(pipeline, f, indent=4)

       # PDALコマンドの実行
       print(f"Processing {las_file}...")
       pdal_command = ["pdal", "pipeline", json_file]
       result = subprocess.run(pdal_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

       if result.returncode == 0:
           print(f"Successfully processed: {las_file} -> {output_file_name}")
       else:
           print(f"Error processing {las_file}:\n{result.stderr.decode()}")


# 使用例
if __name__ == "__main__":
   step = 10  # decimationの間引き間隔
   process_las_files_with_decimation(step)

※あらかじめ、間引き間隔「step = 」の数値を設定しておきましょう。「10」だと点の数を10分の1にします。ファイルサイズも10分の1程度になります。

一括処理したいフォルダに実行ファイルをコピーする

 上で作成した「pipeline.json」と「process_las.py」を変換したいLASファイルが保存されているフォルダにコピーします。
image.png

Anaconda Promptを起動して、ファイルを変換する

 スタートメニューから「Anaconda Prompt」を起動します。
プロンプトにconda activate myenvと入力するとPDALが利用できるようになります。
image.png

 CDで変換したいLASファイルが保存されているフォルダに移動します。
image.png

プロンプトにpython process_las.pyと入力して実行すると、変換処理が開始されます。
image.png

変換されたファイルは、同じフォルダに「Output」というフォルダが作成されて保存されます。
ファイル名には、間引き間隔の数値が付加されます。

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?