<div dir="ltr">  Hi all,<div><br></div><div>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,<a href="http://stackexchange.com/" target="_blank">stackexchange.com</a> recently, and I thought I would try to find an answer- but I've hit a dead end!</div><div>The latest documentation contains a snippet specifically around dealing only with visible features:</div><div><a href="https://qgis.org/api/classQgsFeatureRenderer.html#a981b574dd6813bcc3cd849822af8929b" target="_blank">https://qgis.org/api/classQgsFeatureRenderer.html#a981b574dd6813bcc3cd849822af8929b</a> </div><div><br></div><h2 style="margin-right:15px;padding:8px;border-top:1px solid rgb(174,217,168);border-left:1px solid rgb(174,217,168);border-right:1px solid rgb(174,217,168);border-top-right-radius:4px;border-top-left-radius:4px;background-image:url("");background-repeat:repeat-x;background-color:rgb(228,242,226);line-height:1.25;font-weight:300;float:left;color:rgb(0,0,0);font-family:Roboto,sans-serif"><span style="font-size:13.65px;display:inline-block;vertical-align:middle"><a href="https://qgis.org/api/classQgsFeatureRenderer.html#a981b574dd6813bcc3cd849822af8929b" target="_blank" style="color:rgb(81,162,70);text-decoration-line:none">◆ </a></span>capabilities()</h2><div style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:14px;line-height:22px;font-family:Roboto,sans-serif;padding:0px;margin-bottom:10px;margin-right:5px;width:1010.67px;color:rgb(0,0,0);display:table"><div style="font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:inherit;font-stretch:normal;line-height:22px;border-top:inherit;border-left:1px solid rgb(174,217,168);border-right:inherit;padding:6px 0px;color:rgb(42,85,37);background-color:rgb(225,241,223);border-top-right-radius:inherit;background-image:inherit;background-repeat:inherit;border-top-left-radius:inherit"><table style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:22px;border-spacing:0px"><tbody><tr><td style="width:930px;padding:0px"><table style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:22px;font-family:Roboto,sans-serif;margin-left:6px"><tbody><tr style="font-size:21px"><td style="margin-left:6px;vertical-align:bottom">virtual QgsFeatureRenderer::Capabilities QgsFeatureRenderer::capabilities</td><td style="vertical-align:bottom">(</td><td style="color:rgb(96,32,32);white-space:nowrap;vertical-align:bottom"></td><td style="vertical-align:bottom">)</td><td style="vertical-align:bottom"></td></tr></tbody></table></td><td style="vertical-align:bottom;padding:0px;white-space:nowrap"><span style="margin-left:8px"><span style="background-color:rgb(123,193,114);border-width:1px;border-style:solid;border-color:rgb(95,180,83) rgb(200,229,196) rgb(200,229,196) rgb(95,180,83);color:white;margin-right:4px;padding:2px 3px;border-radius:3px;font-size:7pt;vertical-align:middle">inline</span><span style="background-color:rgb(123,193,114);border-width:1px;border-style:solid;border-color:rgb(95,180,83) rgb(200,229,196) rgb(200,229,196) rgb(95,180,83);color:white;margin-right:4px;padding:2px 3px;border-radius:3px;font-size:7pt;vertical-align:middle">virtual</span></span></td></tr></tbody></table></div><div style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:22px;border-bottom:inherit;border-left:1px solid rgb(174,217,168);border-right:inherit;padding:6px 10px 2px;border-top-width:inherit;background-image:inherit;background-repeat:inherit;background-color:inherit;border-bottom-left-radius:inherit;border-bottom-right-radius:inherit;margin-bottom:1em"><p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:22px">Returns details about internals of this renderer.</p><p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:22px">E.g. if you only want to deal with visible features:</p><div style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:22px;padding:0px;margin:4px 8px 4px 2px;background-color:rgb(251,253,251);border:1px solid rgb(200,229,196)"><div style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:13px;line-height:1;font-family:monospace,fixed;min-height:13px;white-space:pre-wrap;padding-left:53px;padding-bottom:0px;margin:0px"><span style="color:rgb(224,128,0)">if</span> <span style="color:rgb(224,128,0)">not</span> renderer.capabilities().testFlag(QgsFeatureRenderer.Filter) <span style="color:rgb(224,128,0)">or</span> renderer.willRenderFeature(feature, context):</div><div style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:13px;line-height:1;font-family:monospace,fixed;min-height:13px;white-space:pre-wrap;padding-left:53px;padding-bottom:0px;margin:0px">    deal_with_my_feature()</div><div style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:13px;line-height:1;font-family:monospace,fixed;min-height:13px;white-space:pre-wrap;padding-left:53px;padding-bottom:0px;margin:0px"><span style="color:rgb(224,128,0)">else</span>:</div><div style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:13px;line-height:1;font-family:monospace,fixed;min-height:13px;white-space:pre-wrap;padding-left:53px;padding-bottom:0px;margin:0px">    skip_the_curren_feature()</div></div><p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:22px">Reimplemented in <a href="https://qgis.org/api/classQgsRuleBasedRenderer.html#a1576aa5317eb1d9276f403313db68823" target="_blank" style="color:rgb(81,162,70)">QgsRuleBasedRenderer</a>, <a href="https://qgis.org/api/classQgsCategorizedSymbolRenderer.html#aed3ba97c9757f065e74f15d9aaa6cae7" target="_blank" style="color:rgb(81,162,70)">QgsCategorizedSymbolRenderer</a>, <a href="https://qgis.org/api/classQgsPointDistanceRenderer.html#a8ae8d6e2151b3ebb4038fec2d2bccc35" target="_blank" style="color:rgb(81,162,70)">QgsPointDistanceRenderer</a>, <a href="https://qgis.org/api/classQgsInvertedPolygonRenderer.html#a389777639662b9e3e03fa5245bc802de" target="_blank" style="color:rgb(81,162,70)">QgsInvertedPolygonRenderer</a>, <a href="https://qgis.org/api/classQgsSingleSymbolRenderer.html#a6bdbfdee8d84d92168ffd2a45d2c81d8" target="_blank" style="color:rgb(81,162,70)">QgsSingleSymbolRenderer</a>, and <a href="https://qgis.org/api/classQgsGraduatedSymbolRenderer.html#a7e3e3c9e1ce38d984503de720ce94297" target="_blank" style="color:rgb(81,162,70)">QgsGraduatedSymbolRenderer</a>.</p><p style="font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:22px">Definition at line <a href="https://qgis.org/api/qgsrenderer_8h_source.html#l00274" target="_blank" style="color:rgb(81,162,70)">274</a> of file <a href="https://qgis.org/api/qgsrenderer_8h_source.html" target="_blank" style="color:rgb(81,162,70)">qgsrenderer.h</a>.</p></div></div><div>But I cannot work out how to implement this logic.</div><div>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.</div><div>Firstly passing an empty instance:</div><div><br></div><div>layer = iface.activeLayer()<br>feat = layer.getFeature(3)<br>renderer = layer.renderer()<br>test = renderer.willRenderFeature(feat, QgsRenderContext())<br># LINE BELOW CRASHES QGIS<br>test = renderer.willRenderFeature(feat, QgsRenderContext())<br></div><div><br></div><div>And secondly, I tried constructing a QgsRenderContext object with the fromMapSettings() method and passing the canvas.mapSettings() to its constructor:</div><div><br></div><div>layer = iface.activeLayer()<br>feat = layer.getFeature(3)<br>renderer = layer.renderer()<br>settings = iface.mapCanvas().mapSettings()<br>context = QgsRenderContext().fromMapSettings(settings)<br># LINE BELOW CRASHES QGIS<br>test = renderer.willRenderFeature(feat, context)<br></div><div><br></div><div>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.</div><div><br></div><div>Incidentally, if I run the following line:</div><div><br></div><div>renderer.capabilities().testFlag(QgsFeatureRenderer.Filter)<br></div><div><br></div><div>I get an attribute error that Capabilities object has no attribute 'testFlag'</div><div><br></div><div>So at this point I'm a bit stumped. Any help or guidance would be greatly  appreciated.</div><div><br></div><div>Best regards,</div><div><br></div><div>Ben</div><div><br></div><div>Related GIS Stack Exchange questions:</div><div><a href="https://gis.stackexchange.com/questions/350807/checking-if-feature-visible-using-pyqgis" target="_blank">https://gis.stackexchange.com/questions/350807/checking-if-feature-visible-using-pyqgis</a> </div><div><a href="https://gis.stackexchange.com/questions/347951/selecting-only-visible-features-from-different-layers-using-pyqgis" target="_blank">https://gis.stackexchange.com/questions/347951/selecting-only-visible-features-from-different-layers-using-pyqgis</a><div class="gmail-yj6qo"></div><div class="gmail-adL"> <br></div></div><div class="gmail-adL"><br></div></div>