0

Hm, I just encountered a very strange phenomenon with this function I just wrote:

 def loadLayersFromFolder(self, sourceFolder):
 """
 Load all Shpfiles as Layers from a given path (i. e. folder).
 """
 layerList = []
 # Get the filenames using glob
 # see https://gis.stackexchange.com/questions/408958/recursively-load-shapefiles-from-directory-structure-based-on-name-pattern-using
 QgsMessageLog.logMessage("loadLayersFromFolder mit sourceFolder= " + sourceFolder, level=Qgis.Info)
 for filename in glob.glob(sourceFolder + "\*.shp", recursive=True):
 QgsMessageLog.logMessage(filename, level=Qgis.Info)
 layer = iface.addVectorLayer(filename, os.path.basename(filename)[:-4], "ogr")
 layerList.append(layer)
 if not layer.isValid():
 QgsMessageLog.logMessage("Layer " + filename + " failed to load!", level=Qgis.Error)
 #QgsMessageLog.logMessage("Layer " + filename + " failed to load!", level=Qgis.Warning)
 else:
 QgsProject.instance().addMapLayer(layer, True)
 #QgsProject.instance().reloadAllLayers()
 
 return layerList

After running the function the layers are shown in the layer tree, attribute table etc. everything is working fine. However the layers are not shown on the map until I restart QGIS completely. It even goes as far as that no other layer in my existing project is working anymore (when toggeling them on/off) and when I zoom into the map it blurrs. As you can see I even tried the True flag in addMapLayer() and reloadAllLayers() but to no avail.

Here's what its look like when I zoom: enter image description here

I also tried with a new project, same result (ok, the map canvas is obviously all white in this case).

Is this an error or am I missing something?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Sep 20, 2022 at 8:44

1 Answer 1

2

The QgisInterface.addVectorLayer() method, as well as returning a QgsVectorLayer object, also adds the layer to the project. So you are doing this twice using two different methods. I suggest changing:

layer = iface.addVectorLayer(filename, os.path.basename(filename)[:-4], "ogr")

To:

layer = QgsVectorLayer(filename, os.path.basename(filename)[:-4], "ogr")

Testing with the following simplified snippet worked fine for me:

import glob
def loadLayersFromFolder(sourceFolder):
 layerList = []
 for filename in glob.glob(sourceFolder + "/*.shp", recursive=True):
 # Construct the layer object
 layer = QgsVectorLayer(filename, os.path.basename(filename)[:-4], "ogr")
 layerList.append(layer)
 if not layer.isValid():
 QgsMessageLog.logMessage("Layer " + filename + " failed to load!", level=Qgis.Error)
 else:
 # Now add it to the project
 QgsProject.instance().addMapLayer(layer, True)
 return layerList
 
lyrs = loadLayersFromFolder('/path/to/some/folder/containing/shapefiles')
print(lyrs)

P.s. On Ubuntu, I also had to change the backslash to a forward slash here: "/*.shp"

answered Sep 20, 2022 at 10:34
1
  • 1
    Thanks so much Ben, works like a charm :-) I'm going to leave a note in the question I based my code on. Commented Sep 21, 2022 at 7:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.