Wednesday, 7 August 2019

pyqgis - How to run a simple python script for QGIS from outside (e.g. Sublime Text)?


I would like to run a few small and simple python scripts for QGIS (Mac) from "outside" of QGIS. With outside I mean in this context, either the normal os command line (terminal.app) or even better, directly out of Sublime Text (text-editor), but definitely not via the inbuilt QGIS python console.


I've read through various tutorial e.g. http://www.qgis.org/pyqgis-cookbook/intro.html#python-applications and I am able to get a reference to the QGIS app, but unfortunately not to qgis.utils.iface or something else deeper. This little code snippet should for instance print out the name of the active layer ... here is what I have:


    import sys
sys.path.append("/Applications/QGIS.app/Contents/Resources/python")

from qgis.core import *
import qgis.utils

print "helo" # console output: helo


QgsApplication.setPrefixPath("/Applications/QGIS.app/", True)
QgsApplication.initQgis()

print QgsApplication # console output:

print qgis.utils.iface # = console output: none

aLayer = qgis.utils.iface.activeLayer()
print aLayer.name()


QgsApplication.exitQgis()

Please don't reply me now how to create a plugin or something else "heavy". I am just looking for a quick an easy way to shoot scripts out of a comfortable text-editor to QGIS.



Answer



You can't get a reference to the iface object here because it doesn't exist in this context. The iface (QgisInterface) object is a convenience object for plugins, or scripts running inside QGIS, to access the main objects e.g. the map canvas, legend, composer, etc, and only exists when the main application is running.


When you create a standalone Python script using the QGIS APIs none of this stuff exists because you are making your own mapping application.


There are three different situations:



  1. A QGIS plugin


  2. A script that runs inside QGIS (not a plugin) for automation

  3. Standalone app using QGIS APIs


1. and 2. have access to iface, the last one doesn't.


For 3 if you want to create a script that opens a layer in a map canvas you would do this after QgsApplication.initQgis()


map = QgsMapCanavs()
layer = QgsVectoryLayer('path.shp','myshapefile','ogr')
map.setLayerSet([layer])

However if you are really looking for something like 2 then you can just write this in your script editor



from qgis.core import *
from qgis.gui import *
import qgis.utils

qgis.utils.iface.activeLayer()

but this has to be run inside QGIS for qgis.utils to work. That can be done by putting the script on PATH and running import scriptname in the Python console or by using the ScriptRunner plugin.


Note the following isn't QGIS yet


There is a number 4 that hasn't been added yet, and hopefully will be in the future, and that is the option to run QGIS with a commandline arg to say run this code.


For example:



qgis --code=mycodefile.py

Plugin logging (version 1.8)


You can use the QgsMessageLog class to log information to the QGIS log window. The yellow exclamation mark in the bottom right corner.


from qgis.core import *
log = lambda m: QgsMessageLog.logMessage(m,'My Plugin')
log('My message')

or without using lambda


QgsMessageLog.logMessage('My message', 'My Plugin')


I prefer the lambda based one as it is shorter and less typing any time you want to log something.


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