[GIS] Export a QGIS print composer file (.qpt) as PDF from the command line

pyqgisqgisr

I have a workflow where my R script produces csv's and shapefiles. Those csv's and shapefiles then serve as layers in a QGIS project. Every time the R script is run, it overwrites the previous data, and the QGIS project is "dynamically" updated. The QGIS project is all set up such that it never needs to be updated or modified, since it dynamically loads the layers output by the R script.

My current workflow:

  1. Run R script
  2. Open QGIS project file
  3. Open Print composer manager
  4. Select and Open existing print composer layout
  5. Export (print) as PDF
  6. Quit qGIS
  7. View PDF

Desired workflow:

  1. Run R script
  2. View PDF

I can get part of the way there in R by opening the QGIS file in QGIS, by calling

# if on Windows:    
shell.exec("my_qGIS_file.qgs")
# or if on MAC OS X:
system(paste("open", "my_qGIS_file.qgs"))  

but then I can't figure out how to programmatically (from R) produce the pdf and quit qGIS.
I've read and reread the PyQgis cookbook section on the print composer, as well as the other examples on the internet (including this one), but those are both beyond my python comprehension, and seem to be way more complicated than I need. It seems like all the examples are programmatically redefining all the print composer every time the script is run. In this workflow, I already have a print composer layout stored in the project that I had created through the GUI, and have no desire or ability to recreate it with code.

If this can't be done directly in R, I think it's possible to invoke a python script from R via a system call, something like:
source("my_python_script_that_exports_my_PDF.py"), where the python script is self-standing.

Also, I have some QGIS plugins installed that bypass having to open the print composer and just export existing composer layouts cut down the workflow by a few clicks. One of the plugins is "Maps Printer", another is "multiPrint"
Perhaps a python or R script can invoke commands from these plugins?

Most critically I would like any proposed solution to run on windows, but bonus points if the suggested solution:

  1. works on both windows and mac

  2. does NOT actually load the full QGIS GUI (i.e., the person running the script does not see the QGIS loading splash screen or anything QGIS related; they just see the PDF magically show up in a specific directory, perhaps with a file name that is "file_name_" + timestamp.PDF).

I am running QGIS 2.8

Best Answer

There are many solutions

1) You can run directly R scripts in the QGIS Processing Toolbox (Resources on using R in QGIS for R users?, Configuring external applications,...)
The processing Python module use the subprocess module to call the R commands.

2) You can also use R from Python with the rpy2 module and Python from R with the RPython package

3) You can use PyQGIS without opening the application (Standalone applications using QGIS and environment variables and many other examples)

And finally, you can mix Python, PyQGIS, R and many other things using a Jupyter/IPython rmagic notebook (Filling in Python’s gaps in statistics packages with Rmagic)

Related Question