Skip to content
Prev 28993 / 29559 Next

r-sig-geo@r-project.org

Here it is:

Hello,
How can I construct an sf polyline object directly in sf? I have a
data frame with 3 pairs of coordinates and need for each row a 2
segment line.
I did it with sp but would like to migrate to sf:

dput(head(base))
structure(list(Cuenca = c("NA", "NA", "NA", "NA", "NA", "NA"),IDH =
c("AL011851", "AL011851", "AL011851", "AL011851",
"AL011851","AL011851"), W = c(80L, 80L, 80L, 80L, 70L, 60L), LAT1 =
c(28,28, 28.05, 28.15, 28.2, 28.25), LONG1 = c(-95.1, -95.7,
-96.25,-96.65, -96.9, -97.3), LAT3 = c(28, 28.05, 28.15, 28.2,
28.25,28.35), LONG3 = c(-95.7, -96.25, -96.65, -96.9, -97.3, -97.95),
LAT2 = c(28, 28, 28.1, 28.2, 28.2, 28.3), LONG2 = c(-95.4,-96, -96.5,
-96.8, -97, -97.6), Resid = c(6, 6, 4.5, 3, 4.5,6)),row.names = c(NA,
6L), class = "data.frame")

base$ID<-seq(1,nrow(base),1)
base_lines <- apply(base,1,function(x){
  points <- data.frame(lng=as.numeric(c(x["LONG1"],x["LONG2"],x["LONG3"])),lat=as.numeric(c(x["LAT1"],x["LAT2"],x["LAT3"])),stringsAsFactors
= F)
  coordinates(points) <- c("lng","lat")
  Lines(Line(points),ID=as.numeric(x["ID"]))})
row.names(base) <- base$ID
base_lines <- SpatialLinesDataFrame(SpatialLines(base_lines),base)
sflines<-st_as_sf(base_lines)


Le jeu. 30 juin 2022 ? 08:51, Ben Tupper <btupper at bigelow.org> a ?crit :