我一直很努力,与此相伴后,要运行脚本OSGeo4w壳牌,QGIS之外。但是我收到以下错误:
ImportError:没有名为qgis.core的模块
我还阅读了以下帖子,并尝试导入各种模块,但无济于事:
这是一个简单的脚本,它创建一个网格并将一个多边形shapefile剪切到其上。
注意:该脚本已经过测试,可以在QGIS中运行时成功运行。
##Test=name
import os
import glob
import sys
sys.path.append("C:\Program Files\QGIS Brighton\lib;%OSGEO4W_ROOT:\=/%/apps/qgis;%OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\apps\grass\grass-6.4.3\lib;%PATH%")
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
QgsApplication.setPrefixPath("C:\Program Files\QGIS Brighton\apps\qgis", True)
QgsApplication.initQgis()
from os.path import expanduser
home = expanduser("~")
# Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"
def run():
# Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
os.chdir(path_dir + "Shapefiles\\")
for fname in glob.glob("*.shp"):
outputs_1=processing.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405', None)
outputs_2=processing.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res + "/"+ fname)
run()
QgsApplication.exitQgis()
# Remove the above line when running in QGIS
按照答案和@gcarrillo发布的脚本,我终于可以成功导入qgis.core.
模块了。@gcarrillo提供的脚本运行,但是我收到一个Traceback错误:
Traceback (most recent call last):
File "Test.py", line 55, in <module>
run()
File "Test.py", line 53, in run
algClip.processAlgorithm(progress)
File "C:\Users\username\.qgis2\python\plugins\processing\algs\qgis\ftools\Clip.py", line 59, in processAlgorithm
layerA.pendingFields(),
AttributeError: 'NoneType' object has no attribute 'pendingFields'