forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdal_rasterize_bin.cpp
221 lines (193 loc) · 8.6 KB
/
gdal_rasterize_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
/******************************************************************************
*
* Project: GDAL Utilities
* Purpose: Rasterize OGR shapes into a GDAL raster.
* Authors: Even Rouault, <even dot rouault at spatialys dot com>
*
******************************************************************************
* Copyright (c) 2015, Even Rouault <even dot rouault at spatialys dot 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 "commonutils.h"
#include "gdal_utils_priv.h"
#include "gdal_priv.h"
/************************************************************************/
/* Usage() */
/************************************************************************/
static void Usage(const char *pszErrorMsg = nullptr)
{
printf(
"Usage: gdal_rasterize [-b band]* [-i] [-at]\n"
" {[-burn value]* | [-a attribute_name] | [-3d]} [-add]\n"
" [-l layername]* [-where expression] [-sql select_statement]\n"
" [-dialect dialect] [-of format] [-a_srs srs_def] [-to "
"\"NAME=VALUE\"]*\n"
" [-co \"NAME=VALUE\"]* [-a_nodata value] [-init value]*\n"
" [-te xmin ymin xmax ymax] [-tr xres yres] [-tap] [-ts width "
"height]\n"
" [-ot "
"{Byte/Int8/Int16/UInt16/UInt32/Int32/UInt64/Int64/Float32/Float64/\n"
" CInt16/CInt32/CFloat32/CFloat64}] [-optim "
"{[AUTO]/VECTOR/RASTER}] [-q]\n"
" <src_datasource> <dst_filename>\n");
if (pszErrorMsg != nullptr)
fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
exit(1);
}
/************************************************************************/
/* GDALRasterizeOptionsForBinaryNew() */
/************************************************************************/
static GDALRasterizeOptionsForBinary *GDALRasterizeOptionsForBinaryNew(void)
{
return static_cast<GDALRasterizeOptionsForBinary *>(
CPLCalloc(1, sizeof(GDALRasterizeOptionsForBinary)));
}
/************************************************************************/
/* GDALRasterizeOptionsForBinaryFree() */
/************************************************************************/
static void GDALRasterizeOptionsForBinaryFree(
GDALRasterizeOptionsForBinary *psOptionsForBinary)
{
if (psOptionsForBinary == nullptr)
return;
CPLFree(psOptionsForBinary->pszSource);
CPLFree(psOptionsForBinary->pszDest);
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);
/* -------------------------------------------------------------------- */
/* Generic arg processing. */
/* -------------------------------------------------------------------- */
GDALAllRegister();
argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
if (argc < 1)
exit(-argc);
for (int i = 0; i < argc; 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();
}
}
GDALRasterizeOptionsForBinary *psOptionsForBinary =
GDALRasterizeOptionsForBinaryNew();
// coverity[tainted_data]
GDALRasterizeOptions *psOptions =
GDALRasterizeOptionsNew(argv + 1, psOptionsForBinary);
CSLDestroy(argv);
if (psOptions == nullptr)
{
Usage();
}
if (!(psOptionsForBinary->bQuiet))
{
GDALRasterizeOptionsSetProgress(psOptions, GDALTermProgress, nullptr);
}
if (psOptionsForBinary->pszSource == nullptr)
Usage("No input file specified.");
if (psOptionsForBinary->pszDest == nullptr)
Usage("No output file specified.");
/* -------------------------------------------------------------------- */
/* Open input file. */
/* -------------------------------------------------------------------- */
GDALDatasetH hInDS = GDALOpenEx(psOptionsForBinary->pszSource,
GDAL_OF_VECTOR | GDAL_OF_VERBOSE_ERROR,
nullptr, nullptr, nullptr);
if (hInDS == nullptr)
exit(1);
/* -------------------------------------------------------------------- */
/* Open output file if it exists. */
/* -------------------------------------------------------------------- */
GDALDatasetH hDstDS = nullptr;
if (!(psOptionsForBinary->bCreateOutput))
{
CPLPushErrorHandler(CPLQuietErrorHandler);
hDstDS =
GDALOpenEx(psOptionsForBinary->pszDest,
GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR | GDAL_OF_UPDATE,
nullptr, nullptr, nullptr);
CPLPopErrorHandler();
}
if (psOptionsForBinary->pszFormat != nullptr &&
(psOptionsForBinary->bCreateOutput || hDstDS == nullptr))
{
GDALDriverManager *poDM = GetGDALDriverManager();
GDALDriver *poDriver =
poDM->GetDriverByName(psOptionsForBinary->pszFormat);
char **papszDriverMD = (poDriver) ? poDriver->GetMetadata() : nullptr;
if (poDriver == nullptr ||
!CPLTestBool(CSLFetchNameValueDef(papszDriverMD, GDAL_DCAP_RASTER,
"FALSE")) ||
!CPLTestBool(
CSLFetchNameValueDef(papszDriverMD, GDAL_DCAP_CREATE, "FALSE")))
{
fprintf(stderr,
"Output driver `%s' not recognised or does not support "
"direct output file creation.\n",
psOptionsForBinary->pszFormat);
fprintf(stderr, "The following format drivers are configured and "
"support direct output:\n");
for (int iDriver = 0; iDriver < poDM->GetDriverCount(); iDriver++)
{
GDALDriver *poIter = poDM->GetDriver(iDriver);
papszDriverMD = poIter->GetMetadata();
if (CPLTestBool(CSLFetchNameValueDef(
papszDriverMD, GDAL_DCAP_RASTER, "FALSE")) &&
CPLTestBool(CSLFetchNameValueDef(
papszDriverMD, GDAL_DCAP_CREATE, "FALSE")))
{
fprintf(stderr, " -> `%s'\n", poIter->GetDescription());
}
}
exit(1);
}
}
int bUsageError = FALSE;
GDALDatasetH hRetDS = GDALRasterize(psOptionsForBinary->pszDest, hDstDS,
hInDS, psOptions, &bUsageError);
if (bUsageError == TRUE)
Usage();
const int nRetCode = hRetDS ? 0 : 1;
GDALClose(hInDS);
GDALClose(hRetDS);
GDALRasterizeOptionsFree(psOptions);
GDALRasterizeOptionsForBinaryFree(psOptionsForBinary);
GDALDestroyDriverManager();
return nRetCode;
}
MAIN_END