1

This is what I have done.

from qgis.core import *
layer = iface.addVectorLayer(r"C:\\Users\\username\\Desktop\\coastline.shp","coast","ogr")

or

from qgis.core import *
layer = QgsVectorLayer(r"C:\\Users\\username\\Desktop\\coastline.shp","coast","ogr")

Basically it doesn't even appear in the Layers section of QGIS.

The first one says layer is not valid and the second it doesn't cause any problems but it isn't in the layers at all of Qgis?

  • How about r"C:\Users\username\Desktop\coastline.shp" as the path? The r implies that you don't need to escape backslashes, and you were missing a "\" after "C:". – LuWi Jun 12 '18 at 8:28
  • It had the same effect. Why it doesn't show up in the Layers section? – user122244 Jun 12 '18 at 8:35
  • What is the difference between the two approaches you show? Does the file show content when loading via drag-and-drop? Does a "Select CRS" window appear, maybe in the background? – LuWi Jun 12 '18 at 8:42
  • i got what you meant check now these two approaches to see the differences. – user122244 Jun 12 '18 at 8:51
  • 1
    Loading a layer is one thing, another is to register it with the canvas/legend. I strongly recommend the pyqgis cookbook: docs.qgis.org/2.18/en/docs/pyqgis_developer_cookbook/… – Andreas Müller Jun 12 '18 at 9:13
3
from qgis.core import *
layer = QgsVectorLayer(r"C:\\Users\\username\\Desktop\\coastline.shp","coast","ogr")

just loads the specified layer to a variable named "layer" to load layer into QGIS mapcanvas do something like this for QGIS 3.x

QgsProject.instance().addMapLayer(layer)

or

QgsMapLayerRegistry.instance().addMapLayer(layer)

for QGIS 2.x

Your Answer

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

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