<div dir="ltr">Hi,<div><br></div><div>I created the following code:</div><div><br></div><div><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:10.5pt"><span style="color:rgb(0,0,128);font-weight:bold">class </span>FBSegmentPoiComboBox(FBComboBox):<br>    <span style="color:rgb(0,0,128);font-weight:bold">def </span><span style="color:rgb(178,0,178)">__init__</span>(<span style="color:rgb(148,85,141)">self</span>, name, identifier=<span style="color:rgb(0,0,128);font-weight:bold">None</span>, update_method=<span style="color:rgb(0,0,128);font-weight:bold">None</span>, iface=<span style="color:rgb(0,0,128);font-weight:bold">None</span>):<br>        <span style="color:rgb(0,0,128)">super</span>().<span style="color:rgb(178,0,178)">__init__</span>(<span style="color:rgb(102,0,153)">name</span>=name, <span style="color:rgb(102,0,153)">identifier</span>=identifier, <span style="color:rgb(102,0,153)">update_method</span>=update_method)<br>        <span style="color:rgb(148,85,141)">self</span>.iface = iface<br>        <span style="color:rgb(148,85,141)">self</span>.items_list = []<br>        <span style="color:rgb(148,85,141)">self</span>.find_segments()<br>        <span style="color:rgb(148,85,141)">self</span>.rubber_band = QgsRubberBand(<span style="color:rgb(148,85,141)">self</span>.iface.mapCanvas(), <span style="color:rgb(0,0,128);font-weight:bold">False</span>)<br>        <span style="color:rgb(148,85,141)">self</span>.rubber_band.setWidth(<span style="color:rgb(0,0,255)">12</span>)<br>        <span style="color:rgb(148,85,141)">self</span>.rubber_band.setStrokeColor(QColor.fromRgb(<span style="color:rgb(0,0,255)">90</span>, <span style="color:rgb(0,0,255)">0</span>, <span style="color:rgb(0,0,255)">20</span>))<br>        <span style="color:rgb(148,85,141)">self</span>.highlighted.connect(<span style="color:rgb(148,85,141)">self</span>.highlight_segment)<br>        <span style="color:rgb(148,85,141)">self</span>.currentIndexChanged.connect(<span style="color:rgb(148,85,141)">self</span>.clear_rubberband)<br><br>    <span style="color:rgb(0,0,128);font-weight:bold">def </span>find_segments(<span style="color:rgb(148,85,141)">self</span>):<br>        <span style="color:rgb(148,85,141)">self</span>.clear()<br>        <span style="color:rgb(148,85,141)">self</span>.add_item(REFRESH_SEGMENTS_LIST, <span style="color:rgb(0,0,128);font-weight:bold">None</span>)<br>        <span style="color:rgb(128,128,128);font-style:italic"># put a dummy at position 0<br></span><span style="color:rgb(128,128,128);font-style:italic">        </span><span style="color:rgb(148,85,141)">self</span>.items_list = [<span style="color:rgb(0,128,128);font-weight:bold">'dummy'</span>]<br>        segment_layer_list = QgsProject.instance().mapLayersByName(<span style="color:rgb(0,128,128);font-weight:bold">'segment'</span>)<br>        <span style="color:rgb(0,0,128);font-weight:bold">if </span>segment_layer_list:<br>            segment_layer = segment_layer_list[<span style="color:rgb(0,0,255)">0</span>]<br>            layer = QgsProject.instance().layerTreeRoot().findLayer(segment_layer)<br>            <span style="color:rgb(0,0,128);font-weight:bold">if </span>layer:<br>                layer.setItemVisibilityChecked(<span style="color:rgb(0,0,128);font-weight:bold">True</span>)<br>            <span style="color:rgb(0,0,128);font-weight:bold">if </span><span style="color:rgb(148,85,141)">self</span>.iface:<br>                segments = QgsSpatialIndex()<br>                <span style="color:rgb(0,0,128);font-weight:bold">for </span>segment <span style="color:rgb(0,0,128);font-weight:bold">in </span>segment_layer.getFeatures():<br>                    segments.insertFeature(segment)<br>                nearby = segments.nearestNeighbor(<span style="color:rgb(148,85,141)">self</span>.iface.mapCanvas().center(), <span style="color:rgb(102,0,153)">neighbors</span>=<span style="color:rgb(0,0,255)">10</span>) <span style="color:rgb(128,128,128);font-style:italic"># , maxDistance=1000)<br></span><span style="color:rgb(128,128,128);font-style:italic">                </span><span style="color:rgb(0,0,128);font-weight:bold">for </span>featureId <span style="color:rgb(0,0,128);font-weight:bold">in </span>nearby:<br>                    fit2 = segment_layer.getFeatures(QgsFeatureRequest().setFilterFid(featureId))<br>                    ftr = QgsFeature()<br>                    fit2.nextFeature(ftr)<br>                    <span style="color:rgb(148,85,141)">self</span>.add_item(ftr.attribute(<span style="color:rgb(0,0,255)">0</span>), ftr.attribute(<span style="color:rgb(0,0,255)">0</span>))<br>                    <span style="color:rgb(148,85,141)">self</span>.items_list.append(ftr)<br>        <span style="color:rgb(148,85,141)">self</span>.changed = <span style="color:rgb(0,0,128);font-weight:bold">False<br></span><span style="color:rgb(0,0,128);font-weight:bold"><br></span><span style="color:rgb(0,0,128);font-weight:bold">    def </span>highlight_segment(<span style="color:rgb(148,85,141)">self</span>, index):<br>        <span style="color:rgb(148,85,141)">self</span>.rubber_band.reset()<br>        <span style="color:rgb(0,0,128);font-weight:bold">if </span>index == <span style="color:rgb(0,0,255)">0</span>:<br>            <span style="color:rgb(148,85,141)">self</span>.find_segments()<br>        <span style="color:rgb(0,0,128);font-weight:bold">elif </span>index < <span style="color:rgb(0,0,128)">len</span>(<span style="color:rgb(148,85,141)">self</span>.items_list):<br>            <span style="color:rgb(148,85,141)">self</span>.rubber_band.setToGeometry(<span style="color:rgb(148,85,141)">self</span>.items_list[index].geometry())<br><br>    <span style="color:rgb(0,0,128);font-weight:bold">def </span>clear_rubberband(<span style="color:rgb(148,85,141)">self</span>, <span style="color:rgb(128,128,128)">index</span>):<br>        <span style="color:rgb(148,85,141)">self</span>.rubber_band.reset()</pre><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:10.5pt"><br></pre><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:10.5pt">It populates a drop down with all the LineStrings near to the center of the screen.</pre><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:10.5pt">It's not very efficient though, as I need to first pass all the LineStrings on that particular layer to it.</pre><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:10.5pt">Is there a way to replace this code:</pre><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:10.5pt">                <span style="color:rgb(0,0,128);font-weight:bold">for </span>segment <span style="color:rgb(0,0,128);font-weight:bold">in </span>segment_layer.getFeatures():<br>                    segments.insertFeature(segment)
</pre><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:10.5pt">So it only works on a smaller subset of features that are actually visible to on the user's screen?</pre><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:10.5pt">Jo</pre></div></div>