| 34 | /************************************************************************/ |
| 35 | |
| 36 | bool GDALRasterWriteAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt) |
| 37 | { |
| 38 | auto pfnProgress = ctxt.m_pfnProgress; |
| 39 | auto pProgressData = ctxt.m_pProgressData; |
| 40 | auto poSrcDS = m_inputDataset[0].GetDatasetRef(); |
| 41 | CPLAssert(poSrcDS); |
| 42 | CPLAssert(!m_outputDataset.GetDatasetRef()); |
| 43 | |
| 44 | if (m_format == "stream") |
| 45 | { |
| 46 | m_outputDataset.Set(poSrcDS); |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | CPLStringList aosOptions; |
| 51 | if (!m_overwrite) |
| 52 | { |
| 53 | aosOptions.AddString("--no-overwrite"); |
| 54 | } |
| 55 | if (m_appendRaster) |
| 56 | { |
| 57 | CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler); |
| 58 | if (std::unique_ptr<GDALDataset>(GDALDataset::Open( |
| 59 | m_outputDataset.GetName().c_str(), GDAL_OF_RASTER))) |
| 60 | { |
| 61 | aosOptions.AddString("-co"); |
| 62 | aosOptions.AddString("APPEND_SUBDATASET=YES"); |
| 63 | } |
| 64 | } |
| 65 | if (!m_format.empty()) |
| 66 | { |
| 67 | aosOptions.AddString("-of"); |
| 68 | aosOptions.AddString(m_format.c_str()); |
| 69 | } |
| 70 | for (const auto &co : m_creationOptions) |
| 71 | { |
| 72 | aosOptions.AddString("-co"); |
| 73 | aosOptions.AddString(co.c_str()); |
| 74 | } |
| 75 | |
| 76 | GDALTranslateOptions *psOptions = |
| 77 | GDALTranslateOptionsNew(aosOptions.List(), nullptr); |
| 78 | GDALTranslateOptionsSetProgress(psOptions, pfnProgress, pProgressData); |
| 79 | |
| 80 | // Backup error state since GDALTranslate() resets it multiple times |
| 81 | const auto nLastErrorNum = CPLGetLastErrorNo(); |
| 82 | const auto nLastErrorType = CPLGetLastErrorType(); |
| 83 | const std::string osLastErrorMsg = CPLGetLastErrorMsg(); |
| 84 | const auto nLastErrorCounter = CPLGetErrorCounter(); |
| 85 | |
| 86 | GDALDatasetH hSrcDS = GDALDataset::ToHandle(poSrcDS); |
| 87 | auto poRetDS = GDALDataset::FromHandle(GDALTranslate( |
| 88 | m_outputDataset.GetName().c_str(), hSrcDS, psOptions, nullptr)); |
| 89 | GDALTranslateOptionsFree(psOptions); |
| 90 | |
| 91 | if (nLastErrorCounter > 0 && CPLGetErrorCounter() == 0) |
| 92 | { |
| 93 | CPLErrorSetState(nLastErrorType, nLastErrorNum, osLastErrorMsg.c_str(), |
nothing calls this directly
no test coverage detected