5

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?

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
asked Mar 7, 2020 at 21:05

1 Answer 1

3

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 Aug 10, 2020 at 18:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.