2

I would like to start QGIS from the command line. At startup the Georeferencer Plugin dialog should automatically appear and a predefined raster should be loaded.

What's the best and most simple way to achieve this?

I'm aware of the QGIS Command Line Options and I guess I have to write a small Python script to do what I want at startup. Unfortunately I have no programming experience in Python.

1 Answer 1

3

I think writing a startup.py script as you mentioned is probably the best and simplest way. The following is a possible script which:

  1. Enables the Georeferencer GDAL plugin.
  2. Defines a function which loads the raster and triggers the plugin.
  3. Executes the function once the initialization process has completed (i.e. when QGIS has fully loaded).

Here is the code:

import os
from qgis.utils import iface
from PyQt4.QtCore import QSettings
from PyQt4.QtGui import QAction

QSettings().setValue( "Plugins/georefplugin", True )

def load_layer():
    path_to_raster = "path/to/raster"
    raster_name = os.path.splitext(os.path.basename(path_to_raster))[0]
    iface.addRasterLayer(path_to_raster, raster_name)
    iface.mainWindow().findChild(QAction, 'mActionRunGeoref').trigger()

iface.initializationCompleted.connect(load_layer)

Save this in your /.qgis2/python/ directory. Then when you run your command line, just type:

qgis

Which should automatically run the startup script.


Edit:

As per my comment below, I don't think it's currently possible to load a raster directly into the Georeferencer plugin. A workaround could be to trigger the Open Raster icon so that when QGIS is loaded, the plugin and the dialog will immediately be loaded for the user to select their raster.

You could use the following in your startup script for such workaround:

import os
from qgis.utils import iface
from PyQt4.QtCore import QSettings
from PyQt4.QtGui import QAction, QMainWindow

QSettings().setValue( "Plugins/georefplugin", True )

def load_georeferencer():
    iface.mainWindow().findChild(QAction, 'mActionRunGeoref').trigger()
    for x in iface.mainWindow().findChildren(QMainWindow): 
        if x.objectName() == 'QgsGeorefPluginGuiBase':
            for y in x.children():
                if 'mActionOpenRaster' in y.objectName():
                    y.trigger()

iface.initializationCompleted.connect(load_georeferencer)
6
  • That's great - thanks for your answer. I accepted it, because it fits to the question, but actually I wanted the raster to be loaded within the georeferencer plugin window. Do you have another tip how to achieve this? iface.addRasterLayer doesn't work in this context, I guess?
    – nevrome
    Commented Jun 19, 2017 at 13:11
  • @nevrome - Most welcome! Hmm unfortunately, the georeference plugin is integerated into QGIS and there doesn't seem to be a python API to access it. You could, if you wanted, trigger the Open Raster icon from the plugin when QGIS has loaded so that the user can then select their raster. But I don't think you can add a raster within the plugin at the moment.
    – Joseph
    Commented Jun 19, 2017 at 13:38
  • 1
    Ok. I will try to develop a useful workflow to make it as convenient as possible. Thanks again for your help.
    – nevrome
    Commented Jun 19, 2017 at 13:44
  • @nevrome - I've edited the post to include a method which immediately loads the georeferencer plugin and the Open Raster dialog. Hope this is somewhat helpful :)
    – Joseph
    Commented Jun 19, 2017 at 14:03
  • Awesome! I would like to upvote twice!
    – nevrome
    Commented Jun 19, 2017 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.