python - 在独立的 Python QGis 应用程序中加载图层/形状文件时出错

标签 python shapefile qgis

我尝试使用 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 环境变量:
前缀:/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()

最佳答案

我认为前缀路径不正确,路径应该是“/usr/share/qgis”,所以我的前缀只是“/usr”。 我检查了 print QgsApplication.showSettings() 输出中的路径以发现这一点。

关于python - 在独立的 Python QGis 应用程序中加载图层/形状文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26528716/

相关文章:

python - 在 virtualenv 中运行的 django 在执行/usr/share/qgis... 后给出语法错误(不在 virtualenv 中)

javascript - 三JS : Simple City Performance Issue

python - 当调用相同的端点时,带有 CherryPy 的 Bottle 不会表现为多线程

python - 来自通过 Scrapyd 部署的 Scrapy Spider 的自定义 JSON 响应

python - 在 Python 中将 JSON 数据从一个文件附加到另一个文件

安装elastic-search-curator时的Python pip包RequestsDependencyWarning

r - 使用 ggplot2 指定图例中的特定中断

c++ - 如何以编程方式从形状文件中提取纬度/经度?

python - 如何安装 "Apple system Python"的 Python 模块?

java - SHP 打开 : datastore DataStoreFactorySpi is not an ImageIO SPI class