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)