forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdal_translate_bin.cpp
417 lines (365 loc) · 15.5 KB
/
gdal_translate_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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/******************************************************************************
*
* Project: GDAL Utilities
* Purpose: GDAL Image Translator Program
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1998, 2002, Frank Warmerdam
* Copyright (c) 2007-2014, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_string.h"
#include "gdal_version.h"
#include "gdal_priv.h"
#include "ogr_spatialref.h"
#include "commonutils.h"
#include "gdal_utils_priv.h"
/* ******************************************************************* */
/* Usage() */
/* ******************************************************************** */
static void Usage(bool bIsError, const char *pszErrorMsg = nullptr,
bool bShort = true) CPL_NO_RETURN;
static void Usage(bool bIsError, const char *pszErrorMsg, bool bShort)
{
fprintf(
bIsError ? stderr : stdout,
"Usage: gdal_translate [--help] [--help-general] [--long-usage]\n"
" [-ot "
"{Byte/Int8/Int16/UInt16/UInt32/Int32/UInt64/Int64/Float32/Float64/\n"
" CInt16/CInt32/CFloat32/CFloat64}] [-strict]\n"
" [-if <format>]... [-of <format>]\n"
" [-b <band>] [-mask <band>] [-expand {gray|rgb|rgba}]\n"
" [-outsize <xsize>[%%]|0 <ysize>[%%]|0] [-tr <xres> <yres>]\n"
" [-ovr <level>|AUTO|AUTO-<n>|NONE]\n"
" [-r "
"{nearest,bilinear,cubic,cubicspline,lanczos,average,mode}]\n"
" [-unscale] [-scale[_bn] [<src_min> <src_max> [<dst_min> "
"<dst_max>]]]... "
"[-exponent[_bn] <exp_val>]...\n"
" [-srcwin <xoff> <yoff> <xsize> <ysize>] [-epo] [-eco]\n"
" [-projwin <ulx> <uly> <lrx> <lry>] [-projwin_srs <srs_def>]\n"
" [-a_srs <srs_def>] [-a_coord_epoch <epoch>]\n"
" [-a_ullr <ulx> <uly> <lrx> <lry>] [-a_nodata <value>]\n"
" [-a_gt <gt0> <gt1> <gt2> <gt3> <gt4> <gt5>]\n"
" [-a_scale <value>] [-a_offset <value>]\n"
" [-nogcp] [-gcp <pixel> <line> <easting> <northing> "
"[<elevation>]]...\n"
" |-colorinterp{_bn} {red|green|blue|alpha|gray|undefined}]\n"
" |-colorinterp {red|green|blue|alpha|gray|undefined},...]\n"
" [-mo <META-TAG>=<VALUE>]... [-q] [-sds]\n"
" [-co <NAME>=<VALUE>]... [-stats] [-norat] [-noxmp]\n"
" [-oo <NAME>=<VALUE>]...\n"
" <src_dataset> <dst_dataset>\n");
if (!bShort)
{
printf("\n%s\n\n", GDALVersionInfo("--version"));
printf("The following format drivers are configured and support "
"output:\n");
for (int iDr = 0; iDr < GDALGetDriverCount(); iDr++)
{
GDALDriverH hDriver = GDALGetDriver(iDr);
if (GDALGetMetadataItem(hDriver, GDAL_DCAP_RASTER, nullptr) !=
nullptr &&
(GDALGetMetadataItem(hDriver, GDAL_DCAP_CREATE, nullptr) !=
nullptr ||
GDALGetMetadataItem(hDriver, GDAL_DCAP_CREATECOPY, nullptr) !=
nullptr))
{
printf(" %s: %s\n", GDALGetDriverShortName(hDriver),
GDALGetDriverLongName(hDriver));
}
}
}
if (pszErrorMsg != nullptr)
fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
exit(bIsError ? 1 : 0);
}
/************************************************************************/
/* GDALTranslateOptionsForBinaryNew() */
/************************************************************************/
static GDALTranslateOptionsForBinary *GDALTranslateOptionsForBinaryNew(void)
{
return static_cast<GDALTranslateOptionsForBinary *>(
CPLCalloc(1, sizeof(GDALTranslateOptionsForBinary)));
}
/************************************************************************/
/* GDALTranslateOptionsForBinaryFree() */
/************************************************************************/
static void GDALTranslateOptionsForBinaryFree(
GDALTranslateOptionsForBinary *psOptionsForBinary)
{
if (psOptionsForBinary == nullptr)
return;
CPLFree(psOptionsForBinary->pszSource);
CPLFree(psOptionsForBinary->pszDest);
CSLDestroy(psOptionsForBinary->papszOpenOptions);
CPLFree(psOptionsForBinary->pszFormat);
CSLDestroy(psOptionsForBinary->papszAllowInputDrivers);
CPLFree(psOptionsForBinary);
}
/************************************************************************/
/* main() */
/************************************************************************/
MAIN_START(argc, argv)
{
/* Check strict compilation and runtime library version as we use C++ API */
if (!GDAL_CHECK_VERSION(argv[0]))
exit(1);
EarlySetConfigOptions(argc, argv);
/* -------------------------------------------------------------------- */
/* Register standard GDAL drivers, and process generic GDAL */
/* command options. */
/* -------------------------------------------------------------------- */
GDALAllRegister();
argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
if (argc < 1)
exit(-argc);
for (int i = 0; argv != nullptr && argv[i] != nullptr; i++)
{
if (EQUAL(argv[i], "--utility_version"))
{
printf("%s was compiled against GDAL %s and is running against "
"GDAL %s\n",
argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
CSLDestroy(argv);
return 0;
}
else if (EQUAL(argv[i], "--help"))
{
Usage(false, nullptr);
}
else if (EQUAL(argv[i], "--long-usage"))
{
Usage(false, nullptr, FALSE);
}
}
/* -------------------------------------------------------------------- */
/* 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
}
GDALTranslateOptionsForBinary *psOptionsForBinary =
GDALTranslateOptionsForBinaryNew();
GDALTranslateOptions *psOptions =
GDALTranslateOptionsNew(argv + 1, psOptionsForBinary);
CSLDestroy(argv);
if (psOptions == nullptr)
{
Usage(true, nullptr);
}
if (psOptionsForBinary->pszSource == nullptr)
{
Usage(true, "No source dataset specified.");
}
if (psOptionsForBinary->pszDest == nullptr)
{
Usage(true, "No target dataset specified.");
}
if (strcmp(psOptionsForBinary->pszDest, "/vsistdout/") == 0)
{
psOptionsForBinary->bQuiet = TRUE;
}
if (!(psOptionsForBinary->bQuiet))
{
GDALTranslateOptionsSetProgress(psOptions, GDALTermProgress, nullptr);
}
if (psOptionsForBinary->pszFormat)
{
GDALDriverH hDriver =
GDALGetDriverByName(psOptionsForBinary->pszFormat);
if (hDriver == nullptr)
{
fprintf(stderr, "Output driver `%s' not recognised.\n",
psOptionsForBinary->pszFormat);
fprintf(stderr, "The following format drivers are configured and "
"support output:\n");
for (int iDr = 0; iDr < GDALGetDriverCount(); iDr++)
{
hDriver = GDALGetDriver(iDr);
if (GDALGetMetadataItem(hDriver, GDAL_DCAP_RASTER, nullptr) !=
nullptr &&
(GDALGetMetadataItem(hDriver, GDAL_DCAP_CREATE, nullptr) !=
nullptr ||
GDALGetMetadataItem(hDriver, GDAL_DCAP_CREATECOPY,
nullptr) != nullptr))
{
fprintf(stderr, " %s: %s\n",
GDALGetDriverShortName(hDriver),
GDALGetDriverLongName(hDriver));
}
}
GDALTranslateOptionsFree(psOptions);
GDALTranslateOptionsForBinaryFree(psOptionsForBinary);
GDALDestroyDriverManager();
exit(1);
}
}
/* -------------------------------------------------------------------- */
/* Attempt to open source file. */
/* -------------------------------------------------------------------- */
GDALDatasetH hDataset = GDALOpenEx(
psOptionsForBinary->pszSource, GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
psOptionsForBinary->papszAllowInputDrivers,
psOptionsForBinary->papszOpenOptions, nullptr);
if (hDataset == nullptr)
{
GDALDestroyDriverManager();
exit(1);
}
/* -------------------------------------------------------------------- */
/* Handle subdatasets. */
/* -------------------------------------------------------------------- */
if (!psOptionsForBinary->bCopySubDatasets &&
GDALGetRasterCount(hDataset) == 0 &&
CSLCount(GDALGetMetadata(hDataset, "SUBDATASETS")) > 0)
{
fprintf(stderr, "Input file contains subdatasets. Please, select one "
"of them for reading.\n");
GDALClose(hDataset);
GDALDestroyDriverManager();
exit(1);
}
int bUsageError = FALSE;
GDALDatasetH hOutDS = nullptr;
GDALDriverH hOutDriver = nullptr;
if (psOptionsForBinary->pszFormat == nullptr)
{
hOutDriver = GDALGetDriverByName(
GetOutputDriverForRaster(psOptionsForBinary->pszDest));
}
else
{
hOutDriver = GDALGetDriverByName(psOptionsForBinary->pszFormat);
}
if (hOutDriver == nullptr)
{
fprintf(stderr, "Output driver not found.\n");
GDALClose(hDataset);
GDALDestroyDriverManager();
exit(1);
}
bool bCopyCreateSubDatasets =
(GDALGetMetadataItem(hOutDriver, GDAL_DCAP_SUBCREATECOPY, nullptr) !=
nullptr);
if (psOptionsForBinary->bCopySubDatasets &&
CSLCount(GDALGetMetadata(hDataset, "SUBDATASETS")) > 0)
{
if (bCopyCreateSubDatasets)
{
// GDAL sets the size of the dataset with subdatasets to 512x512
// this removes the srcwin function from this operation
hOutDS = GDALTranslate(psOptionsForBinary->pszDest, hDataset,
psOptions, &bUsageError);
GDALClose(hOutDS);
}
else
{
char **papszSubdatasets = GDALGetMetadata(hDataset, "SUBDATASETS");
char *pszSubDest = static_cast<char *>(
CPLMalloc(strlen(psOptionsForBinary->pszDest) + 32));
CPLString osPath = CPLGetPath(psOptionsForBinary->pszDest);
CPLString osBasename = CPLGetBasename(psOptionsForBinary->pszDest);
CPLString osExtension =
CPLGetExtension(psOptionsForBinary->pszDest);
CPLString osTemp;
const char *pszFormat = nullptr;
if (CSLCount(papszSubdatasets) / 2 < 10)
{
pszFormat = "%s_%d";
}
else if (CSLCount(papszSubdatasets) / 2 < 100)
{
pszFormat = "%s_%002d";
}
else
{
pszFormat = "%s_%003d";
}
const char *pszDest = pszSubDest;
for (int i = 0; papszSubdatasets[i] != nullptr; i += 2)
{
char *pszSource =
CPLStrdup(strstr(papszSubdatasets[i], "=") + 1);
osTemp = CPLSPrintf(pszFormat, osBasename.c_str(), i / 2 + 1);
osTemp = CPLFormFilename(osPath, osTemp, osExtension);
strcpy(pszSubDest, osTemp.c_str());
hDataset =
GDALOpenEx(pszSource, GDAL_OF_RASTER, nullptr,
psOptionsForBinary->papszOpenOptions, nullptr);
CPLFree(pszSource);
if (!psOptionsForBinary->bQuiet)
printf("Input file size is %d, %d\n",
GDALGetRasterXSize(hDataset),
GDALGetRasterYSize(hDataset));
hOutDS =
GDALTranslate(pszDest, hDataset, psOptions, &bUsageError);
if (hOutDS == nullptr)
break;
GDALClose(hOutDS);
}
CPLFree(pszSubDest);
}
if (bUsageError == TRUE)
Usage(true);
GDALClose(hDataset);
GDALTranslateOptionsFree(psOptions);
GDALTranslateOptionsForBinaryFree(psOptionsForBinary);
GDALDestroy();
return 0;
}
if (!psOptionsForBinary->bQuiet)
printf("Input file size is %d, %d\n", GDALGetRasterXSize(hDataset),
GDALGetRasterYSize(hDataset));
hOutDS = GDALTranslate(psOptionsForBinary->pszDest, hDataset, psOptions,
&bUsageError);
if (bUsageError == TRUE)
Usage(true);
int nRetCode = hOutDS ? 0 : 1;
/* Close hOutDS before hDataset for the -f VRT case */
if (GDALClose(hOutDS) != CE_None)
{
nRetCode = 1;
if (CPLGetLastErrorType() == CE_None)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Unknown error occurred in GDALClose()");
}
}
GDALClose(hDataset);
GDALTranslateOptionsFree(psOptions);
GDALTranslateOptionsForBinaryFree(psOptionsForBinary);
GDALDestroy();
return nRetCode;
}
MAIN_END