Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2025/12/21 21:44:02.661 (台灣標準時)

GPX ファイルを読み込んで経度緯度標高を抽出する方法

Python で GPX ファイルを読み込み、経度・緯度・標高を抽出する方法を調べ たので、それについて記録しておくでござる。 gpxpy モジュールを使えば 簡単でござる。

試しに作ってみたスクリプトは以下の通りでござる。


#!/usr/pkg/bin/python3

#
# Time-stamp: <2025/12/20 21:47:37 (UT+08:00) daisuke>
#

# importing argparse module
import argparse

# importing sys module
import sys

# importing pathlib module
import pathlib

# importing gpxpy module
import gpxpy
import gpxpy.gpx

# importing numpy module
import numpy

# importing datetime module
import datetime

# initialising a parser
parser = argparse.ArgumentParser (description='reading GPX file')

# adding arguments
parser.add_argument ('-o', '--output', default='out.txt', help='output file')
parser.add_argument ('files', nargs='+', help='input GPX files')

# parsing arguments
args = parser.parse_args ()

# input parameters
list_input_files = args.files
file_output      = args.output

# making an empty list for storing data
list_gpx_files = []

# making pathlib object
path_output = pathlib.Path (file_output)

# existing check of output file
if (path_output.exists ()):
    # printing message
    print (f'#')
    print (f'# ERROR: output file \"{file_output}\" DOES exist!')
    print (f'# ERROR: stopping the script...')
    print (f'#')
    # stopping the script
    sys.exit (0)

# date and time now
now_utc = datetime.datetime.now (datetime.timezone.utc)

# processing GPX files one-by-one
for file_gpx in list_input_files:
    # making pathlib object
    path_gpx = pathlib.Path (file_gpx)
    # if the file is NOT a GPX file, then skip
    if (path_gpx.suffix != '.gpx'):
        # printing message
        print (f'#')
        print (f'# WARNING: the file \"{file_gpx}\" is NOT a GPX file!')
        print (f'# WARNING: skipping the file \"{file_gpx}\"...')
        print (f'#')
        # skipping the file
        continue
    # if the file does not exist, the skip
    if not (path_gpx.exists ()):
        # printing message
        print (f'#')
        print (f'# WARNING: the file \"{file_gpx}\" does NOT exist!')
        print (f'# WARNING: skipping the file \"{file_gpx}\"...')
        print (f'#')
        # skipping the file
        continue
    # appending file name into list
    list_gpx_files.append (file_gpx)
    # opening GPX file
    with open (file_gpx, 'r') as fh_in:
        # reading data from GPX file
        data_gpx = gpxpy.parse (fh_in)
    # making empty lists for storing data
    list_longitude = []
    list_latitude  = []
    list_elevation = []
    # extracting longitude, latitude, and elevation
    for track in data_gpx.tracks:
        for segment in track.segments:
            for point in segment.points:
                list_longitude.append (point.longitude)
                list_latitude.append (point.latitude)
                list_elevation.append (point.elevation)
    # making numpy arrays
    array_longitude = numpy.array (list_longitude)
    array_latitude  = numpy.array (list_latitude)
    array_elevation = numpy.array (list_elevation)
    # opening output file for writing
    with open (file_output, 'w') as fh_out:
        # writing header into output file
        fh_out.write (f'#\n')
        fh_out.write (f'# Geographical data extracted from GPX file\n')
        fh_out.write (f'#\n')
        fh_out.write (f'#  GPX file(s):\n')
        for file_gpx in list_gpx_files:
            fh_out.write (f'#   {file_gpx}\n')
        fh_out.write (f'#\n')
        fh_out.write (f'#  Date/time now in UTC:\n')
        fh_out.write (f'#   {now_utc}\n')
        fh_out.write (f'#\n')
        fh_out.write (f'#  Format of this file:\n')
        fh_out.write (f'#   first column  : longitude in degree\n')
        fh_out.write (f'#   second column : latitude in degree\n')
        fh_out.write (f'#   third column  : elevation in metre\n')
        fh_out.write (f'#\n')
        # writing data into output file
        for i in range (len (array_longitude)):
            # longitude, latitude, and elevation
            longitude = array_longitude[i]
            latitude  = array_latitude[i]
            elevation = array_elevation[i]
            # writing data into output file
            fh_out.write (f'{longitude:+13.8f}' \
                          + f'  {latitude:+13.8f}' \
                          + f'  {elevation:8.3f}\n')

実行してみた結果は、以下の通りでござる。


% python3 test_gpxpy_01.py -h
usage: test_gpxpy_01.py [-h] [-o OUTPUT] files [files ...]

reading GPX file

positional arguments:
  files                input GPX files

options:
  -h, --help           show this help message and exit
  -o, --output OUTPUT  output file

% python3 test_gpxpy_01.py -o luofu.txt luofu.gpx
% head -20 luofu.txt 
#
# Geographical data extracted from GPX file
#
#  GPX file(s):
#   luofu.gpx
#
#  Date/time now in UTC:
#   2025-12-20 13:47:43.625907+00:00
#
#  Format of this file:
#   first column  : longitude in degree
#   second column : latitude in degree
#   third column  : elevation in metre
#
+121.36867315   +24.79438984   308.400
+121.36869418   +24.79436729   308.800
+121.36869553   +24.79437995   309.000
+121.36869175   +24.79438162   309.200
+121.36869117   +24.79438758   308.600
+121.36868245   +24.79438741   308.400

fig_202512/www_gpxpy_00.png
fig_202512/www_gpxpy_01.png
fig_202512/www_gpxpy_02.png


Frequently accessed files

  1. Misc___Taiwan/20240207_00.html
  2. Computer___TeX/20231107_00.html
  3. Computer___TeX/20240410_00.html
  4. Misc___Taiwan/20240819_00.html
  5. Book___Chinese/20240424_00.html
  6. Computer___TeX/20230726_01.html
  7. Computer___TeX/20240411_00.html
  8. Misc___Japan/20240718_00.html
  9. Computer___Python/20250330_00.html
  10. Misc___Taiwan/20240903_01.html
  11. Computer___NetBSD/20250301_01.html
  12. Computer___Network/20230516_00.html
  13. Computer___TeX/20240414_00.html
  14. Computer___NetBSD/20230119_00.html
  15. Computer___TeX/20240414_01.html
  16. Computer___Network/20240130_00.html
  17. Computer___FreeBSD/20220621_0.html
  18. Computer___Network/20241214_00.html
  19. Computer___Python/20220518_0.html
  20. Computer___Python/20220715_0.html
  21. Computer___NetBSD/20250409_00.html
  22. Computer___NetBSD/20240810_00.html
  23. Computer___NetBSD/20250113_00.html
  24. Computer___Python/20240101_00.html
  25. Computer___Debian/20230102_00.html
  26. Computer___NetBSD/20240805_03.html
  27. Computer___NetBSD/20241102_00.html
  28. Computer___Network/20220413_1.html
  29. Misc___Japan/20240610_00.html
  30. Computer___Hardware/20240820_00.html


HTML file generated by Kinoshita Daisuke.