2

I try to build mapping and navigation app using QGIS 2.18 and I start by doing the same thing in this book "building-mapping-applications-with-qgis" chapters 8 but I face problem in add the basemap when I write the code and try to run it, it does not give me any errors but it show me only a white UI

import os, os.path, sys
from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
sys.path.insert(0, 'C:/Users/aldeb/Desktop/MAPTRAILS/map')
sys.path.insert(0, 'C:/Users/aldeb/Desktop/MAPTRAILS/map/resources')
from ui_mainWindow import Ui_MainWindow
import resources
from constants import *
from mapTools import *
class MapWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.connect(self.actionQuit, SIGNAL("triggered()"),self.quit)
        self.connect(self.actionZoomIn, SIGNAL("triggered()"),self.zoomIn)
        self.connect(self.actionZoomOut, SIGNAL("triggered()"),self.zoomOut)
        self.connect(self.actionPan, SIGNAL("triggered()"),self.setPanMode)
        self.connect(self.actionEdit, SIGNAL("triggered()"),self.setEditMode)
        self.connect(self.actionAddTrack, SIGNAL("triggered()"),self.addTrack)
        self.connect(self.actionEditTrack, SIGNAL("triggered()"),self.editTrack)
        self.connect(self.actionDeleteTrack, SIGNAL("triggered()"),self.deleteTrack)
        self.connect(self.actionGetInfo, SIGNAL("triggered()"),self.getInfo)
        self.connect(self.actionSetStartPoint, SIGNAL("triggered()"),self.setStartingPoint)
        self.connect(self.actionSetEndPoint, SIGNAL("triggered()"),self.setEndingPoint)
        self.connect(self.actionFindShortestPath, SIGNAL("triggered()"),self.findShortestPath)
        self.mapCanvas = QgsMapCanvas()
        self.mapCanvas.useImageToRender(False)
        self.mapCanvas.setCanvasColor(Qt.white)
        self.mapCanvas.show()
        layout = QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.mapCanvas)
        self.centralWidget.setLayout(layout)

    def zoomIn(self):
        self.mapCanvas.zoomIn()
    def zoomOut(self):
        self.mapCanvas.zoomOut()
    def quit(self):
        pass

    def setPanMode(self):
        pass

    def setEditMode(self):
        pass

    def addTrack(self):
        pass

    def editTrack(self):
        pass

    def deleteTrack(self):
        pass

    def getInfo(self):
        pass

    def setStartingPoint(self):
        pass

    def setEndingPoint(self):
        pass

    def findShortestPath(self):
        pass

# this is the code for add the raster and other layers 
    def setupMapLayers(self):
        cur_dir = os.path.dirname(os.path.realpath(__file__))
        layers = []
        filename = os.path.join(cur_dir, "", "basemap.tif")
        self.baseLayer = QgsRasterLayer(filename, "basemap")
        QgsMapLayerRegistry.instance().addMapLayer(self.baseLayer)
        layers.append(QgsMapCanvasLayer(self.baseLayer))
        filename = os.path.join(cur_dir, "data","ne_10m_populated_places","ne_10m_populated_places.shp")
        self.landmark_layer = QgsVectorLayer(filename,"landmarks", "ogr")
        QgsMapLayerRegistry.instance().addMapLayer(self.landmark_layer)
        uri = QgsDataSourceURI()
        uri.setDatabase(os.path.join(cur_dir, "data","tracks.sqlite"))
        uri.setDataSource('', 'tracks', 'GEOMETRY')
        self.trackLayer = QgsVectorLayer(uri.uri(), "Tracks","spatialite")
        QgsMapLayerRegistry.instance().addMapLayer(self.trackLayer)
        layers.append(QgsMapCanvasLayer(self.trackLayer))
        self.shortestPathLayer = QgsVectorLayer("LineString?crs=EPSG:4326","shortestPathLayer", "memory")
        QgsMapLayerRegistry.instance().addMapLayer(self.shortestPathLayer)
        layers.append(QgsMapCanvasLayer(self.shortestPathLayer))
        self.startPointLayer = QgsVectorLayer("Point?crs=EPSG:4326","startPointLayer", "memory")
        QgsMapLayerRegistry.instance().addMapLayer(self.startPointLayer)
        layers.append(QgsMapCanvasLayer(self.startPointLayer))
        self.endPointLayer = QgsVectorLayer("Point?crs=EPSG:4326","endPointLayer", "memory")
        QgsMapLayerRegistry.instance().addMapLayer(self.endPointLayer)
        layers.append(QgsMapCanvasLayer(self.endPointLayer))
        layers.reverse()
        self.mapCanvas.setLayerSet(layers)
        self.mapCanvas.setExtent(self.baseLayer.extent())

def main():
    QgsApplication.setPrefixPath("C:\\Program Files\\QGIS 2.18\\apps\\qgis-ltr", True)
    qgs = QgsApplication([], False)
    qgs.initQgis()
    app = QApplication(sys.argv)
    window = MapWindow()
    window.show()
    window.raise_()
    window.setupMapLayers()
    window.setPanMode()
    app.exec_()
    app.deleteLater()
    QgsApplication.exitQgis()
if __name__ == "__main__":
    main()

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.