2

Does anyone know if there is a way to convert position data (latitude and longitude) created in R to GPX format to use with navigation software?

I have been searching and havent found an answer.

In R you can bring in GPX formatted data using the readGPS()function in maptools.

I have also found the GPS Visualizer website which will allow me to convert data to GPX.

1
  • The function writeOGR in the rgdal package is a wrapper for GDAL's org2ogr and will allow you to write to a GPX format. The ogr2ogr utility, and in turn writeOGR, is for handling conversion of vector formats. You will probably have to call explicitly call gpx the diver using the "driver" argument and any additional parameters (eg., "waypoints, "routes") using the "dataset_options" argument. Commented Mar 31, 2016 at 16:17

3 Answers 3

6

writeOGR should be the way to go. I believe you were pretty close with the code you provided in the comments. However, adding a couple of things to the writeOGR function will help ensure that the file is written. If you add dataset_options="GPX_USE_EXTENSIONS=yes", then you will no longer get the error, "Creating Name field failed". Below is my example using a few coordinates for waypoints:

x<-c(-85.57768, -85.53748, -85.56880, -85.59405, -85.57524, -85.56148, -85.59133, -85.58460, -85.55561, -85.53497)
y<-c(30.30360, 30.32251, 30.28610, 30.31114, 30.32091, 30.34385, 30.26825, 30.31113, 30.35082, 30.32276)
a<-c(1:10)
xy<-data.frame(x,y,a)

latslongs <- SpatialPointsDataFrame(coords=xy[,c(1,2)],data=xy,proj4string =CRS("+proj=longlat + ellps=WGS84")) 

writeOGR(latslongs, dsn="C:/Users/UserPerson/File/gpxTEST.gpx",
     dataset_options="GPX_USE_EXTENSIONS=yes",layer="waypoints",driver="GPX", overwrite_layer = T)
5

I found that this simple solution from Simon Urbanek that worked well for me. I modified his function to write to file without having to call cat() outside the function. An output file must be specified and can be .gpx or .txt.

writeGPX <- function(lat, lon, time, out_file) { o <- c('<gpx version="1.1" creator="R">','<trk>','<trkseg>') if (missing(time)) o <- c(o, paste('<trkpt lat="',lat,'" lon="',lon,'" />', sep='')) else o <- c(o, paste('<trkpt lat="',lat,'" lon="',lon,'"><time>',paste(gsub(' ','T', as.character(time)), 'Z', sep=''),'</time></trkpt>', sep='')) o <- c(o, '</trkseg>', '</trk>', '</gpx>') if (is.character(out_file) || inherits(out_file, "connection")) cat(o, file=out_file, sep='\n') }

Example:

lat <- c(40.779, 40.777)  
lon <- c(-74.428,-74.418)  
writeGPX(lat, lon, Sys.time(), out_file="test.gpx")  

You can load this file into Google Earth (File > Open). Insure that you select "Create KML LineStrings".

1
  • that is simple and genious!
    – shosaco
    Commented Sep 9, 2020 at 18:08
1

You can use the writeOGR module to export to any geospatial format supported by OGR, including GPX. It is part of the rgdal package.

Here's an article covering the writeOGR module

and here's the OGR manual's GPX driver page

4
  • Thank you . I have tried to use the writeOGR module, but keep getting the error message Creation of output file failed.
    – user41509
    Commented Mar 31, 2016 at 19:20
  • My code is #make location_information a spatial points shapefile #get lat/long from location_information xy2<-location_information[,c(2,3)] ll_points <- SpatialPointsDataFrame(coords=xy2,data=location_information,proj4string =CRS("+proj=longlat + ellps=WGS84")) summary(ll_points) writeOGR(ll_points, dsn="N:/2015 Projects/Surveys/Station Allocation/gpxfile.gpx",layer="waypoints3",driver="GPX")
    – user41509
    Commented Mar 31, 2016 at 19:22
  • I may make post a new question because I cant fit an example data set in the comments.
    – user41509
    Commented Mar 31, 2016 at 19:24
  • Yeah, a new question is probably appropriate since the error is a new issue. Commented Mar 31, 2016 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.