1

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")

4
  • Has any of the tabs of the QGIS log panel any relevant comment? – Jakob Jan 27 at 7:32
  • Did you try QgsProject.instance().addMapLayer(vlayer, True) ? What you describe is exactly like when you import a layer with QgsProject.instance().addMapLayer(vlayer, False) so maybe the default value of the function is False on this version. – JULESG Jan 27 at 7:50
  • Are you returning the FlagNoThreading flag in the flags() method inside your QgsProcessingAlgorithm 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:45
  • Hi Jakob. There is no log panels that provide any information; as I mentioned, there are no exceptions thrown. JULESG, I took your advice, no changes; issue is still present. Ben, I'm not sure what you mean, I'm new to scripting in PyGIS and so I don't know the issues commonly associated with loading layers. I'm using the template provided in the processing toolbox, within which QgsProcessingAlgorithm is called by the main class. – Yasob Jan 29 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.