Tuesday 21 May 2019

qgis - Open Georeferencer and load raster at startup



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.



Answer



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)

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