<div dir="ltr">Hi List,<div><br></div><div>We use GDAL3+ with MapWinGIS (C++ ActiveX control) and I'm working on making unit/functional tests for it.</div><div><br></div><div>In one of our functions, we use GDALTranslate().</div><div>I'm creating the unit tests in C#:</div><div>We pass the options as a string array to our C++ function, which expects <span style="font-family:monospace">SAFEARRAY*</span> </div><div><br></div><div>This one is working:</div><div><font face="monospace">var options = new[]<br>{<br> "-of", "GTiff",<br> "-co", "TILED=YES"<br>};</font><br>And this one is causing an AccessViolationException<br><font face="monospace">var options = new[]<br>{<br> "-ot", "Float32",<br> "-tr", "0.2", "0.2",<br> "-r", "average",<br> "-projwin", "-180", "90", "180", "-90"<br>};</font><br> <br></div><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">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:</div><div dir="ltr"><font face="monospace">gdal_translate -ot "Float32" -tr 0.2 0.2 -r "average" -projwin -180 90 180 -90 float32_50m.tif float32_50m-translated.tif</font><br></div><div dir="ltr">So clearly we're doing something wrong in probably creating the array of options.</div><div dir="ltr"><br>
Our C++ code looks like this:</div><div dir="ltr"><font face="monospace">STDMETHODIMP CGdalUtils::GdalRasterTranslate(BSTR sourceFilename, BSTR destinationFilename, SAFEARRAY* options, VARIANT_BOOL* retVal)<br>{<br> AFX_MANAGE_STATE(AfxGetStaticModuleState());<br> *retVal = VARIANT_FALSE;<br> _detailedError = "No error";<br><br> const CStringW srcFilename(sourceFilename);<br><br> // Open file as GdalDataset:<br> const auto dt = GdalHelper::OpenRasterDatasetW(srcFilename, GA_ReadOnly);<br> if (!dt)<br> {<br> _detailedError = "Can't open " + srcFilename + " as a raster file.";<br> ErrorMsg(tkINVALID_FILENAME);<br> goto cleaning;<br> }<br><br> // Make options: <br> char** translateOptions = nullptr;<br> translateOptions = ConvertSafeArrayToChar(options);<br><br> const auto gdalTranslateOptions = GDALTranslateOptionsNew(translateOptions, nullptr);<br> if (!gdalTranslateOptions)<br> {<br> _detailedError = "Can't convert the option array to GDALTranslateOptions";<br> ErrorMessage(tkINVALID_PARAMETERS_ARRAY);<br> goto cleaning;<br> }<br><br> // Call the gdalTranslate function:<br> GDALTranslateOptionsSetProgress(gdalTranslateOptions, GDALProgressCallback, ¶ms);<br> m_globalSettings.SetGdalUtf8(true);<br> const auto dtNew = GDALTranslate(CString(destinationFilename), dt, gdalTranslateOptions, nullptr);<br> m_globalSettings.SetGdalUtf8(false);<br><br> if (dtNew)<br> {<br> *retVal = VARIANT_TRUE;<br> GDALClose(dtNew);<br> }<br> else<br> {<br> ErrorMessage(tkGDAL_ERROR);<br> _detailedError = CPLGetLastErrorMsg();<br> }<br><br>cleaning:<br> if (gdalTranslateOptions)<br> GDALTranslateOptionsFree(gdalTranslateOptions);<br><br> if (translateOptions)<br> CSLDestroy(translateOptions);<br><br> if (dt)<br> GDALClose(dt);<br><br> return S_OK;<br>}</font><br><br>My questions:<br><ol><li>How should the options array be formatted?</li><li>And how can we detect if the
<span style="font-family:monospace">gdalTranslateOptions</span> are correct before we send them to <span style="font-family:monospace">GDALTranslate</span>?</li></ol></div><div dir="ltr"><br></div><div>Thanks,</div><div><br></div><div dir="ltr">Paul<br><br>
</div></div></div></div></div></div></div></div>