TransWikia.com

Python - Correct way of closing ogr.DataSource() objects

Geographic Information Systems Asked on August 11, 2021

I learned that I need to close objects that I open, usually using a with open(): statement that does the closing for you after leaving it. With GDAL/OGR, this does not work and objects must be closed manually. In GDAL, this works by

from osgeo import gdal
ds = gdal.Open(MY_FILE, gdal.GA_ReadOnly)
ds = None

With OGR, I found two ways to do that:

from osgeo import ogr
ds = ogr.Open(MY_FILE)
ds = None

# outputs:
# R6025
# - pure virtual function call

# crashes the debugger

ds.Destroy()

# same

While it seems to work fine with both methods when I run the full script, it crashes my debugger when I run it in debug mode (PyCharm, in my case). In the console, ds = None seems to work, while ds.Destroy() crashes the whole console with the Pure virtual function call output.

Is this specific to PyCharm or am I doing something essentially wrong?

One Answer

It seems that the problem is related to PyCharm. I have tested it in Anaconda/Spyder and VS Code without a problem.

As I recall properly, the only way of 'closing' a file with GDAL is with

from osgeo import ogr
file = ogr.Open(MY_FILE)
file = None

Correct answer by Daniel on August 11, 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