Pergunta

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

Foi útil?

Solução

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.

Outras dicas

I had some problems implementing the code for input from a simple data.frame and wanted to provide the full code for someone working with that kind of data (instead of from a shapefile). This is simply a very slightly modified answer from @Wiebe's answer, without having to go search out what @Auriel Fournier's original data looked like.Thank you @Wiebe - your answer helped me solve my problem too.

The data look like this:

dat
            name  Latitude Longitude
1 AP1402_C110617 -78.43262  45.45142
2 AP1402_C111121 -78.42433  45.47371
3 AP1402_C111617 -78.41053  45.45600
4 AP1402_C112200 -78.42115  45.53047
5 AP1402_C112219 -78.41262  45.50071
6 AP1402_C112515 -78.42140  45.43471

Code to get it into GPX for mapsource using writeOGR:

setwd("C:\\Users\\YourfileLocation")

dat <- structure(list(name = c("AP1402_C110617", "AP1402_C111121", "AP1402_C111617", 
"AP1402_C112200", "AP1402_C112219", "AP1402_C112515"), Latitude = c(-78.4326169598409, 
-78.4243276812641, -78.4105301310195, -78.4211498660601, -78.4126208020092, 
-78.4214041610924), Longitude = c(45.4514150332163, 45.4737126348589, 
45.4560042609868, 45.5304703938887, 45.5007103937952, 45.4347135938299
)), .Names = c("name", "Latitude", "Longitude"), row.names = c(NA, 
6L), class = "data.frame")

library(rgdal)
library(sp)

dat <- SpatialPointsDataFrame(data=dat,  coords=dat[,2:3], proj4string=CRS("+proj=longlat +datum=WGS84"))

writeOGR(dat,
         dsn="TEST3234.gpx", layer="waypoints", driver="GPX",
         dataset_options="GPX_USE_EXTENSIONS=yes")
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top