Dear,
I would like to generate kml file with lines (<LineString>) of
different style (definitely color, but width would also be nice to
see). However with kmlLine from maptools package I am able only to
output the single first Lines object (first row of
SpatialLinesDataFrame)
I guess the option would be to have single Lines object (instead of
list of them), but then also the style wouldn't vary among them... My
code so far:
library(maptools)
library(sp)
out <- structure(list(I = structure(c(1L, 7L, 3L, 4L, 5L, 1L, 2L, 9L,
6L, 3L,
10L, 8L), .Label = c("0.34", "0.35", "0.43",
"0.47", "0.48", "0.52",
"0.64", "0.72", "0.77", "0.92"), class =
"factor"), BF structure(c(1L, 9L, 4L, 6L, 7L, 2L, 3L, 11L, 8L, 5L,
12L, 10L), .Label
= c("3.16", "3.2", "3.44", "4.71",
"4.75", "5.54", "5.76", "6.77",
"11.33", "16.45", "21.24", "75.16" ),
class = "factor"), from structure(c(4L, 3L, 3L, 5L, 5L, 5L, 1L, 2L,
4L, 4L, 4L, 1L), .Label c("Bissau", "Caio",
"CotedIvoire", "Gambia", "Ghana"), class
"factor"), to = structure(c(2L, 4L, 1L, 5L, 1L, 2L, 2L, 6L, 3L, 4L,
6L,
2L), .Label = c("BurkinaFaso", "Caio",
"CotedIvoire", "Ghana",
"Nigeria", "Senegal"), class = "factor"), x =
c(-16.57534, -4.027751,
-4.027751, -0.1963, -0.1963, -0.1963, -15.5982, -16.2027, -16.57534,
-16.57534, -16.57534, -15.5982), y = c(13.4526, 5.336318, 5.336318,
5.555, 5.555, 5.555, 11.86398, 11.9277, 13.4526, 13.4526, 13.4526,
11.86398 ), xend = c(-16.2027, -0.1963, -1.5352, 7.489, -1.5352,
-16.2027, -16.2027, -17.3333, -4.027751, -0.1963, -17.3333, -16.2027
), yend = c(11.9277, 5.555, 12.357,
9.058, 12.357, 11.9277, 11.9277, 14.75, 5.336318, 5.555, 14.75,
11.9277)), .Names = c("I", "BF", "from",
"to", "x", "y", "xend",
"yend"), row.names = c(NA, -12L), class = "data.frame")
N <- dim(out)[1]
RowAsMatrix <- function(lines, row) {
rbind(c(lines[row,]$x, lines[row,]$y), c(lines[row,]$xend,
lines[row,]$yend) ) }
# list of coordinate matrixes to list of Line objects
lines_list <- list()
for(i in 1 : N ) {
lines_list[[i]] <- Line( RowAsMatrix(out[5:8], i) )
}
# list of Line objects to Lines object
lines_list_list <- list()
for (i in 1 : N) {
lines_list_list[[i]] <- Lines(lines_list[[i]], ID paste( out$BF[i] ) ) }
Lines object to SpatialLines object
s_l <- SpatialLines(lines_list_list)
SpatialLines object to SpatialLinesDataFrame object
df = data.frame(z = c(1 : N), row.names = sapply(slot(s_l, "lines"),
function(x) slot(x, "ID"))) sl_df <- SpatialLinesDataFrame(s_l,
data df)
kmlLine(sl_df, kmlfile = "output.kml")
--
while(!succeed) { try(); }