Arthur is a statistician, data scientist, environmental economist, organic food grower, amateur graphic designer and urbanist. He loves food, bike touring, coding, handcrafts, arts, epistemology, maps and social sciences. He speaks French, English, Danish, basic Spanish, and is learning Italian and German.

He doesn't always talk about himself in the third person.

How to convert GPX to geoJSON with R?

Before I started to import the whole itinerary of route EuroVelo 3 from Trondheim to Santiago de Compostella, using OpenStreetMap data, I had already started collecting information about the itinerary from different sources.

For instance, the folks behind the French association have done a great work at collecting information about EuroVelo tracks both in France and Belgium, while they were editing a topoguide for . They even allow us to download the GPS track from Namur (Belgium) to Tours (France). The rest of the route, between Tours and Irun (Spain), is to be published this summer – at least that’s what said Brunaud Devillard on behalf of CycloTransEurope when I wrote to inform them about my future trip.

In this post, I will show how to download the GPX track and convert it to geoJSON with . I suppose that you have a good command of programation in general, and that you have had an introduction to R. There are basicly 2 options, depending on if you have the gdal library installed on your computer or notOn Linux, you can easily install it with Synaptic whereas on Mac, I would recommand using Homebrew. I have no idea how to install gdal on a Windows computer.. The gdal library provides a wide range of drivers for reading and writing spatial data.

Method 1. Quite transparently, the rgdal package is an interface with gdal. The exact list of all available drivers is given within R by ogrDrivers(). In case the GPX was not included in the list, please see the alternative solution.

library(dplyr) # for chaining with %>%
               # f(a,b) is the same as a %>% f(b)
               # or b %>% f(a, .)

# remove the first (empty) line
# and store the file as 'namur-tours.gpx'
'http://eurovelo3.fr/services?' %>%
    paste0('service=download-gpx&rid=1') %>%
    readLines %>%
    last %>%
    cat(file='namur-tours.gpx')

# list the layers
library(sp)
library(rgdal)
ogrListLayers('namur-tours.gpx')

# read the data
namur_tours <- readOGR(
    'namur-tours.gpx',
    layer='tracks'
)

# export them as geoJSON
library(geojsonio)
geojson_write(
    namur_tours,
    file = 'namur-tours.geojson'
)

Now, the file can be rendered seamlessly with onto a map.

Method 2. The other solution takes advantage of the very definition of the GPX format, which is just a variety of XML. We can thus use the xml2 package for reading the file, and then build a spatial object from scratch with the sp package.

library(dplyr) # for chaining with %>%

# read as XML file
library(xml2)
namur_tours <-
  'http://eurovelo3.fr/services?' %>%
  paste0('service=download-gpx&rid=1') %>%
  readLines %>%
  last %>%
  read_xml

# extract coordinates of points
# points are recorded as 'trkpt' (trackpoint)
# in the namespace 'd1'
namur_tours_ns     <- xml_ns(namur_tours)
namur_tours_pts    <- namur_tours %>% xml_find_all(
  "//d1:trkpt",
  namur_tours_ns
)
namur_tours_lat    <- namur_tours_pts %>%
  xml_attr('lon')
namur_tours_long   <- namur_tours_pts %>%
  xml_attr('lat')
namur_tours_coords <- cbind(
  longitude =  as.numeric(namur_tours_lat),
  latitude  =  as.numeric(namur_tours_long)
)

# build the SpatialLine object
library(sp)
namur_tours_line  <- Line(namur_tours_coords)
namur_tours_lines <- Lines(namur_tours_line, "NT")
namur_tours       <- SpatialLines(
  LinesList   = list(namur_tours_lines),
  proj4string = CRS("+init=epsg:4326")
)

# export them as geoJSON
library(geojsonio)
geojson_write(
  namur_tours,
  file = 'namur-tours.geojson'
)