[R-sig-Geo] r-sig-geo maybe from r-project.org

David Romero rod@romero @end|ng |rom gm@||@com
Thu Jun 30 16:19:00 CEST 2022


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 using bigelow.org> a écrit :
>
> Hi,
>
> Hmmm... I think I can see what you are trying, but the mailing list
> mangles HTML messages (switch your email to client to plain text mode
> for this list).  Mifght you share what you get when you run,
>
> dput(head(base))
>
> in R?  That way each of us would have a small sample of your data.
> And also resend the code to the list in plain text so we don't have to
> to yoga moves to read the code?
>
> Best wishes,
> Ben
>
>
> On Thu, Jun 30, 2022 at 9:30 AM David Romero <rodaromero using gmail.com> wrote:
> >
> > 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:
> >
> > 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)
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo using r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>
> --
> Ben Tupper (he/him)
> Bigelow Laboratory for Ocean Science
> East Boothbay, Maine
> http://www.bigelow.org/
> https://eco.bigelow.org



More information about the R-sig-Geo mailing list