r-spatial / RQGIS3

GNU Lesser General Public License v3.0
69 stars 19 forks source link

problem sending more than a file to "native:mergevectorlayers" #4

Closed iquincoces closed 5 years ago

iquincoces commented 5 years ago

Hi, we have been trying to use function "native:mergevectorlayers" for merging a big number of point vector shp files in RQGIS3 in different ways but we are always having an error

Following your advice in https://github.com/jannes-m/RQGIS/issues/110, since both functions needs the same type of data for input list[str]: list of layer sources we coded this way in order to have an input similar to the one adviced for RQGIS library

library("RQGIS3") set_env() params = get_args_man(alg = "native:mergevectorlayers")

wd = getwd() listanombres = list.files(pattern = "*.shp")

params$LAYERS = paste0(paste(wd, listanombres, sep="/"), collapse=";") params$CRS= "EPSG:4326" params$OUTPUT= "PHYtotal.shp" run_qgis(alg = "native:mergevectorlayers", params = params, load_output = FALSE)

But it seems not to work for RQGIS3 since both (your example) and our code have the same error for the list of input files.

Any idea to solve the problem?

run_qgis(alg = "grass7:r.stats", params = p) Error in py_run_string_impl(code, local, convert) : QgsProcessingException: Unable to execute algorithm Incorrect parameter value for input

Detailed traceback: File "", line 1, in File "/usr/share/qgis/python/plugins/processing/core/Processing.py", line 139, in runAlgorithm raise QgsProcessingException(msg)

run_qgis(alg = "native:mergevectorlayers", params = params, load_output = FALSE) Error in py_run_string_impl(code, local, convert) : QgsProcessingException: Unable to execute algorithm Incorrect parameter value for LAYERS

Detailed traceback: File "", line 1, in File "/usr/share/qgis/python/plugins/processing/core/Processing.py", line 139, in runAlgorithm raise QgsProcessingException(msg)

Thanks in advance

jannes-m commented 5 years ago

Hey, I have checked and QGGIS3 expects a Python list containing the files as input for a multipleinput parameter as opposed to the previous expected format (file1.shp;file2.shp;file3.shp). I will try to implement it in the coming two weeks, and will let you know. In the meantime, you might also use sf if the files are not too large, e.g.:

library("sf")
nc = st_read(system.file("shape/nc.shp", package = "sf"))
rbind(nc, nc)

One question: You seem to be using Linux. Which distribution are you using, and have you run RQGIS3 in RStudio? Because in my case, running RQGIS3 crashes the RStudio R session though running R and RQGIS from the terminal works without problems.

iquincoces commented 5 years ago

Hi Jannes

I'm using LinuxMint 19.1 Tessa with QGIS, R and Rstudio last versions, I confirm you that RQGIS3 crashes RStudio but it works fine in R terminal. One month ago I was using last UBUNTU LTS version with the same results. The problem with native:mergevectorlayers is not only present in this algoritm since the same problem arised whrn using saga:mergevectorlayers that expects the same type of data as input. I'll give a try to sf library

Best

Inaki

jannes-m commented 5 years ago

Should work now. Please note that you now have to use a list() to specify multiple input files instead of separating multiple files via a semi-colon in one string.

# attach packages
library("sf")
#> Linking to GEOS 3.5.1, GDAL 2.1.2, PROJ 4.9.3
library("RQGIS3")
#> Loading required package: reticulate
# attach data
data("random_points")

# split data
pt_1 = random_points[1:10, ]
pt_2 = random_points[91:100, ]
# run QGIS algorithm
alg = "native:mergevectorlayers"
params = get_args_man(alg = alg)
#> Assuming that your root path is '/usr'!
params$LAYERS = list(pt_1, pt_2)
params$OUTPUT = file.path(tempdir(), "out.shp")
out = run_qgis(alg = alg, params = params, load_output = TRUE)
#> $OUTPUT
#> [1] "/tmp/RtmplgAkPA/out.shp"

# works also when specifying a list containing the filenames of the vector
# layers to merge
file_1 = file.path(tempdir(), "file_1.shp")
file_2 = file.path(tempdir(), "file_2.shp")
st_write(pt_1, file_1, delete_layer = TRUE)
#> Writing layer `file_1' to data source `/tmp/RtmplgAkPA/file_1.shp' using driver `ESRI Shapefile'
#> features:       10
#> fields:         2
#> geometry type:  Point
st_write(pt_2, file_2, delete_layer = TRUE)
#> Writing layer `file_2' to data source `/tmp/RtmplgAkPA/file_2.shp' using driver `ESRI Shapefile'
#> features:       10
#> fields:         2
#> geometry type:  Point
params$LAYERS = list(file_1, file_2)
out_2 = run_qgis(alg = alg, params = params, load_output = TRUE)  
#> $OUTPUT
#> [1] "/tmp/RtmplgAkPA/out.shp"
plot(st_geometry(out_2))

Created on 2019-03-19 by the reprex package (v0.2.1)