-
Notifications
You must be signed in to change notification settings - Fork 47
Problem with RQGIS not closing the file connection with QGIS #82
Comments
Probably there's nothing we can do here.We asked the reticulate developers if there was a way to manually close the Python tunnel, they were afraid this would cause too much trouble. |
Probably there's nothing we can do here.We asked the reticulate developers if there was a way to manually close the Python tunnel, they were afraid this would cause too much trouble (see .rstudio/reticulate#27). Will have a look when I am back from my holidays. |
That is an horrible news! I've integrated RQGIS into my work process which do multiple task on raster tiles and save temporary files in the process. I was hoping to parallelized or loop through all my tiles. For that I needed to delete the temporary files as they become useless. If I don't do it, I'll finish with Terabytes of useless data and it won't work... I don't know how I'll get around it... I may need to use directly |
Sorry to hear that.Maybe there is a way to close a custom QGIS Python application. There soil should be, however, there me nasty side effects. As I said, will look into it when I am back. |
I just ran: library("RQGIS")
run_qgis(alg = "gdalogr:rastercalculator",
INPUT_A = dem,
OUTPUT = "depth_D500b.tif",
FORMULA = "A*2",
RTYPE = 1)
file.remove(file.path(tempdir(), "depth_D500b.tif")) This worked for me, i.e. the output file was removed. Therefore, I don't think that your problem is related to (R)QGIS but rather to your access rights (but just guessing). Try also to run RStudio or R as an administrator. |
sorry, you were referring to the input file, ok, will have a look. |
py_run_string("app.exitQgis()") lets you close the QGIS session, and then you can also delete the input file, in your case this was |
That's already a big help, thanks and have a good vacation! I'll wait! |
Have been doing some digging, and it seems you have encountered a QGIS problem. Apparently, QGIS locks the input files. You can release the lock, though this is a bit more complicated than I have previously suspected (see e.g. this post and this one). The bad new is that fiddling around with the QGIS session - our initial approach ( The good news is, there might be a way to solve this issue Taking your example as a use case again: # save raster to a file
file = normalizePath(file.path(tempdir(), "dem.tif"), "/")
writeRaster(dem, file)
# import raster into QGIS and register it
py_run_string(sprintf("rlayer = QgsRasterLayer('%s', 'input')", file))
py_run_string("QgsMapLayerRegistry.instance().addMapLayer(rlayer)")
# run the processing
py_run_string(
paste("processing.runalg('gdalogr:rastercalculator', {'INPUT_A':rlayer",
"'OUTPUT':'C:/Users/pi37pat/AppData/Local/Temp/RtmpWmA1KZ/depth.tif'",
"'FORMULA':'A*2', 'RTYPE':1})", sep = ","))
# remove the input raster layer again
py_run_string("QgsMapLayerRegistry.instance().removeMapLayer(rlayer.id())")
py_run_string("del rlayer")
# delete it from disk
file.remove(file) However, this approach requires some tweaking of the existing code base, and I have to think about possible side effects. Will put the new code into a new branch but again give me some time. |
I have created a new branch called devtools::install_github("jannes-m/RQGIS", ref = "unlock") This should address your issue: data("dem", package = "RQGIS")
raster::writeRaster(dem, file.path(tempdir(), "dem.tif"))
run_qgis(alg = "gdalogr:rastercalculator",
INPUT_A = file.path(tempdir(), "dem.tif"),
OUTPUT = "depth_D500b.tif",
FORMULA = "A*2",
RTYPE = 1)
file.remove(list.files(tempdir(), full.names = TRUE)) Let me know, if this works for you or if you encounter any trouble. Please note that the work on this branch is not entirely done. I have to still find a way how to deal with |
I've tried this code (which is my real code) and it works perfectly: library(RQGIS);monqgis <- set_env("C:\\OSGeo4W64")
params.default.rastercalculator <- get_args_man(alg = "gdalogr:rastercalculator", qgis_env = monqgis)
params <- params.default.rastercalculator
path <- "C:/Users/Bastien/Desktop/Codes corrections A/data/temp/"
params$INPUT_A <- paste0(path,"Ratio_BLDG.sdat")
params$INPUT_B <- paste0(path,"beta1_KR_IF_bldg.sdat")
params$OUTPUT <- paste0(path, "KR_bldg_transfo.tif")
params$FORMULA <- paste0(convertir_KR_vers_IF$beta_0_bldg[1],"+(A*B)")
params$RTYPE <- "5"
system.time(run_qgis(alg = "gdalogr:rastercalculator", params=params, qgis_env = monqgis))
file.remove(list.files("C:\\Users\\Bastien\\Desktop\\Codes corrections A\\data\\temp\\", full.names = T)) Notice that it's what I think you call Thank you again for you time |
I was referring to the QGIS input type |
FYI: I tried your reprex data("dem", package = "RQGIS")
raster::writeRaster(dem, file.path(tempdir(), "dem.tif"))
run_qgis(alg = "gdalogr:rastercalculator",
INPUT_A = file.path(tempdir(), "dem.tif"),
OUTPUT = "depth_D500b.tif",
FORMULA = "A*2",
RTYPE = 1)
file.remove(list.files(tempdir(), full.names = TRUE)) with both |
Thanks for testing @pat-s. Thought so, that this is a Windows-only issue. |
@jannes-m whats the status here? |
Well, I am not sure about possible side-effects. Therefore, I would leave the "unlocking procedure" in this branch as long as it is not thoroughly tested. And it seems @BastienFR is the only one so far needing this feature, therefore no rush to integrate it into master. Besides, maybe this issue will be resolved with the release of QGIS 3.... |
Hi, possibly related to this: today I was noticing a strange behaviour while trying to run algorithm "alg = "qgis:eliminatesliverpolygons". In practice, when running multiple times the algorithm within the same R session but with different parameters, the input params were apparently not "updated" after the first run, and the result was always that of the first run. The only way to have "new" results seemed to be to restart the "R" session. However, after installing the "unlock" branch the problem apparently disappeared: don't know if this is related to a similar issue, or to other differences with respect to the CRAN version. I am working on Ubuntu 17.04, R 3.4.3 PS: really nice package! Thanks! Ps2 : also possibly related to #74 |
Hi, Pls note that I have updated now also the unlock branch, i.e., you will have all the new features we have added to the master. Again, thanks for reporting back, really helpful! |
Well, indeed, the unlock branch resolves also #74. So this is a strong incentive to work further on the unlock-solution. Basically, this would solve:
|
Glad it helped! Then for the moment I'll keep working on the "unlock" branch. |
Hi. I dug in this a bit more since I had a hunch that there was a simple "file overwrite" problem. In fact, passing files as inputs in the example of #74 works properly: spdf1_file <- tempfile(fileext = ".shp")
spdf2_file <- tempfile(fileext = ".shp")
spdf3_file <- tempfile(fileext = ".shp")
st_write(st_as_sf(SPDF1), spdf1_file)
st_write(st_as_sf(SPDF2), spdf2_file)
st_write(st_as_sf(SPDF3), spdf3_file)
test1 <- RQGIS::run_qgis("qgis:intersection", INPUT = spdf1_file, INPUT2 = spdf2_file,
OUTPUT = file.path(tempdir(), "test1.shp"),
load_output = TRUE)
test2 <- RQGIS::run_qgis("qgis:intersection", INPUT = spdf1_file, INPUT2 = spdf3_file,
OUTPUT = file.path(tempdir(), "test2.shp"),
load_output = TRUE)
identical(test1, test2)
[1] FALSE
The problem appears to be in
solves the issue. The culprit appears to be the call to cap <- capture.output({
suppressWarnings(
test <-
try(write_sf(params[[i]], fname, quiet = TRUE), silent = TRUE)
)
}) not throwing an error when trying to overwrite a file, because by default it uses "delete_layer = TRUE", and therefore not triggering the "catching error" mechanism immediately below. Therefore, also this simple change appears to solve the issue: cap <- capture.output({
suppressWarnings(
test <-
try(write_sf(params[[i]], fname, quiet = TRUE, delete_layer = FALSE), silent = TRUE)
)
}) However, I think that using the "tempfile" solution could be better, because it would prevent any "name clashing" problems. Only problem could be that tempfiles could clutter the tempdir(), but a cleanup solution could be easy to implement. HTH |
@lbusett Thanks for the input. I would have to take a look at this. In any case, it appears that But what you propose in the end (deleting input files), probably only works with the unlock-branch, otherwise QGIS will prevent it. In any case, I will think about using |
You're welcome. Note that using |
Hello,
I've found a problem with the package RQGIS that is preventing me from having a good workflow. The problem is that (I think) the package doesn't close the file connection with QGIS after the analysis. Here is a reproducible example:
You can download my raster here: https://www.dropbox.com/s/6zsmz5kxp0d86tu/depth_D500.tif?dl=0 but I'm pretty sure any raster will have the problem.
As you can see, when I'm trying to delete my files, I cannot delete one of them (in that case depth_D500.tif) which is the input of my analysis. The only way I can delete it after is by closing R. Then the connection is drop and I can delete it. This is annoying because I want to delete my temporary files to reduce disk usage.
I think the problem is generalized (i.e. not limited to the "gdalogr:rastercalculator" module) as I experienced it with "grass7:r.reclass" as well.
my session info:
Thanks
Bastien
The text was updated successfully, but these errors were encountered: