LoginSignup
2
2

More than 5 years have passed since last update.

python で GPXファイルのデータ変換(time補間)

Posted at

目的

GPXファイル(trk:トラックポイント)の時間データを変換する
 - timeに無効値が入り、Google Earth Proがフリーズする場合があるので補間した
 - 無効値例:2000-00-00T00:00:00Z (gpxgyでパースすると Noneになる)

環境:
* Windows10 Home 64bit
* Anaconda3-5.2.0-Windows-x86_64 / Python 3.6 version
* gpxpy 1.3.4

【準備】gpxpyインストール

1. Anaconda Navigator上で [Environments]→[base(root)] →[Open Terminal]

2. インストール
  pip install gpxpy
   →"distributed 1.21.8 requires msgpack, which is not installed."と警告がでるのでmsgpackも
  pip install msgpack

【プログラム例】

変換前ファイル(sample_timeloss.gpx)にtime補正をして、変換後ファイル(sample_timefix.gpx)に出力する

import gpxpy
import gpxpy.gpx
from datetime import datetime, timedelta

gpx_file_r = open('sample_timeloss.gpx', 'r')
gpx_file_w = open('sample_timefix.gpx', 'w')

# Parsing an gpx file():
gpx = gpxpy.parse(gpx_file_r)

time_set = datetime(2018,1,1,0,0)   # (1)initial date
gps_delta = timedelta(seconds=1)    # (2)gps period: 1s

for track in gpx.tracks:
    for segment in track.segments:
        for point in segment.points:
            if point.time == None:               # (3) invalid time
                time_set = time_set + gps_delta
                point.time = time_set
            else:                                # (4) valid time
                  time_set = point.time  
#            print('Point at ({0},{1}) -> {2}'.format(point.latitude, point.longitude, point.time))

gpx_file_w.write(gpx.to_xml())
gpx_file_r.close()
gpx_file_w.close()

(1) GPXファイルの先頭でtimeが無効値がだった場合の初期値
(2) ここではtrkpt間隔は1s
(3) timeが無効値(None)の場合は、time_setをtrkpt間隔分更新した値を設定
(4) 有効値はtime_setに保持する。無効値の変換用

【変換例】

左(変換前)→右(変換後)のようにtime無効値を補間できた
result.PNG
★ Google Earth Proもフリーズしなくなった

【参考文献】

【1】https://qiita.com/key/items/20f903a5ac66b6f09782
【2】Pythonではじめるデータラングリング ―データの入手、準備、分析、プレゼンテーション (Jacqueline Kazil)
【3】https://pypi.org/project/gpxpy/
【4】https://docs.python.jp/2/library/datetime.html

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