Friday, 10 May 2019

r - Export a QGIS print composer file (.qpt) as PDF from the command line


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




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...