0

I am trying to load a vector file to QGIS Python console.


v_layer = iface.addVectorLayer("G://Consultancy\Data\Transfer\Bodhi (Louis) Gorringe\QGIS\Shapefiles\Biological Shapefiles\Seabirds\At sea densities of gannet in the breeding season\710_br.shp")

Below identifies the error code I always get when I enter this script.

enter image description here

From looking at other examples online it seems that they add a bit at the end of the script within single quote marks ('). As seen in the image below in the red box.

enter image description here

I am aware this is specified for where they have saved their documents, but I am unaware of what the (') columns mean. I feel this is the reason why my vector layer wont load. Can anyone confirm, or know a reason why the vector layer wont loads?

  • 3
    Please remember to include the error text as ASCII in the body of your question. Images are not legible on all devices and are not searchable. – Vince Jan 31 at 13:48
5

These are mentioned in the QGIS documentation:

layer = iface.addVectorLayer("/path/to/shapefile/file.shp", "layer name you like", "ogr")

So the parameters are:

  1. Path to shapefile;
  2. The name you want to give to the shapefile when it is loaded;
  3. The name of the vector data source/provider.
3

Along with @Joseph answer, the shp path has both "/" and "\" characters which will cause other errors. Here are three examples of valid slash path options:

# three valid shp path options
layer = iface.addVectorLayer("C:/path/to/shapefile/file.shp", "layer name you like", "ogr")
layer = iface.addVectorLayer("C:\\path\\to\\shapefile\\file.shp", "layer name you like", "ogr")
layer = iface.addVectorLayer(r"C:\path\to\shapefile\file.shp", "layer name you like", "ogr")
  • will the name of the vector data source/provide most likley be 'ogr'. I am trying to get used to python scripting and some of my shapefiles dont work with 'ogr' ending. How would I identify the data source/provider for individual shapefiles? – Bodhi Jan 31 at 14:34
  • Good question, however I would recommend you post that as a new question in this forum and include any error messages you may receive when using the addVectorLayer() method. – artwork21 Jan 31 at 14:46
  • Thankyou both, and sounds good to me. Will do it now – Bodhi Jan 31 at 14:47

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.