5 Best Ways to Convert CSV to GPX in Python

πŸ’‘ Problem Formulation: Converting data from a CSV file to GPX format is a common task for developers working with geospatial data, particularly in areas like GIS and fitness app development. The input typically includes coordinates (latitude and longitude) and other optional information (like elevation or timestamps), and the desired output is a GPX file that could be used in mapping software or GPS devices.

Method 1: Using pandas and gpxpy

This method involves the use of `pandas` for data manipulation and `gpxpy` for creating the GPX file. First, read the CSV file with pandas, then loop through the rows to create GPX points, and finally, generate a GPX format file using `gpxpy`.

Here’s an example:

import pandas as pd
import gpxpy
import gpxpy.gpx

# Read the CSV file
data = pd.read_csv('data.csv')

# Create a new GPX object
gpx = gpxpy.gpx.GPX()

# Create GPX track
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)

# Create GPX segment
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)

# Iterate over the dataframe and create GPX track points
for index, row in data.iterrows():
    gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(row['latitude'], row['longitude'], elevation=row['elevation']))

# Write to a file
filename = "output.gpx"
with open(filename, "w") as file:
    file.write(gpx.to_xml())

Output: A GPX file named ‘output.gpx’.

This snipped reads a CSV file containing latitude, longitude, and elevation columns using pandas. A GPX file structure is generated by creating a GPX object and populating it with track points from the CSV, then written out to an XML format that conforms to the GPX schema.

Method 2: Using xml.etree.ElementTree

The `xml.etree.ElementTree` module can be used to build the GPX XML structure manually after reading the CSV with built-in `csv` library. This method requires a better understanding of GPX schema and XML but is more customizable.

Here’s an example:

import csv
import xml.etree.ElementTree as ET

# Create the root element
gpx = ET.Element('gpx', version="1.1", creator="csv_to_gpx.py")

# Open the CSV file and read rows
with open('data.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        # Create a new 'trkpt' element for each row
        trkpt = ET.SubElement(gpx, 'trkpt', lat=str(row['latitude']), lon=str(row['longitude']))
        ele = ET.SubElement(trkpt, 'ele')
        ele.text = str(row['elevation'])

# Generate the tree and write to file
tree = ET.ElementTree(gpx)
tree.write('output.gpx')

Output: A GPX file named ‘output.gpx’.

This code snippet creates an XML tree structure using Python’s `xml.etree.ElementTree` module, representing GPX format with track points (`trkpt`) including their latitude, longitude, and elevation (if available). It reads each row from the CSV file and adds corresponding elements to the tree, which is then saved as a GPX file.

Method 3: Using simplekml for KML Conversion

Although not directly producing a GPX file, `simplekml` can be used to convert CSV to KML, which can afterwards be easily converted to GPX using various online tools or software. This method is particularly useful if KML is also an acceptable format for your application.

Here’s an example:

import simplekml
import csv

kml = simplekml.Kml()

with open("data.csv", "r") as csvfile:
    reader = csv.reader(csvfile)
    next(reader)  # Skip header line
    for row in reader:
        kml.newpoint(coords=[(row[1], row[0])]) # longitude, latitude

kml.save("output.kml")

Output: A KML file named ‘output.kml’.

In this snippet, the `simplekml` library is utilized to create a KML file. It reads the longitude and latitude values from the CSV file and creates KML points which are then saved to an output file. The KML file can be converted to GPX through various third-party tools or software if necessary.

Method 4: Creating a Custom Python Script

For full control over the conversion process, writing a custom Python script to parse the CSV and construct the GPX file as text may be the best approach. The advantage of this method is that no external libraries are needed.

Here’s an example:

def csv_to_gpx(input_csv, output_gpx):
    with open(input_csv, 'r') as csvfile, open(output_gpx, 'w') as gpxfile:
        gpxfile.write('\n')
        gpxfile.write('\n')

        reader = csv.reader(csvfile)
        next(reader)  # Skip header
        for row in reader:
            gpxfile.write(f'  \n')
            gpxfile.write(f'    {row[2]}\n')
            gpxfile.write('  \n')

        gpxfile.write('')

csv_to_gpx('data.csv', 'output.gpx')

Output: A GPX file named ‘output.gpx’.

By creating a function `csv_to_gpx`, this code reads a CSV file line by line and writes a GPX-formatted XML file directly. It utilizes the Python standard `csv` module for reading and writes the GPX file using basic file operations, without external library dependencies.

Bonus One-Liner Method 5: Using `awk` in Command Line

For those comfortable with shell scripting, `awk` can provide a one-liner solution that runs outside of Python to convert CSV to GPX.

Here’s an example:

awk -F',' 'BEGIN {print "\n"} {print "" $3 ""} END {print ""}' data.csv > output.gpx

Output: A GPX file named ‘output.gpx’.

This one-liner uses `awk`, a powerful text-processing programming language, to interpret the CSV file’s contents and transform them into a GPX file structure which is then redirected to ‘output.gpx’. It assumes the CSV file uses commas to separate the columns and that latitude, longitude, and elevation are in the first three columns, respectively.

Summary/Discussion

  • Method 1: Using pandas and gpxpy. While this method harnesses the data processing power of pandas and the specialization of gpxpy, it requires the installation of these external libraries and may be overkill for simple conversions.
  • Method 2: Using xml.etree.ElementTree. This method is more versatile and doesn’t rely on third-party libraries; however, it is lower-level and requires a solid understanding of XML.
  • Method 3: Using simplekml for KML Conversion. This indirect approach can be useful but requires a secondary step of converting KML to GPX. It’s good for workflows already incorporating KML files.
  • Method 4: Creating a Custom Python Script. Highly customizable and with no dependencies, this method is robust but might be prone to errors if not implemented with a thorough understanding of the GPX schema.
  • Bonus Method 5: Using `awk` in Command Line. Ideal for quick conversions on UNIX-like systems; however, it requires shell access and familiarity with command-line tools.