3

I have been piecing code together from a number of posts to python outside of QGIS that will open a QGIS instance and then load specific data I feed it. I am not to familiar with using the QGIS API commands, so I am a bit lost. I assume that I am not properly pointing to the instance that is being open. Seems like there are a lot of questions revolving around this topic, but I haven't been able to find something up to date (QGIS 3.16) or that uses scripting outside of the QGIS shell.

Any suggestions?

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

app_path = "C:/Program Files/QGIS 3.16/bin/qgis-ltr-bin.exe"

def open_app(application_path): subprocess.call(application_path)

def open_layers(): qgishome = "C:/Program Files/QGIS 3.16/apps/qgis-ltr/" app = QgsApplication([], True) QgsApplication.setPrefixPath(qgishome, True) QgsApplication.initQgis()

vlayer = QgsVectorLayer("D:/my_shapefile.shp", 'layer', 'ogr')

### EDITS ###
ex = vlayer.extent()
canvas = QgsMapCanvas()
canvas.setExtent(ex)
### EDITS ###

QgsProject.instance().addMapLayer(vlayer)

def main(): open_app(app_path) open_layers()

if name == "main": main()

Class Edits

With the advice from @J. Monticolo I created a class and defined self.canvas, but the same problem occurs. A small window appears and disappears as if the app loads and exits. I'll admit, I have not worked with classes much so I am a bit rusty.

class Test():
    def __init__(self):
        pass
def open_layers(self):
    qgishome = "C:/Program Files/QGIS 3.16/apps/qgis-ltr/"
    app = QgsApplication([], True)
    QgsApplication.setPrefixPath(qgishome, True)
    QgsApplication.initQgis()

    vlayer = QgsVectorLayer("D:/my_shapefile.shp", 'layer', 'ogr')
    self.canvas = QgsMapCanvas()
    ex = vlayer.extent()
    self.canvas = QgsMapCanvas()
    self.canvas.setExtent(ex)

    QgsProject.instance().addMapLayer(vlayer)
    self.canvas.show()

def main(): app = Test() app.open_layers()

Binx
  • 1,350
  • 1
  • 9
  • 35
  • 1
    I added the QgsMapCanvas (used this post for reference). The code runs without error, but it does not open QGIS.exe and load the layer. That is why I had the subprocess.call(application_path). – Binx Jun 07 '22 at 14:04
  • It seems that I need to call app somewhere? – Binx Jun 07 '22 at 14:06
  • At the end? A small window launched for a fraction of a second and then disappeared. No errors received. Does my qgishome variable need to be pointing to an executable? – Binx Jun 07 '22 at 14:15
  • Please check edits @J.Monticolo – Binx Jun 07 '22 at 15:27
  • I found this post that adds three lines to the end of open_layers() that fixes the window from disappearing. But the window that is loaded looks almost like a white terminal that cannot do anything. It is not a QGIS instance. – Binx Jun 07 '22 at 15:37
  • self.app and self.vlayer. Same problem. Even tried self.ex. – Binx Jun 07 '22 at 15:39
  • It is a QgsMapCanvas instance, inside a Python3 windows (looks like a blank terminal). – J. Monticolo Jun 07 '22 at 15:56
  • Hmmm, maybe I did not make my question clear. I am looking to launch QGIS and then "drag and drop" wanted layers programmatically. This will allow say a user to bypass the need to do it themselves. The user will need to edit the loaded layers. So I am not looking for a "blank canvas" but actually the QGIS GUI. – Binx Jun 07 '22 at 16:16
  • If this is going to be too convoluted, then I will have the user do it manually. Just wanted to try automating the process. – Binx Jun 07 '22 at 16:17
  • See here : https://docs.qgis.org/3.22/en/docs/pyqgis_developer_cookbook/intro.html#running-python-code-when-qgis-starts – J. Monticolo Jun 07 '22 at 16:22
  • Thanks @J.Monticolo! I found another post that seems like it touches on a lot of this. I feel that I am falling into the rabbit hole, so I am going to put this project on hold for a bit. Again appreciate the help and feedback! – Binx Jun 07 '22 at 16:34

0 Answers0