簡體   English   中英

Python QGIS:圖層無法加載

[英]Python QGIS: Layer failed to load

我正在嘗試從Python中的SHP文件加載QGIS矢量層。 每當我運行它時,layer.isValid()始終返回False(在這種情況下為“ Layer無效!”)。 我不確定在這里做錯了什么,或者不確定是否錯誤地實例化了QgsVectorLayer變量。

import sys
import os
from qgis.core import *
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches


QgsApplication.setPrefixPath("/usr", True)
qgs = QgsApplication(sys.argv, False)
qgs.initQgis()

layer=QgsVectorLayer("/Users/ANON/Desktop/MassShapeFiles/MassachusettsTownBoundaries.shp", "MassachusettsTownBoundaries", "ogr")
providers = QgsProviderRegistry.instance().providerList()
for provider in providers:
    print provider
if not layer.isValid():
    print "Layer failed to load!"
provider = layer.dataProvider()

謝謝!

我認為您的路徑格式錯誤。 尋找路徑結構,我假設您在Windows系統中,因此Windows路徑應為:

"\\Users\\ANON\\Desktop\\MassShapeFiles\\MassachusettsTownBoundaries.shp"

使用雙反斜杠表示法以避免python的誤解

您在Windows系統中。 但是您已經將qgis前綴路徑用作linux系統。 通過打印QgsApplication.showSettings從qgis python控制台獲取正確的qgis前綴路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM