1

I have this standalone script to display shapefile. I followed the qgis python cookbook procedure, but i got this error. "NameError: name 'QgsApplication' is not defined". Here is my whole code. Someone please tell me where am wrong. I verified my setPrefixPath by running it in Qgis python console

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys, os

class Mapbox(QMainWindow):    
    def __init__(self, shapefile):
        QMainWindow.__init__(self)
        self.setWindowTitle("Map Box Viewer")
        canvas = QgsMapCanvas()
        canvas.useImageToRender(False)
        canvas.setCanvasColor(Qt.white)
        canvas.show()

        layer = QgsVectorLayer(shapefile, "layer1", "ogr")
        if not layer.isValid():
            raise IOError("Invalid shapefile")

        QgsMapLayerRegistry.instance().addMapLayer(layer)
        canvas.setExtent(layer.extent())
        canvas.setLayerSet([QgsMapCanvasLayer(layer)])        
        self.setCentralWidget(canvas)

app = QApplication(sys.argv)        
QgsApplication.setPrefixPath("C:\OSGeo4W64\apps\qgis", True)        
QgsApplication.initQgis()        
viewer = Mapbox(r"shapefile.shp")        
viewer.show()

app.exec_()        
QgsApplication.exitQgis()
0

You're missing the import statement for the QgsApplication, add from qgis.core import QgsApplication. This may give you an ImportError if your qgis.core is not in your enviroment variables, the process to fix this vary depending on your operational system

Also, QgsApplication is a subclass from QApplication so you do not need to use both. Here's what you can do:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import QgsApplication, QgsMapCanvas, QgsVectorLayer, QgsMapLayerRegistry, QgsMapCanvasLayer
import sys, os

class Mapbox(QMainWindow):    
    def __init__(self, shapefile):
        QMainWindow.__init__(self)
        self.setWindowTitle("Map Box Viewer")
        canvas = QgsMapCanvas()
        canvas.useImageToRender(False)
        canvas.setCanvasColor(Qt.white)
        canvas.show()

        layer = QgsVectorLayer(shapefile, "layer1", "ogr")
        if not layer.isValid():
            raise IOError("Invalid shapefile")

        QgsMapLayerRegistry.instance().addMapLayer(layer)
        canvas.setExtent(layer.extent())
        canvas.setLayerSet([QgsMapCanvasLayer(layer)])        
        self.setCentralWidget(canvas)

app = QgsApplication(sys.argv, True)  # the second arg "True", says that you are using a interface
app.setPrefixPath("C:/OSGeo4W64/apps/qgis", True)  # Use forward slash in paths or os.path
app.initQgis()        
viewer = Mapbox(os.path.abspath("shapefile.shp"))  # QgsVectorLayer needs the absolute path to create a valid layer
viewer.show()

app.exec_()        
app.exit()

I have add some minor changes to the code.

Side note: QGis and PyQt are different things, PyQt is a python api of Qt. It is used to create interfaces, QGis makes use of PyQt to make its interfaces

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.