1

I'm trying to write a stand alone script that will open a canvas with map tools and zoom to a lat/long with raster images. I'm starting by trying to run the script here http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/canvas.html#using-map-tools-with-canvas, and combining it with the standalone script here: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/intro.html#using-pyqgis-in-standalone-scripts.

from qgis.gui import *
from PyQt4.QtGui import QAction, QMainWindow
from PyQt4.QtCore import SIGNAL, Qt

QgsApplication.setPrefixPath(""C:/OSGeo4W/apps/qgis"", True)

# create a reference to the QgsApplication, setting the
# second argument to False disables the GUI
qgs = QgsApplication([], False)

# load providers
qgs.initQgis()

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.setLayerSet([QgsMapCanvasLayer(layer)])

    self.setCentralWidget(self.canvas)

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

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

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

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

    # create the map tools
    self.toolPan = QgsMapToolPan(self.canvas)
    self.toolPan.setAction(actionPan)
    self.toolZoomIn = QgsMapToolZoom(self.canvas, False) # false = in
    self.toolZoomIn.setAction(actionZoomIn)
    self.toolZoomOut = QgsMapToolZoom(self.canvas, True) # true = out
    self.toolZoomOut.setAction(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)   

The console is returning:

C:\OSGeo4W\bin\python.exe C:/OSGeo4W/Pytest2/canvasTools.py
ImportError: No module named site

Process finished with exit code 1
4
  • Could you edit your question to describe exactly what the problem is please? I.e. do you receive an error message when you run the script? Does it run but nothing shows up? etc.
    – Joseph
    Jan 11 '17 at 9:46
  • Yes, I added the error message and a little bit to the intro. Nothing other the error message shows up. Jan 18 '17 at 3:44
  • Check if you have the python module site.py in your QGIS directory. E.g. C:/OSGeo4W64/apps/Python27/Lib/site.py
    – Joseph
    Jan 18 '17 at 10:09
  • Yes I have that in my QGIS directory. Jan 22 '17 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.