How to manipulate gpx files using python

convert to/from gpx files to/from different formats using python

Nidhaloff
3 min readJun 23, 2020

Introduction

GPX, or GPS Exchange Format, is an XML schema designed as a common GPS data format for software applications. It can be used to describe waypoints, tracks, and routes.

For further analysis, it can be tedious to work with gpx. Therefore, it is often converted to other suitable formats for analysis like csv or excel sheets. This is mostly useful in data science, machine learning and autonomous driving applications.

Goal

In this article, we will explore different ways to convert gpx files to other different formats like csv, json, excel sheets and dataframes. Likewise, the conversion can take place in the other direction, where the user may have a csv file composed of coordinates (longitudes and latitudes) and the goal is to convert it to a gpx file.

Tool

I worked much with gpx files as a software engineer and sometimes it took a lot of boilerplate code to convert gpx to other formats for further analysis. Therefore, I decided back then to create gpx_converter.

gpx_converter is a python package, which allows developers to manipulate gpx file very easily. here is a link to the docs.

The following code snippets demonstrate how to use the gpx_converter package.

Installation

First of all, we will use pip to install the package

pip install gpx-converter

GPX to CSV

from gpx_converter import ConverterConverter(input_file=your_input_file).gpx_to_csv(output_file=your_output_file)

So what’s happening here?

First we import the Convert class from gpx_converter. Then, we give the path to the input file as an argument to the Converter.

Finally, we use the gpx_to_csv method to convert the gpx file to csv, where the desired output file is given as an argument to the method.

GPX to Excel

Similarly, the following code is used to convert gpx to an excel sheet

from gpx_converter import ConverterConverter(input_file=your_input_file).gpx_to_excel(output_file=your_output_file)

GPX to Dataframe

from gpx_converter import Converterdf = Converter(input_file=your_input_file).gpx_to_pandas_dataframe()

GPX to JSON

from gpx_converter import ConverterConverter(input_file=your_input_file).gpx_to_json(output_file=your_output_file)

CSV to GPX

Alternatively, gpx_converter allows to convert other formats to gpx. For instance, from csv to gpx

from gpx_converter import ConverterConverter(input_file=your_input_file).csv_to_gpx(lats_colname=column_name_of_latitudes,                                                      longs_colname=column_name_of_longitudes,                                                   output_file=your_output_file)
  • lats_colname: refers to the column name of the latitudes values in the csv file
  • longs_colname: refers to the column name of the longitudes values in the csv file
  • output_file: path where the converted file will be stored

Automating the conversion of multiple files

Furthermore, if we have many csv files in a directory and the goal is to convert every single file to gpx. It would be helpful to automate the process. The following code demonstrates how to do this with gpx_converter

from gpx_converter import ConverterConverter.convert_multi_csv_to_gpx(dirpath='your_directory/')

Conclusion

Manipulating gpx files is a daily routine for developers, especially in the autonomous driving field. The gpx_converter package provides a simple API for this task. here is the link to the github repository.

Feel free to contribute to the project or give me feedback :)

--

--

Nidhaloff
Nidhaloff

Written by Nidhaloff

Software Engineer | machine learning | IoT | Open Source ❤

No responses yet