0

I'm having an issue with QGIS not identifying all layers correctly.

In my project, I have a group named "Mapas" with the following structure:

Layers inside "Mapas":

localizacao_pl municipal_limits_ibge_pl.shp classificacao_climatica_koppen_pl.shp Groups inside "Mapas":

localizacao with the layers: localizacao_pl Google_Satellite_localizacao Clima with two subgroups: Mapa_clima_direito Mapa_clima_esquerdo Inside the Mapa_clima_direito and Mapa_clima_esquerdo subgroups, there are the following layers:

localizacao_pl municipal_limits_ibge_pl.shp classificacao_climatica_koppen_pl.shp

picture of layers loaded in qgis

However, when I map layers, it does not show the correct number of layers:

all_layers = QgsProject.instance().mapLayers().values()
# Print names of all layers
for layer in all_layers:
    print(layer.name())

123_localizacao_pl
123_localizacao_pl
123_localizacao_pl
758_classificacao_climatica_koppen_pl.shp
758_classificacao_climatica_koppen_pl.shp
758_municipal_limits_ibge_pl.shp
758_municipal_limits_ibge_pl.shp
Google_Satellite_localizacao

This issue is causing problems because:

I am creating a script to add layers automatically to the project root, then move them to the appropriate group and remove them from the root. This works until the "Localizacao" group is processed; once it enters the "Clima" group, the script does not delete layers from the root. It is not applying styles to all layers with the same name, only a few. How can I determine the cause of this issue?

2 Answers 2

1

The problem was that I was using this code to import layers:

def loadVectorLayer(pathToLayer, nameOfLayer, groupName):
try:
    print('Checking if path to layer exists...')
    if not os.path.exists(pathToLayer):
        raise Exception(f"Layer file not found: {pathToLayer}")
    
    print(f'Loading layer from path: {pathToLayer}...')
    vlayer = QgsVectorLayer(pathToLayer, nameOfLayer)
    if not vlayer.isValid():
        raise Exception("Layer failed to load!")
    
    # Adding layer to map
    QgsProject.instance().addMapLayer(vlayer)
    
    # Search and find the layer to clone
    print("Cloning layer...")
    layer = root.findLayer(vlayer.id())
    if layer is None:
        raise Exception("Layer ID not found!")
    clone = layer.clone()
    
    # Find the group
    print('Looking for group...')
    group = root.findGroup(groupName)
    if group is None:
        raise Exception("Group not found!")
    group.addChildNode(0, clone)
    
    # Remove the original layer
    print("Removing original layer...")
    layer_node = root.findLayer(vlayer.id())
    root.removeChildNode(layer_node)
    
    print(f'Layer {nameOfLayer} loaded successfully.')
except Exception as e:
    print(f"An error occurred: {e}")
    print('')

And it was making a copy but keeping it related to the original (when I manually deleted the original in root the copy loses reference). The solution that I found is to change to this code so I don´t need to delete the original:

def loadVectorLayer(pathToLayer, nameOfLayer, groupName):
try:
    print('Checking if path to layer exists...')
    if not os.path.exists(pathToLayer):
        raise Exception(f"Layer file not found: {pathToLayer}")
    
    print(f'Loading layer from path: {pathToLayer}...')
    vector_layer = QgsVectorLayer(pathToLayer, nameOfLayer)
    if not vector_layer.isValid():
        raise Exception("Layer failed to load!")
    
    # Adding layer to map
    QgsProject.instance().addMapLayer(vector_layer, False)
    
    layer = QgsLayerTreeLayer(vector_layer)
    # Find the group
    print('Looking for group...')
    group = root.findGroup(groupName)
    if group is None:
        raise Exception("Group not found!")
    group.insertChildNode(0, layer)
except Exception as e:
    print(f"An error occurred while loading Vector Layer: {e}")
    print('')
0

The issue is probably related to the fact that you have duplicate layers.

Not just layer names, but the layer definition - including style/visibility settings etc are duplicated.

eg: 123_localizacao_pl

It has printed it 3 times in the output. It is listed 5 times in the layer listing. However 3 of those layer definitions look similar, whilst the other 2 appear to have unique/different styles.

In other words, 3 different definitions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.