4

我尝试将具有多行的空间对象(从 OSM 检索到的河流)转换为 KML。对于具有单线的对象,使用 kmlLine 很容易。但是,对于多行,以下方法不起作用,我尝试从文档中调整示例是徒劳的:

# get OSM data:
library(osmar)
library(maptools)

salzach <- get_osm(relation(408582), full = T)
sp_salzach <- as_sp(salzach, what = "lines")

# convert to KML:
kmlLine(sp_salzach, "salzach.kml", lwd = 3, col = "blue", name = "Salzach")
Warning:
In kmlLine(sp_salzach, "salzach.kml", lwd = 3, col = "blue", name = "Salzach") :
  Only the first Lines object with the ID '23633534' is taken from 'obj'

# shell.exec("salzach.kml")
4

1 回答 1

1

As it says in the Details of ?kmlLine, if you provide a spatialLinesDataFrame as first argument, it will use only the first element of the spatialLinesDataFrame object. Since

 sp_salzach@data$id[1]
 [1] 23633534

this is the Lines object with the above ID, therefore the warning. sp_salzach contains 74 Lines objects, not 1. If you want to apply kmlLines to each of these lines you would need to do sth. like this:

for( i in seq_along(sp_salzach) ) {

    kmlLine(sp_salzach@lines[[i]], kmlfile = paste0("salzach", i, ".kml"), 
            lwd = 3, col = "blue", name = paste0("Salzach", i))

}

This will create 74 .kml files in your working directory, one for each Lines object in sp_salzach, although I'm not sure if this is what you want.

EDIT:

If you don't adapt the name in each iteration, you get all lines in one file, at least if opened with google earth it seems to work, i.e.:

for( i in seq_along(sp_salzach) ) {

        kmlLine(sp_salzach@lines[[i]], kmlfile = "salzach.kml", 
                lwd = 3, col = "blue", name = "Salzach")

}
于 2012-12-22T02:10:39.803 回答