6

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?

4
  • do you also have the .shx file for it? Jun 23, 2012 at 15:50
  • The message log (click /!\ at the end of the status bar) might have more information.
    – jef
    Jun 23, 2012 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, 2012 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, 2012 at 23:03

2 Answers 2

10

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.

1

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

layer = QgsVectorLayer("c:/coast.shp", "coast_shp", "ogr")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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