使用writeOGR {rgdal}写入gpx文件时出错

时间:2014-07-31 17:00:00

标签: gpx rgdal

我正在尝试使用writeOGR创建一个gpx点文件。 writeOGR()将创建一个没有错误的shp文件,但如果我尝试编写KML或GPX文件,我会收到此错误。我在Windows上使用R 3.1.1和rgdal 0.8-16(我在7和8上尝试过,同样的问题)。

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

在地理坐标系中,我已经发现这很重要

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"

任何人都可以提供有关如何解决此错误的任何指导吗?

以下是我使用的文件和代码。

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

2 个答案:

答案 0 :(得分:3)

你已经发现这些格式只接受地理坐标(lat-long,而非投影),至少对于GPX文件,允许的字段非常有限,例如名称的“name”,海拔的“ele”和“时间“为日期时间信息。文件中的@data字段与这些字段不匹配,从而导致错误。 可以使用

编写这些额外的字段
dataset_options="GPX_USE_EXTENSIONS=yes"

在这种情况下,它们将作为子类添加到“扩展”字段中,但许多简单的gps接收器不会读取或使用这些字段。要使用名称创建一个非常简单的航点文件,请使用以下过程。

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

对我来说,这已执行并创建了一个工作gpx文件,我的gps接受了显示的名称。

答案 1 :(得分:1)

我在从简单的data.frame实现输入代码时遇到了一些问题,并希望为使用这种数据的人(而不是shapefile)提供完整的代码。这只是来自@ Wiebe答案的一个非常轻微修改的答案,无需查看@Auriel Fournier的原始数据。感谢@Wiebe - 您的回答也帮助我解决了我的问题。

数据如下所示:

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

使用writeOGR将代码放入GPX for mapsource的代码:

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