[R-sig-Geo] v.split.length (GRASS) in R

"Jannes Münchow" malNamalJa at gmx.de
Wed Nov 23 15:45:24 CET 2016


To achieve this, you can also use the RQGIS-package in conjunction with GRASS given you have installed QGIS along with GRASS (for more information have a look at vignette("install_guide", package = "RQGIS"):

# construct a SpatialPointsDataFrame (which will be accepted as input by
# run_qgis)
library("sp")
# from the sp vignette:
l1 <- cbind(c(1, 2, 3), c(3, 2, 2))
rownames(l1) <- letters[1:3]
l1a <- cbind(l1[, 1] + 0.05, l1[, 2] + 0.05)
rownames(l1a) <- letters[1:3]
l2 <- cbind(c(1, 2, 3), c(1, 1.5, 1))
rownames(l2) <- letters[1:3]
Sl1 <- Line(l1)
Sl1a <- Line(l1a)
Sl2 <- Line(l2)
S1 <- Lines(list(Sl1, Sl1a), ID = "a")
S2 <- Lines(list(Sl2), ID = "b")
Sl <- SpatialLines(list(S1, S2))
# convert it to a SpatialLinesDataFrame
Sl <- SpatialLinesDataFrame(Sl, data = data.frame(1:2), match.ID = FALSE)
proj4string(Sl) <- CRS("+proj=longlat +datum=WGS84")

# Now use RQGIS
library("RQGIS")
# indicate where QGIS is installed on your computer
qgis_env <- set_env("C:/OSGeo4W64/")
args <- get_args_man("grass7:v.split.length", qgis_env = qgis_env, 
                     options = TRUE)
# have a look at the GRASS online help
open_help("grass7:v.split.length", qgis_env = qgis_env)

# specify the necessary arguments
args$input <- Sl
# here length corresponds to one decimal degree
args$length <- "1"
args$output <- file.path(tempdir(), "out.shp")
# load the output directly into R again
out <- run_qgis(alg = "grass7:v.split.length", params = args,
                load_output = args$output,
                qgis_env = qgis_env)
length(Sl)  # 2 line objects
length(out)  #  9 line objects
# Have a look at the output
plot(out, col = rep(c("blue", "green", "black"), 3))
 
Cheers, 

Jannes



More information about the R-sig-Geo mailing list