LoginSignup
23
22

More than 5 years have passed since last update.

[GPS]Pythonでkmlファイルを作成する

Posted at

概要

Pythonを使って、三次元地理空間情報であるkmlファイルを作成し、Google Earthで表示する

事前準備

simplekmlが必要なので、OSに合わせてsimplekmlのライブラリをインストールする

目的

緯度、経度、高度をまとめたcsvファイルからkmlファイルを作成することに。xmlベースなので、ElementTreeでも良かったんだが、調べてみたらkmlを作れるsimplekmlがあるので、simplekmlを使ってkmlファイルを作成する。

ソース

※csv読み込みの部分はサンプル用配列に置き換えました。

gpsdata_to_kml.py
#!/usr/bin/python2.7
#-*- coding: utf-8 -*-
import simplekml

# ポイント形式で表示
## [場所名, 経度, 緯度]
sample_points = [["東京駅", 139.766389, 35.681340],
                 ["有楽町駅", 139.763360, 35.675056],
                 ["新宿駅", 139.700432, 35.690938],
                 ["池袋駅", 139.711570, 35.730235],
                 ["秋葉原駅", 139.774091, 35.698704],
                 ["上野駅", 139.777195, 35.713714]]


kml = simplekml.Kml()
for point in sample_points:
    kml.newpoint(name=unicode(point[0], 'utf-8'), coords=[(point[1], point[2])])

kml.save('yamanote_line.kml')

# 高度を含めてLINESTRING形式で表示
## [物体名, 経度, 緯度, 高度, 色]
sample_linestrings = [["スカイツリー", 139.810657, 35.710089, 634.000000, simplekml.Color.grey],
                      ["○ンダム", 139.810557, 35.710089, 18.000000 , simplekml.Color.white],
                      ["○ルトラマン", 139.810657, 35.709989, 40.000000, simplekml.Color.red],
                      ["○イターン3", 139.810757, 35.710089, 120.000000, simplekml.Color.yellow],
                      ["○ンバスター", 139.810657, 35.710189, 200.000000, simplekml.Color.black]]

kml = simplekml.Kml()

for linestring in sample_linestrings:
    ls = kml.newlinestring(name=unicode(linestring[0], 'utf-8'))
    ls.style.linestyle.color = linestring[4]
    ls.style.linestyle.width = 8
    ls.extrude = 1
    ls.altitudemode = simplekml.AltitudeMode.absolute
    ls.coords = [(float(linestring[1]), float(linestring[2]), float(linestring[3]))]

kml.save('skytree.kml')

Google Earthで出力

ポイント形式で表示

img_1.jpg

LINESTRING形式で表示

img_2.jpg

23
22
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
23
22