Monday, 14 November 2016

Script that runs automatically from the QGIS Python Console when QGIS starts, using schedule tasks on Windows 10


I'm working with QGIS 2.8 and windows 10. Is it possible that after open QGIS using the schedule tasks from windows 10, I could open the python console and run a script within QGIS programmatically. How can I automate the following actions after open the QGIS:



  1. open python console

  2. open a script


  3. run a script


Any help?




EDIT:


I've tried the two ways to open the python console, as suggested by Germán in this post "How to show Python console at QGIS program start" and it works. The problem occurs when I want to run my script. I tried to add my code these code lines as you suggest to define a startup.py file in my directory (C:\Users\USER\.qgis2\python), but as soon as I open QGIS I get this error:


"Traceback (most recent call last): File "", line 1, in File "C:/PROGRA~1/QGISWI~1/apps/qgis-ltr/./python\qgis\utils.py", line 478, in _import mod = _builtin_import(name, globals, locals, fromlist, level) File "C:/PROGRA~1/QGISWI~1/apps/qgis-ltr/./python\qgis\user.py", line 38, in execfile(startuppy, locals(), globals()) File "C:/Users/USER/.qgis2//python\startup.py", line 17, in layers =iface.legendInterface().layers() NameError: name 'iface' is not defined"

Answer



To automatically open the QGIS Python Console when QGIS starts, you can read How to show Python console at QGIS program start.


However, I guess your ultimate intention is to run the script (even if it doesn't run from the QGIS Python Console), so you can create (if it doesn't exist already) a startup.py file in the QGIS Python directory:




  • On GNU/Linux: /home/USER/.qgis2/python/

  • On Windows: C:\Program Files\QGIS\python\ or C:\Documents and Settings\$USERNAME\.qgis\python\ (I don't really know about paths on Windows 10, but if you have problems finding the correct Python path, just open the Plugin manager, click on any installed 3rd party plugin and read the path where it's installed).


And then either:




  1. Call your script from startup.py:


    import my_script     
    my_script.run() # If that's the method you want to execute from your script


    or,




  2. Just put your script code into the startup.py file, which is the same as copying your Python script to the aforementioned folder and rename it to startup.py






EDIT: Explain caveat when using the startup.py approach and suggest another approach


startup.py will be executed everytime you open QGIS, which gives you no control on whether you want to automatically run your script or just run QGIS to work on a new project.



Therefore, I suggest you to take another approach: use QGIS project macros.


Say you have a script (myScript.py) you want to run when QGIS starts from your schedule tasks. Copy/move the script into the QGIS Python path or into the same directory where your QGIS project will be saved.


Let's assume your script looks like this:


from qgis.utils import iface

def run():
iface.addVectorLayer("/Path/To/geodata/myShape.shp","layer1","ogr")

Open a new QGIS project and click Project-->Project Properties...-->Macros, enable the Python macros checkbox, and write this:


import myScript


def openProject():
myScript.run()

def saveProject():
pass

def closeProject():
pass


Click OK and go to Settings-->Options...-->General to choose Enable macros Always.


Save your QGIS project as, let's say, run_script_project.qgs.


Now, every time you want to get your script automatically executed when starting QGIS, just start QGIS from the console, in this way:


qgis --project /Path/To/Your/Project/run_script_project.qgs

Once QGIS opens the project, the run() method of your script will be executed.


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...