1

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.

  • 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. – Jeffrey Evans Mar 31 '16 at 16:17
4

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)
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

  • Thank you . I have tried to use the writeOGR module, but keep getting the error message Creation of output file failed. – user41509 Mar 31 '16 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 Mar 31 '16 at 19:22
  • I may make post a new question because I cant fit an example data set in the comments. – user41509 Mar 31 '16 at 19:24
  • Yeah, a new question is probably appropriate since the error is a new issue. – HeyOverThere Mar 31 '16 at 21:21
1

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, 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(file) || inherits(file, "connection")) cat(o, file=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".

0

I know this does not answer the question in R. but if you have ArcGIS you can download this tool to convert and feature to GPX http://www.arcgis.com/home/item.html?id=067d6ab392b24497b8466eb8447ea7eb. Then you can upload those GPX coordinates to an app called http://www.gpxnavigator.com/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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