LCOV - code coverage report
Current view: top level - apps - gdalmdimtranslate_bin.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 36 53 67.9 %
Date: 2024-05-03 15:49:35 Functions: 1 2 50.0 %

          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             :  * Permission is hereby granted, free of charge, to any person obtaining a
      11             :  * copy of this software and associated documentation files (the "Software"),
      12             :  * to deal in the Software without restriction, including without limitation
      13             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      14             :  * and/or sell copies of the Software, and to permit persons to whom the
      15             :  * Software is furnished to do so, subject to the following conditions:
      16             :  *
      17             :  * The above copyright notice and this permission notice shall be included
      18             :  * in all copies or substantial portions of the Software.
      19             :  *
      20             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      21             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      22             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      23             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      24             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      25             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      26             :  * DEALINGS IN THE SOFTWARE.
      27             :  ****************************************************************************/
      28             : 
      29             : #include "cpl_string.h"
      30             : #include "gdal_version.h"
      31             : #include "commonutils.h"
      32             : #include "gdal_utils_priv.h"
      33             : #include "gdal_priv.h"
      34             : 
      35             : /************************************************************************/
      36             : /*                               Usage()                                */
      37             : /************************************************************************/
      38             : 
      39           0 : static void Usage(bool bIsError, const char *pszErrorMsg = nullptr)
      40             : 
      41             : {
      42           0 :     fprintf(bIsError ? stderr : stdout,
      43             :             "Usage: gdalmdimtranslate [--help] [--help-general]\n"
      44             :             "                         [-if <format>]... [-of <format>]\n"
      45             :             "                         [-co <NAME>=<VALUE>]...\n"
      46             :             "                         [-array <array_spec>]...\n"
      47             :             "                         [-arrayoption <NAME>=<VALUE>]...\n"
      48             :             "                         [-group <group_spec>]...\n"
      49             :             "                         [-subset <subset_spec>]...\n"
      50             :             "                         [-scaleaxes <scaleaxes_spec>]\n"
      51             :             "                         [-oo <NAME>=<VALUE>]...\n"
      52             :             "                         <src_filename> <dst_filename>\n");
      53             : 
      54           0 :     if (pszErrorMsg != nullptr)
      55           0 :         fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
      56             : 
      57           0 :     exit(bIsError ? 1 : 0);
      58             : }
      59             : 
      60             : /************************************************************************/
      61             : /*                                main()                                */
      62             : /************************************************************************/
      63             : 
      64           4 : MAIN_START(argc, argv)
      65             : {
      66             :     /* Check strict compilation and runtime library version as we use C++ API */
      67           4 :     if (!GDAL_CHECK_VERSION(argv[0]))
      68           0 :         exit(1);
      69             : 
      70           4 :     EarlySetConfigOptions(argc, argv);
      71             : 
      72             :     /* -------------------------------------------------------------------- */
      73             :     /*      Generic arg processing.                                         */
      74             :     /* -------------------------------------------------------------------- */
      75           4 :     GDALAllRegister();
      76           4 :     argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
      77           4 :     if (argc < 1)
      78           0 :         exit(-argc);
      79             : 
      80          18 :     for (int i = 0; i < argc; i++)
      81             :     {
      82          15 :         if (EQUAL(argv[i], "--utility_version"))
      83             :         {
      84           1 :             printf("%s was compiled against GDAL %s and "
      85             :                    "is running against GDAL %s\n",
      86             :                    argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
      87           1 :             CSLDestroy(argv);
      88           1 :             return 0;
      89             :         }
      90          14 :         else if (EQUAL(argv[i], "--help"))
      91             :         {
      92           0 :             Usage(false);
      93             :         }
      94             :     }
      95             : 
      96           3 :     GDALMultiDimTranslateOptionsForBinary sOptionsForBinary;
      97             :     // coverity[tainted_data]
      98             :     GDALMultiDimTranslateOptions *psOptions =
      99           3 :         GDALMultiDimTranslateOptionsNew(argv + 1, &sOptionsForBinary);
     100           3 :     CSLDestroy(argv);
     101             : 
     102           3 :     if (psOptions == nullptr)
     103             :     {
     104           0 :         Usage(true);
     105             :     }
     106             : 
     107           3 :     if (!(sOptionsForBinary.bQuiet))
     108             :     {
     109           3 :         GDALMultiDimTranslateOptionsSetProgress(psOptions, GDALTermProgress,
     110             :                                                 nullptr);
     111             :     }
     112             : 
     113           3 :     if (sOptionsForBinary.osSource.empty())
     114           0 :         Usage(true, "No input file specified.");
     115             : 
     116           3 :     if (sOptionsForBinary.osDest.empty())
     117           0 :         Usage(true, "No output file specified.");
     118             : 
     119             :     /* -------------------------------------------------------------------- */
     120             :     /*      Open input file.                                                */
     121             :     /* -------------------------------------------------------------------- */
     122           3 :     GDALDatasetH hInDS = GDALOpenEx(
     123             :         sOptionsForBinary.osSource.c_str(),
     124             :         GDAL_OF_RASTER | GDAL_OF_MULTIDIM_RASTER | GDAL_OF_VERBOSE_ERROR,
     125           3 :         sOptionsForBinary.aosAllowInputDrivers.List(),
     126           3 :         sOptionsForBinary.aosOpenOptions.List(), nullptr);
     127             : 
     128           3 :     if (hInDS == nullptr)
     129           1 :         exit(1);
     130             : 
     131             :     /* -------------------------------------------------------------------- */
     132             :     /*      Open output file if in update mode.                             */
     133             :     /* -------------------------------------------------------------------- */
     134           2 :     GDALDatasetH hDstDS = nullptr;
     135           2 :     if (sOptionsForBinary.bUpdate)
     136             :     {
     137           0 :         CPLPushErrorHandler(CPLQuietErrorHandler);
     138           0 :         hDstDS = GDALOpenEx(sOptionsForBinary.osDest.c_str(),
     139             :                             GDAL_OF_RASTER | GDAL_OF_MULTIDIM_RASTER |
     140             :                                 GDAL_OF_VERBOSE_ERROR | GDAL_OF_UPDATE,
     141             :                             nullptr, nullptr, nullptr);
     142           0 :         CPLPopErrorHandler();
     143             :     }
     144             : 
     145           2 :     int bUsageError = FALSE;
     146             :     GDALDatasetH hRetDS =
     147           2 :         GDALMultiDimTranslate(sOptionsForBinary.osDest.c_str(), hDstDS, 1,
     148             :                               &hInDS, psOptions, &bUsageError);
     149           2 :     if (bUsageError == TRUE)
     150           0 :         Usage(true);
     151           2 :     int nRetCode = hRetDS ? 0 : 1;
     152             : 
     153           2 :     if (GDALClose(hRetDS) != CE_None)
     154           0 :         nRetCode = 1;
     155             : 
     156           2 :     GDALClose(hInDS);
     157           2 :     GDALMultiDimTranslateOptionsFree(psOptions);
     158             : 
     159           2 :     GDALDestroyDriverManager();
     160             : 
     161           2 :     return nRetCode;
     162             : }
     163             : 
     164           0 : MAIN_END

Generated by: LCOV version 1.14