EDIT: Code sample at bottom.
I am running into an issue with QGIS v3.12.3.
Currently, I am attempting to import a shapefile using QgsVectorLayer's addMapLayer method, as outlined in the cookbook (the first table of https://docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/loadlayer.html section 3.1) via a processing toolbox script.
After executing the code however, the imported shapefile is not displayed on the Layers list in the QGIS GUI for the user; nothing shows up, as if nothing had been imported at all.
This problem appears to be unique to when the code is executed in a script; running the code directly in the built-in Python console appears to work fine and as intended, with the shapefile being imported and displayed normally.
The confusing thing is that the script runs without errors, and the shapefiles appear to actually be imported properly; they are just not displayed in the GUI for the user. As an example, in the attached image you can see that the imported layer (testlayer) exists and can be selected in other scripts, but is not being displayed in the sidebar. https://i.imgur.com/HSzqAOe.png
Anecdotally, colleagues are also running into the same issue which did not exist in an earlier version of QGIS, so I was wondering if anyone else is able to replicate this problem? A simple importation of any shape file using the cookbook's guidelines successfully would be enough to let me know that the issue is on my end.
I am unable to update QGIS at this time, but changelogs for v3.14 and v3.16 do not seem to indicate this particular issue has been fixed
The following is a snippet of code that reproduces this issue:
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import *
from qgis import processing
import qgis.utils
import geopandas as gp
import pandas as pd
import os
import osmnx as ox
north_lat = 33.5050
south_lat = 33.4859
west_lon = -111.9909
east_lon = -112.0149
save_path = os.getcwd()+"/Temp/Temp/Temp/" #specify whererever save path is
custom = r'["highway"~"primary|secondary|road"]'
G = ox.graph.graph_from_bbox(north_lat, south_lat, east_lon, west_lon, network_type='drive', simplify=True, custom_filter=custom)
G_projected=ox.projection.project_graph(G, to_crs={'init': 'epsg:4326'})
ox.io.save_graph_shapefile(G_projected, filepath=save_path)
shape_layer = QgsVectorLayer(save_path + "edges.shp", "edges", "ogr")
QgsProject.instance().addMapLayer(shape_layer, True)
print("done")
QgsProject.instance().addMapLayer(vlayer, True)
? What you describe is exactly like when you import a layer withQgsProject.instance().addMapLayer(vlayer, False)
so maybe the default value of the function isFalse
on this version. – JULESG Jan 27 at 7:50FlagNoThreading
flag in theflags()
method inside yourQgsProcessingAlgorithm
subclass? Manipulating the project such as loading layers is not thread safe. If you want more useful help you should edit your question to include a reproducible code example. – Ben W Jan 27 at 8:45QgsProcessingAlgorithm
is called by the main class. – Yasob Jan 29 at 5:35