Reputation: 1176
I got a file containing the following data:
str(dat)
List of 2
$ x: Named num [1:28643] 2714769 2728569 NA 2728569 2740425 ...
..- attr(*, "names")= chr [1:28643] "h" "h" "" "h" ...
$ y: Named num [1:28643] 925000 925000 NA 925000 925000 ...
..- attr(*, "names")= chr [1:28643] "h" "h" "" "h" ...
- attr(*, "class")= chr [1:2] "bor" "list"
dat$x[1:10]
h h h h h h h
2714769 2728569 NA 2728569 2740425 NA 2740425 2751585 NA 2751585
dat$y[1:10]
h h h h h h h
925000 925000 NA 925000 925000 NA 925000 925000 NA 925000
class(dat)
"bor" "list"
table(names(dat$x))
h
479 28164
table(names(dat$y))
h
479 28164
plot(dat, type='l') results in a nice map.
I read about an old/simple form of line-'objects' used in S in "Applied Spatial Data Analysis with R" (Bivand, Pebesma, Gomez-Rubio; Springer 2008) on Page 38, which seem to have similarities to my file. This format defines a line as "start-point; end-point; NA" triplet.
Do you know this format? How can I convert it to an sp-object?
Thanks in advance
Upvotes: 2
Views: 131
Reputation: 4021
Based on your information, here is one possilbe way to go:
Assuming that your data represent lines and that the NA
values indicate the end of each line, you can convert your data to spatial lines doing the following:
# Creating artificial data for the example
dat <- list()
dat$x <- rnorm(1000) + rep(c(rep(0, 99), NA), 10)
dat$y <- dat$x + rnorm(1000)
# For simplicity, convert to data frame
# (this would be the first step for you to do with your data)
mydat <- data.frame(x = dat$x, y = dat$y)
# Convert each part to a line, using the NA values as breaks
mylines <- list()
last <- 1
for(i in 1:nrow(mydat)){
if(is.na(mydat$x[i])){
print(i)
mylines[[as.character(i)]] <- Lines(Line(mydat[last:(i-1),]), ID = as.character(i))
last <- i+1
}
}
# Convert to spatial lines object
mylines <- SpatialLines(mylines)
# Plot to see if it worked
plot(mylines)
Upvotes: 1