class see_map(QDialog): #app2 = QgsApplication([], True) #app2.initQgis() def __init__(self, file_name): super().__init__() # Creación del Lienzo / Canvas self.canvas = QgsMapCanvas(self) self.setWindowTitle("Map of Track / Mapa del Recorrido") self.canvas.setCanvasColor(Qt.white) self.initGui() if not file_name == None: vlayer = QgsVectorLayer(file_name, "Track Converted", "ogr") #vlayer.updateExtents() self.project.instance().addMapLayer(vlayer) vlayer.renderer().symbol().setWidth(2) vlayer.renderer().symbol().setColor(QColor.fromRgb(250, 0, 0)) self.view.refreshLayerSymbology(vlayer.id()) uri = 'crs=EPSG:4326&dpiMode=7&format=image/jpeg&layers=IGNBaseTodo&styles=default&tileMatrixSet=EPSG:4326&url=http://www.ign.es/wmts/ign-base' map_spain = QgsRasterLayer(uri, "Spain", "wms") self.root.insertLayer(1, map_spain) self.project.instance().addMapLayer(map_spain) if not map_spain.isValid(): print("Track failed to load! /n" + file_name) else: print("Track Load Correct! " + file_name) else: uri = 'crs=EPSG:4326&dpiMode=7&format=image/jpeg&layers=IGNBaseTodo&styles=default&tileMatrixSet=EPSG:4326&url=http://www.ign.es/wmts/ign-base' map_spain = QgsRasterLayer(uri, "Spain", "wms") self.project.instance().addMapLayer(map_spain) if not map_spain.isValid(): print("Track failed to load! /n" + file_name) else: print("Track Load Correct! " + file_name) def zoomIn(self): self.canvas.setMapTool(self.toolZoomIn) def zoomOut(self): self.canvas.setMapTool(self.toolZoomOut) def pan(self): self.canvas.setMapTool(self.toolPan) def file(self): filename, _ = QFileDialog.getOpenFileName(self, 'Selecciona Fichero GPX', 'c:/', 'QGis (*.qgz)') if filename: self.project.read(filename) def initGui(self): icon = QIcon() icon.addPixmap( QPixmap( "C:/Roberto/Visual_Studio_Code/GisBike/programa/IMG/Qgis.png"), QIcon.Normal, QIcon.Off) self.setWindowIcon(icon) self.setWindowModality(Qt.WindowModal) self.setWindowFlags(Qt.Window | Qt.WindowSystemMenuHint | Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint) self.resize(1200, 600) self.verticalLayout_2 = QVBoxLayout(self) self.splitter = QSplitter(self) self.splitter.setOrientation(Qt.Horizontal) self.actionZoomIn = QPushButton("Zoom in", self.splitter) self.actionZoomOut = QPushButton("Zoom out", self.splitter) self.actionPan = QPushButton("Pan", self.splitter) self.actionFile = QPushButton("File", self.splitter) self.project = QgsProject.instance() self.project.read('C:/Users/Fcc/Desktop/QGis/pruebas2.qgz') self.root = QgsProject.instance().layerTreeRoot() self.bridge = QgsLayerTreeMapCanvasBridge(self.root, self.canvas) self.bridge.setCanvasLayers() self.bridge.setAutoSetupOnFirstLayer(True) #https://gis.stackexchange.com/questions/141516/adding-legend-to-canvas-in-standalone-pyqgis-application self.model = QgsLayerTreeModel(self.root) self.model.setFlag(QgsLayerTreeModel.AllowNodeReorder) self.model.setFlag(QgsLayerTreeModel.AllowNodeRename) self.model.setFlag(QgsLayerTreeModel.AllowNodeChangeVisibility) self.model.setFlag(QgsLayerTreeModel.ShowLegend) self.view = QgsLayerTreeView(self.splitter) self.view.setModel(self.model) self.view.setFixedWidth(150) #self.view.resize(150,600) self.widget = QWidget(self.splitter) self.verticalLayout = QVBoxLayout(self.widget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout = QHBoxLayout() self.horizontalLayout.addWidget(self.actionZoomIn) self.horizontalLayout.addWidget(self.actionZoomOut) self.horizontalLayout.addWidget(self.actionPan) self.horizontalLayout.addWidget(self.actionFile) self.verticalLayout.addLayout(self.horizontalLayout) self.verticalLayout.addWidget(self.canvas) self.verticalLayout_2.addWidget(self.splitter) self.actionZoomIn.clicked.connect(self.zoomIn) self.actionZoomOut.clicked.connect(self.zoomOut) self.actionPan.clicked.connect(self.pan) self.actionFile.clicked.connect(self.file) # create the map tools self.toolPan = QgsMapToolPan(self.canvas) self.toolZoomIn = QgsMapToolZoom(self.canvas, False) # false = in self.toolZoomOut = QgsMapToolZoom(self.canvas, True) # true = out