Skip to main content

Transforming GPX geolocation data to GeoJSON

· 3 min read
Marcelo Malacalza
Marcelo Malacalza
Fullstack Developer

Have you ever found yourself needing to convert GPX (GPS Exchange Format) files to GeoJSON in your map and geolocation projects? If so, we at Auravant show you a solution we use to make this task simple and fast.

thumbnail image

GPX and GeoJSON

First of all, if you are not familiar with these formats, here is a brief explanation of each of them.

GPX (GPS Exchange Format) is a file format used to store and share GPS data such as routes, tracks and waypoints. It is designed to be readable by both humans and machines, and is widely used in GPS applications and on websites that provide maps and geographic data. GPX is based on XML, so data is stored in a format of tags and attributes that are easy to read and process.

GeoJSON, on the other hand, is a JSON-based file format used to represent geographic data on the web. It is widely used in mapping and online geographic data visualisation applications, and is supported by most web mapping libraries.

Although GPX and GeoJSON are similar in some respects, there are some important differences between them. GPX is more versatile and can store a larger amount of geographic data, such as velocity, altitude and time, while GeoJSON focuses more on the representation of geometries and attributes. In addition, GPX is more suitable for data exchange between different applications and devices, while GeoJSON is more suitable for data visualisation on the web.

Solutions and tools

In Auravant, we use the @tmcw/togeojson library to convert GPX to GeoJSON. This library is very easy to use and allows, from reading a GPX file with a FileReader and converting it to XML, to parse it to GeoJSON. In addition, this library is very versatile and allows us to convert not only GPX files, but also KML and KMZ files, which is very useful in our daily development.

We start by importing the library:

import { gpx } from '@tmcw/togeojson'

We create a function that receives the .GPX file as a parameter to execute it once the users load the file.

Inside that function, we are going to create a reader, which in the result of the reading (reader.result) is going to return the XML.

We have to parse that XML with DOMParser so that @tmcw/togeojson can read the file correctly.

Finally, once the file is parsed, we can run the gpx function provided by the library and we get our precious GeoJSON as a result.

const convertGPX = (file) => {
const reader = new FileReader()
reader.addEventListener('loadend', function () {
const xml = reader.result
const gpxFile = new DOMParser().parseFromString(xml, 'text/xml')
const geoJson = gpx(gpxFile)
})
reader.readAsText(file)
}

Considerations

Generally, GeoJSON resulting from .GPX files have LineString features, so we use tools like TurfJS to do a LineString to Polygon transformation.

Also, being GPS-plotted polygons, the last coordinate often does not match the first one as it should be in a valid Auravant field. For that reason, in addition to the conversion, we also have to do coordinate management to be able to close and form that polygon.

In any case, it is important to keep in mind that the conversion from GPX to GeoJSON implies the interpretation of the information contained in the GPX file and its representation in a different format. Therefore, it is possible that some loss of information or errors may occur in the conversion, especially if the GPX file contains non-standard or custom elements or attributes.