I'm trying to intercept SpatialLinesDataFrame (170 fields and 140000 lines) with a SpatialPolygonDataFrame using RQGIS.I'm following the instruction from github. I modified the ../processing/gui/AlgorithmExecutor.py files accordingly. My lines and polygon have numeric dataframes.

I'm running the following code:


load("data/out/network/lines.rda") #lines
load("data/out/network/g.rda") #polygon
dir_tmp <- "/home/sergio"
my_env <- set_env()
params <- get_args_man(alg = "qgis:intersection", 
                       qgis_env = my_env)
params$INPUT <- lines
params$INPUT2 <- g
params$OUTPUT <- file.path(dir_tmp, "COg.shp")

system.time(out <- run_qgis(alg = "qgis:intersection",
                params = params,
                load_output = params$OUTPUT,
                qgis_env = my_env))

Error in dirname(x) : a character vector argument expected
[1] "/usr"

[1] "/usr/bin/qgis"

[1] "/usr/share/qgis/python/plugins"

How to fix: Error in dirname(x) : a character vector argument expected?

  • 2
    would be helped by a reproducible example Aug 17, 2016 at 17:57
  • i know, but the results are part of a paper, if I upload the files, i'm afraid i would have some issues or complications
    – Sergio
    Oct 1, 2016 at 18:44
  • 1
    You can anonymize your data; see here for some advice on how to do so Oct 1, 2016 at 18:55

1 Answer 1


I have to agree with Michael in that a reproducible example would really help! Anyways, let us create a spatial line and polygon object:

# construct coordinates for a polygon and a line object
coords_1 <- matrix(data = c(0, 0, 1, 0, 1, 1, 0, 1, 0, 0),
                   ncol = 2, byrow = TRUE)
coords_2 <- cbind(c(-0.5, 0.5, 1.4), c(0.1, 0.4, 0.6))
# convert the coordinates into spatial objects
poly_1 <- SpatialPolygons(list(Polygons(list(Polygon(coords_1)), 1))) %>%
  as(., "SpatialPolygonsDataFrame")
line_1 <- SpatialLines(list(Lines(list(Line(coords_2)), 1)))
line_1 <- SpatialLinesDataFrame(line_1, data.frame(dummy = 1))
# visualize
plot(line_1, col = "lightblue", add = TRUE)

Now the question is what you would like to do. There are two ways how you can intersect polygons and lines. The first would be to intersect the line object with the polygons. The second option is to intersect the polygon object with the line object. Unfortunately, the QGIS help (open_help("qgis:intersection")) does not clarify this. This leaves us with the trial-and-error approach.

qgis_env <- set_env("C:/OSGeo4W64/")
params <- get_args_man(alg = "qgis:intersection", 
                       qgis_env = qgis_env)
params$INPUT <- poly_1
params$INPUT2 <- line_1
params$OUTPUT <- "intersection.shp"
out <- run_qgis(alg = "qgis:intersection",
                params = params,
                load_output = params$OUTPUT,
                qgis_env = qgis_env)

This produces no output and following error:

ERROR 1: Attempt to write non-polygon (LINESTRING) geometry to POLYGON type shapefile

This indicates that INPUT2 probably should not be a line object. Ok, so let's reverse the inputs:

params$INPUT <- line_1
params$INPUT2 <- poly_1
out <- run_qgis(alg = "qgis:intersection",
                params = params,
                load_output = params$OUTPUT,
                qgis_env = qgis_env)
plot(out, col = "red", add = TRUE)

Ok, this works, and means that QGIS lets you intersect lines with polygons. However, if you want to intersect polygons with lines, check out e.g., saga:polygonlineintersection:

args <- get_args_man(alg = "saga:polygonlineintersection", qgis_env = qis_env)
args$POLYGONS <- poly_1
args$LINES <- line_1
args$INTERSECT <- "poly_int.shp"
out <- run_qgis(alg = "saga:polygonlineintersection",
                params = args,
                load_output = args$INTERSECT,
                qgis_env = qgis_env)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.