Page 4 sur 18
Manage layers
Remove all the layers from the QGIS canvas
project = QgsProject.instance() project.removeAllMapLayers() project.clear() iface.mapCanvas().refresh()
Clone a layer
oldLayer = project.mapLayersByName("Old layer")[0] newLayer = oldLayer.clone() newLayer.setName('New layer') project.addMapLayer(newLayer)
Rename a layer
mes_sommets.setName('New name')
Copy-paste a layer with all his files (tab, shp ...) and open them
If you have a layer named test in a directory named my layer, you can create a copy named new file in the same directory and open them in QGIS:
import os import shutil QgsProject.instance().removeAllMapLayers() iface.mapCanvas().refresh() directory = r'C:/Users/Georges/Downloads/my layer' new_file = 'new file' # REMOVE NEW LAYER IF EXISTS files = os.listdir(directory) for f in files: if f.startswith(new_file): try: os.remove(directory + '/' + f) except: pass # COPY LAYER files = os.listdir(directory) for f in files: if not f.startswith(new_file): shutil.copyfile(directory + '/' + f, directory + '/' + new_file + '.' + f.split('.')[1]) print('Layer copied!') test = QgsVectorLayer(directory + '/test.tab', 'test', 'ogr') QgsProject.instance().addMapLayer(test) new = QgsVectorLayer(directory + '/' + new_file + '.tab', 'new file', 'ogr') QgsProject.instance().addMapLayer(new)
Change a layer's encoding
myLayer = QgsVectorLayer(r'C:/Users/Georges/Downloads/my layer/new_file.tab', new_file, 'ogr') myLayer.setProviderEncoding('ISO-8859-1')
Create a group and put a layer into
directory = r'C:/Users/Georges/Downloads/my layer' newfile = QgsVectorLayer(directory + '/new file.tab', 'newfile', 'ogr') QgsProject.instance().addMapLayer(newfile) groupName = 'Mon joli groupe' root = QgsProject.instance().layerTreeRoot() group = root.addGroup(groupName) layer = QgsProject.instance().mapLayersByName('newfile')[0] mylayer = root.findLayer(layer.id()) myClone = mylayer.clone() parent = mylayer.parent() group = root.findGroup('Mon joli groupe') group.insertChildNode(0, myClone) parent.removeChildNode(mylayer)
Hide the rasters bands
root = QgsProject.instance().layerTreeRoot() for layer in QgsProject.instance().mapLayers().values(): if layer.type() == QgsMapLayerType.RasterLayer: LayerNode = root.findLayer(layer.id()) LayerNode.setExpanded(False)
Uncheck a layer
project = QgsProject.instance() myLayer = project.mapLayersByName('My layer')[0] project.layerTreeRoot().findLayer(myLayer.id()).setItemVisibilityCheckedParentRecursive(False)
Transform a layer projection
This is not a reprojection, which need a processing.run
, here below it is just a transformation, which can be unefficient according your context.
project = QgsProject.instance() myLayer = project.mapLayersByName("My layer")[0] my_projection = QgsCoordinateReferenceSystem(4326) myLayer.setCrs(my_projection) my_transform = QgsCoordinateTransform(myLayer.crs(), iface.mapCanvas().mapSettings().destinationCrs(), project) myLayerExtent = my_transform.transform(myLayer.extent()) iface.mapCanvas().setExtent(myLayerExtent) iface.mapCanvas().refresh()
Manage the layers display order
firstLayer = project.mapLayersByName("First layer")[0] secondLayer = project.mapLayersByName("Second layer")[0] thirdLayer = project.mapLayersByName("Third layer")[0] root = project.layerTreeRoot() root.setHasCustomLayerOrder (True) order = root.customLayerOrder() order.insert(0, order.pop(order.index(firstLayer))) order.insert(1, order.pop(order.index(secondLayer))) order.insert(2, order.pop(order.index(thirdLayer))) root.setCustomLayerOrder(order)
Another way cloning the layer:
myLayer = QgsVectorLayer(myPathLayer, "Buffer area", "ogr") project.addMapLayer(peaks_buffer) root = project.layerTreeRoot() myBelowLayer = root.findLayer(myLayer.id()) myClone = myBelowLayer.clone() parent = myBelowLayer.parent() parent.insertChildNode(-1, myClone) parent.removeChildNode(myBelowLayer)
Zoom on a layer
countries = QgsVectorLayer(myPath + 'simple_countries/simple_countries.shp', 'Countries', 'ogr') extent_countries = countries.extent() iface.mapCanvas().setExtent(extent_countries) iface.mapCanvas().refresh()