[gdal-dev] replacing a dataset in C++

Michael Sumner mdsumner at gmail.com
Sat May 10 15:24:00 PDT 2025


Ugh, sorry made a mistake in the code, fixed for osArrayName:

auto poSrcDS = std::unique_ptr<GDALDataset, GDALDatasetUniquePtrReleaser>(
         GDALDataset::Open(osFilename, GDAL_OF_RASTER |
GDAL_OF_VERBOSE_ERROR,
                          aosAllowedDrivers.List(), aosOpenOptions.List(),
                          nullptr));
// now open in mdim mode, and get the specified MDArray in Classic form
with index_x,index_y
auto poMDimDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(osFilename,
GDAL_OF_MULTIDIM_RASTER));
auto poMdimGroup = poMDimDS->GetRootGroup();
// ---auto poArrayDS = poMdimGroup->OpenMDArrayFromFullname(osFilename);
// +++auto poArrayDS = poMdimGroup->OpenMDArrayFromFullname(osArrayName);
auto poClassicDS = poArrayDS->AsClassicDataset(index_x, index_y);

// FIXME how to safely swap poSrcDS with poClassicDS
poSrcDS = std::unique_ptr<GDALDataset,
GDALDatasetUniquePtrReleaser>(poClassicDS);


On Sun, May 11, 2025 at 8:20 AM Michael Sumner <mdsumner at gmail.com> wrote:

> Hello, I have a question about safely re-casting a dataset to another, I
> want to use Multidim to re-orient an MDArray as a classic 2D raster. I was
> a bit surprised that this works (I just overwrite poSrcDS with the
> classic form):
>
> I'm passing in 'osFilename', 'osArrayName' and two dimension indexes
> 'index_x' and 'index_y', the first "Open()" line is a literal copy from
> vrtdataset.cpp:
>
>
> //  I've removed all the nullptr checks from this code
> auto poSrcDS = std::unique_ptr<GDALDataset, GDALDatasetUniquePtrReleaser>(
>          GDALDataset::Open(osFilename, GDAL_OF_RASTER |
> GDAL_OF_VERBOSE_ERROR,
>                           aosAllowedDrivers.List(), aosOpenOptions.List(),
>                           nullptr));
> // now open in mdim mode, and get the specified MDArray in Classic form
> with index_x,index_y
> auto poMDimDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(osFilename,
> GDAL_OF_MULTIDIM_RASTER));
> auto poMdimGroup = poMDimDS->GetRootGroup();
> auto poArrayDS = poMdimGroup->OpenMDArrayFromFullname(osFilename);
> auto poClassicDS = poArrayDS->AsClassicDataset(index_x, index_y);
>
> // FIXME how to safely swap poSrcDS with poClassicDS
> poSrcDS = std::unique_ptr<GDALDataset,
> GDALDatasetUniquePtrReleaser>(poClassicDS);
>
> My question is about the last line where poClassic "replaces" poSrcDS:
> what am I responsible for to make that safe?
>
> Thanks!
>
>
> --
> Michael Sumner
> Research Software Engineer
> Australian Antarctic Division
> Hobart, Australia
> e-mail: mdsumner at gmail.com
>


-- 
Michael Sumner
Research Software Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: mdsumner at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20250511/2c2cc9e4/attachment.htm>


More information about the gdal-dev mailing list