LCOV - code coverage report
Current view: top level - apps - gdalmdimtranslate_bin.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 31 45 68.9 %
Date: 2024-11-21 22:18:42 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL Utilities
       4             :  * Purpose:  Command line application to convert a multidimensional raster
       5             :  * Author:   Even Rouault,<even.rouault at spatialys.com>
       6             :  *
       7             :  * ****************************************************************************
       8             :  * Copyright (c) 2019, Even Rouault <even.rouault at spatialys.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "cpl_string.h"
      14             : #include "gdal_version.h"
      15             : #include "commonutils.h"
      16             : #include "gdal_utils_priv.h"
      17             : #include "gdal_priv.h"
      18             : 
      19             : /**
      20             :  * @brief Makes sure the GDAL library is properly cleaned up before exiting.
      21             :  * @param nCode exit code
      22             :  * @todo Move to API
      23             :  */
      24           1 : static void GDALExit(int nCode)
      25             : {
      26           1 :     GDALDestroy();
      27           1 :     exit(nCode);
      28             : }
      29             : 
      30             : /************************************************************************/
      31             : /*                               Usage()                                */
      32             : /************************************************************************/
      33             : 
      34           0 : static void Usage()
      35             : {
      36           0 :     fprintf(stderr, "%s\n", GDALMultiDimTranslateAppGetParserUsage().c_str());
      37           0 :     GDALExit(1);
      38           0 : }
      39             : 
      40             : /************************************************************************/
      41             : /*                                main()                                */
      42             : /************************************************************************/
      43             : 
      44           4 : MAIN_START(argc, argv)
      45             : {
      46             :     /* Check strict compilation and runtime library version as we use C++ API */
      47           4 :     if (!GDAL_CHECK_VERSION(argv[0]))
      48           0 :         GDALExit(1);
      49             : 
      50           4 :     EarlySetConfigOptions(argc, argv);
      51             : 
      52             :     /* -------------------------------------------------------------------- */
      53             :     /*      Generic arg processing.                                         */
      54             :     /* -------------------------------------------------------------------- */
      55           4 :     GDALAllRegister();
      56             : 
      57           4 :     argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
      58           4 :     if (argc < 1)
      59           0 :         GDALExit(-argc);
      60             : 
      61             :     /* -------------------------------------------------------------------- */
      62             :     /*      Parse command line                                              */
      63             :     /* -------------------------------------------------------------------- */
      64             : 
      65           6 :     GDALMultiDimTranslateOptionsForBinary sOptionsForBinary;
      66             : 
      67             :     std::unique_ptr<GDALMultiDimTranslateOptions,
      68             :                     decltype(&GDALMultiDimTranslateOptionsFree)>
      69             :         psOptions{GDALMultiDimTranslateOptionsNew(argv + 1, &sOptionsForBinary),
      70           4 :                   GDALMultiDimTranslateOptionsFree};
      71           3 :     CSLDestroy(argv);
      72           3 :     if (!psOptions)
      73             :     {
      74           0 :         Usage();
      75             :     }
      76             : 
      77           3 :     if (!(sOptionsForBinary.bQuiet))
      78             :     {
      79           3 :         GDALMultiDimTranslateOptionsSetProgress(psOptions.get(),
      80             :                                                 GDALTermProgress, nullptr);
      81             :     }
      82             : 
      83             :     /* -------------------------------------------------------------------- */
      84             :     /*      Open input file.                                                */
      85             :     /* -------------------------------------------------------------------- */
      86           3 :     GDALDatasetH hInDS = GDALOpenEx(
      87             :         sOptionsForBinary.osSource.c_str(),
      88             :         GDAL_OF_RASTER | GDAL_OF_MULTIDIM_RASTER | GDAL_OF_VERBOSE_ERROR,
      89           3 :         sOptionsForBinary.aosAllowInputDrivers.List(),
      90           3 :         sOptionsForBinary.aosOpenOptions.List(), nullptr);
      91             : 
      92           3 :     if (hInDS == nullptr)
      93           1 :         GDALExit(1);
      94             : 
      95             :     /* -------------------------------------------------------------------- */
      96             :     /*      Open output file if in update mode.                             */
      97             :     /* -------------------------------------------------------------------- */
      98           2 :     GDALDatasetH hDstDS = nullptr;
      99             :     // Note: since bUpdate is never changed and defaults to false this block
     100             :     //       will never be executed
     101           2 :     if (sOptionsForBinary.bUpdate)
     102             :     {
     103           0 :         CPLPushErrorHandler(CPLQuietErrorHandler);
     104           0 :         hDstDS = GDALOpenEx(sOptionsForBinary.osDest.c_str(),
     105             :                             GDAL_OF_RASTER | GDAL_OF_MULTIDIM_RASTER |
     106             :                                 GDAL_OF_VERBOSE_ERROR | GDAL_OF_UPDATE,
     107             :                             nullptr, nullptr, nullptr);
     108           0 :         CPLPopErrorHandler();
     109             :     }
     110             : 
     111           2 :     int bUsageError = FALSE;
     112             :     GDALDatasetH hRetDS =
     113           2 :         GDALMultiDimTranslate(sOptionsForBinary.osDest.c_str(), hDstDS, 1,
     114           2 :                               &hInDS, psOptions.get(), &bUsageError);
     115             : 
     116           2 :     if (bUsageError == TRUE)
     117           0 :         Usage();
     118             : 
     119           2 :     int nRetCode = hRetDS ? 0 : 1;
     120             : 
     121           2 :     if (GDALClose(hRetDS) != CE_None)
     122           0 :         nRetCode = 1;
     123             : 
     124           2 :     if (GDALClose(hInDS) != CE_None)
     125           0 :         nRetCode = 1;
     126             : 
     127           2 :     GDALDestroy();
     128             : 
     129           2 :     return nRetCode;
     130             : }
     131             : 
     132           0 : MAIN_END

Generated by: LCOV version 1.14