Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
Python
QGIS
Uncategorized

Convert geojson to gpx

Free Options for Converting Geojson to GPX

Converting GeoJSON data into GPX format involves transforming geographic data structured in the GeoJSON format, which is based on JSON, into the GPX (GPS Exchange Format), a lightweight XML data format designed for sharing GPS routes, waypoints, and tracks. To accomplish this conversion, you’ll typically use a script or a software tool that can parse the GeoJSON data and output it in the GPX structure.

Quickmaptools.com is by far the easiest way to convert between geojson and GPX online. But if you are looking for desktop options continue reading!

Convert geojson to gpx using python

Here’s a basic Python script that demonstrates how you might convert a simple GeoJSON containing Point features into a GPX file. This example uses the gpxpy library for creating GPX files, so you’ll need to install it first if you haven’t already (pip install gpxpy).

Please note that this script is a starting point and might need adjustments based on your specific GeoJSON structure, especially if it includes complex geometries like LineStrings (for tracks or routes) or MultiPoints.

import gpxpy
import gpxpy.gpx
import json

# Sample GeoJSON data
geojson_data = {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "Sample Point 1"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [ -122.42, 37.78 ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "Sample Point 2"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [ -122.43, 37.77 ]
      }
    }
  ]
}

# Create GPX file
gpx = gpxpy.gpx.GPX()

# Convert each GeoJSON feature into a GPX waypoint
for feature in geojson_data['features']:
    if feature['geometry']['type'] == 'Point':
        lon, lat = feature['geometry']['coordinates']
        name = feature['properties']['name'] if 'name' in feature['properties'] else None
        waypoint = gpxpy.gpx.GPXWaypoint(latitude=lat, longitude=lon, name=name)
        gpx.waypoints.append(waypoint)

# Generate GPX string
gpx_string = gpx.to_xml()
print(gpx_string)

# Optionally, save to a file
with open('output.gpx', 'w') as gpx_file:
    gpx_file.write(gpx_string)

This script:

  1. Creates a new GPX object.
  2. Iterates over each feature in the GeoJSON data.
  3. Checks if the feature is a Point (you’d need to add more logic for Lines or Polygons).
  4. Creates a GPX waypoint for each GeoJSON point, including coordinates and optionally a name.
  5. Converts the GPX object to an XML string and prints it.
  6. Optionally, the GPX data is saved to a file.

This is a straightforward conversion. If your GeoJSON includes lines (tracks or routes) or more complex features, you’ll need to add additional logic to handle those cases, possibly creating GPX tracks or routes instead of waypoints.

Convert GeoJSON to GPX format using GDAL

To convert GeoJSON to GPX format using GDAL (Geospatial Data Abstraction Library), you can use the ogr2ogr command-line utility, which is part of the GDAL suite. GDAL is a powerful library for reading, writing, and transforming geospatial data formats, and ogr2ogr is specifically designed for converting vector data between different formats.

Here’s a general command to convert a GeoJSON file to a GPX file using ogr2ogr:

ogr2ogr -f GPX output.gpx input.geojson

This command tells ogr2ogr to:

  • Convert to the GPX format (-f GPX).
  • Output the result to output.gpx.
  • Read from the input file input.geojson.

Details and Options

  • Specifying GPX elements: GPX format supports different types of data, such as waypoints, tracks, and routes. By default, ogr2ogr might convert all compatible features to waypoints. If you want to specify the type of GPX element for your features, you can use the -dsco option. For example, to create tracks, you can use:
  ogr2ogr -f GPX output.gpx input.geojson -dsco GPX_USE_EXTENSIONS=YES -nlt MULTILINESTRING
  • Character Encoding: GPX files are typically encoded in UTF-8. If your GeoJSON file uses a different encoding, you might need to specify the source encoding with the -lco ENCODING=UTF-8 option.
  • Filtering and Transformation: ogr2ogr supports a wide range of options for filtering (e.g., -where, -spat) and transforming (e.g., -t_srs) data during conversion.

Installation

If you don’t have GDAL installed:

Example Command in a Terminal

After installing GDAL, open your terminal or command prompt, navigate to the directory containing your GeoJSON file, and run the ogr2ogr command with the appropriate parameters for your file.

This approach using GDAL is particularly useful for batch processing or integrating into scripts for automated workflows.

Converting GeoJSON to GPX using QGIS

Converting GeoJSON to GPX in QGIS is a straightforward process thanks to QGIS’s extensive support for various geographic data formats. Here’s how to do it:

1. Open QGIS

If you haven’t already, download and install QGIS from the official QGIS website. Once installed, open QGIS.

2. Load Your GeoJSON File

  • Open the Data: Go to Layer > Add Layer > Add Vector Layer... or click on the “Open Data Source Manager” button in the toolbar.
  • Browse for your GeoJSON File: In the Data Source Manager, select the Vector tab if it’s not already selected. Click on the ... button to browse for your GeoJSON file. Select your file and click Open.
  • Add the File to Your Project: After selecting your file, it should appear in the “Vector Dataset(s)” box. Click Add and then Close the Data Source Manager. Your GeoJSON data will now be visible on the map.

3. Convert GeoJSON to GPX

  • Right-click on the Layer: In the Layers panel, right-click on your GeoJSON layer.
  • Export: Choose Export > Save Features As....
  • Configure the Export Settings:
    • Format: From the “Format” dropdown menu, select GPX.
    • File Name and Location: Click on the ... button to choose where to save your GPX file and what to name it.
    • GPX Use: GPX files differentiate between waypoints, routes, and tracks. Choose the appropriate option for your data in the “GPX_USE” field. If your GeoJSON represents points, you might select waypoints. For line strings, choose routes or tracks as applicable.
    • CRS: Ensure the CRS (Coordinate Reference System) is set correctly. GPX files traditionally use WGS84 (EPSG:4326).
    • Other Options: You may configure other options as needed, but the defaults are usually sufficient for a basic conversion.
  • Export: Click OK to export your file. You might receive a prompt asking you to select the feature encoding. UTF-8 is a safe choice for most applications.

4. Check Your GPX File

After exporting, it’s a good idea to load your GPX file back into QGIS or another GIS tool to ensure it looks correct. You can do this in QGIS by using the “Add Vector Layer” tool again, but this time selecting your newly created GPX file.

Additional Tips

  • Attribute Data: GPX is a simpler format than GeoJSON and might not support all the attributes (properties) your GeoJSON file has. It’s focused on geographical features like location, elevation, and time. If you have complex attributes, you might need to find alternative ways to preserve this information, such as encoding it in the name or description fields.
  • Complex Geometries: If your GeoJSON contains complex geometries that do not directly translate to waypoints, routes, or tracks, you may need to preprocess or simplify your data before exporting.

Using QGIS for this conversion provides a GUI-based approach that can be easier for those not familiar with scripting or command-line tools.

frequently asked questions (FAQs) related to this conversion:

1. What is the difference between GeoJSON and GPX formats?

2. Can I convert GeoJSON to GPX without losing data?

  • The conversion might result in some data loss, especially for attributes that don’t have direct equivalents in GPX format. GPX primarily focuses on spatial elements (like waypoints, tracks, and routes) and some basic metadata (like names and descriptions), whereas GeoJSON can contain a wide range of attributes.

3. Are there online tools available for converting GeoJSON to GPX?

  • Yes, several online tools and services can convert GeoJSON to GPX. However, the capabilities and limitations of these tools can vary, especially concerning the size of the GeoJSON file and the retention of attribute data.

4. How can I convert GeoJSON to GPX using command-line tools?

  • Tools like GDAL (Geospatial Data Abstraction Library) can be used to convert GeoJSON to GPX via command line. The ogr2ogr utility, which comes with GDAL, is particularly useful for this.

5. Can I perform this conversion in programming languages like Python?

  • Yes, there are libraries in Python (e.g., gpxpy for GPX and geopandas or json for GeoJSON) that allow for the parsing and conversion of geospatial data between these formats.

6. How do I choose between waypoints, tracks, and routes when converting to GPX?

  • Waypoints are individual points that mark specific locations.
  • Tracks are ordered lists of points that describe a path.
  • Routes are similar to tracks but are intended for navigation between waypoints.
    The choice depends on the nature of your GeoJSON data and how you intend to use the GPX file.

7. Can I convert complex GeoJSON geometries (like polygons) to GPX?

  • GPX does not natively support polygons. Complex geometries might need to be simplified or converted into a series of waypoints, tracks, or routes depending on the use case.

8. What tools support converting GeoJSON to GPX?

  • Besides online converters and command-line tools like GDAL, desktop GIS software such as QGIS can convert GeoJSON files to GPX format through its GUI, offering a more user-friendly approach for those not comfortable with command-line interfaces.

9. Is it possible to automate the conversion process?

  • Yes, by using scripts in programming languages like Python, or batch processing in command-line utilities, the conversion process can be automated, which is particularly useful for handling large datasets or multiple files.

10. Where can I find documentation or resources to help with the conversion?

  • Official documentation for tools like GDAL, QGIS, and libraries like gpxpy provide valuable resources. Additionally, GIS forums, Stack Overflow, and official documentation for the specific tools or libraries you’re using are excellent places to find help and examples.

About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.