Skip to content

Instantly share code, notes, and snippets.

@ThierryO
Created January 13, 2015 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThierryO/acb6d513a4b873d7a047 to your computer and use it in GitHub Desktop.
Save ThierryO/acb6d513a4b873d7a047 to your computer and use it in GitHub Desktop.
create lines from consecutive coordinates
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