1

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.

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()

1 Answer 1

2

Here some tips to fix your problem :

  1. you need to arrange your import statement. import qgis libs before pyqt.
  2. Use from qgis.core import* instead of import qgis.core wich is also not a good practice.
  3. create an instance of QApplication([]) like app=QApplication([])
  4. Exit from the application like QgsApplication.exitQgis()

So your code should looks like :

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

# Supply path to the QGis resources on your PC
# noinspection PyTypeChecker
app = QApplication([])
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()

With this code adapt for Windows I get this output :

C:\OSGeo4W64\bin\python.exe C:/Deve/test.py
Application state:
QGIS_PREFIX_PATH env var:       C:\OSGeo4W64\apps\qgis
Prefix:     C:\OSGeo4W64\apps\qgis
Plugin Path:        C:\OSGeo4W64\apps\qgis/plugins
Package Data Path:  C:\OSGeo4W64\apps\qgis/.
Active Theme Name:  
Active Theme Path:  /themes\\icons/
Default Theme Path: :/images/themes/default/
SVG Search Paths:   C:\OSGeo4W64\apps\qgis/./svg/
User DB Path:   C:\OSGeo4W64\apps\qgis/./resources/qgis.db
Auth DB Path:   qgis-auth.db

Provider List
[u'DB2', u'WFS', u'arcgisfeatureserver', u'arcgismapserver', u'delimitedtext', u'gdal', u'gpx', u'memory', u'mssql', u'ogr', u'ows', u'postgres', u'spatialite', u'virtual', u'wcs', u'wms']
Providers found ok!
File exists; Path is correct
('E:\\Prestations\\LEM20171102_URB_plu_laia\\extract_zone.shp', 'extract_zone', 'ogr')
Yes, layer loads successfully
[0L]

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.