[gdal-dev] help please with close/reopen and GDALSubdatasetInfo

Michael Sumner mdsumner at gmail.com
Sat Dec 2 20:10:25 PST 2023


May I please ask for assistance with this code?  I'm trying to close a
dataset with subdatasets and then reopen if the requested subdataset is
found.

It segfaults for all but the last subdataset name ... and I'm at a loss for
what I'm doing wrong.

Thank you.

Code below, and this gist documents it with a little more details:
https://gist.github.com/mdsumner/2ffc9b4746454fa0152b4a250900e535

Cheers, Mike


#include "gdal.h"
#include "gdal_priv.h"
#include <iostream>
int main(int argc, char **argv) {
  GDALAllRegister();

  auto poSrcDS =
    GDALDataset::Open(argv[1], GDAL_OF_RASTER, nullptr, nullptr, nullptr);
  if (poSrcDS == nullptr)
  {
    return 0;
  }
  char **papszSubdatasets = poSrcDS->GetMetadata("SUBDATASETS");
  int nSubdatasets = CSLCount(papszSubdatasets);
  char *pszSubdatasetSource = nullptr;
  if (nSubdatasets > 0)
  {
    for (int j = 0; j < nSubdatasets; j += 2)
    {
      pszSubdatasetSource = CPLStrdup(strstr(papszSubdatasets[j], "=") + 1);
      GDALSubdatasetInfoH info = GDALGetSubdatasetInfo(pszSubdatasetSource);
      if ( EQUAL(argv[2], GDALSubdatasetInfoGetSubdatasetComponent(info))) {
        std::cout << pszSubdatasetSource << "\n";
        poSrcDS->ReleaseRef();
        poSrcDS = GDALDataset::Open(pszSubdatasetSource, GDAL_OF_RASTER,
nullptr, nullptr, nullptr);
        CPLFree(pszSubdatasetSource);
        GDALDestroySubdatasetInfo(info);

      }
    }
  }

  poSrcDS->ReleaseRef();
  return 1;
}

-- 
Michael Sumner
Software and Database 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/20231203/65a3eff8/attachment.htm>


More information about the gdal-dev mailing list