Skip to content

Commit 002e4ff

Browse files
gshiromagmgunter
authored andcommitted
Add close_dataset() to pybind Raster.cpp (#863)
* add close_dataset() to pybind Raster.cpp * Update python/extensions/pybind_isce3/io/Raster.cpp Co-authored-by: Geoffrey M Gunter <[email protected]> * only call GDALClose from Raster destructor if attribute _dataset is not null * check GDAL dataset ownership before deleting it Co-authored-by: Geoffrey M Gunter <[email protected]>
1 parent 3990cb8 commit 002e4ff

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

cxx/isce3/io/Raster.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ int isce3::io::Raster::setEPSG(int epsgcode)
233233
// Destructor. When GDALOpenShared() is used the dataset is dereferenced
234234
// and closed only if the referenced count is less than 1.
235235
isce3::io::Raster::~Raster() {
236-
if (_owner) {
236+
if (_owner and _dataset != nullptr) {
237237
GDALClose( _dataset );
238238
}
239239
}

cxx/isce3/io/Raster.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ class isce3::io::Raster {
132132
* @param[in] ds GDALDataset pointer*/
133133
inline void dataset(GDALDataset* ds) { _dataset=ds; }
134134

135+
/** GDALDataset owner getter*/
136+
inline bool dataset_owner() const { return _owner; }
137+
135138
/** Return GDALDatatype of specified band
136139
*
137140
* @param[in] band Band number in 1-index*/

python/extensions/pybind_isce3/io/Raster.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ void addbinding(py::class_<Raster> & pyRaster)
9393
"Create a VRT raster dataset from list of rasters",
9494
py::arg("path"),
9595
py::arg("raster_list"))
96+
.def("close_dataset", [](Raster & self)
97+
{
98+
if (self.dataset_owner()) {
99+
auto gdal_ds = self.dataset();
100+
delete gdal_ds;
101+
}
102+
self.dataset(nullptr);
103+
},
104+
R"(
105+
Close the dataset.
106+
107+
Decrements the reference count of the underlying `GDALDataset`, which,
108+
if this was the last open instance, causes the dataset to be closed
109+
and any cached changes to be flushed to disk.
110+
111+
This invalidates the `Raster` instance -- it cannot be used after closing
112+
the underlying dataset.
113+
)")
96114
.def(py::init([](std::uintptr_t py_ds_ptr)
97115
{
98116
auto gdal_ds = reinterpret_cast<GDALDataset*>(py_ds_ptr);

0 commit comments

Comments
 (0)