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 season710円_br.shp")
Below identifies the error code I always get when I enter this script.
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.
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?
-
3Please 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– Vince2019年01月31日 13:48:01 +00:00Commented Jan 31, 2019 at 13:48
2 Answers 2
These are mentioned in the QGIS documentation:
layer = iface.addVectorLayer("/path/to/shapefile/file.shp", "layer name you like", "ogr")
So the parameters are:
- Path to shapefile;
- The name you want to give to the shapefile when it is loaded;
- The name of the vector data source/provider.
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?Louis Tate– Louis Tate2019年01月31日 14:34:44 +00:00Commented Jan 31, 2019 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– artwork212019年01月31日 14:46:34 +00:00Commented Jan 31, 2019 at 14:46
-
Thankyou both, and sounds good to me. Will do it nowLouis Tate– Louis Tate2019年01月31日 14:47:59 +00:00Commented Jan 31, 2019 at 14:47