LCOV - code coverage report
Current view: top level - apps - gdal_rasterize_bin.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 30 58 51.7 %
Date: 2024-11-21 22:18:42 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL Utilities
       4             :  * Purpose:  Rasterize OGR shapes into a GDAL raster.
       5             :  * Authors:  Even Rouault, <even dot rouault at spatialys dot com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2015, Even Rouault <even dot rouault at spatialys dot 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             : /*                               Usage()                                */
      21             : /************************************************************************/
      22             : 
      23           0 : static void Usage()
      24             : 
      25             : {
      26           0 :     fprintf(stderr, "%s\n", GDALRasterizeAppGetParserUsage().c_str());
      27           0 :     exit(1);
      28             : }
      29             : 
      30             : /************************************************************************/
      31             : /*                                main()                                */
      32             : /************************************************************************/
      33             : 
      34          10 : MAIN_START(argc, argv)
      35             : {
      36             : 
      37             :     /* Check strict compilation and runtime library version as we use C++ API */
      38          10 :     if (!GDAL_CHECK_VERSION(argv[0]))
      39           0 :         exit(1);
      40             : 
      41          10 :     EarlySetConfigOptions(argc, argv);
      42             : 
      43             :     /* -------------------------------------------------------------------- */
      44             :     /*      Register standard GDAL drivers, and process generic GDAL        */
      45             :     /*      command options.                                                */
      46             :     /* -------------------------------------------------------------------- */
      47          10 :     GDALAllRegister();
      48          10 :     argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
      49          10 :     if (argc < 1)
      50           0 :         exit(-argc);
      51             : 
      52             :     /* -------------------------------------------------------------------- */
      53             :     /*      Generic arg processing.                                         */
      54             :     /* -------------------------------------------------------------------- */
      55             : 
      56          19 :     GDALRasterizeOptionsForBinary sOptionsForBinary;
      57             :     std::unique_ptr<GDALRasterizeOptions, decltype(&GDALRasterizeOptionsFree)>
      58             :         psOptions{GDALRasterizeOptionsNew(argv + 1, &sOptionsForBinary),
      59          10 :                   GDALRasterizeOptionsFree};
      60             : 
      61           9 :     CSLDestroy(argv);
      62             : 
      63           9 :     if (!psOptions)
      64             :     {
      65           0 :         Usage();
      66             :     }
      67             : 
      68           9 :     if (!(sOptionsForBinary.bQuiet))
      69             :     {
      70           8 :         GDALRasterizeOptionsSetProgress(psOptions.get(), GDALTermProgress,
      71             :                                         nullptr);
      72             :     }
      73             : 
      74             :     /* -------------------------------------------------------------------- */
      75             :     /*      Open input file.                                                */
      76             :     /* -------------------------------------------------------------------- */
      77           9 :     GDALDatasetH hInDS = GDALOpenEx(sOptionsForBinary.osSource.c_str(),
      78             :                                     GDAL_OF_VECTOR | GDAL_OF_VERBOSE_ERROR,
      79             :                                     /*papszAllowedDrivers=*/nullptr,
      80           9 :                                     sOptionsForBinary.aosOpenOptions.List(),
      81             :                                     /*papszSiblingFiles=*/nullptr);
      82             : 
      83           9 :     if (hInDS == nullptr)
      84           0 :         exit(1);
      85             : 
      86             :     /* -------------------------------------------------------------------- */
      87             :     /*      Open output file if it exists.                                  */
      88             :     /* -------------------------------------------------------------------- */
      89           9 :     GDALDatasetH hDstDS = nullptr;
      90           9 :     if (!(sOptionsForBinary.bCreateOutput))
      91             :     {
      92           3 :         CPLPushErrorHandler(CPLQuietErrorHandler);
      93             :         hDstDS =
      94           3 :             GDALOpenEx(sOptionsForBinary.osDest.c_str(),
      95             :                        GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR | GDAL_OF_UPDATE,
      96             :                        nullptr, nullptr, nullptr);
      97           3 :         CPLPopErrorHandler();
      98             :     }
      99             : 
     100           9 :     if (!sOptionsForBinary.osFormat.empty() &&
     101           0 :         (sOptionsForBinary.bCreateOutput || hDstDS == nullptr))
     102             :     {
     103           0 :         GDALDriverManager *poDM = GetGDALDriverManager();
     104             :         GDALDriver *poDriver =
     105           0 :             poDM->GetDriverByName(sOptionsForBinary.osFormat.c_str());
     106           0 :         char **papszDriverMD = (poDriver) ? poDriver->GetMetadata() : nullptr;
     107           0 :         if (poDriver == nullptr ||
     108           0 :             !CPLTestBool(CSLFetchNameValueDef(papszDriverMD, GDAL_DCAP_RASTER,
     109           0 :                                               "FALSE")) ||
     110           0 :             !CPLTestBool(
     111             :                 CSLFetchNameValueDef(papszDriverMD, GDAL_DCAP_CREATE, "FALSE")))
     112             :         {
     113           0 :             fprintf(stderr,
     114             :                     "Output driver `%s' not recognised or does not support "
     115             :                     "direct output file creation.\n",
     116             :                     sOptionsForBinary.osFormat.c_str());
     117           0 :             fprintf(stderr, "The following format drivers are configured and "
     118             :                             "support direct output:\n");
     119             : 
     120           0 :             for (int iDriver = 0; iDriver < poDM->GetDriverCount(); iDriver++)
     121             :             {
     122           0 :                 GDALDriver *poIter = poDM->GetDriver(iDriver);
     123           0 :                 papszDriverMD = poIter->GetMetadata();
     124           0 :                 if (CPLTestBool(CSLFetchNameValueDef(
     125           0 :                         papszDriverMD, GDAL_DCAP_RASTER, "FALSE")) &&
     126           0 :                     CPLTestBool(CSLFetchNameValueDef(
     127             :                         papszDriverMD, GDAL_DCAP_CREATE, "FALSE")))
     128             :                 {
     129           0 :                     fprintf(stderr, "  -> `%s'\n", poIter->GetDescription());
     130             :                 }
     131             :             }
     132           0 :             exit(1);
     133             :         }
     134             :     }
     135             : 
     136           9 :     int bUsageError = FALSE;
     137             :     GDALDatasetH hRetDS =
     138           9 :         GDALRasterize(sOptionsForBinary.osDest.c_str(), hDstDS, hInDS,
     139           9 :                       psOptions.get(), &bUsageError);
     140             : 
     141           9 :     if (bUsageError == TRUE)
     142           0 :         Usage();
     143             : 
     144           9 :     int nRetCode = hRetDS ? 0 : 1;
     145             : 
     146           9 :     GDALClose(hInDS);
     147             : 
     148           9 :     if (GDALClose(hRetDS) != CE_None)
     149           0 :         nRetCode = 1;
     150             : 
     151           9 :     GDALDestroyDriverManager();
     152             : 
     153           9 :     return nRetCode;
     154             : }
     155             : 
     156           0 : MAIN_END

Generated by: LCOV version 1.14