<div dir="ltr"><div>Dear Greg,</div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">pá 26. 12. 2025 v 16:48 odesílatel Greg Troxel via QGIS-User <<a href="mailto:qgis-user@lists.osgeo.org">qgis-user@lists.osgeo.org</a>> napsal:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Looking at your code above, I wonder if the layer returned by clip_layer<br>
holds a reference to the first argument.  (This should be easy to<br>
understand from code reading).<br></blockquote><div><br></div><div>Yes, it's strange. The number of open files is not increasing when the clip function is not called:</div><div><br></div><div>"""</div><div>for sublayer in layers:                                                                                     <br>    name = <a href="http://sublayer.name">sublayer.name</a>()                                                                                  <br>    layer = QgsVectorLayer(sublayer.uri(), 'ogr')    </div><div>    del layer</div><div>    print(name, open_files_count())</div><div>"""</div><div><br></div><div>In this case only 2 files are open:</div><div><br></div><div>ZeleznicniStaniceZastavka 2<br>ZeleznicniPrejezd_b 2<br>ZdrojPodzemnichVod 2</div><div>...</div><div><br></div><div>I tried to replace clip function:</div><div><br></div><div>"""</div><div>def clip_layer(layer: QgsVectorLayer, extent: QgsRectangle, layer_name: str) -> QgsVectorLayer:<br>    extent_geom = QgsGeometry.fromRect(extent)<br>    clipped_layer = QgsVectorLayer(<br>        f"{QgsWkbTypes.displayString(layer.wkbType())}?crs={layer.crs().authid()}",<br>        layer_name,<br>        "memory"<br>    )<br><br>    clipped_layer.dataProvider().addAttributes(layer.fields())<br>    clipped_layer.updateFields()<br><br>    for feature in layer.getFeatures(QgsFeatureRequest().setFilterRect(extent)):<br>        geom = feature.geometry()<br>        if geom.intersects(extent_geom):<br>            clipped_feature = QgsFeature()<br>            clipped_feature.setGeometry(geom.intersection(extent_geom))<br>            clipped_feature.setAttributes(feature.attributes())<br>            clipped_layer.dataProvider().addFeature(clipped_feature)<br>            clipped_feature = None<br><br>    clipped_layer.commitChanges()<br><br>    return clipped_layer</div><div>"""</div><div><br></div><div>By processing:</div><div><br></div><div>"""</div><div>clipped_layers = []                                                                                        </div>for sublayer in layers:                                                                                     <br>    name = <a href="http://sublayer.name">sublayer.name</a>()                                                                                  <br>    layer = QgsVectorLayer(sublayer.uri(), 'ogr')                                                           <br>                                                                                                            <br>    # clipped_layer = clip_layer(layer, extent, <a href="http://layer.name">layer.name</a>())                                               <br>    clipped_layer = processing.run("native:extractbyextent", {                                              <br>        'INPUT': layer,                                                                                     <br>        'EXTENT': extent,                                                                                   <br>        'CLIP': True,                                                                                       <br>        'OUTPUT': 'memory:'                                                                                 <br>    })['OUTPUT']                                                                                            <br>                                                                                                            <br>    del layer                                                                                               <br>                                                                                                            <br>    print(name, open_files_count())                                                                         <br>    if clipped_layer.featureCount() == 0:                                                                   <br>        del clipped_layer                                                                                   <br>        continue                                                                                            <br>    clipped_layers.append(clipped_layer)    <div>"""</div><div><br></div><div>The result is an increasing number of open files again.</div><div><br></div><div>ZeleznicniStaniceZastavka 3<br>ZeleznicniPrejezd_b 4<br>ZdrojPodzemnichVod 5<br></div><div><br></div><div>I tried to pass the layer name without creating instance of QgsVectorLayer explicitly:</div><div><br></div><div>"""</div><div>    clipped_layer = processing.run("native:extractbyextent", {                                              <br>        'INPUT': sublayer.uri(),                                                                            <br>        'EXTENT': extent,                                                                                   <br>        'CLIP': True,                                                                                       <br>        'OUTPUT': 'memory:'                                                                                 <br>    })['OUTPUT']  </div><div>"""</div><div><br></div><div>But the result is the same - increasing number of open files.</div><div><br></div><div>I wonder how it's possible? Thanks for feedback in advance, Martin</div><div> </div><div>--</div></div><div dir="ltr" class="gmail_signature"><div dir="ltr">Martin Landa<div><a href="https://geomatics.fsv.cvut.cz/en/employees/martin-landa/" target="_blank">https://geomatics.fsv.cvut.cz/en/employees/martin-landa/</a><br><a href="https://gismentors.cz/mentors/landa" target="_blank">https://gismentors.cz/mentors/landa</a></div></div></div></div>