Victor Zhiyu Lee Victor Zhiyu Lee - 6 days ago 5
Python Question

Error Loading Layer/Shapefile in a Standalone Python QGis Application

I tried loading a shapefile on Python using PyQgis API but to no avail. I double-checked the path to shapefile and found it to be correct. QGIS module also seems to be imported just fine. When I checked on the provider list in QgsRegistry, it returns nothing. May I know what I'm missing or how I should troubleshoot?

I am using Ubuntu 12.04, QGIS 2.4.0 Chugiak and Python 2.7.3.
Thank you in advance!

Following are my output and code:

"
/usr/bin/python2.7 /home/victorzhiyulee/IdeaProjects/Delineation/select_dun_calculate_print.py
Application state:
QGIS_PREFIX_PATH env var:

Prefix: /usr/bin/qgis
Plugin Path: /usr/bin/qgis/lib/qgis/plugins
Package Data Path: /usr/bin/qgis/share/qgis
Active Theme Name:

Active Theme Path: :/images/themes//
Default Theme Path: :/images/themes/default/
SVG Search Paths: /usr/bin/qgis/share/qgis/svg/
User DB Path: /usr/bin/qgis/share/qgis/resources/qgis.db

Provider List

Could not find OGR provider!
File exists; Path is correct
('/home/victorzhiyulee/Desktop/dun.shp', 'dun', 'ogr')
Layer failed to load!

Process finished with exit code 0
"

__author__ = 'victorzhiyulee'
# Importing QGis API
# Importing OGR & OSR
import os
import sys
import PyQt4.QtCore
import PyQt4.QtGui
import qgis.core
import qgis.gui
from qgis.core import *
from qgis.gui import *
from osgeo import ogr, osr
from PyQt4.QtCore import *

# Supply path to the QGis resources on your PC
# noinspection PyTypeChecker
QgsApplication.setPrefixPath("/usr/bin/qgis", True)
# Load providers
QgsApplication.initQgis()
# Show setting of parameters
print QgsApplication.showSettings()

# Load vector layer
data_source = "/home/victorzhiyulee/Desktop/dun.shp"
layer_name = "dun"
provider_name = "ogr"
fileInfo = QFileInfo(data_source)

print('Provider List')
print(QgsProviderRegistry.instance().providerList())

r = QgsProviderRegistry.instance()
if not 'ogr' in r.providerList():
print 'Could not find OGR provider!'
else:
print 'Providers found ok!'
# Add layer to the registry
layer = QgsVectorLayer(data_source, fileInfo.fileName(), provider_name)
QgsMapLayerRegistry.instance().addMapLayer(layer)

if fileInfo.exists():
print("File exists; Path is correct")
print(data_source, layer_name, provider_name)
layer = QgsVectorLayer(data_source, fileInfo.fileName(), provider_name)
if not layer.isValid():
print("Layer failed to load!")
else:
print("Yes, layer loads successfully")
features = layer.getFeatures()
else:
print("Check if your path is correct")
QgsApplication.exitQgis()
iteration = layer.getFeatures()
for features in iteration:
# Fetch attributes
attris = features.attributes()
print(attris)
QgsApplication.exitQgis()

Answer

I think that the prefix path isn't correct, the path shuld be "/usr/share/qgis", so the prefix for me is only "/usr". I have check the paths in the output of print QgsApplication.showSettings() to discover this.

Comments