File: gdaldem_bin.cpp

package info (click to toggle)
gdal 3.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 89,664 kB
  • sloc: cpp: 1,136,033; ansic: 197,355; python: 35,910; java: 5,511; xml: 4,011; sh: 3,950; cs: 2,443; yacc: 1,047; makefile: 288
file content (245 lines) | stat: -rw-r--r-- 9,480 bytes parent folder | download
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
/******************************************************************************
 *
 * Project:  GDAL DEM Utilities
 * Purpose:
 * 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_vsi.h"
#include <stdlib.h>
#include <math.h>

#include "cpl_conv.h"
#include "cpl_string.h"
#include "gdal_version.h"
#include "gdal_utils_priv.h"
#include "gdal_priv.h"
#include "commonutils.h"

/************************************************************************/
/*                               Usage()                                */
/************************************************************************/

static void Usage(const char *pszErrorMsg = nullptr)

{
    printf(
        " Usage: \n"
        " - To generate a shaded relief map from any GDAL-supported elevation "
        "raster : \n\n"
        "     gdaldem hillshade input_dem output_hillshade \n"
        "                 [-z ZFactor (default=1)] [-s scale* (default=1)] \n"
        "                 [-az Azimuth (default=315)] [-alt Altitude "
        "(default=45)]\n"
        "                 [-alg ZevenbergenThorne] [-combined | "
        "-multidirectional | -igor]\n"
        "                 [-compute_edges] [-b Band (default=1)] [-of format] "
        "[-co \"NAME=VALUE\"]* [-q]\n"
        "\n"
        " - To generates a slope map from any GDAL-supported elevation raster "
        ":\n\n"
        "     gdaldem slope input_dem output_slope_map \n"
        "                 [-p use percent slope (default=degrees)] [-s scale* "
        "(default=1)]\n"
        "                 [-alg ZevenbergenThorne]\n"
        "                 [-compute_edges] [-b Band (default=1)] [-of format] "
        "[-co \"NAME=VALUE\"]* [-q]\n"
        "\n"
        " - To generate an aspect map from any GDAL-supported elevation "
        "raster\n"
        "   Outputs a 32-bit float tiff with pixel values from 0-360 "
        "indicating azimuth :\n\n"
        "     gdaldem aspect input_dem output_aspect_map \n"
        "                 [-trigonometric] [-zero_for_flat]\n"
        "                 [-alg ZevenbergenThorne]\n"
        "                 [-compute_edges] [-b Band (default=1)] [-of format] "
        "[-co \"NAME=VALUE\"]* [-q]\n"
        "\n"
        " - To generate a color relief map from any GDAL-supported elevation "
        "raster\n"
        "     gdaldem color-relief input_dem color_text_file "
        "output_color_relief_map\n"
        "                 [-alpha] [-exact_color_entry | "
        "-nearest_color_entry]\n"
        "                 [-b Band (default=1)] [-of format] [-co "
        "\"NAME=VALUE\"]* [-q]\n"
        "     where color_text_file contains lines of the format "
        "\"elevation_value red green blue\"\n"
        "\n"
        " - To generate a Terrain Ruggedness Index (TRI) map from any "
        "GDAL-supported elevation raster\n"
        "     gdaldem TRI input_dem output_TRI_map\n"
        "                 [-alg Wilson|Riley]\n"
        "                 [-compute_edges] [-b Band (default=1)] [-of format] "
        "[-co \"NAME=VALUE\"]* [-q]\n"
        "\n"
        " - To generate a Topographic Position Index (TPI) map from any "
        "GDAL-supported elevation raster\n"
        "     gdaldem TPI input_dem output_TPI_map\n"
        "                 [-compute_edges] [-b Band (default=1)] [-of format] "
        "[-co \"NAME=VALUE\"]* [-q]\n"
        "\n"
        " - To generate a roughness map from any GDAL-supported elevation "
        "raster\n"
        "     gdaldem roughness input_dem output_roughness_map\n"
        "                 [-compute_edges] [-b Band (default=1)] [-of format] "
        "[-co \"NAME=VALUE\"]* [-q]\n"
        "\n"
        " Notes : \n"
        "   Scale is the ratio of vertical units to horizontal\n"
        "    for Feet:Latlong use scale=370400, for Meters:LatLong use "
        "scale=111120 \n\n");

    if (pszErrorMsg != nullptr)
        fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);

    exit(1);
}

/************************************************************************/
/*                       GDALDEMProcessingOptionsForBinaryNew()             */
/************************************************************************/

static GDALDEMProcessingOptionsForBinary *
GDALDEMProcessingOptionsForBinaryNew(void)
{
    return static_cast<GDALDEMProcessingOptionsForBinary *>(
        CPLCalloc(1, sizeof(GDALDEMProcessingOptionsForBinary)));
}

/************************************************************************/
/*                       GDALDEMProcessingOptionsForBinaryFree()            */
/************************************************************************/

static void GDALDEMProcessingOptionsForBinaryFree(
    GDALDEMProcessingOptionsForBinary *psOptionsForBinary)
{
    if (psOptionsForBinary)
    {
        CPLFree(psOptionsForBinary->pszProcessing);
        CPLFree(psOptionsForBinary->pszSrcFilename);
        CPLFree(psOptionsForBinary->pszColorFilename);
        CPLFree(psOptionsForBinary->pszDstFilename);
        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 < 2)
    {
        Usage("Not enough arguments.");
    }

    if (EQUAL(argv[1], "--utility_version") ||
        EQUAL(argv[1], "--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[1], "--help"))
        Usage();

    GDALDEMProcessingOptionsForBinary *psOptionsForBinary =
        GDALDEMProcessingOptionsForBinaryNew();
    // coverity[tainted_data]
    GDALDEMProcessingOptions *psOptions =
        GDALDEMProcessingOptionsNew(argv + 1, psOptionsForBinary);
    CSLDestroy(argv);

    if (psOptions == nullptr)
    {
        Usage();
    }

    if (!(psOptionsForBinary->bQuiet))
    {
        GDALDEMProcessingOptionsSetProgress(psOptions, GDALTermProgress,
                                            nullptr);
    }

    if (psOptionsForBinary->pszSrcFilename == nullptr)
    {
        Usage("Missing source.");
    }
    if (EQUAL(psOptionsForBinary->pszProcessing, "color-relief") &&
        psOptionsForBinary->pszColorFilename == nullptr)
    {
        Usage("Missing color file.");
    }
    if (psOptionsForBinary->pszDstFilename == nullptr)
    {
        Usage("Missing destination.");
    }

    // Open Dataset and get raster band.
    GDALDatasetH hSrcDataset =
        GDALOpen(psOptionsForBinary->pszSrcFilename, GA_ReadOnly);

    if (hSrcDataset == nullptr)
    {
        fprintf(stderr, "GDALOpen failed - %d\n%s\n", CPLGetLastErrorNo(),
                CPLGetLastErrorMsg());
        GDALDestroyDriverManager();
        exit(1);
    }

    int bUsageError = FALSE;
    GDALDatasetH hOutDS = GDALDEMProcessing(
        psOptionsForBinary->pszDstFilename, hSrcDataset,
        psOptionsForBinary->pszProcessing, psOptionsForBinary->pszColorFilename,
        psOptions, &bUsageError);
    if (bUsageError)
        Usage();
    const int nRetCode = hOutDS ? 0 : 1;

    GDALClose(hSrcDataset);
    GDALClose(hOutDS);
    GDALDEMProcessingOptionsFree(psOptions);
    GDALDEMProcessingOptionsForBinaryFree(psOptionsForBinary);

    GDALDestroyDriverManager();

    return nRetCode;
}
MAIN_END