forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdalwarp_bin.cpp
309 lines (268 loc) · 10.4 KB
/
gdalwarp_bin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/******************************************************************************
*
* Project: High Performance Image Reprojector
* Purpose: Test program for high performance warper API.
* Author: Frank Warmerdam <[email protected]>
*
******************************************************************************
* Copyright (c) 2002, i3 - information integration and imaging
* Fort Collin, CO
* Copyright (c) 2007-2015, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cpl_string.h"
#include "cpl_error_internal.h"
#include "gdal_version.h"
#include "commonutils.h"
#include "gdal_utils_priv.h"
#include <vector>
/************************************************************************/
/* GDALExit() */
/* This function exits and cleans up GDAL and OGR resources */
/* Perhaps it should be added to C api and used in all apps? */
/************************************************************************/
static int GDALExit(int nCode)
{
const char *pszDebug = CPLGetConfigOption("CPL_DEBUG", nullptr);
if (pszDebug && (EQUAL(pszDebug, "ON") || EQUAL(pszDebug, "")))
{
GDALDumpOpenDatasets(stderr);
CPLDumpSharedList(nullptr);
}
GDALDestroyDriverManager();
OGRCleanupAll();
exit(nCode);
}
/************************************************************************/
/* Usage() */
/************************************************************************/
static void Usage()
{
fprintf(stderr, "%s\n", GDALWarpAppGetParserUsage().c_str());
GDALExit(1);
}
/************************************************************************/
/* WarpTermProgress() */
/************************************************************************/
static int gnSrcCount = 0;
static int CPL_STDCALL WarpTermProgress(double dfProgress,
const char *pszMessage, void *)
{
static CPLString osLastMsg;
static int iSrc = -1;
if (pszMessage == nullptr)
{
iSrc = 0;
}
else if (pszMessage != osLastMsg)
{
if (!osLastMsg.empty())
GDALTermProgress(1.0, nullptr, nullptr);
printf("%s : ", pszMessage);
osLastMsg = pszMessage;
iSrc++;
}
return GDALTermProgress(dfProgress * gnSrcCount - iSrc, nullptr, nullptr);
}
/************************************************************************/
/* main() */
/************************************************************************/
MAIN_START(argc, argv)
{
EarlySetConfigOptions(argc, argv);
CPLDebugOnly("GDAL", "Start");
/* -------------------------------------------------------------------- */
/* Register standard GDAL drivers, and process generic GDAL */
/* command options. */
/* -------------------------------------------------------------------- */
GDALAllRegister();
argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
if (argc < 1)
GDALExit(-argc);
/* -------------------------------------------------------------------- */
/* Set optimal setting for best performance with huge input VRT. */
/* The rationale for 450 is that typical Linux process allow */
/* only 1024 file descriptors per process and we need to keep some */
/* spare for shared libraries, etc. so let's go down to 900. */
/* And some datasets may need 2 file descriptors, so divide by 2 */
/* for security. */
/* -------------------------------------------------------------------- */
if (CPLGetConfigOption("GDAL_MAX_DATASET_POOL_SIZE", nullptr) == nullptr)
{
#if defined(__MACH__) && defined(__APPLE__)
// On Mach, the default limit is 256 files per process
// TODO We should eventually dynamically query the limit for all OS
CPLSetConfigOption("GDAL_MAX_DATASET_POOL_SIZE", "100");
#else
CPLSetConfigOption("GDAL_MAX_DATASET_POOL_SIZE", "450");
#endif
}
GDALWarpAppOptionsForBinary sOptionsForBinary;
/* coverity[tainted_data] */
GDALWarpAppOptions *psOptions =
GDALWarpAppOptionsNew(argv + 1, &sOptionsForBinary);
CSLDestroy(argv);
if (psOptions == nullptr)
{
Usage();
}
if (sOptionsForBinary.aosSrcFiles.size() == 1 &&
sOptionsForBinary.aosSrcFiles[0] == sOptionsForBinary.osDstFilename &&
sOptionsForBinary.bOverwrite)
{
CPLError(CE_Failure, CPLE_IllegalArg,
"Source and destination datasets must be different.\n");
GDALExit(1);
}
/* -------------------------------------------------------------------- */
/* Open source files. */
/* -------------------------------------------------------------------- */
GDALDatasetH *pahSrcDS = nullptr;
int nSrcCount = 0;
for (int i = 0; i < sOptionsForBinary.aosSrcFiles.size(); ++i)
{
nSrcCount++;
pahSrcDS = static_cast<GDALDatasetH *>(
CPLRealloc(pahSrcDS, sizeof(GDALDatasetH) * nSrcCount));
pahSrcDS[i] =
GDALOpenEx(sOptionsForBinary.aosSrcFiles[i],
GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
sOptionsForBinary.aosAllowedInputDrivers.List(),
sOptionsForBinary.aosOpenOptions.List(), nullptr);
if (pahSrcDS[i] == nullptr)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Failed to open source file %s\n",
sOptionsForBinary.aosSrcFiles[i]);
while (nSrcCount--)
{
GDALClose(pahSrcDS[nSrcCount]);
pahSrcDS[nSrcCount] = nullptr;
}
CPLFree(pahSrcDS);
GDALWarpAppOptionsFree(psOptions);
GDALExit(2);
}
}
/* -------------------------------------------------------------------- */
/* Does the output dataset already exist? */
/* -------------------------------------------------------------------- */
/* FIXME ? source filename=target filename and -overwrite is definitely */
/* an error. But I can't imagine of a valid case (without -overwrite), */
/* where it would make sense. In doubt, let's keep that dubious
* possibility... */
bool bOutStreaming = false;
if (sOptionsForBinary.osDstFilename == "/vsistdout/")
{
sOptionsForBinary.bQuiet = true;
bOutStreaming = true;
}
#ifdef S_ISFIFO
else
{
VSIStatBufL sStat;
if (VSIStatExL(sOptionsForBinary.osDstFilename.c_str(), &sStat,
VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0 &&
S_ISFIFO(sStat.st_mode))
{
bOutStreaming = true;
}
}
#endif
GDALDatasetH hDstDS = nullptr;
if (bOutStreaming)
{
GDALWarpAppOptionsSetWarpOption(psOptions, "STREAMABLE_OUTPUT", "YES");
}
else
{
std::vector<CPLErrorHandlerAccumulatorStruct> aoErrors;
CPLInstallErrorHandlerAccumulator(aoErrors);
hDstDS = GDALOpenEx(
sOptionsForBinary.osDstFilename.c_str(),
GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR | GDAL_OF_UPDATE, nullptr,
sOptionsForBinary.aosDestOpenOptions.List(), nullptr);
CPLUninstallErrorHandlerAccumulator();
if (hDstDS != nullptr)
{
for (size_t i = 0; i < aoErrors.size(); i++)
{
CPLError(aoErrors[i].type, aoErrors[i].no, "%s",
aoErrors[i].msg.c_str());
}
}
}
if (hDstDS != nullptr && sOptionsForBinary.bOverwrite)
{
GDALClose(hDstDS);
hDstDS = nullptr;
}
bool bCheckExistingDstFile =
!bOutStreaming && hDstDS == nullptr && !sOptionsForBinary.bOverwrite;
if (hDstDS != nullptr && sOptionsForBinary.bCreateOutput)
{
if (sOptionsForBinary.aosCreateOptions.FetchBool("APPEND_SUBDATASET",
false))
{
GDALClose(hDstDS);
hDstDS = nullptr;
bCheckExistingDstFile = false;
}
else
{
CPLError(CE_Failure, CPLE_AppDefined,
"Output dataset %s exists,\n"
"but some command line options were provided indicating a "
"new dataset\n"
"should be created. Please delete existing dataset and "
"run again.\n",
sOptionsForBinary.osDstFilename.c_str());
GDALExit(1);
}
}
/* Avoid overwriting an existing destination file that cannot be opened in
*/
/* update mode with a new GTiff file */
if (bCheckExistingDstFile)
{
CPLPushErrorHandler(CPLQuietErrorHandler);
hDstDS = GDALOpen(sOptionsForBinary.osDstFilename.c_str(), GA_ReadOnly);
CPLPopErrorHandler();
if (hDstDS)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Output dataset %s exists, but cannot be opened in update "
"mode\n",
sOptionsForBinary.osDstFilename.c_str());
GDALClose(hDstDS);
GDALExit(1);
}
}
if (!(sOptionsForBinary.bQuiet))
{
gnSrcCount = nSrcCount;
GDALWarpAppOptionsSetProgress(psOptions, WarpTermProgress, nullptr);
GDALWarpAppOptionsSetQuiet(psOptions, false);
}
int bUsageError = FALSE;
GDALDatasetH hOutDS =
GDALWarp(sOptionsForBinary.osDstFilename.c_str(), hDstDS, nSrcCount,
pahSrcDS, psOptions, &bUsageError);
if (bUsageError)
Usage();
int nRetCode = (hOutDS) ? 0 : 1;
GDALWarpAppOptionsFree(psOptions);
// Close first hOutDS since it might reference sources (case of VRT)
if (GDALClose(hOutDS ? hOutDS : hDstDS) != CE_None)
nRetCode = 1;
for (int i = 0; i < nSrcCount; i++)
{
GDALClose(pahSrcDS[i]);
}
CPLFree(pahSrcDS);
GDALDumpOpenDatasets(stderr);
OGRCleanupAll();
return nRetCode;
}
MAIN_END