5

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!"
  • No it doesn't, You need to create an Qgis instance. follow the steps describe here : gis.stackexchange.com/q/176821/41673 – SIGIS Mar 30 '16 at 7: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: gis.stackexchange.com/questions/189735/… – brgionta Apr 25 '16 at 1:24
3

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.

1

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.

  • 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 1 '16 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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