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?

GeojsonからKML変換

Posted at

GeoJSONファイルから特定地域のデータを抽出し、KML形式で保存するPythonスクリプト

はじめに

このスクリプトは、GeoJSONファイルから特定の地域のデータを抽出し、新しい属性を追加した上でKML形式のファイルとして保存します。地理空間データの加工と変換に活用できます。

必要なライブラリ

import geopandas as gpd
import os
import numpy as np

GeoJSONファイルの読み込み

file_path = r"データフォルダ\土砂災害警戒区域データ.geojson"
gdf = gpd.read_file(file_path)

データの加工

特定の条件に基づいて新しい属性を追加します。

gdf["name"] = np.where(
gdf["属性A"].isin([1, 3]), "yellow",
np.where(gdf["属性A"].isin([2, 4]), "red", "不明")
)

出力先フォルダの設定と作成

output_folder = r"出力先フォルダ\filtered_kml"
if not os.path.exists(output_folder):
os.makedirs(output_folder)

抽出する地域名の設定

regions = ["地域A", "地域B", "地域C", "地域D", "地域E", "地域F", "地域G", "地域H", "地域I"]

データの抽出とKML形式での保存

for region in regions:
filtered_gdf = gdf[gdf['地域カラム'].str.contains(region, na=False)]

if not filtered_gdf.empty:
output_file = f"{output_folder}\{region}.kml"
filtered_gdf.to_file(output_file, driver="KML")
print(f"{region} のデータをKMLで保存しました: {output_file}")
else:
print(f"{region} のデータはありませんでした。")

ポイント

  • 具体的な地域名やパス、属性名は実際の環境とデータに合わせて調整してください
  • 新しい属性の追加ロジックは、データの内容に応じて適切に設定してください
  • KML形式での保存には、geopandasの機能を利用しています

注意事項

  • 事前にgeopandas, numpy, osのインストールが必要です
  • 大容量データの処理には時間がかかる可能性があります
  • データの著作権や利用規約を必ず確認してください
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?