6

I am trying to add a Shapefile outside of the QGIS environment using PyQGIS. Ideally this would be done without creating a map in QGIS. I've started with the code below but am receiving the following error:

QObject::connect: Cannot connect <null>::raiseError< QString > to QgsVectorLayer::raiseError< QString >

Does the command below create a new map canvas?

from qgis.core import *
import qgis.utils

layer = QgsVectorLayer("F:\\IrrigatedLands\\FC_qgis\\boundary.shp", "testlayer_shp", "ogr")
#if not layer.isValid():
  #print "Layer failed to load!"
Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
brgionta
  • 507
  • 5
  • 14
  • No it doesn't, You need to create an Qgis instance. follow the steps describe here : http://gis.stackexchange.com/q/176821/41673 – SIGIS Mar 30 '16 at 07:50
  • Make sure you set your PATH and PYTHONPATH correctly before running the above as described here. Once they're set, your above code should work fine =) – Joseph Mar 30 '16 at 10:05
  • I answered your (to this date) unsolved question. Did it solve your problem? – Germán Carrillo Apr 24 '16 at 20:39
  • Yes it did. I had to initialize the QgsApplication. Although when changing the script to a more complex geoprocess following the initialization lines, python crashes per the following post: http://gis.stackexchange.com/questions/189735/how-to-iterate-over-layers-and-export-them-as-png-images-with-pyqgis-in-a-standa – brgionta Apr 25 '16 at 01:24

2 Answers2

8

When you intend to run PyQGIS scripts out of QGIS, you need to initialize a QgsApplication so that it loads data providers and other resources. The following code snippet should work:

from qgis.core import *
from PyQt4.QtGui import QApplication

app = QApplication([])
QgsApplication.setPrefixPath("C:\\OSGeo4W\\apps\\qgis\\", True) # Adjust prefix path according to your installation (see note below)
QgsApplication.initQgis()

layer = QgsVectorLayer("F:\\IrrigatedLands\\FC_qgis\\boundary.shp", "testlayer_shp", "ogr")

if not layer.isValid():
  print "Layer failed to load!"

Now you can start doing anything with your valid Shapefile, even without a map canvas.

Note: See https://gis.stackexchange.com/a/155852/4972 for more details about setting a prefix path on both Windows and GNU/Linux.

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
3

I tried out this code (for my own shapefile and operative Linux system) and it was successfully:

from qgis.core import *
import qgis.utils

layer = QgsVectorLayer("/home/zeito/pyqgis_data/polygon8.shp", "testlayer_shp", "ogr")

if not layer.isValid():
    print "Layer failed to load!"

else:
    print "Layer was loaded successfully!"

QgsMapLayerRegistry.instance().addMapLayer(layer)

In your case, try out:

from qgis.core import *
import qgis.utils

layer = QgsVectorLayer("F:/IrrigatedLands/FC_qgis/boundary.shp", "testlayer_shp", "ogr")

if not layer.isValid():
  print "Layer failed to load!"

else:
    print "Layer was loaded successfully!"

QgsMapLayerRegistry.instance().addMapLayer(layer)

If the layer failed to load then, your path is probably wrong.

xunilk
  • 29,891
  • 4
  • 41
  • 80
  • 1
    I am able to execute from qgis.core import * and import qgis.utils with no errors in the OSGeo4W shell but adding the layer I am still getting QObject::connect: Cannot connect <null>::raiseError< QString > to QgsVectorLayer::raiseError< QString > The above script works in the Python console within QGIS – brgionta Apr 01 '16 at 18:48