[Qgis-user] Python Script, Layer is not valid

nicolas.cadieux at archeotec.ca nicolas.cadieux at archeotec.ca
Tue Nov 3 13:22:30 PST 2015


Hi,
I am writing a small python script to use in a stand alone application.  
It is the first time I use the QGIS API in Python.  I can't seem to be 
able to open a Shape file using QGIS through Python.  The following 
script returns a "layer is not valid".  I can open the layer from the 
QGIS Python console and i is valid.  I just can't do it from Python 
2.7.  Since I can import the qgis.core and others, I am assuming that my 
Python installation and path's are ok.  I am using Eclipse configured as 
explained in the latest Cookbook (as best as I could).  (I am stating 
Eclipse with the .bat file).

For now, I will use Fiona to open the shape file but I would like to go 
with the QGIS API.  Can anyone help?

Thanks!
Nicolas

============
from qgis.gui import *
from qgis.utils import*
from qgis.core import *

QgsApplication.setPrefixPath("C:/OSGeo4W64/apps/qgis", True)
app = QgsApplication([], True)
QgsApplication.initQgis()

layer = 
QgsVectorLayer('c:/pytemp/PhD_Limite_Exterieur_Sol_et_Veg_151029.shp', 
'shpFilter', 'ogr')

if layer.isValid():
     print 'layer is valid'

else:
     print 'layer is not valid'
============



More information about the Qgis-user mailing list