Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GDAL DEM Utilities
4 : * Purpose:
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_vsi.h"
30 : #include <stdlib.h>
31 : #include <math.h>
32 :
33 : #include "cpl_conv.h"
34 : #include "cpl_string.h"
35 : #include "gdal_version.h"
36 : #include "gdal_utils_priv.h"
37 : #include "gdal_priv.h"
38 : #include "commonutils.h"
39 :
40 : /************************************************************************/
41 : /* Usage() */
42 : /************************************************************************/
43 :
44 0 : static void Usage(bool bIsError, const char *pszErrorMsg = nullptr)
45 :
46 : {
47 0 : fprintf(
48 : bIsError ? stderr : stdout,
49 : " Usage: [--help] [--help-general]\n"
50 : " - To generate a shaded relief map from any GDAL-supported elevation "
51 : "raster : \n\n"
52 : " gdaldem hillshade <input_dem> <output_hillshade> \n"
53 : " [-z <zfactor>] [-s <scale>] \n"
54 : " [-az <azimuth>] [-alt <altitude>]\n"
55 : " [-alg ZevenbergenThorne] [-combined | "
56 : "-multidirectional | -igor]\n"
57 : " [-compute_edges] [-b <Band>] [-of <format>] "
58 : "[-co <NAME>=<VALUE>]... [-q]\n"
59 : "\n"
60 : " - To generates a slope map from any GDAL-supported elevation raster "
61 : ":\n\n"
62 : " gdaldem slope <input_dem> <output_slope_map> \n"
63 : " [-p] [-s <scale>]\n"
64 : " [-alg ZevenbergenThorne]\n"
65 : " [-compute_edges] [-b <band>] [-of <format>] "
66 : "[-co <NAME>=<VALUE>]... [-q]\n"
67 : "\n"
68 : " - To generate an aspect map from any GDAL-supported elevation "
69 : "raster\n"
70 : " Outputs a 32-bit float tiff with pixel values from 0-360 "
71 : "indicating azimuth :\n\n"
72 : " gdaldem aspect <input_dem> <output_aspect_map> \n"
73 : " [-trigonometric] [-zero_for_flat]\n"
74 : " [-alg ZevenbergenThorne]\n"
75 : " [-compute_edges] [-b <band>] [-of format] "
76 : "[-co <NAME>=<VALUE>]... [-q]\n"
77 : "\n"
78 : " - To generate a color relief map from any GDAL-supported elevation "
79 : "raster\n"
80 : " gdaldem color-relief <input_dem> <color_text_file> "
81 : "<output_color_relief_map>\n"
82 : " [-alpha] [-exact_color_entry | "
83 : "-nearest_color_entry]\n"
84 : " [-b <band>] [-of format] "
85 : "[-co <NAME>=<VALUE>]... [-q]\n"
86 : " where color_text_file contains lines of the format "
87 : "\"elevation_value red green blue\"\n"
88 : "\n"
89 : " - To generate a Terrain Ruggedness Index (TRI) map from any "
90 : "GDAL-supported elevation raster\n"
91 : " gdaldem TRI <input_dem> <output_TRI_map>\n"
92 : " [-alg Wilson|Riley]\n"
93 : " [-compute_edges] [-b <band>] [-of <format>] "
94 : "[-co <NAME>=<VALUE>]... [-q]\n"
95 : "\n"
96 : " - To generate a Topographic Position Index (TPI) map from any "
97 : "GDAL-supported elevation raster\n"
98 : " gdaldem TPI <input_dem> <output_TPI_map>\n"
99 : " [-compute_edges] [-b <band>] [-of <format>] "
100 : "[-co <NAME>=<VALUE>]... [-q]\n"
101 : "\n"
102 : " - To generate a roughness map from any GDAL-supported elevation "
103 : "raster\n"
104 : " gdaldem roughness <input_dem> <output_roughness_map>\n"
105 : " [-compute_edges] [-b <band>] [-of <format>] "
106 : "[-co <NAME>=<VALUE>]... [-q]\n"
107 : "\n"
108 : " Notes : \n"
109 : " Scale is the ratio of vertical units to horizontal\n"
110 : " for Feet:Latlong use scale=370400, for Meters:LatLong use "
111 : "scale=111120 \n\n");
112 :
113 0 : if (pszErrorMsg != nullptr)
114 0 : fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
115 :
116 0 : exit(bIsError ? 1 : 0);
117 : }
118 :
119 : /************************************************************************/
120 : /* GDALDEMProcessingOptionsForBinaryNew() */
121 : /************************************************************************/
122 :
123 : static GDALDEMProcessingOptionsForBinary *
124 21 : GDALDEMProcessingOptionsForBinaryNew(void)
125 : {
126 : return static_cast<GDALDEMProcessingOptionsForBinary *>(
127 21 : CPLCalloc(1, sizeof(GDALDEMProcessingOptionsForBinary)));
128 : }
129 :
130 : /************************************************************************/
131 : /* GDALDEMProcessingOptionsForBinaryFree() */
132 : /************************************************************************/
133 :
134 21 : static void GDALDEMProcessingOptionsForBinaryFree(
135 : GDALDEMProcessingOptionsForBinary *psOptionsForBinary)
136 : {
137 21 : if (psOptionsForBinary)
138 : {
139 21 : CPLFree(psOptionsForBinary->pszProcessing);
140 21 : CPLFree(psOptionsForBinary->pszSrcFilename);
141 21 : CPLFree(psOptionsForBinary->pszColorFilename);
142 21 : CPLFree(psOptionsForBinary->pszDstFilename);
143 21 : CPLFree(psOptionsForBinary);
144 : }
145 21 : }
146 :
147 : /************************************************************************/
148 : /* main() */
149 : /************************************************************************/
150 :
151 22 : MAIN_START(argc, argv)
152 :
153 : {
154 : /* Check strict compilation and runtime library version as we use C++ API */
155 22 : if (!GDAL_CHECK_VERSION(argv[0]))
156 0 : exit(1);
157 :
158 22 : EarlySetConfigOptions(argc, argv);
159 :
160 : /* -------------------------------------------------------------------- */
161 : /* Register standard GDAL drivers, and process generic GDAL */
162 : /* command options. */
163 : /* -------------------------------------------------------------------- */
164 22 : GDALAllRegister();
165 22 : argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
166 22 : if (argc < 2)
167 : {
168 0 : Usage(true, "Not enough arguments.");
169 : }
170 :
171 22 : if (EQUAL(argv[1], "--utility_version") ||
172 21 : EQUAL(argv[1], "--utility-version"))
173 : {
174 1 : printf(
175 : "%s was compiled against GDAL %s and is running against GDAL %s\n",
176 : argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
177 1 : CSLDestroy(argv);
178 1 : return 0;
179 : }
180 21 : else if (EQUAL(argv[1], "--help"))
181 0 : Usage(false);
182 :
183 : GDALDEMProcessingOptionsForBinary *psOptionsForBinary =
184 21 : GDALDEMProcessingOptionsForBinaryNew();
185 : // coverity[tainted_data]
186 : GDALDEMProcessingOptions *psOptions =
187 21 : GDALDEMProcessingOptionsNew(argv + 1, psOptionsForBinary);
188 21 : CSLDestroy(argv);
189 :
190 21 : if (psOptions == nullptr)
191 : {
192 0 : Usage(true);
193 : }
194 :
195 21 : if (!(psOptionsForBinary->bQuiet))
196 : {
197 21 : GDALDEMProcessingOptionsSetProgress(psOptions, GDALTermProgress,
198 : nullptr);
199 : }
200 :
201 21 : if (psOptionsForBinary->pszSrcFilename == nullptr)
202 : {
203 0 : Usage(true, "Missing source.");
204 : }
205 21 : if (EQUAL(psOptionsForBinary->pszProcessing, "color-relief") &&
206 12 : psOptionsForBinary->pszColorFilename == nullptr)
207 : {
208 0 : Usage(true, "Missing color file.");
209 : }
210 21 : if (psOptionsForBinary->pszDstFilename == nullptr)
211 : {
212 0 : Usage(true, "Missing destination.");
213 : }
214 :
215 : // Open Dataset and get raster band.
216 : GDALDatasetH hSrcDataset =
217 21 : GDALOpen(psOptionsForBinary->pszSrcFilename, GA_ReadOnly);
218 :
219 21 : if (hSrcDataset == nullptr)
220 : {
221 0 : fprintf(stderr, "GDALOpen failed - %d\n%s\n", CPLGetLastErrorNo(),
222 : CPLGetLastErrorMsg());
223 0 : GDALDestroyDriverManager();
224 0 : exit(1);
225 : }
226 :
227 21 : int bUsageError = FALSE;
228 42 : GDALDatasetH hOutDS = GDALDEMProcessing(
229 21 : psOptionsForBinary->pszDstFilename, hSrcDataset,
230 21 : psOptionsForBinary->pszProcessing, psOptionsForBinary->pszColorFilename,
231 : psOptions, &bUsageError);
232 21 : if (bUsageError)
233 0 : Usage(true);
234 21 : const int nRetCode = hOutDS ? 0 : 1;
235 :
236 21 : GDALClose(hSrcDataset);
237 21 : GDALClose(hOutDS);
238 21 : GDALDEMProcessingOptionsFree(psOptions);
239 21 : GDALDEMProcessingOptionsForBinaryFree(psOptionsForBinary);
240 :
241 21 : GDALDestroyDriverManager();
242 :
243 21 : return nRetCode;
244 : }
245 :
246 0 : MAIN_END
|