Created
January 13, 2015 22:45
-
-
Save ThierryO/acb6d513a4b873d7a047 to your computer and use it in GitHub Desktop.
create lines from consecutive coordinates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sp) | |
library(plyr) | |
library(reshape2) | |
library(rgdal) | |
n <- 1e4 | |
dataset <- data.frame( | |
ID = seq_len(n), | |
X = runif(n), | |
Y = runif(n), | |
Attribuut = sample(LETTERS, n, replace = TRUE) | |
) | |
id <- head(dataset$ID, -1) | |
start <- head(dataset[, c("X", "Y", "Attribuut")], -1) | |
einde <- tail(dataset[, c("X", "Y", "Attribuut")], -1) | |
start$ID <- id | |
einde$ID <- id | |
start$Tijdstip <- "T1" | |
einde$Tijdstip <- "T2" | |
posities <- rbind(start, einde) | |
attributen <- dcast(ID ~ Tijdstip, data = posities, value.var = "Attribuut") | |
lijnen <- dlply(posities, ~ ID, function(pos){ | |
Lines( | |
slinelist = list( | |
coords = Line(pos[, c("X", "Y")]) | |
), | |
ID = pos$ID[1] | |
) | |
}) | |
sp.lijnen <- SpatialLinesDataFrame( | |
sl = SpatialLines( | |
LinesList = lijnen, | |
proj4string = CRS("+proj=longlat +datum=WGS84") | |
), | |
data = attributen | |
) | |
writeOGR( | |
object = sp.lijnen, | |
dsn = "c:/tmp", | |
layer = "lijnen", | |
driver = "ESRI Shapefile" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment