1

I am trying to make a standalone app, to be able to select and download some spatial data that I keep around. I've read around and found I can do that with Qgis and Qt, so I grabbed some example code from the docs, and assigned it a shapefile to open.

And it wouldn't work, regardless of what I tried with PATH and PROJ_LIB. While I was trying to work out my issues with:

proj_create_from_database: Cannot find proj.db
proj_get_authorities_from_database: Cannot find proj.db

I finally found out, I can avoid that by using:

C:\Program Files\QGIS 3.16\bin>python-qgis-ltr.bat myApp.py

Now a window is shown for a split second, then it disappears and the program finishes, as cmd is ready for new inputs.

Here is the whole code:

import sys
sys.path.append(r"C:\Program Files\QGIS 3.16\apps\qgis-ltr\python")
import os

os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'C:\Program Files\QGIS 3.16\apps\Qt5\plugins'
os.environ['PROJ_LIB '] = r'C:\Program Files\QGIS 3.16\share\proj'
os.environ['PYTHONPATH'] = r'C:\Program Files\QGIS 3.16\apps\Python37'

from qgis.PyQt.QtGui import (
    QColor,
)

from qgis.PyQt.QtCore import Qt, QRectF

from qgis.core import (
    QgsVectorLayer,
    QgsPoint,
    QgsPointXY,
    QgsProject,
    QgsGeometry,
    QgsMapRendererJob,
)

from qgis.core import *

from qgis.gui import (
    QgsMapCanvas,
    QgsVertexMarker,
    QgsMapCanvasItem,
    QgsRubberBand,
    QgsMapToolPan,
    QgsMapToolZoom,
)

from qgis.PyQt.QtWidgets import QAction, QMainWindow

class MyWnd(QMainWindow):
    def __init__(self, layer):
        QMainWindow.__init__(self)

        self.canvas = QgsMapCanvas()
        self.canvas.setCanvasColor(Qt.white)

        self.canvas.setExtent(layer.extent())
        self.canvas.setLayers([layer])

        self.setCentralWidget(self.canvas)

        self.actionZoomIn = QAction("Zoom in", self)
        self.actionZoomOut = QAction("Zoom out", self)
        self.actionPan = QAction("Pan", self)

        self.actionZoomIn.setCheckable(True)
        self.actionZoomOut.setCheckable(True)
        self.actionPan.setCheckable(True)

        self.actionZoomIn.triggered.connect(self.zoomIn)
        self.actionZoomOut.triggered.connect(self.zoomOut)
        self.actionPan.triggered.connect(self.pan)

        self.toolbar = self.addToolBar("Canvas actions")
        self.toolbar.addAction(self.actionZoomIn)
        self.toolbar.addAction(self.actionZoomOut)
        self.toolbar.addAction(self.actionPan)

        # create the map tools
        self.toolPan = QgsMapToolPan(self.canvas)
        self.toolPan.setAction(self.actionPan)
        self.toolZoomIn = QgsMapToolZoom(self.canvas, False) # false = in
        self.toolZoomIn.setAction(self.actionZoomIn)
        self.toolZoomOut = QgsMapToolZoom(self.canvas, True) # true = out
        self.toolZoomOut.setAction(self.actionZoomOut)

        self.pan()

    def zoomIn(self):
        self.canvas.setMapTool(self.toolZoomIn)

    def zoomOut(self):
        self.canvas.setMapTool(self.toolZoomOut)

    def pan(self):
        self.canvas.setMapTool(self.toolPan)

qgs = QgsApplication([], True)

qgs.initQgis()

pathToShp = r"D:\Docs\myTestShape.shp"
inLayer = QgsVectorLayer(pathToShp, "myTestShape", "or")

w = MyWnd(inLayer)
w.show()

What do I need to do to see something displayed and pan around it?

1 Answer 1

2

You need to add at the end of your code the following

exitcode = qgs.exec()
QgsApplication.exitQgis()
sys.exit(exitcode)

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.