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

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

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

问题

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

  1. 我想使用PyQGIS创建一个自定义处理算法它能够接受矢量图层作为输入在这种情况下是点类型),然后对其要素进行处理只要选择整个图层它就能正常工作但是如果我尝试仅处理选定的要素它就无法正常工作
  2. 我使用`QgsProcessingParameterFeatureSource`来允许仅处理选定的要素选项显示出来我可以启用复选框但是当我执行算法时`parameterAsVectorLayer`返回`NoneType`
  3. 以下是一个最小工作示例用于复现这个问题
  4. ```python
  5. from qgis.PyQt.QtCore import QCoreApplication
  6. from qgis.core import (
  7. QgsProcessing,
  8. QgsProcessingAlgorithm,
  9. QgsProcessingParameterFeatureSource
  10. )
  11. name = "selectedonly"
  12. display_name = "仅选定要素示例"
  13. group = "测试"
  14. group_id = "test"
  15. short_help_string = "用于展示我的问题的最小工作示例代码。"
  16. class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
  17. def tr(self, string):
  18. return QCoreApplication.translate('Processing', string)
  19. def createInstance(self):
  20. return ExampleProcessingAlgorithm()
  21. def name(self):
  22. return name
  23. def displayName(self):
  24. return self.tr(display_name)
  25. def group(self):
  26. return self.tr(group)
  27. def groupId(self):
  28. return group_id
  29. def shortHelpString(self):
  30. return self.tr(short_help_string)
  31. def initAlgorithm(self, config=None):
  32. self.addParameter(
  33. QgsProcessingParameterFeatureSource(
  34. 'INPUT',
  35. self.tr('某些点矢量图层。'),
  36. types=[QgsProcessing.TypeVectorPoint]
  37. )
  38. )
  39. def processAlgorithm(self, parameters, context, feedback):
  40. layer = self.parameterAsVectorLayer(
  41. parameters,
  42. 'INPUT',
  43. context
  44. )
  45. return {"OUTPUT": layer}

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

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. 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`.
  5. 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)

  1. def createInstance(self):
  2. return ExampleProcessingAlgorithm()
  3. def name(self):
  4. return name
  5. def displayName(self):
  6. return self.tr(display_name)
  7. def group(self):
  8. return self.tr(group)
  9. def groupId(self):
  10. return group_id
  11. def shortHelpString(self):
  12. return self.tr(short_help_string)
  13. def initAlgorithm(self, config=None):
  14. self.addParameter(
  15. QgsProcessingParameterFeatureSource(
  16. &#39;INPUT&#39;,
  17. self.tr(&#39;Some point vector layer.&#39;),
  18. types=[QgsProcessing.TypeVectorPoint]
  19. )
  20. )
  21. def processAlgorithm(self, parameters, context, feedback):
  22. layer = self.parameterAsVectorLayer(
  23. parameters,
  24. &#39;INPUT&#39;,
  25. context
  26. )
  27. return {&quot;OUTPUT&quot;: layer}
  1. 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.
  2. 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.
  3. I&#39;m using QGIS-version 3.22 LTR, if it&#39;s relevant.
  4. Can anybody tell me what I&#39;m doing wrong?
  5. </details>
  6. # 答案1
  7. **得分**: 0
  8. 我建议您尝试在"processAlgorithm"方法中使用"parameterAsSource"方法。
  9. layer = self.parameterAsSource(
  10. parameters,
  11. 'INPUT',
  12. context
  13. )
  14. <details>
  15. <summary>英文:</summary>
  16. I would suggest you trying to use the method &#39;parameterAsSource&#39; in the &#39;processAlgorithm&#39; method.
  17. layer = self.parameterAsSource(
  18. parameters,
  19. &#39;INPUT&#39;,
  20. context
  21. )
  22. </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:

确定