PyQGIS, 自定义处理算法: 如何仅使用选定的要素?

huangapple go评论54阅读模式
英文:

PyQGIS, custom processing algorithm: How to use selected features only?

问题

以下是您要的代码部分的中文翻译:

我想使用PyQGIS创建一个自定义处理算法它能够接受矢量图层作为输入在这种情况下是点类型),然后对其要素进行处理只要选择整个图层它就能正常工作但是如果我尝试仅处理选定的要素它就无法正常工作

我使用`QgsProcessingParameterFeatureSource`来允许仅处理选定的要素选项显示出来我可以启用复选框但是当我执行算法时,`parameterAsVectorLayer`返回`NoneType`。

以下是一个最小工作示例用于复现这个问题

```python
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (
    QgsProcessing,
    QgsProcessingAlgorithm,
    QgsProcessingParameterFeatureSource
)

name = "selectedonly"
display_name = "仅选定要素示例"
group = "测试"
group_id = "test"
short_help_string = "用于展示我的问题的最小工作示例代码。"

class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
    def tr(self, string):
        return QCoreApplication.translate('Processing', string)

    def createInstance(self):
        return ExampleProcessingAlgorithm()

    def name(self):
        return name

    def displayName(self):
        return self.tr(display_name)

    def group(self):
        return self.tr(group)

    def groupId(self):
        return group_id

    def shortHelpString(self):
        return self.tr(short_help_string)

    def initAlgorithm(self, config=None):
        self.addParameter(
            QgsProcessingParameterFeatureSource(
                'INPUT',
                self.tr('某些点矢量图层。'),
                types=[QgsProcessing.TypeVectorPoint]
            )
        )

    def processAlgorithm(self, parameters, context, feedback):
        layer = self.parameterAsVectorLayer(
            parameters,
            'INPUT',
            context
        )
        return {"OUTPUT": layer}

如果我在整个图层上工作,输出将是{"OUTPUT": <QgsVectorLayer: '新临时层'(内存)>},这是我预期的结果。

如果我只处理选定的要素,输出是{"OUTPUT": None},这对我来说没有意义。我在执行之前当然选择了一些要素。

我使用的是QGIS版本3.22 LTR,如果相关的话。能有人告诉我我做错了什么吗?


<details>
<summary>英文:</summary>

I want to create a custom processing algorithm with PyQGIS, which is able to take a vector layer as input (in this case of type point) and then do something with it&#39;s features. It&#39;s working well as long as I just choose the whole layer. But it doesn&#39;t work if I&#39;m trying to work on selected features only.

I&#39;m using `QgsProcessingParameterFeatureSource` to be able to work on selected features only. The option is shown and I can enable the checkbox. But when I&#39;m executing the algorithm, I get `NoneType` as return of `parameterAsVectorLayer`.

Below you&#39;ll find a minimal working example to reproduce the problem:

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSource
)

name = "selectedonly"
display_name = "Selected features only example"
group = "Test"
group_id = "test"
short_help_string = "Minimal working example code for showing my problem."

class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
def tr(self, string):
return QCoreApplication.translate('Processing', string)

def createInstance(self):
    return ExampleProcessingAlgorithm()

def name(self):
    return name

def displayName(self):
    return self.tr(display_name)

def group(self):
    return self.tr(group)

def groupId(self):
    return group_id

def shortHelpString(self):
    return self.tr(short_help_string)

def initAlgorithm(self, config=None):
    self.addParameter(
        QgsProcessingParameterFeatureSource(
            &#39;INPUT&#39;,
            self.tr(&#39;Some point vector layer.&#39;),
            types=[QgsProcessing.TypeVectorPoint]
        )
    )

def processAlgorithm(self, parameters, context, feedback):
    layer = self.parameterAsVectorLayer(
        parameters,
        &#39;INPUT&#39;,
        context
    )
    return {&quot;OUTPUT&quot;: layer}

If I&#39;m working on the whole layer, the output is {&#39;OUTPUT&#39;: \&lt;QgsVectorLayer: &#39;Neuer Tempor&#228;rlayer&#39; (memory)\&gt;}, which is what I would expect.

If I&#39;m working on selected features only, my output is {&#39;OUTPUT&#39;: None}, which doesn&#39;t makes sense to me. I selected some of the features before executing of course.

I&#39;m using QGIS-version 3.22 LTR, if it&#39;s relevant.

Can anybody tell me what I&#39;m doing wrong?

</details>


# 答案1
**得分**: 0

我建议您尝试在"processAlgorithm"方法中使用"parameterAsSource"方法。

layer = self.parameterAsSource(
        parameters,
        'INPUT',
        context
    )

<details>
<summary>英文:</summary>

I would suggest you trying to use the method &#39;parameterAsSource&#39; in the &#39;processAlgorithm&#39; method.

    layer = self.parameterAsSource(
            parameters,
            &#39;INPUT&#39;,
            context
        )



</details>



huangapple
  • 本文由 发表于 2023年2月8日 20:46:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定