PyQGIS Georeferencing Layer Path – How to Open Georeferencer with Layer Path in PyQGIS Plugin

georeferencinginterfacepyqgisqgis-3qgis-plugins

I develop a QGIS plugin that includes a step of georeferencing. I want to open the Georeferencer from QGIS and to load an image with its path when I click on a PushButton.

I found this topic : Open Georeferencer and load raster at startup

which seemed to conclude that it's not possible to pass an argument when opening the Georeferencer (and the proposed code doesn't really work for me) :

self.iface.mainWindow().findChild(QAction, "&Georeferencer").trigger()
for x in self.iface.mainWindow().findChildren(QMainWindow):
    if x.objectName() == 'QgsGeorefPluginGuiBase':
        for y in x.children():
            if 'mActionOpenRaster' in y.objectName():
                y.trigger()

I got the following error :
AttributeError: 'NoneType' object has no attribute 'trigger' so "&Georeferencer" surely isn't the good attribute to open it.

It's been answered for QGIS 2. Is there any way to access Georeferencer window from QGIS 3 interface and pass an image as input?

I looked at Processing toolbox and the Georeferencer is not part of it.

Best Answer

To open Georeferencer window in QGIS 3:

iface.mainWindow().findChildren(QAction, 'mActionShowGeoreferencer')[0].trigger()

To open "Open Raster" dialog

iface.mainWindow().findChildren(QAction, 'mActionOpenRaster')[0].trigger()