これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。
HTML ファイル生成日時: 2025/12/21 21:44:02.661 (台灣標準時)
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
|
|---|
|
|