Hi all,

I am having trouble working out how to use the willRenderFeature() method
of QgsFeatureRenderer classes. There have been a couple of questions
related to this on gis,stackexchange.com recently, and I thought I would
try to find an answer- but I've hit a dead end!
The latest documentation contains a snippet specifically around dealing
only with visible features:
https://qgis.org/api/classQgsFeatureRenderer.html#a981b574dd6813bcc3cd849822af8929b


◆
<https://qgis.org/api/classQgsFeatureRenderer.html#a981b574dd6813bcc3cd849822af8929b>
capabilities()
virtual QgsFeatureRenderer::Capabilities QgsFeatureRenderer::capabilities (
) inlinevirtual

Returns details about internals of this renderer.

E.g. if you only want to deal with visible features:
if not renderer.capabilities().testFlag(QgsFeatureRenderer.Filter) or
renderer.willRenderFeature(feature, context):
deal_with_my_feature()
else:
skip_the_curren_feature()

Reimplemented in QgsRuleBasedRenderer
<https://qgis.org/api/classQgsRuleBasedRenderer.html#a1576aa5317eb1d9276f403313db68823>
, QgsCategorizedSymbolRenderer
<https://qgis.org/api/classQgsCategorizedSymbolRenderer.html#aed3ba97c9757f065e74f15d9aaa6cae7>
, QgsPointDistanceRenderer
<https://qgis.org/api/classQgsPointDistanceRenderer.html#a8ae8d6e2151b3ebb4038fec2d2bccc35>
, QgsInvertedPolygonRenderer
<https://qgis.org/api/classQgsInvertedPolygonRenderer.html#a389777639662b9e3e03fa5245bc802de>
, QgsSingleSymbolRenderer
<https://qgis.org/api/classQgsSingleSymbolRenderer.html#a6bdbfdee8d84d92168ffd2a45d2c81d8>,
and QgsGraduatedSymbolRenderer
<https://qgis.org/api/classQgsGraduatedSymbolRenderer.html#a7e3e3c9e1ce38d984503de720ce94297>
.

Definition at line 274
<https://qgis.org/api/qgsrenderer_8h_source.html#l00274> of file
qgsrenderer.h <https://qgis.org/api/qgsrenderer_8h_source.html>.
But I cannot work out how to implement this logic.
E.g. I have a vector layer which is symbolized with a
QgsCategorizedSymbolRenderer and I would like to exclude features belonging
to categories which are unchecked in the TOC from, for example, an
intersection test against a QgsRectangle. If I run the following in the
Python console, QGIS crashes when passing a QgsRenderContext object as the
second argument of the renderer.willRenderFeature() method.
Firstly passing an empty instance:

layer = iface.activeLayer()
feat = layer.getFeature(3)
renderer = layer.renderer()
test = renderer.willRenderFeature(feat, QgsRenderContext())
# LINE BELOW CRASHES QGIS
test = renderer.willRenderFeature(feat, QgsRenderContext())

And secondly, I tried constructing a QgsRenderContext object with the
fromMapSettings() method and passing the canvas.mapSettings() to its
constructor:

layer = iface.activeLayer()
feat = layer.getFeature(3)
renderer = layer.renderer()
settings = iface.mapCanvas().mapSettings()
context = QgsRenderContext().fromMapSettings(settings)
# LINE BELOW CRASHES QGIS
test = renderer.willRenderFeature(feat, context)

I'm sure I am doing something wrong because I don't have a thorough
understanding of how to properly construct or use this context object.

Incidentally, if I run the following line:

renderer.capabilities().testFlag(QgsFeatureRenderer.Filter)

I get an attribute error that Capabilities object has no attribute
'testFlag'

So at this point I'm a bit stumped. Any help or guidance would be greatly
appreciated.

Best regards,

Ben

Related GIS Stack Exchange questions:
https://gis.stackexchange.com/questions/350807/checking-if-feature-visible-using-pyqgis

https://gis.stackexchange.com/questions/347951/selecting-only-visible-features-from-different-layers-using-pyqgis
_______________________________________________
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to