From b0e674499204d2a1adff86cf16cc2b513405f6e1 Mon Sep 17 00:00:00 2001 From: ai Date: Mon, 19 Feb 2024 05:06:23 +0300 Subject: [PATCH 1/2] Update vrt.py to fix python optimization In the line raster_band = gdal.Open(src['SourceFilename']).GetRasterBand(src['SourceBand']) python optimizes it and releases the opened dataset before a next line uses raster_band. As a result the raster_band handle is already invalid at that time and can not be used. A salution that prevents the release of the opened dataset is a variable containing dataset. It is defined as: ds = gdal.Open(src['SourceFilename']) --- nansat/vrt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nansat/vrt.py b/nansat/vrt.py index 56188c44..a59f8675 100644 --- a/nansat/vrt.py +++ b/nansat/vrt.py @@ -1729,7 +1729,8 @@ def _make_source_bands_xml(src_in): # find DataType of source (if not given in src) if src['SourceBand'] > 0 and 'DataType' not in src: - raster_band = gdal.Open(src['SourceFilename']).GetRasterBand(src['SourceBand']) + ds = gdal.Open(src['SourceFilename']) + raster_band = ds.GetRasterBand(src['SourceBand']) src['DataType'] = raster_band.DataType if 'xSize' not in src or 'ySize' not in src: From a302ed5724a3d2504be2779134d15134a15be2b9 Mon Sep 17 00:00:00 2001 From: Alexey Ivanov <30471305+an-ivanov@users.noreply.github.com> Date: Thu, 11 Apr 2024 13:50:02 +0300 Subject: [PATCH 2/2] Update nansat/vrt.py Co-authored-by: Adrien Perrin <33750187+aperrin66@users.noreply.github.com> --- nansat/vrt.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nansat/vrt.py b/nansat/vrt.py index a59f8675..6bc929b4 100644 --- a/nansat/vrt.py +++ b/nansat/vrt.py @@ -1729,6 +1729,8 @@ def _make_source_bands_xml(src_in): # find DataType of source (if not given in src) if src['SourceBand'] > 0 and 'DataType' not in src: + # prevent Python from releasing the dataset's handle + # prematurely ds = gdal.Open(src['SourceFilename']) raster_band = ds.GetRasterBand(src['SourceBand']) src['DataType'] = raster_band.DataType