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!"
4
  • No it doesn't, You need to create an Qgis instance. follow the steps describe here : gis.stackexchange.com/q/176821/41673
    – SIGIS
    Commented Mar 30, 2016 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
    Commented Mar 30, 2016 at 10:05
  • I answered your (to this date) unsolved question. Did it solve your problem? Commented Apr 24, 2016 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
    Commented Apr 25, 2016 at 1:24

2 Answers 2

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.

1
  • 1
    in QGIS 3.x use from PyQt5.QtWidgets import QApplication instead of from PyQt4.QtGui import QApplication Commented Apr 23, 2022 at 21:04
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.

1
  • 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
    Commented Apr 1, 2016 at 18:48

Your Answer

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

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