我尝试使用PyQgis API在Python上加载shapefile但无济于事。我仔细检查了shapefile的路径,发现它是正确的。 QGIS模块似乎也进口得很好。当我在QgsRegistry中检查提供者列表时,它什么都不返回。我可以知道我错过了什么或者应该如何排除故障吗?
我使用的是Ubuntu 12.04,QGIS 2.4.0 Chugiak和Python 2.7.3。 提前谢谢!
以下是我的输出和代码:
”
/usr/bin/python2.7 /home/victorzhiyulee/IdeaProjects/Delineation/select_dun_calculate_print.py
申请状态:
QGIS_PREFIX_PATH env var:
前缀:/ usr / bin / qgis
插件路径:/ usr / bin / qgis / lib / qgis / plugins
包数据路径:/ usr / bin / qgis / share / qgis
活动主题名称:
活动主题路径:: / images / themes //
默认主题路径:: / images / themes / default /
SVG搜索路径:/ usr / bin / qgis / share / qgis / svg /
用户数据库路径:/usr/bin/qgis/share/qgis/resources/qgis.db
提供商名单 找不到OGR提供商! 文件已存在;路径是正确的 ('/home/victorzhiyulee/Desktop/dun.shp','dun','ogr') 图层无法加载!
进程以退出代码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()
答案 0 :(得分:7)
我认为前缀路径不正确,路径为“/ usr / share / qgis”,因此我的前缀只是“/ usr”。 我已经检查了print QgsApplication.showSettings()输出中的路径来发现它。