英文:
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's features. It's working well as long as I just choose the whole layer. But it doesn't work if I'm trying to work on selected features only.
I'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'm executing the algorithm, I get `NoneType` as return of `parameterAsVectorLayer`.
Below you'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(
'INPUT',
self.tr('Some point vector layer.'),
types=[QgsProcessing.TypeVectorPoint]
)
)
def processAlgorithm(self, parameters, context, feedback):
layer = self.parameterAsVectorLayer(
parameters,
'INPUT',
context
)
return {"OUTPUT": layer}
If I'm working on the whole layer, the output is {'OUTPUT': \<QgsVectorLayer: 'Neuer Temporärlayer' (memory)\>}, which is what I would expect.
If I'm working on selected features only, my output is {'OUTPUT': None}, which doesn't makes sense to me. I selected some of the features before executing of course.
I'm using QGIS-version 3.22 LTR, if it's relevant.
Can anybody tell me what I'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 'parameterAsSource' in the 'processAlgorithm' method.
layer = self.parameterAsSource(
parameters,
'INPUT',
context
)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论