1 | /****************************************************************************** |
---|
2 | * $Id: gdal_contour.cpp 25643 2013-02-12 13:50:42Z bishop $ |
---|
3 | * |
---|
4 | * Project: Contour Generator |
---|
5 | * Purpose: Contour Generator mainline. |
---|
6 | * Author: Frank Warmerdam <warmerdam@pobox.com> |
---|
7 | * |
---|
8 | ****************************************************************************** |
---|
9 | * Copyright (c) 2003, Applied Coherent Technology (www.actgate.com). |
---|
10 | * |
---|
11 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
12 | * copy of this software and associated documentation files (the "Software"), |
---|
13 | * to deal in the Software without restriction, including without limitation |
---|
14 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
15 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
16 | * Software is furnished to do so, subject to the following conditions: |
---|
17 | * |
---|
18 | * The above copyright notice and this permission notice shall be included |
---|
19 | * in all copies or substantial portions of the Software. |
---|
20 | * |
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
---|
22 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
---|
24 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
27 | * DEALINGS IN THE SOFTWARE. |
---|
28 | ****************************************************************************/ |
---|
29 | |
---|
30 | #include "gdal.h" |
---|
31 | #include "gdal_alg.h" |
---|
32 | #include "cpl_conv.h" |
---|
33 | #include "cpl_string.h" |
---|
34 | #include "ogr_api.h" |
---|
35 | #include "ogr_srs_api.h" |
---|
36 | #ifdef ZOO_SERVICE |
---|
37 | #include "service.h" |
---|
38 | #endif |
---|
39 | CPL_CVSID("$Id: gdal_contour.cpp 25643 2013-02-12 13:50:42Z bishop $"); |
---|
40 | |
---|
41 | #ifdef ZOO_SERVICE |
---|
42 | extern "C" { |
---|
43 | #endif |
---|
44 | |
---|
45 | /************************************************************************/ |
---|
46 | /* ArgIsNumeric() */ |
---|
47 | /************************************************************************/ |
---|
48 | |
---|
49 | static int ArgIsNumeric( const char *pszArg ) |
---|
50 | |
---|
51 | { |
---|
52 | return CPLGetValueType(pszArg) != CPL_VALUE_STRING; |
---|
53 | } |
---|
54 | |
---|
55 | /************************************************************************/ |
---|
56 | /* Usage() */ |
---|
57 | /************************************************************************/ |
---|
58 | |
---|
59 | static void Usage(const char* pszErrorMsg = NULL) |
---|
60 | |
---|
61 | { |
---|
62 | printf( |
---|
63 | "Usage: gdal_contour [-b <band>] [-a <attribute_name>] [-3d] [-inodata]\n" |
---|
64 | " [-snodata n] [-f <formatname>] [-i <interval>]\n" |
---|
65 | " [-f <formatname>] [[-dsco NAME=VALUE] ...] [[-lco NAME=VALUE] ...]\n" |
---|
66 | " [-off <offset>] [-fl <level> <level>...]\n" |
---|
67 | " [-nln <outlayername>] [-q]\n" |
---|
68 | " <src_filename> <dst_filename>\n" ); |
---|
69 | |
---|
70 | if( pszErrorMsg != NULL ) |
---|
71 | fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg); |
---|
72 | |
---|
73 | exit( 1 ); |
---|
74 | } |
---|
75 | |
---|
76 | /************************************************************************/ |
---|
77 | /* main() */ |
---|
78 | /************************************************************************/ |
---|
79 | |
---|
80 | #define CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(nExtraArg) \ |
---|
81 | do { if (i + nExtraArg >= argc) \ |
---|
82 | Usage(CPLSPrintf("%s option requires %d argument(s)", argv[i], nExtraArg)); } while(0) |
---|
83 | |
---|
84 | #ifndef ZOO_SERVICE |
---|
85 | int main( int argc, char ** argv ) |
---|
86 | #else |
---|
87 | #ifdef WIN32 |
---|
88 | __declspec(dllexport) |
---|
89 | #endif |
---|
90 | int Gdal_Contour(maps*& conf,maps*& inputs,maps*& outputs) |
---|
91 | #endif |
---|
92 | { |
---|
93 | fprintf(stderr,"DEBUG HELLO %f %d\n",__FILE__,__LINE__); |
---|
94 | fflush(stderr); |
---|
95 | GDALDatasetH hSrcDS; |
---|
96 | int i, b3D = FALSE, bNoDataSet = FALSE, bIgnoreNoData = FALSE; |
---|
97 | int nBandIn = 1; |
---|
98 | double dfInterval = 0.0, dfNoData = 0.0, dfOffset = 0.0; |
---|
99 | const char *pszSrcFilename = NULL; |
---|
100 | const char *pszDstFilename = NULL; |
---|
101 | const char *pszElevAttrib = NULL; |
---|
102 | const char *pszFormat = "ESRI Shapefile"; |
---|
103 | char **papszDSCO = NULL, **papszLCO = NULL; |
---|
104 | double adfFixedLevels[1000]; |
---|
105 | int nFixedLevelCount = 0; |
---|
106 | const char *pszNewLayerName = "contour"; |
---|
107 | int bQuiet = FALSE; |
---|
108 | GDALProgressFunc pfnProgress = NULL; |
---|
109 | fprintf(stderr,"DEBUG HELLO %f %d\n",__FILE__,__LINE__); |
---|
110 | fflush(stderr); |
---|
111 | #ifndef ZOO_SERVICE |
---|
112 | /* Check that we are running against at least GDAL 1.4 */ |
---|
113 | /* Note to developers : if we use newer API, please change the requirement */ |
---|
114 | if (atoi(GDALVersionInfo("VERSION_NUM")) < 1400) |
---|
115 | { |
---|
116 | fprintf(stderr, "At least, GDAL >= 1.4.0 is required for this version of %s, " |
---|
117 | "which was compiled against GDAL %s\n", argv[0], GDAL_RELEASE_NAME); |
---|
118 | exit(1); |
---|
119 | } |
---|
120 | #endif |
---|
121 | GDALAllRegister(); |
---|
122 | OGRRegisterAll(); |
---|
123 | |
---|
124 | #ifndef ZOO_SERVICE |
---|
125 | argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 ); |
---|
126 | |
---|
127 | /* -------------------------------------------------------------------- */ |
---|
128 | /* Parse arguments. */ |
---|
129 | /* -------------------------------------------------------------------- */ |
---|
130 | for( i = 1; i < argc; i++ ) |
---|
131 | { |
---|
132 | if( EQUAL(argv[i], "--utility_version") ) |
---|
133 | { |
---|
134 | printf("%s was compiled against GDAL %s and is running against GDAL %s\n", |
---|
135 | argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME")); |
---|
136 | return 0; |
---|
137 | } |
---|
138 | else if( EQUAL(argv[i], "--help") ) |
---|
139 | Usage(); |
---|
140 | else if( EQUAL(argv[i],"-a") ) |
---|
141 | { |
---|
142 | CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); |
---|
143 | pszElevAttrib = argv[++i]; |
---|
144 | } |
---|
145 | else if( EQUAL(argv[i],"-off") ) |
---|
146 | { |
---|
147 | CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); |
---|
148 | dfOffset = atof(argv[++i]); |
---|
149 | } |
---|
150 | else if( EQUAL(argv[i],"-i") ) |
---|
151 | { |
---|
152 | CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); |
---|
153 | dfInterval = atof(argv[++i]); |
---|
154 | } |
---|
155 | else if( EQUAL(argv[i],"-fl") ) |
---|
156 | { |
---|
157 | if( i >= argc-1 ) |
---|
158 | Usage(CPLSPrintf("%s option requires at least 1 argument", argv[i])); |
---|
159 | while( i < argc-1 |
---|
160 | && nFixedLevelCount |
---|
161 | < (int)(sizeof(adfFixedLevels)/sizeof(double)) |
---|
162 | && ArgIsNumeric(argv[i+1]) ) |
---|
163 | adfFixedLevels[nFixedLevelCount++] = atof(argv[++i]); |
---|
164 | } |
---|
165 | else if( EQUAL(argv[i],"-b") ) |
---|
166 | { |
---|
167 | CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); |
---|
168 | nBandIn = atoi(argv[++i]); |
---|
169 | } |
---|
170 | else if( EQUAL(argv[i],"-f") ) |
---|
171 | { |
---|
172 | CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); |
---|
173 | pszFormat = argv[++i]; |
---|
174 | } |
---|
175 | else if( EQUAL(argv[i],"-dsco") ) |
---|
176 | { |
---|
177 | CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); |
---|
178 | papszDSCO = CSLAddString(papszDSCO, argv[++i] ); |
---|
179 | } |
---|
180 | else if( EQUAL(argv[i],"-lco") ) |
---|
181 | { |
---|
182 | CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); |
---|
183 | papszLCO = CSLAddString(papszLCO, argv[++i] ); |
---|
184 | } |
---|
185 | else if( EQUAL(argv[i],"-3d") ) |
---|
186 | { |
---|
187 | b3D = TRUE; |
---|
188 | } |
---|
189 | else if( EQUAL(argv[i],"-snodata") ) |
---|
190 | { |
---|
191 | CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); |
---|
192 | bNoDataSet = TRUE; |
---|
193 | dfNoData = atof(argv[++i]); |
---|
194 | } |
---|
195 | else if( EQUAL(argv[i],"-nln") ) |
---|
196 | { |
---|
197 | CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); |
---|
198 | pszNewLayerName = argv[++i]; |
---|
199 | } |
---|
200 | else if( EQUAL(argv[i],"-inodata") ) |
---|
201 | { |
---|
202 | bIgnoreNoData = TRUE; |
---|
203 | } |
---|
204 | else if ( EQUAL(argv[i],"-q") || EQUAL(argv[i],"-quiet") ) |
---|
205 | { |
---|
206 | bQuiet = TRUE; |
---|
207 | } |
---|
208 | else if( pszSrcFilename == NULL ) |
---|
209 | { |
---|
210 | pszSrcFilename = argv[i]; |
---|
211 | } |
---|
212 | else if( pszDstFilename == NULL ) |
---|
213 | { |
---|
214 | pszDstFilename = argv[i]; |
---|
215 | } |
---|
216 | else |
---|
217 | Usage("Too many command options."); |
---|
218 | } |
---|
219 | #else |
---|
220 | bQuiet = TRUE; |
---|
221 | bIgnoreNoData = TRUE; |
---|
222 | map* tmpMap=NULL; |
---|
223 | tmpMap=NULL; |
---|
224 | tmpMap=getMapFromMaps(inputs,"a","value"); |
---|
225 | if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){ |
---|
226 | pszElevAttrib = strdup(tmpMap->value); |
---|
227 | } |
---|
228 | tmpMap=getMapFromMaps(inputs,"off","value"); |
---|
229 | if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){ |
---|
230 | dfOffset = atof(tmpMap->value); |
---|
231 | } |
---|
232 | tmpMap=getMapFromMaps(inputs,"i","value"); |
---|
233 | if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){ |
---|
234 | dfInterval = atof(tmpMap->value); |
---|
235 | } |
---|
236 | tmpMap=getMapFromMaps(inputs,"b","value"); |
---|
237 | if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){ |
---|
238 | nBandIn = atoi(tmpMap->value); |
---|
239 | } |
---|
240 | tmpMap=getMapFromMaps(inputs,"InputDSN","value"); |
---|
241 | if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){ |
---|
242 | pszSrcFilename = strdup(tmpMap->value); |
---|
243 | } |
---|
244 | tmpMap=getMapFromMaps(inputs,"OutputDSN","value"); |
---|
245 | if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){ |
---|
246 | pszDstFilename = strdup(tmpMap->value); |
---|
247 | } |
---|
248 | #endif |
---|
249 | if( dfInterval == 0.0 && nFixedLevelCount == 0 ) |
---|
250 | { |
---|
251 | Usage("Neither -i nor -fl are specified."); |
---|
252 | } |
---|
253 | |
---|
254 | if (pszSrcFilename == NULL) |
---|
255 | { |
---|
256 | Usage("Missing source filename."); |
---|
257 | } |
---|
258 | |
---|
259 | if (pszDstFilename == NULL) |
---|
260 | { |
---|
261 | Usage("Missing destination filename."); |
---|
262 | } |
---|
263 | |
---|
264 | if (!bQuiet) |
---|
265 | pfnProgress = GDALTermProgress; |
---|
266 | |
---|
267 | /* -------------------------------------------------------------------- */ |
---|
268 | /* Open source raster file. */ |
---|
269 | /* -------------------------------------------------------------------- */ |
---|
270 | GDALRasterBandH hBand; |
---|
271 | |
---|
272 | hSrcDS = GDALOpen( pszSrcFilename, GA_ReadOnly ); |
---|
273 | if( hSrcDS == NULL ){ |
---|
274 | #ifndef ZOO_SERVICE |
---|
275 | exit( 2 ); |
---|
276 | #else |
---|
277 | setMapInMaps(conf,"lenv","message","Unable to open the file"); |
---|
278 | #endif |
---|
279 | } |
---|
280 | hBand = GDALGetRasterBand( hSrcDS, nBandIn ); |
---|
281 | if( hBand == NULL ) |
---|
282 | { |
---|
283 | #ifndef ZOO_SERVICE |
---|
284 | CPLError( CE_Failure, CPLE_AppDefined, |
---|
285 | "Band %d does not exist on dataset.", |
---|
286 | nBandIn ); |
---|
287 | exit(2); |
---|
288 | #else |
---|
289 | char tmp[1024]; |
---|
290 | sprintf(tmp,"Band %d does not exist on dataset.",nBandIn); |
---|
291 | setMapInMaps(conf,"lenv","message",tmp); |
---|
292 | return SERVICE_FAILED; |
---|
293 | #endif |
---|
294 | } |
---|
295 | |
---|
296 | if( !bNoDataSet && !bIgnoreNoData ) |
---|
297 | dfNoData = GDALGetRasterNoDataValue( hBand, &bNoDataSet ); |
---|
298 | |
---|
299 | /* -------------------------------------------------------------------- */ |
---|
300 | /* Try to get a coordinate system from the raster. */ |
---|
301 | /* -------------------------------------------------------------------- */ |
---|
302 | OGRSpatialReferenceH hSRS = NULL; |
---|
303 | |
---|
304 | const char *pszWKT = GDALGetProjectionRef( hSrcDS ); |
---|
305 | |
---|
306 | if( pszWKT != NULL && strlen(pszWKT) != 0 ) |
---|
307 | hSRS = OSRNewSpatialReference( pszWKT ); |
---|
308 | |
---|
309 | /* -------------------------------------------------------------------- */ |
---|
310 | /* Create the outputfile. */ |
---|
311 | /* -------------------------------------------------------------------- */ |
---|
312 | OGRDataSourceH hDS; |
---|
313 | OGRSFDriverH hDriver = OGRGetDriverByName( pszFormat ); |
---|
314 | OGRFieldDefnH hFld; |
---|
315 | OGRLayerH hLayer; |
---|
316 | |
---|
317 | if( hDriver == NULL ) |
---|
318 | { |
---|
319 | #ifndef ZOO_SERVICE |
---|
320 | fprintf( stderr, "Unable to find format driver named %s.\n", |
---|
321 | pszFormat ); |
---|
322 | exit( 10 ); |
---|
323 | #else |
---|
324 | char tmp[1024]; |
---|
325 | sprintf( tmp, "Unable to find format driver named %s.\n", pszFormat ); |
---|
326 | setMapInMaps(conf,"lenv","message",tmp); |
---|
327 | return SERVICE_FAILED; |
---|
328 | #endif |
---|
329 | } |
---|
330 | |
---|
331 | hDS = OGR_Dr_CreateDataSource( hDriver, pszDstFilename, papszDSCO ); |
---|
332 | if( hDS == NULL ){ |
---|
333 | #ifndef ZOO_SERVICE |
---|
334 | exit( 1 ); |
---|
335 | #else |
---|
336 | setMapInMaps(conf,"lenv","message","Unable to create the file"); |
---|
337 | return SERVICE_FAILED; |
---|
338 | #endif |
---|
339 | } |
---|
340 | |
---|
341 | hLayer = OGR_DS_CreateLayer( hDS, pszNewLayerName, hSRS, |
---|
342 | b3D ? wkbLineString25D : wkbLineString, |
---|
343 | papszLCO ); |
---|
344 | if( hLayer == NULL ) |
---|
345 | exit( 1 ); |
---|
346 | |
---|
347 | hFld = OGR_Fld_Create( "ID", OFTInteger ); |
---|
348 | OGR_Fld_SetWidth( hFld, 8 ); |
---|
349 | OGR_L_CreateField( hLayer, hFld, FALSE ); |
---|
350 | OGR_Fld_Destroy( hFld ); |
---|
351 | |
---|
352 | if( pszElevAttrib ) |
---|
353 | { |
---|
354 | hFld = OGR_Fld_Create( pszElevAttrib, OFTReal ); |
---|
355 | OGR_Fld_SetWidth( hFld, 12 ); |
---|
356 | OGR_Fld_SetPrecision( hFld, 3 ); |
---|
357 | OGR_L_CreateField( hLayer, hFld, FALSE ); |
---|
358 | OGR_Fld_Destroy( hFld ); |
---|
359 | } |
---|
360 | |
---|
361 | /* -------------------------------------------------------------------- */ |
---|
362 | /* Invoke. */ |
---|
363 | /* -------------------------------------------------------------------- */ |
---|
364 | CPLErr eErr; |
---|
365 | |
---|
366 | eErr = GDALContourGenerate( hBand, dfInterval, dfOffset, |
---|
367 | nFixedLevelCount, adfFixedLevels, |
---|
368 | bNoDataSet, dfNoData, hLayer, |
---|
369 | OGR_FD_GetFieldIndex( OGR_L_GetLayerDefn( hLayer ), |
---|
370 | "ID" ), |
---|
371 | (pszElevAttrib == NULL) ? -1 : |
---|
372 | OGR_FD_GetFieldIndex( OGR_L_GetLayerDefn( hLayer ), |
---|
373 | pszElevAttrib ), |
---|
374 | pfnProgress, NULL ); |
---|
375 | |
---|
376 | OGR_DS_Destroy( hDS ); |
---|
377 | GDALClose( hSrcDS ); |
---|
378 | |
---|
379 | if (hSRS) |
---|
380 | OSRDestroySpatialReference( hSRS ); |
---|
381 | |
---|
382 | #ifndef ZOO_SERVICE |
---|
383 | CSLDestroy( argv ); |
---|
384 | CSLDestroy( papszDSCO ); |
---|
385 | CSLDestroy( papszLCO ); |
---|
386 | GDALDestroyDriverManager(); |
---|
387 | OGRCleanupAll(); |
---|
388 | return 0; |
---|
389 | #else |
---|
390 | GDALDestroyDriverManager(); |
---|
391 | OGRCleanupAll(); |
---|
392 | char tmp[1024]; |
---|
393 | sprintf(tmp,"File %s successfully created.",pszDstFilename); |
---|
394 | setMapInMaps(outputs,"Result","value",tmp); |
---|
395 | return SERVICE_SUCCEEDED; |
---|
396 | #endif |
---|
397 | } |
---|
398 | |
---|
399 | #ifdef ZOO_SERVICE |
---|
400 | } |
---|
401 | #endif |
---|