[Qgis-user] reduce number of open files in pyqgis script

Martin Landa landa.martin at gmail.com
Fri Jan 2 11:20:50 PST 2026


Dear Greg,

pá 26. 12. 2025 v 16:48 odesílatel Greg Troxel via QGIS-User <
qgis-user at lists.osgeo.org> napsal:

> Looking at your code above, I wonder if the layer returned by clip_layer
> holds a reference to the first argument.  (This should be easy to
> understand from code reading).
>

Yes, it's strange. The number of open files is not increasing when the clip
function is not called:

"""
for sublayer in layers:

    name = sublayer.name()

    layer = QgsVectorLayer(sublayer.uri(), 'ogr')
    del layer
    print(name, open_files_count())
"""

In this case only 2 files are open:

ZeleznicniStaniceZastavka 2
ZeleznicniPrejezd_b 2
ZdrojPodzemnichVod 2
...

I tried to replace clip function:

"""
def clip_layer(layer: QgsVectorLayer, extent: QgsRectangle, layer_name:
str) -> QgsVectorLayer:
    extent_geom = QgsGeometry.fromRect(extent)
    clipped_layer = QgsVectorLayer(

f"{QgsWkbTypes.displayString(layer.wkbType())}?crs={layer.crs().authid()}",
        layer_name,
        "memory"
    )

    clipped_layer.dataProvider().addAttributes(layer.fields())
    clipped_layer.updateFields()

    for feature in
layer.getFeatures(QgsFeatureRequest().setFilterRect(extent)):
        geom = feature.geometry()
        if geom.intersects(extent_geom):
            clipped_feature = QgsFeature()
            clipped_feature.setGeometry(geom.intersection(extent_geom))
            clipped_feature.setAttributes(feature.attributes())
            clipped_layer.dataProvider().addFeature(clipped_feature)
            clipped_feature = None

    clipped_layer.commitChanges()

    return clipped_layer
"""

By processing:

"""
clipped_layers = []

for sublayer in layers:

    name = sublayer.name()

    layer = QgsVectorLayer(sublayer.uri(), 'ogr')



    # clipped_layer = clip_layer(layer, extent, layer.name())

    clipped_layer = processing.run("native:extractbyextent", {

        'INPUT': layer,

        'EXTENT': extent,

        'CLIP': True,

        'OUTPUT': 'memory:'

    })['OUTPUT']



    del layer



    print(name, open_files_count())

    if clipped_layer.featureCount() == 0:

        del clipped_layer

        continue

    clipped_layers.append(clipped_layer)
"""

The result is an increasing number of open files again.

ZeleznicniStaniceZastavka 3
ZeleznicniPrejezd_b 4
ZdrojPodzemnichVod 5

I tried to pass the layer name without creating instance of QgsVectorLayer
explicitly:

"""
    clipped_layer = processing.run("native:extractbyextent", {

        'INPUT': sublayer.uri(),

        'EXTENT': extent,

        'CLIP': True,

        'OUTPUT': 'memory:'

    })['OUTPUT']
"""

But the result is the same - increasing number of open files.

I wonder how it's possible? Thanks for feedback in advance, Martin

--
Martin Landa
https://geomatics.fsv.cvut.cz/en/employees/martin-landa/
https://gismentors.cz/mentors/landa
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20260102/313019d7/attachment.htm>


More information about the QGIS-User mailing list