rspatial / raster

R raster package https://rspatial.github.io/raster/reference/raster-package.html
GNU General Public License v3.0
161 stars 52 forks source link

spLines: ids ordered as char #312

Closed aloboa closed 1 year ago

aloboa commented 1 year ago

Given (from the example in the help page):

x1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60))
x2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55))
x3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45))
x4 <- rbind(c(41,-41.5), c(51,-35), c(62,-41), c(51,-50))
x <- rbind(x1,x2,x3,x4)
colnames(x) <- c("x","y")
x <- data.frame(x)
x$id <- 1:16
x$field <- letters[1:16]
head(x,3)

> head(x,3)
     x   y id field
1 -180 -20  1     a
2 -140  55  2     b
3   10   0  3     c

and this ugly code:

a <- spLines(coordinates(x)[1:2,])
for(i in 2:(nrow(x)-1)) {
  a <- rbind(a,spLines(coordinates(x)[i:(i+1),]))
}

I get:

> names(a)
 [1] "1"   "11"  "12"  "13"  "14"  "15"  "16"  "17"  "18"  "19"  "110" "111" "112" "113" "114"

Is this not potentially a problem to later link to the data frame to create an SpatialLinesDataFrame? I report in case this is a bug or an unintentional feature.

rhijmans commented 1 year ago

Your code does not run for me, but I think I can answer your question. When creating a SpatialLinesDataFrame you can use match.ID = FALSE, or first change the row names.