Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GDAL Utilities 4 : * Purpose: Command line application to build VRT datasets from raster products 5 : *or content of SHP tile index Author: Even Rouault, <even dot rouault at 6 : *spatialys dot com> 7 : * 8 : ****************************************************************************** 9 : * Copyright (c) 2007-2016, Even Rouault <even dot rouault at spatialys dot com> 10 : * 11 : * SPDX-License-Identifier: MIT 12 : ****************************************************************************/ 13 : 14 : #include "cpl_string.h" 15 : #include "cpl_error.h" 16 : #include "commonutils.h" 17 : #include "gdal_version.h" 18 : #include "gdal_utils_priv.h" 19 : 20 : /************************************************************************/ 21 : /* Usage() */ 22 : /************************************************************************/ 23 : 24 : static void Usage() CPL_NO_RETURN; 25 : 26 1 : static void Usage() 27 : 28 : { 29 1 : fprintf(stderr, "%s\n", GDALBuildVRTGetParserUsage().c_str()); 30 1 : exit(1); 31 : } 32 : 33 : /************************************************************************/ 34 : /* main() */ 35 : /************************************************************************/ 36 : 37 20 : MAIN_START(argc, argv) 38 : 39 : { 40 20 : EarlySetConfigOptions(argc, argv); 41 : 42 : /* -------------------------------------------------------------------- */ 43 : /* Register standard GDAL drivers, and process generic GDAL */ 44 : /* command options. */ 45 : /* -------------------------------------------------------------------- */ 46 20 : GDALAllRegister(); 47 20 : argc = GDALGeneralCmdLineProcessor(argc, &argv, 0); 48 20 : if (argc < 1) 49 1 : exit(-argc); 50 : 51 19 : GDALBuildVRTOptionsForBinary sOptionsForBinary; 52 : /* coverity[tainted_data] */ 53 : GDALBuildVRTOptions *psOptions = 54 19 : GDALBuildVRTOptionsNew(argv + 1, &sOptionsForBinary); 55 19 : CSLDestroy(argv); 56 : 57 19 : if (psOptions == nullptr) 58 : { 59 0 : Usage(); 60 : } 61 : 62 19 : if (!(sOptionsForBinary.bQuiet)) 63 : { 64 19 : GDALBuildVRTOptionsSetProgress(psOptions, GDALTermProgress, nullptr); 65 : } 66 : 67 : /* Avoid overwriting a non VRT dataset if the user did not put the */ 68 : /* filenames in the right order */ 69 : VSIStatBuf sBuf; 70 19 : if (!sOptionsForBinary.bOverwrite) 71 : { 72 : int bExists = 73 19 : (VSIStat(sOptionsForBinary.osDstFilename.c_str(), &sBuf) == 0); 74 19 : if (bExists) 75 : { 76 0 : GDALDriverH hDriver = GDALIdentifyDriver( 77 : sOptionsForBinary.osDstFilename.c_str(), nullptr); 78 0 : if (hDriver && 79 0 : !(EQUAL(GDALGetDriverShortName(hDriver), "VRT") || 80 0 : (EQUAL(GDALGetDriverShortName(hDriver), "API_PROXY") && 81 0 : EQUAL(CPLGetExtensionSafe( 82 : sOptionsForBinary.osDstFilename.c_str()) 83 : .c_str(), 84 : "VRT")))) 85 : { 86 0 : fprintf( 87 : stderr, 88 : "'%s' is an existing GDAL dataset managed by %s driver.\n" 89 : "There is an high chance you did not put filenames in the " 90 : "right order.\n" 91 : "If you want to overwrite %s, add -overwrite option to the " 92 : "command line.\n\n", 93 : sOptionsForBinary.osDstFilename.c_str(), 94 : GDALGetDriverShortName(hDriver), 95 : sOptionsForBinary.osDstFilename.c_str()); 96 0 : Usage(); 97 : } 98 : } 99 : } 100 : 101 19 : int bUsageError = FALSE; 102 19 : GDALDatasetH hOutDS = GDALBuildVRT( 103 : sOptionsForBinary.osDstFilename.c_str(), 104 : sOptionsForBinary.aosSrcFiles.size(), nullptr, 105 19 : sOptionsForBinary.aosSrcFiles.List(), psOptions, &bUsageError); 106 19 : if (bUsageError) 107 1 : Usage(); 108 18 : int nRetCode = (hOutDS) ? 0 : 1; 109 : 110 18 : GDALBuildVRTOptionsFree(psOptions); 111 : 112 18 : CPLErrorReset(); 113 : // The flush to disk is only done at that stage, so check if any error has 114 : // happened 115 18 : if (GDALClose(hOutDS) != CE_None) 116 0 : nRetCode = 1; 117 18 : if (CPLGetLastErrorType() != CE_None) 118 1 : nRetCode = 1; 119 : 120 18 : GDALDumpOpenDatasets(stderr); 121 : 122 18 : GDALDestroyDriverManager(); 123 : 124 18 : OGRCleanupAll(); 125 : 126 18 : return nRetCode; 127 : } 128 : 129 0 : MAIN_END