问题描述
我需要转换这个:
from qgis.core import QgsProcessing
from qgis.core import QgsProcessingalgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFeatureSink
import processing
class Modelo(QgsProcessingalgorithm):
def initAlgorithm(self,config=None):
self.addParameter(QgsProcessingParameterVectorLayer('inpput','input_file.shp',defaultValue=None))
self.addParameter(QgsProcessingParameterFeatureSink('output','UF_extraido.shp',type=QgsProcessing.TypeVectorAnyGeometry,createByDefault=True,defaultValue=None))
def processAlgorithm(self,parameters,context,model_Feedback):
# Use a multi-step Feedback,so that individual child algorithm progress reports are adjusted for the
# overall progress through the model
Feedback = QgsProcessingMultiStepFeedback(1,model_Feedback)
results = {}
outputs = {}
# Extrair por expressão
alg_params = {
'EXPRESSION': '\"S_majority\" = 1','INPUT': parameters['input'],'OUTPUT': parameters['output']
}
outputs['ExtrairPorExpresso'] = processing.run('native:extractbyexpression',alg_params,context=context,Feedback=Feedback,is_child_algorithm=True)
results['output'] = outputs['ExtrairPorExpresso']['OUTPUT']
return results
def name(self):
return 'modelo'
def displayName(self):
return 'modelo'
def group(self):
return ''
def groupId(self):
return ''
def createInstance(self):
return Modelo()
这是在 QGIS 中使用的自定义脚本。
... 像这样:
def extract_by_expression(input_shape_file_path,output_shape_file_path):
parameters = {
'EXPRESSION': '\"S_majority\" = 1','INPUT': QgsProcessingParameterVectorLayer('input',input_shape_file_path,defaultValue=None),'OUTPUT': QgsProcessingParameterFeatureSink('output',output_shape_file_path,defaultValue=None)
}
processing.run('native:extractbyexpression',parameters)
我为其他算法做了这个,一切正常。但是对于这种特定情况,我收到此错误:
Traceback (most recent call last):
File "/Applications/QGIS.app/Contents/MacOS/../Resources/python/code.py",line 90,in runcode
exec(code,self.locals)
File "<input>",line 1,in <module>
File "<string>",line 467,line 460,in extract_by_expression
File "/Applications/QGIS.app/Contents/MacOS/../Resources/python/plugins/processing/tools/general.py",line 108,in run
return Processing.runAlgorithm(algOrName,onFinish,Feedback,context)
File "/Applications/QGIS.app/Contents/MacOS/../Resources/python/plugins/processing/core/Processing.py",line 168,in runAlgorithm
raise QgsProcessingException(msg)
_core.QgsProcessingException: Unable to execute algorithm
Could not load source layer for INPUT: invalid value
输入文件没问题,它正在用于其他算法。所以我想这不是文件本身的问题。
我正在使用 QGIS 3.18.1-Zürich,其想法是能够编写自己的脚本,而不是在 QGIS 界面中手动执行此操作。
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)