Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I've installed the qgis on ubuntu 12.04 following the instructions on http://hub.qgis.org/projects/quantum-gis/wiki/Download. I am trying to test the Python API and try to do the follwoing

from qgis.core import *
layer = QgsVectorLayer("coast.shp", "coast_shp", "ogr")

But layer.isValid() returns false. Any ideas why it might happen?

share|improve this question
    
do you also have the .shx file for it? –  lynxlynxlynx Jun 23 '12 at 15:50
    
The message log (click /!\ at the end of the status bar) might have more information. –  jef Jun 23 '12 at 18:29
1  
I have the same problem. If I load my .shp file in QGIS, it loads up nicely with all attributes; yes, the shx file is there. I have tried specifying the full path for the .shp file, even though it's local. No effect. Where is the message log? There is no error output, it seems, from this command. ((Oops, this isn't an answer. Should I have made this a comment/reply?)) –  CPBL Jul 10 '12 at 20:33
    
Vadim, there are three versions of installs for Ubuntu listed on the page you noted: release, master, ubuntugis. Which did you use? –  dakcarto Jul 10 '12 at 23:03

2 Answers 2

i think you need to give fullpath of your shapefile in windows

layer = QgsVectorLayer("c:/coast.shp", "coast_shp", "ogr")
share|improve this answer

I had similar issue.

Found that the problem could be due to the following possibilities:

  1. When you set up the QgsApplication, the PrefixPath must be done correctly.

    My QGIS was installed on /usr/share/qgis.

    But somehow I need to set the PrefixPath as "/usr" & not "/usr/share/qgis":

    QgsApplication.setPrefixPath("/usr", True)
    
  2. The path of the .shp files.

    Assuming they are under a sub-directory from current path, "Maps/", you can load the file with:

    mapFile = "Maps/world_borders.shp"
    layer = QgsVectorLayer(os.path.abspath(mapFileName), mapFileName[mapFileName.rfind("/")+1:-4], "ogr")
    
  3. Lastly, not all .shp files are created 'equal'. ;P

    Some .shp file can be loaded without problem into QGIS & displayed.

    Some, probably created in ESRI ArcMap, require the rest of the files (.shp, .shx, .dbf, .prj, .sbn, .sbx)

Hope this info help.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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