Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a vector layer which I need to load and edit (add several attribute columns). I am doing this without opening QGIS, so within a python script. I have the following piece of code for the layer loading:

from osgeo import gdal
from osgeo import ogr

from gdalconst import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *

layer = QgsVectorLayer("/home/usr/Desktop/EligibleAreas_polygons.shp", "EligibleAreas_polygons", "ogr")
if not layer.isValid():
    print "Eligible areas layer failed to load!"

When I execute the python script I get the message that the shape file could not be loaded. When I try executing the same script from the python console in QGIS, I get no warning. What could be the problem?

When I execute the next step (creating an attribute column "Land_Area" of doubles) within the python console:

# add new fields
layer.addAttribute(QgsField("LandArea", QVariant.Double))

I get False. Can someone give me a suggestion on how to solve this?

share|improve this question
up vote 6 down vote accepted

From the documentation Using PyQGIS in custom application, you need to add the paths to where QGIS is installed and load its providers before you can use its features and tools:

from qgis.core import *

# supply path to where is your qgis installed
QgsApplication.setPrefixPath("/path/to/qgis/installation", True)

# load providers
QgsApplication.initQgis()

And end your script with the following to do a clean up:

QgsApplication.exitQgis()

Edit:

Few things to note:

  • You may need to add the qgis application class reference object app = QApplication([], True)

  • To add attributes to the shapefile, you need to use addAttributes() and not addAttribute (not plural). This can only be done by first accessing the layer's dataProvider().

The following script works for me (note that you will need to change your paths as I use Windows):

from osgeo import gdal
from osgeo import ogr

from gdalconst import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *

QgsApplication( [], False, home + "/AppData/Local/Temp" )
QgsApplication.setPrefixPath("C://OSGeo4W64//apps//qgis", True)
app = QApplication([], True)
QgsApplication.initQgis()

layer = QgsVectorLayer("/home/usr/Desktop/EligibleAreas_polygons.shp", "EligibleAreas_polygons", "ogr")
if not layer.isValid():
    print "Eligible areas layer failed to load!"

layer.dataProvider().addAttributes( [ QgsField("LandArea",QVariant.Double) ] )

QgsApplication.exitQgis()
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.