<div dir="ltr">Just to provide some resolution, this was answered thoroughly with good code examples by another GIS Stack Exchange user in this question:<div><br></div><div><a href="https://gis.stackexchange.com/questions/350807/checking-if-feature-visible-using-pyqgis">https://gis.stackexchange.com/questions/350807/checking-if-feature-visible-using-pyqgis</a> <br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Feb 16, 2020 at 2:11 PM Ben Wirf <<a href="mailto:ben.wirf@gmail.com">ben.wirf@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><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" style="color:rgb(81,162,70);text-decoration-line:none" target="_blank">◆ </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" style="color:rgb(81,162,70)" target="_blank">QgsRuleBasedRenderer</a>, <a href="https://qgis.org/api/classQgsCategorizedSymbolRenderer.html#aed3ba97c9757f065e74f15d9aaa6cae7" style="color:rgb(81,162,70)" target="_blank">QgsCategorizedSymbolRenderer</a>, <a href="https://qgis.org/api/classQgsPointDistanceRenderer.html#a8ae8d6e2151b3ebb4038fec2d2bccc35" style="color:rgb(81,162,70)" target="_blank">QgsPointDistanceRenderer</a>, <a href="https://qgis.org/api/classQgsInvertedPolygonRenderer.html#a389777639662b9e3e03fa5245bc802de" style="color:rgb(81,162,70)" target="_blank">QgsInvertedPolygonRenderer</a>, <a href="https://qgis.org/api/classQgsSingleSymbolRenderer.html#a6bdbfdee8d84d92168ffd2a45d2c81d8" style="color:rgb(81,162,70)" target="_blank">QgsSingleSymbolRenderer</a>, and <a href="https://qgis.org/api/classQgsGraduatedSymbolRenderer.html#a7e3e3c9e1ce38d984503de720ce94297" style="color:rgb(81,162,70)" target="_blank">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" style="color:rgb(81,162,70)" target="_blank">274</a> of file <a href="https://qgis.org/api/qgsrenderer_8h_source.html" style="color:rgb(81,162,70)" target="_blank">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></div><div> <br></div></div><div><br></div></div>
</blockquote></div>