[gdal-dev] gdal_translate options

Paul Meems bontepaarden at gmail.com
Wed Mar 9 07:14:19 PST 2022


Hi List,

We use GDAL3+ with MapWinGIS (C++ ActiveX control) and I'm working on
making unit/functional tests for it.

In one of our functions, we use GDALTranslate().
I'm creating the unit tests in C#:
We pass the options as a string array to our C++ function, which expects
SAFEARRAY*

This one is working:
var options = new[]
{
    "-of", "GTiff",
    "-co",  "TILED=YES"
};
And this one is causing an AccessViolationException
var options = new[]
{
      "-ot", "Float32",
      "-tr", "0.2", "0.2",
      "-r", "average",
      "-projwin", "-180", "90", "180", "-90"
};

When I run the last one on the command line using the same GDAL version and
the same input file, it is working as well:
gdal_translate -ot "Float32" -tr  0.2 0.2 -r "average" -projwin -180 90 180
-90 float32_50m.tif float32_50m-translated.tif
So clearly we're doing something wrong in probably creating the array of
options.

Our C++ code looks like this:
STDMETHODIMP CGdalUtils::GdalRasterTranslate(BSTR sourceFilename, BSTR
destinationFilename, SAFEARRAY* options, VARIANT_BOOL* retVal)
{
  AFX_MANAGE_STATE(AfxGetStaticModuleState());
  *retVal = VARIANT_FALSE;
  _detailedError = "No error";

  const CStringW srcFilename(sourceFilename);

  // Open file as GdalDataset:
  const auto dt = GdalHelper::OpenRasterDatasetW(srcFilename, GA_ReadOnly);
  if (!dt)
  {
    _detailedError = "Can't open " + srcFilename + " as a raster file.";
    ErrorMsg(tkINVALID_FILENAME);
    goto cleaning;
  }

  // Make options:
  char** translateOptions = nullptr;
  translateOptions = ConvertSafeArrayToChar(options);

  const auto gdalTranslateOptions =
GDALTranslateOptionsNew(translateOptions, nullptr);
  if (!gdalTranslateOptions)
  {
    _detailedError = "Can't convert the option array to
GDALTranslateOptions";
    ErrorMessage(tkINVALID_PARAMETERS_ARRAY);
    goto cleaning;
  }

  // Call the gdalTranslate function:
  GDALTranslateOptionsSetProgress(gdalTranslateOptions,
GDALProgressCallback, &params);
  m_globalSettings.SetGdalUtf8(true);
  const auto dtNew = GDALTranslate(CString(destinationFilename), dt,
gdalTranslateOptions, nullptr);
  m_globalSettings.SetGdalUtf8(false);

  if (dtNew)
  {
    *retVal = VARIANT_TRUE;
    GDALClose(dtNew);
  }
  else
  {
    ErrorMessage(tkGDAL_ERROR);
    _detailedError = CPLGetLastErrorMsg();
  }

cleaning:
  if (gdalTranslateOptions)
    GDALTranslateOptionsFree(gdalTranslateOptions);

  if (translateOptions)
    CSLDestroy(translateOptions);

  if (dt)
    GDALClose(dt);

  return S_OK;
}

My questions:

   1. How should the options array be formatted?
   2. And how can we detect if the  gdalTranslateOptions are correct before
   we send them to GDALTranslate?


Thanks,

Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20220309/2fe8098f/attachment.html>


More information about the gdal-dev mailing list