微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我在 QGIS 上为 native:extractbyexpression 算法收到“无法为 INPUT 加载源层:无效值”?

如何解决为什么我在 QGIS 上为 native:extractbyexpression 算法收到“无法为 INPUT 加载源层:无效值”?

我需要转换这个:

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 举报,一经查实,本站将立刻删除。