LCOV - code coverage report
Current view: top level - apps - gdal_rasterize_bin.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 38 71 53.5 %
Date: 2024-05-03 15:49:35 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             :  * 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(
      43             :         bIsError ? stderr : stdout,
      44             :         "Usage: gdal_rasterize [--help] [--help-general]\n"
      45             :         "       [-b <band>]... [-i] [-at]\n"
      46             :         "       [-oo <NAME>=<VALUE>]...\n"
      47             :         "       {[-burn <value>]... | [-a <attribute_name>] | [-3d]} [-add]\n"
      48             :         "       [-l <layername>]... [-where <expression>] "
      49             :         "[-sql <select_statement>|@<filename>]\n"
      50             :         "       [-dialect <dialect>] [-of <format>] [-a_srs <srs_def>] [-to "
      51             :         "<NAME>=<VALUE>]...\n"
      52             :         "       [-co <NAME>=<VALUE>]... [-a_nodata <value>] [-init "
      53             :         "<value>]...\n"
      54             :         "       [-te <xmin> <ymin> <xmax> <ymax>] [-tr <xres> <yres>] [-tap] "
      55             :         "[-ts <width> "
      56             :         "<height>]\n"
      57             :         "       [-ot "
      58             :         "{Byte/Int8/Int16/UInt16/UInt32/Int32/UInt64/Int64/Float32/Float64/\n"
      59             :         "             CInt16/CInt32/CFloat32/CFloat64}] [-optim "
      60             :         "{AUTO|VECTOR|RASTER}] [-q]\n"
      61             :         "       <src_datasource> <dst_filename>\n");
      62             : 
      63           0 :     if (pszErrorMsg != nullptr)
      64           0 :         fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
      65           0 :     exit(bIsError ? 1 : 0);
      66             : }
      67             : 
      68             : /************************************************************************/
      69             : /*                                main()                                */
      70             : /************************************************************************/
      71             : 
      72          10 : MAIN_START(argc, argv)
      73             : {
      74             :     /* Check strict compilation and runtime library version as we use C++ API */
      75          10 :     if (!GDAL_CHECK_VERSION(argv[0]))
      76           0 :         exit(1);
      77             : 
      78          10 :     EarlySetConfigOptions(argc, argv);
      79             : 
      80             :     /* -------------------------------------------------------------------- */
      81             :     /*      Generic arg processing.                                         */
      82             :     /* -------------------------------------------------------------------- */
      83          10 :     GDALAllRegister();
      84          10 :     argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
      85          10 :     if (argc < 1)
      86           0 :         exit(-argc);
      87             : 
      88         142 :     for (int i = 0; i < argc; i++)
      89             :     {
      90         133 :         if (EQUAL(argv[i], "--utility_version"))
      91             :         {
      92           1 :             printf("%s was compiled against GDAL %s and "
      93             :                    "is running against GDAL %s\n",
      94             :                    argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
      95           1 :             CSLDestroy(argv);
      96           1 :             return 0;
      97             :         }
      98         132 :         else if (EQUAL(argv[i], "--help"))
      99             :         {
     100           0 :             Usage(false);
     101             :         }
     102             :     }
     103             : 
     104           9 :     GDALRasterizeOptionsForBinary sOptionsForBinary;
     105             :     // coverity[tainted_data]
     106             :     GDALRasterizeOptions *psOptions =
     107           9 :         GDALRasterizeOptionsNew(argv + 1, &sOptionsForBinary);
     108           9 :     CSLDestroy(argv);
     109             : 
     110           9 :     if (psOptions == nullptr)
     111             :     {
     112           0 :         Usage(true);
     113             :     }
     114             : 
     115           9 :     if (!(sOptionsForBinary.bQuiet))
     116             :     {
     117           8 :         GDALRasterizeOptionsSetProgress(psOptions, GDALTermProgress, nullptr);
     118             :     }
     119             : 
     120           9 :     if (sOptionsForBinary.osSource.empty())
     121           0 :         Usage(true, "No input file specified.");
     122             : 
     123           9 :     if (!sOptionsForBinary.bDestSpecified)
     124           0 :         Usage(true, "No output file specified.");
     125             : 
     126             :     /* -------------------------------------------------------------------- */
     127             :     /*      Open input file.                                                */
     128             :     /* -------------------------------------------------------------------- */
     129           9 :     GDALDatasetH hInDS = GDALOpenEx(sOptionsForBinary.osSource.c_str(),
     130             :                                     GDAL_OF_VECTOR | GDAL_OF_VERBOSE_ERROR,
     131             :                                     /*papszAllowedDrivers=*/nullptr,
     132           9 :                                     sOptionsForBinary.aosOpenOptions.List(),
     133             :                                     /*papszSiblingFiles=*/nullptr);
     134             : 
     135           9 :     if (hInDS == nullptr)
     136           0 :         exit(1);
     137             : 
     138             :     /* -------------------------------------------------------------------- */
     139             :     /*      Open output file if it exists.                                  */
     140             :     /* -------------------------------------------------------------------- */
     141           9 :     GDALDatasetH hDstDS = nullptr;
     142           9 :     if (!(sOptionsForBinary.bCreateOutput))
     143             :     {
     144           3 :         CPLPushErrorHandler(CPLQuietErrorHandler);
     145             :         hDstDS =
     146           3 :             GDALOpenEx(sOptionsForBinary.osDest.c_str(),
     147             :                        GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR | GDAL_OF_UPDATE,
     148             :                        nullptr, nullptr, nullptr);
     149           3 :         CPLPopErrorHandler();
     150             :     }
     151             : 
     152           9 :     if (!sOptionsForBinary.osFormat.empty() &&
     153           0 :         (sOptionsForBinary.bCreateOutput || hDstDS == nullptr))
     154             :     {
     155           0 :         GDALDriverManager *poDM = GetGDALDriverManager();
     156             :         GDALDriver *poDriver =
     157           0 :             poDM->GetDriverByName(sOptionsForBinary.osFormat.c_str());
     158           0 :         char **papszDriverMD = (poDriver) ? poDriver->GetMetadata() : nullptr;
     159           0 :         if (poDriver == nullptr ||
     160           0 :             !CPLTestBool(CSLFetchNameValueDef(papszDriverMD, GDAL_DCAP_RASTER,
     161           0 :                                               "FALSE")) ||
     162           0 :             !CPLTestBool(
     163             :                 CSLFetchNameValueDef(papszDriverMD, GDAL_DCAP_CREATE, "FALSE")))
     164             :         {
     165           0 :             fprintf(stderr,
     166             :                     "Output driver `%s' not recognised or does not support "
     167             :                     "direct output file creation.\n",
     168             :                     sOptionsForBinary.osFormat.c_str());
     169           0 :             fprintf(stderr, "The following format drivers are configured and "
     170             :                             "support direct output:\n");
     171             : 
     172           0 :             for (int iDriver = 0; iDriver < poDM->GetDriverCount(); iDriver++)
     173             :             {
     174           0 :                 GDALDriver *poIter = poDM->GetDriver(iDriver);
     175           0 :                 papszDriverMD = poIter->GetMetadata();
     176           0 :                 if (CPLTestBool(CSLFetchNameValueDef(
     177           0 :                         papszDriverMD, GDAL_DCAP_RASTER, "FALSE")) &&
     178           0 :                     CPLTestBool(CSLFetchNameValueDef(
     179             :                         papszDriverMD, GDAL_DCAP_CREATE, "FALSE")))
     180             :                 {
     181           0 :                     fprintf(stderr, "  -> `%s'\n", poIter->GetDescription());
     182             :                 }
     183             :             }
     184           0 :             exit(1);
     185             :         }
     186             :     }
     187             : 
     188           9 :     int bUsageError = FALSE;
     189           9 :     GDALDatasetH hRetDS = GDALRasterize(sOptionsForBinary.osDest.c_str(),
     190             :                                         hDstDS, hInDS, psOptions, &bUsageError);
     191           9 :     if (bUsageError == TRUE)
     192           0 :         Usage(true);
     193           9 :     int nRetCode = hRetDS ? 0 : 1;
     194             : 
     195           9 :     GDALClose(hInDS);
     196           9 :     if (GDALClose(hRetDS) != CE_None)
     197           0 :         nRetCode = 1;
     198           9 :     GDALRasterizeOptionsFree(psOptions);
     199             : 
     200           9 :     GDALDestroyDriverManager();
     201             : 
     202           9 :     return nRetCode;
     203             : }
     204             : 
     205           0 : MAIN_END

Generated by: LCOV version 1.14