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?

More than 3 years have passed since last update.

【GIS】KMLファイルを出力する

Posted at

はじめに

KMLでデータを出力したくなってきたので、python でKMLを読み書きできるライブアリを調べていました。ところ、simplekmlが使えそうだったのでメモを書いておきます。

作業メモ

インストール

特に何事もなく。。

$python -m pip install simplekml
Collecting simplekml
  Downloading simplekml-1.3.5-py3-none-any.whl (65 kB)
     |████████████████████████████████| 65 kB 912 kB/s
Installing collected packages: simplekml
Successfully installed simplekml-1.3.5

緯度経度のある座標値を保存する

ファイルへの保存について確認していきます。以下に使い方は紹介されています。

3種類のデータを保存できるらしい。

  • Point
  • LineString
  • Polygon

についてデータを書けるようです。

点(NewPoint)を保存してみる

simplekmlのインスタンスを作り、newpoint を設定し、saveするだけ。

import simplekml
kml = simplekml.Kml()
kml.newpoint(name="Tokyo Station", coords=[(139.7673068,35.6809591,0)],description="station")
kml.newpoint(name="Kitte", coords=[(139.7647979003,35.679746926311,100)],description="building")
kml.save("tokyo.kml")

保存したtokyo.kmlはGoogle Earth で見ることができます。

ロードの仕方の解説がありました。

If you have a KML file from previous versions of Google Earth, you can view it in Google Earth for web browsers.

  1. Using the web browser on your computer, open Google Earth.
  1. On the left, click Projects Project.
  2. Click the New Project button.
  • To add a file directly to your computer, select Import KML File.
  • To add a file from your Google Drive or a shared file, select Import KML file from Drive.
  1. Select the KML file you'd like to view.
    The files will be automatically saved to your KML files within Projects.

ですが、これは見る角度によってKitteのピンが隠れてしまいました。どうしたら良いのだろう。。。

image.png

Getting Started に書かれているように LineString、Polygon も動きました。

LineStringの例:

lin = kml.newlinestring(name="Pathway", description="A pathway in Kirstenbosch",
                        coords=[(18.43312,-33.98924), (18.43224,-33.98914),
                                (18.43144,-33.98911), (18.43095,-33.98904)])

Polygon の例:

pol = kml.newpolygon(name="Atrium Garden",
                     outerboundaryis=[(18.43348,-33.98985), (18.43387,-33.99004),
                                      (18.43410,-33.98972), (18.43371,-33.98952),
                                      (18.43348,-33.98985)],
                     innerboundaryis=[(18.43360,-33.98982), (18.43386,-33.98995),
                                      (18.43401,-33.98974), (18.43376,-33.98962),
                                      (18.43360,-33.98982)])

PolygonもGoogle Earthの角度やズームの大きさで変わってくる感じ。

まとめ

simplekml はインストールとGoogle Earthで表示する点の座標な書けそう。明日、使うかな。。。
(2021/05/31 1:30)

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?