3

我正在尝试使用 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

4

2 回答 2

3

您已经发现这些格式仅接受地理坐标(经纬度,未投影),并且至少对于 GPX 文件,允许的字段非常有限,例如名称的“名称”,海拔的“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 使用显示的名称接受了该文件。

于 2014-11-22T23:43:56.100 回答
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 以用于 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")
于 2014-12-05T15:47:04.220 回答