简体   繁体   English

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

[英]Error Loading Layer/Shapefile in a Standalone Python QGis Application

I tried loading a shapefile on Python using PyQgis API but to no avail. 我尝试使用PyQgis API在Python上加载shapefile但无济于事。 I double-checked the path to shapefile and found it to be correct. 我仔细检查了shapefile的路径,发现它是正确的。 QGIS module also seems to be imported just fine. QGIS模块似乎也进口得很好。 When I checked on the provider list in QgsRegistry, it returns nothing. 当我在QgsRegistry中检查提供者列表时,它什么都不返回。 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. 我使用的是Ubuntu 12.04,QGIS 2.4.0 Chugiak和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: “ /usr/bin/python2.7 /home/victorzhiyulee/IdeaProjects/Delineation/select_dun_calculate_print.py应用程序状态: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: 前缀:/ usr / bin / qgis插件路径:/ usr / bin / qgis / lib / qgis / plugins包数据路径:/ usr / bin / qgis / share / qgis活动主题名称:
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 活动主题路径::/ images / themes //默认主题路径::/ images / themes / default / SVG搜索路径:/ usr / bin / qgis / share / qgis / svg /用户数据库路径:/ usr / bin / qgis /share/qgis/resources/qgis.db

Provider List Could not find OGR provider! 提供商列表无法找到OGR提供商! File exists; 文件已存在; Path is correct ('/home/victorzhiyulee/Desktop/dun.shp', 'dun', 'ogr') Layer failed to load! 路径是正确的('/home/victorzhiyulee/Desktop/dun.shp','dun','ogr')图层无法加载!

Process finished with exit code 0 " 处理完成,退出代码为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()

I think that the prefix path isn't correct, the path shuld be "/usr/share/qgis", so the prefix for me is only "/usr". 我认为前缀路径不正确,路径为“/ usr / share / qgis”,所以我的前缀只是“/ usr”。 I have check the paths in the output of print QgsApplication.showSettings() to discover this. 我已经检查了打印QgsApplication.showSettings()输出中的路径以发现这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM