1 /******************************************************************************
2  * $Id: dted_test.c b1c9c12ad373e40b955162b45d704070d4ebf7b0 2019-06-19 16:50:15 +0200 Even Rouault $
3  *
4  * Project:  DTED Translator
5  * Purpose:  Test mainline for DTED writer.
6  * Author:   Frank Warmerdam, warmerdam@pobox.com
7  *
8  ******************************************************************************
9  * Copyright (c) 2001, Frank Warmerdam
10  * Copyright (c) 2007, Even Rouault <even dot rouault at spatialys.com>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  *****************************************************************************/
30 
31 #include "gdal.h"
32 #include "dted_api.h"
33 
34 /************************************************************************/
35 /*                               Usage()                                */
36 /************************************************************************/
37 
Usage()38 static void Usage()
39 
40 {
41     printf( "Usage: dted_test [-trim] [-fill n] [-level n] <in_file> [<out_level>]\n" );
42     exit(0);
43 }
44 
45 /************************************************************************/
46 /*                                main()                                */
47 /************************************************************************/
main(int argc,char ** argv)48 int main( int argc, char ** argv )
49 
50 {
51     GDALDatasetH hSrcDS;
52     int         iY, iX, nOutLevel=0, nXSize, nYSize, iArg, nFillDist=0;
53     void        *pStream;
54     GInt16      *panData;
55     const char  *pszFilename = NULL;
56     GDALRasterBandH hSrcBand;
57     double       adfGeoTransform[6];
58     int          bEnableTrim = FALSE;
59     GInt16       noDataValue = 0;
60     int          bHasNoData;
61 
62 /* -------------------------------------------------------------------- */
63 /*      Identify arguments.                                             */
64 /* -------------------------------------------------------------------- */
65 
66     for( iArg = 1; iArg < argc; iArg++ )
67     {
68         if( EQUAL(argv[iArg],"-trim") )
69             bEnableTrim = TRUE;
70 
71         else if( EQUAL(argv[iArg],"-fill") )
72             nFillDist = atoi(argv[++iArg]);
73 
74         else if( EQUAL(argv[iArg],"-level") )
75             nOutLevel = atoi(argv[++iArg]);
76         else
77         {
78             if( pszFilename != NULL )
79                 Usage();
80             pszFilename = argv[iArg];
81         }
82     }
83 
84     if( pszFilename == NULL )
85         Usage();
86 
87 /* -------------------------------------------------------------------- */
88 /*      Open input file.                                                */
89 /* -------------------------------------------------------------------- */
90     GDALAllRegister();
91     hSrcDS = GDALOpen( pszFilename, GA_ReadOnly );
92     if( hSrcDS == NULL )
93         exit(1);
94 
95     hSrcBand = GDALGetRasterBand( hSrcDS, 1 );
96 
97     noDataValue = (GInt16)GDALGetRasterNoDataValue(hSrcBand, &bHasNoData);
98 
99     nXSize = GDALGetRasterXSize( hSrcDS );
100     nYSize = GDALGetRasterYSize( hSrcDS );
101 
102     GDALGetGeoTransform( hSrcDS, adfGeoTransform );
103 
104 /* -------------------------------------------------------------------- */
105 /*      Create output stream.                                           */
106 /* -------------------------------------------------------------------- */
107     pStream = DTEDCreatePtStream( ".", nOutLevel );
108 
109     if( pStream == NULL )
110         exit( 1 );
111 
112 /* -------------------------------------------------------------------- */
113 /*      Process all the profiles.                                       */
114 /* -------------------------------------------------------------------- */
115     panData = (GInt16 *) malloc(sizeof(GInt16) * nXSize);
116 
117     for( iY = 0; iY < nYSize; iY++ )
118     {
119         GDALRasterIO( hSrcBand, GF_Read, 0, iY, nXSize, 1,
120                       panData, nXSize, 1, GDT_Int16, 0, 0 );
121 
122         if (bHasNoData)
123         {
124             for( iX = 0; iX < nXSize; iX++ )
125             {
126                 if (panData[iX] == noDataValue)
127                     panData[iX] = DTED_NODATA_VALUE;
128             }
129         }
130 
131         for( iX = 0; iX < nXSize; iX++ )
132         {
133             DTEDWritePt( pStream,
134                          adfGeoTransform[0]
135                          + adfGeoTransform[1] * (iX + 0.5)
136                          + adfGeoTransform[2] * (iY + 0.5),
137                          adfGeoTransform[3]
138                          + adfGeoTransform[4] * (iX + 0.5)
139                          + adfGeoTransform[5] * (iY + 0.5),
140                          panData[iX] );
141         }
142     }
143 
144     free( panData );
145 
146 /* -------------------------------------------------------------------- */
147 /*      Cleanup.                                                        */
148 /* -------------------------------------------------------------------- */
149     if( bEnableTrim )
150         DTEDPtStreamTrimEdgeOnlyTiles( pStream );
151 
152     if( nFillDist > 0 )
153         DTEDFillPtStream( pStream, nFillDist );
154 
155     DTEDClosePtStream( pStream );
156     GDALClose( hSrcDS );
157 
158     exit( 0 );
159 }
160