4

I loaded vector layers and rendered them without a problem but when I tried to add a raster layer as well the map suddenly only shows the raster image and my vector layers are missing.

Anything I can try to diagnose the issue here. I was following the routine steps in the PyQGIS 2.0 cookbook.

    import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    from qgis.core import *
    from qgis.gui import *
    import sys
    import os
    from mapui import Ui_MainWindow

    os.environ['QGISHOME'] = 'C:\\Program Files\\QGIS Brighton\\apps\\qgis'


    class ShapeViewer(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        #Required by Qt4 to initialize the UI
        self.setupUi(self)

        #set the title for the app
        self.setWindowTitle("Map Viewer")

        #create the map canvas
        self.canvas = QgsMapCanvas()
        self.canvas.setCanvasColor(Qt.white)
        self.canvas.enableAntiAliasing(True)
        #self.canvas.useImageToRender(False)
        self.canvas.show()

        self.clickTool = QgsMapToolEmitPoint(self.canvas)

        self.layout = QVBoxLayout(self.QgsMapCanvas)
        self.layout.addWidget(self.canvas)

        layers = []

        uri = QgsDataSourceURI()
        #underlying map
        raster_layer = QgsRasterLayer("d:\\working\\hyp_lr\\hyp_lr\\hyp_lr.tif", 'geography layer')

        if not raster_layer.isValid():
            print "raster layer is invalid!"
            return

        QgsMapLayerRegistry.instance().addMapLayer(raster_layer)
        self.canvas.setExtent(raster_layer.extent())
        cl_raster = QgsMapCanvasLayer(raster_layer)

        layers.append(cl_raster)


        uri = QgsDataSourceURI()
        uri.setConnection("my_linux_box","5432","mydb","me","mypwd")
        uri.setDataSource('public','some_table','geom','')
        vector_layer = QgsVectorLayer(uri.uri(), 'Some Data', "postgres")

        if not vector_layer.isValid():
            print "Vector layer is invalid!"
            return

        QgsMapLayerRegistry.instance().addMapLayer(vector_layer)

        self.canvas.setExtent(vector_layer.extent())

        vector_canvas_layer = QgsMapCanvasLayer(vector_layer)
        layers.append(vector_canvas_layer)


        ####
        self.canvas.setLayerSet(layers)


        self.canvas.show()



def main(argv):
    #create Qt application
    app = QApplication(argv)

    #init qgis libs
    qgis_prefix = 'C:\\OSGEO4~1\\apps\\qgis'
    QgsApplication.setPrefixPath(qgis_prefix, True)
    QgsApplication.initQgis()

    #create the main window
    wnd = ShapeViewer()

    #Move the app window to upper left
    wnd.move(100, 100)
    wnd.show()

    #run!
    retval = app.exec_()

    #exit
    QgsApplication.exitQgis()
    sys.exit(retval)


if __name__=='__main__':
    main(sys.argv)
4
  • So the raster layer is on top of the vector layers?
    – underdark
    Commented Jan 5, 2015 at 18:24
  • no raster's the first layer I load and then subsequently load the vector layers on top of it.
    – JMI
    Commented Jan 5, 2015 at 20:49
  • Please provide the code you used. There is nothing to gain by guessing what might be wrong here.
    – underdark
    Commented Jan 5, 2015 at 20:52
  • i added it. sorry for the delay.
    – JMI
    Commented Jan 5, 2015 at 22:06

1 Answer 1

2

When you add the vector layer to the map, you need to put it on top of the raster layer.

So, instead of:

layers.append(vector_canvas_layer)

which puts the vector at the bottom, write this:

layers.insert(0, vector_canvas_layer)

which inserts the vector layer at the position 0 (at the top) of your layer stack.

You should now be able to visualize both your vector and raster data.

0

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.