0

I used the following line to try and add a vector shapefile to my qgis map, though it gives me "invalid layer" errors. Does anyone see something I don't?

iface.addVectorLayer(r"/Users/pgcseismolab/Desktop/jaden_pgc_archive/Wells/WD/Shapefiles/wd_14/wd14.shp","test","ogr")

I'm fairly new to python- do I have to define a variable before running this command? *on Mac OSX

12
  • your layer isn't right.. first of all it's not a complete path (perhaps it should start with c:\\Users\\...) and secondly it's full of escape chars.. either double your slashes or put an r in front to make it a raw string. Read about strings, quotes and slashes here docs.python.org/2.0/ref/strings.html , that should put you on the right path. Commented Jul 12, 2017 at 21:40
  • I should mention that the file path is on mac, if that makes a difference. I'll try the suggested changes- thank you!
    – jrowley
    Commented Jul 12, 2017 at 21:42
  • It shouldn't, strings are strings, that's one of the beauties of python - it's the same on all platforms. I'm not familiar with Linux or Mac so perhaps the path is right.. I'm looking at it from a windows point of view, this might help stackoverflow.com/questions/41905880/… which says the path should use a forward slash '/' which isn't the same as a backslash '\', the forward slash is not an escape sequence initiator. Commented Jul 12, 2017 at 21:44
  • I added the suggested changes above- still no luck but I'll update with any progress. Should be the simplest python command so I'm hoping this isn't the first roadblock
    – jrowley
    Commented Jul 12, 2017 at 21:51
  • What error message do you get? Perhaps more lines of code would give some more context. Commented Jul 12, 2017 at 21:54

1 Answer 1

1

I missed an underscore in the filename wd_14. Using os.pathexists, I determined that there was an issue with the file name:

os.path.exists(r"/Users/pgcseismolab/Desktop/jaden_pgc_archive/Wells/WD/Shapefiles/wd_14/wd14.shp")

False

Then, using this code I was able to properly load my layer:

iface.addVectorLayer(r"/Users/pgcseismolab/Desktop/jaden_pgc_archive/Wells/WD/Shapefiles/wd_14/wd_14.shp","test","ogr")
2
  • 2
    It's great that you got it working but that answer is a little brief and might be subject to derision. Can you edit your answer and put in some of your code to show how you got it working please. Did you end up using os.path.exists()? I would like to know if that works as advertised on a Mac platform... there is also os.path.isdir() stackoverflow.com/questions/15077424/… which is useful, exists is True whether the object is a folder or file so you need to test if the target is a file or folder that exists. Commented Jul 12, 2017 at 23:31
  • yes I did- that was very useful in finding the root cause of the problem. I will certainly make more use of the os.path options with OSX
    – jrowley
    Commented Jul 13, 2017 at 16:15

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