def currentRowChanged(self, selected, deselected): indexes = self.annotationView.selectedIndexes() if len(indexes) == 0: return rowIndex = indexes[0].row() targetLayer = None for node in QgsProject.instance().layerTreeRoot().findLayers(): layer = node.layer() if isinstance(layer, QgsRasterLayer) and layer.dataProvider( ).dataSourceUri() == self.model.item(rowIndex, 2).text(): targetLayer = layer break root = QgsProject.instance().layerTreeRoot() if targetLayer is None: file = QFileInfo(self.model.item(rowIndex, 2).text()) path = file.filePath() base = file.baseName() targetLayer = QgsRasterLayer(path, base) QgsProject.instance().addMapLayer(targetLayer, False) root.insertLayer(0, targetLayer) else: clonedLayer = targetLayer.clone() QgsProject.instance().removeMapLayer(targetLayer.id()) QgsProject.instance().addMapLayer(clonedLayer) targetLayer = clonedLayer iface.setActiveLayer(targetLayer) crs = QgsCoordinateReferenceSystem(self.model.item(rowIndex, 3).text()) QgsProject.instance().setCrs(crs) iface.mapCanvas().setExtent(targetLayer.extent()) point1 = QgsPointXY(float(self.model.item(rowIndex, 8).text()), float(self.model.item(rowIndex, 9).text())) point2 = QgsPointXY(float(self.model.item(rowIndex, 8).text()), float(self.model.item(rowIndex, 11).text())) point3 = QgsPointXY(float(self.model.item(rowIndex, 10).text()), float(self.model.item(rowIndex, 11).text())) point4 = QgsPointXY(float(self.model.item(rowIndex, 10).text()), float(self.model.item(rowIndex, 9).text())) self.resetSelectionBand() self.selectionBand.addPoint(point1, False) self.selectionBand.addPoint(point2, False) self.selectionBand.addPoint(point3, False) self.selectionBand.addPoint(point4, True) self.selectionBand.setOpacity(0.5) self.selectionBand.show()
def testClone(self): myPath = os.path.join(unitTestDataPath('raster'), 'band1_float32_noct_epsg4326.tif') myFileInfo = QFileInfo(myPath) myBaseName = myFileInfo.baseName() layer = QgsRasterLayer(myPath, myBaseName) renderer = layer.renderer().clone() renderer.setOpacity(33.3) layer.setRenderer(renderer) # clone layer clone = layer.clone() # generate xml from layer layer_doc = QDomDocument("doc") layer_elem = layer_doc.createElement("maplayer") layer.writeLayerXml(layer_elem, layer_doc, QgsReadWriteContext()) # generate xml from clone clone_doc = QDomDocument("doc") clone_elem = clone_doc.createElement("maplayer") clone.writeLayerXml(clone_elem, clone_doc, QgsReadWriteContext()) # replace id within xml of clone clone_id_elem = clone_elem.firstChildElement("id") clone_id_elem_patch = clone_doc.createElement("id") clone_id_elem_patch_value = clone_doc.createTextNode(layer.id()) clone_id_elem_patch.appendChild(clone_id_elem_patch_value) clone_elem.replaceChild(clone_id_elem_patch, clone_id_elem) # update doc clone_doc.appendChild(clone_elem) layer_doc.appendChild(layer_elem) # compare xml documents self.assertEqual(layer_doc.toString(), clone_doc.toString())