Search code examples
gpxrgdal

Error using writeOGR {rgdal} to write gpx file


I am trying to use writeOGR to create a gpx file of points. writeOGR() will create a shp file with no error, but if I try to write a KML or GPX file I get this error. I'm using R 3.1.1 and rgdal 0.8-16 on Windows (I've tried it on 7 and 8, same issue).

writeOGR(points, driver="KML", layer="random_2014",dsn="C:/Users/amvander/Downloads")
Error in writeOGR(points, driver = "KML", layer = "random_2014", dsn = "C:/Users/amvander/Downloads") : 
Creation of output file failed

It is in geographic coordinates, I already figured out that that was important

summary(points)
Object of class SpatialPointsDataFrame
Coordinates:
        min       max
x -95.05012 -95.04392
y  40.08884  40.09588
Is projected: FALSE 
proj4string :
[+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0]
Number of points: 20
Data attributes:
       x                y              ID           
Min.   :-95.05   Min.   :40.09   Length:20         
1st Qu.:-95.05   1st Qu.:40.09   Class :character  
Median :-95.05   Median :40.09   Mode  :character  
Mean   :-95.05   Mean   :40.09                     
3rd Qu.:-95.05   3rd Qu.:40.09                     
Max.   :-95.04   Max.   :40.10       

str(points)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
..@ data       :'data.frame': 20 obs. of  3 variables:
.. ..$ x : num [1:20] -95 -95 -95 -95 -95 ...
.. ..$ y : num [1:20] 40.1 40.1 40.1 40.1 40.1 ...
.. ..$ ID: chr [1:20] "nvsanc_1" "nvsanc_2" "nvsanc_3" "nvsanc_4" ...
..@ coords.nrs : num(0) 
..@ coords     : num [1:20, 1:2] -95 -95 -95 -95 -95 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "x" "y"
..@ bbox       : num [1:2, 1:2] -95.1 40.1 -95 40.1
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
.. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"

can anyone provide any guidance on how to get around this error?

Here are the files I used and the code.

https://www.dropbox.com/sh/r7kz3p68j58c189/AACH0U_PLH7Y6cZW1wdFLQTOa/random_2014


Solution

  • you already figured out these formats will only accept geographic coordinates (lat-long, not projected) and at least for GPX files there are very limited fields allowed, like "name" for the name, "ele" for elevation and "time" for date-time information. The @data fields in your file do not match those and thus cause an error. It is possible to write those extra fields using

    dataset_options="GPX_USE_EXTENSIONS=yes"
    

    in that case they will be added as subclasses in an "extensions" field, but many simple gps receivers will not read or use those fields though. To create a very simple waypoint file with names use the following procedure.

    #I will use your dataset points
    
    #if not already re-project your points as lat-long
    ll_points <- spTransform(points, CRS("+proj=longlat + ellps=WGS84"))
    
    # use the ID field for the names
    ll_points@data$name <- ll_points@data$ID  
    
    #Now only write the "name" field to the file
    writeOGR(ll_points["name"], driver="GPX", layer="waypoints", 
       dsn="C:/whateverdir/gpxfile.gpx")
    

    for me this executed and created a working gpx file that my gps accepted with displayed names.