Index de l'article

Analyze layers

Count layer fields

project = QgsProject.instance()
myLayer = project.mapLayersByName("My layer")[0]
print(myLayer.fields().count())

Count layer features

project = QgsProject.instance()
myLayer = project.mapLayersByName("Adresses")[0]
layerNumberFeatures = myLayer.featureCount()
print(layerNumberFeatures)

Display layer fields names

project = QgsProject.instance()
myLayer = project.mapLayersByName("My layer")[0]
print(myLayer.fields().names())

Get layer projection

project = QgsProject.instance()
myLayer = project.mapLayersByName("Adresses")[0]
layerProj = myLayer.crs()
print(layerProj)

Check if a layer is valid

myLayer = QgsVectorLayer(monRepertoire, 'My layer', 'ogr')
 
if myLayer.isValid():
    print('My layer', 'is valid')