TransWikia.com

Unload QgsVectorLayer from Python script

Geographic Information Systems Asked on May 6, 2021

I have a Python script that processes several country sized OpenStreetMap PBF files at a time. The problem I’m having is that: each time I open and process one of these files, I lose anywhere from 200MB to a gig of hard drive space (I’m assuming to some temp storage directory somewhere).

When I process loads of these files, I eventually end up running out of disk space and the script crashes. I’ve tried several ways to get the script to unload the QgsVectorLayer object that I’m processing in an attempt to reclaim some disk space. These include:

  1. Placing the loading & processing of the individual layers into a separate method that (I assumed) would be automatically removed once the method returns.
  2. Explicitly calling del on the layer object.
  3. Calling QgsProject.instance().removeMapLayer on the layer in the hope to get it removed some how.

Unfortunately, none of these worked, so far the only thing that seems to unload the layers and delete the temporary files is allowing the script to complete fully.

Is there a way for me to ensure that, once I’m done with a QgsVectorLayer, I can close it, and have it clean up its temp data?

One Answer

It has been answered here and here.

You need to first add the layer to map even if you don't display it before removing it.

QgsProject.instance().addMapLayer(vlayer, False)
QgsProject.instance().removeMapLayer(vlayer.id())
os.remove(vlayer.source())

It usually works, but I recently had an issue with FLT files (raster format) which seems to stick whatever I try.

Answered by Jean-François Bourdon on May 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP