source: trunk/zoo-project/zoo-services/gdal/contour/service.c @ 900

Last change on this file since 900 was 766, checked in by djay, 8 years ago

Add support for GDAL 2 (base-vect-ops).

File size: 13.8 KB
Line 
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#include "service_internal.h"
39#endif
40CPL_CVSID("$Id: gdal_contour.cpp 25643 2013-02-12 13:50:42Z bishop $");
41
42#ifdef ZOO_SERVICE
43extern "C" {
44#endif
45
46/************************************************************************/
47/*                            ArgIsNumeric()                            */
48/************************************************************************/
49
50static int ArgIsNumeric( const char *pszArg )
51
52{
53    return CPLGetValueType(pszArg) != CPL_VALUE_STRING;
54}
55
56/************************************************************************/
57/*                               Usage()                                */
58/************************************************************************/
59
60static void Usage(const char* pszErrorMsg = NULL)
61
62{
63    printf( 
64        "Usage: gdal_contour [-b <band>] [-a <attribute_name>] [-3d] [-inodata]\n"
65        "                    [-snodata n] [-f <formatname>] [-i <interval>]\n"
66        "                    [-f <formatname>] [[-dsco NAME=VALUE] ...] [[-lco NAME=VALUE] ...]\n"   
67        "                    [-off <offset>] [-fl <level> <level>...]\n" 
68        "                    [-nln <outlayername>] [-q]\n"
69        "                    <src_filename> <dst_filename>\n" );
70
71    if( pszErrorMsg != NULL )
72        fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
73
74    exit( 1 );
75}
76
77#ifdef ZOO_SERVICE
78  int pfnProgress1( double dfComplete, const char *pszMessage, void *pData)
79  {
80    maps* conf=(maps*)pData;
81    map* tmpMap=getMapFromMaps(conf,"lenv","status");
82    char tmpStr[4];
83    sprintf(tmpStr,"%d",(int) (dfComplete*100));
84    if(tmpMap!=NULL && strcmp(tmpMap->value,tmpStr)!=0){
85      if( pszMessage != NULL && strlen(pszMessage)>1){
86        updateStatus(conf,(int) (dfComplete*100), pszMessage);
87      }     
88      else
89        updateStatus(conf,(int) (dfComplete*100), "Processing...");
90    }
91    return TRUE;
92  }
93#endif
94
95/************************************************************************/
96/*                                main()                                */
97/************************************************************************/
98
99#define CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(nExtraArg) \
100    do { if (i + nExtraArg >= argc) \
101        Usage(CPLSPrintf("%s option requires %d argument(s)", argv[i], nExtraArg)); } while(0)
102
103#ifndef ZOO_SERVICE
104int main( int argc, char ** argv )
105#else
106#ifdef WIN32
107__declspec(dllexport)
108#endif
109  int Gdal_Contour(maps*& conf,maps*& inputs,maps*& outputs)
110#endif
111{
112    fprintf(stderr,"DEBUG HELLO %f %d\n",__FILE__,__LINE__);
113    fflush(stderr);
114    GDALDatasetH        hSrcDS;
115    int i, b3D = FALSE, bNoDataSet = FALSE, bIgnoreNoData = FALSE;
116    int nBandIn = 1;
117    double dfInterval = 0.0, dfNoData = 0.0, dfOffset = 0.0;
118    const char *pszSrcFilename = NULL;
119    const char *pszDstFilename = NULL;
120    const char *pszElevAttrib = NULL;
121    const char *pszFormat = "ESRI Shapefile";
122    char        **papszDSCO = NULL, **papszLCO = NULL;
123    double adfFixedLevels[1000];
124    int    nFixedLevelCount = 0;
125    const char *pszNewLayerName = "contour";
126    int bQuiet = FALSE;
127#ifndef ZOO_SERVICE
128    /* Check that we are running against at least GDAL 1.4 */
129    /* Note to developers : if we use newer API, please change the requirement */
130    if (atoi(GDALVersionInfo("VERSION_NUM")) < 1400)
131    {
132        fprintf(stderr, "At least, GDAL >= 1.4.0 is required for this version of %s, "
133                "which was compiled against GDAL %s\n", argv[0], GDAL_RELEASE_NAME);
134        exit(1);
135    }
136#endif
137    GDALAllRegister();
138    OGRRegisterAll();
139
140#ifndef ZOO_SERVICE
141    argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 );
142
143/* -------------------------------------------------------------------- */
144/*      Parse arguments.                                                */
145/* -------------------------------------------------------------------- */
146    for( i = 1; i < argc; i++ )
147    {
148        if( EQUAL(argv[i], "--utility_version") )
149        {
150            printf("%s was compiled against GDAL %s and is running against GDAL %s\n",
151                   argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
152            return 0;
153        }
154        else if( EQUAL(argv[i], "--help") )
155            Usage();
156        else if( EQUAL(argv[i],"-a") )
157        {
158            CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
159            pszElevAttrib = argv[++i];
160        }
161        else if( EQUAL(argv[i],"-off") )
162        {
163            CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
164            dfOffset = atof(argv[++i]);
165        }
166        else if( EQUAL(argv[i],"-i") )
167        {
168            CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
169            dfInterval = atof(argv[++i]);
170        }
171        else if( EQUAL(argv[i],"-fl") )
172        {
173            if( i >= argc-1 )
174                Usage(CPLSPrintf("%s option requires at least 1 argument", argv[i]));
175            while( i < argc-1 
176                   && nFixedLevelCount
177                             < (int)(sizeof(adfFixedLevels)/sizeof(double))
178                   && ArgIsNumeric(argv[i+1]) )
179                adfFixedLevels[nFixedLevelCount++] = atof(argv[++i]);
180        }
181        else if( EQUAL(argv[i],"-b") )
182        {
183            CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
184            nBandIn = atoi(argv[++i]);
185        }
186        else if( EQUAL(argv[i],"-f") )
187        {
188            CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
189            pszFormat = argv[++i];
190        }
191        else if( EQUAL(argv[i],"-dsco") )
192        {
193            CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
194            papszDSCO = CSLAddString(papszDSCO, argv[++i] );
195        }
196        else if( EQUAL(argv[i],"-lco") )
197        {
198            CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
199            papszLCO = CSLAddString(papszLCO, argv[++i] );
200        }
201        else if( EQUAL(argv[i],"-3d")  )
202        {
203            b3D = TRUE;
204        }
205        else if( EQUAL(argv[i],"-snodata") )
206        {
207            CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
208            bNoDataSet = TRUE;
209            dfNoData = atof(argv[++i]);
210        }
211        else if( EQUAL(argv[i],"-nln") )
212        {
213            CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
214            pszNewLayerName = argv[++i];
215        }
216        else if( EQUAL(argv[i],"-inodata") )
217        {
218            bIgnoreNoData = TRUE;
219        }
220        else if ( EQUAL(argv[i],"-q") || EQUAL(argv[i],"-quiet") )
221        {
222            bQuiet = TRUE;
223        }
224        else if( pszSrcFilename == NULL )
225        {
226            pszSrcFilename = argv[i];
227        }
228        else if( pszDstFilename == NULL )
229        {
230            pszDstFilename = argv[i];
231        }
232        else
233            Usage("Too many command options.");
234    }
235#else
236    bQuiet = TRUE;
237    bIgnoreNoData = TRUE;
238    map* tmpMap=NULL;
239    tmpMap=NULL;
240    tmpMap=getMapFromMaps(inputs,"a","value");
241    if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){
242      pszElevAttrib = strdup(tmpMap->value);
243    }
244    tmpMap=getMapFromMaps(inputs,"off","value");
245    if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){
246      dfOffset = atof(tmpMap->value);
247    }
248    tmpMap=getMapFromMaps(inputs,"i","value");
249    if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){
250      dfInterval = atof(tmpMap->value);
251    }
252    tmpMap=getMapFromMaps(inputs,"b","value");
253    if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){
254      nBandIn = atoi(tmpMap->value);
255    }
256    tmpMap=getMapFromMaps(inputs,"InputDSN","value");
257    if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){
258      pszSrcFilename = strdup(tmpMap->value);
259    }
260    tmpMap=getMapFromMaps(inputs,"OutputDSN","value");
261    if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){
262      pszDstFilename = strdup(tmpMap->value);
263    }
264#endif
265    if( dfInterval == 0.0 && nFixedLevelCount == 0 )
266    {
267        Usage("Neither -i nor -fl are specified.");
268    }
269
270    if (pszSrcFilename == NULL)
271    {
272        Usage("Missing source filename.");
273    }
274
275    if (pszDstFilename == NULL)
276    {
277        Usage("Missing destination filename.");
278    }
279   
280    /*if (!bQuiet)
281      pfnProgress = GDALTermProgress;*/
282    //pfnProgress = GDALTermProgress;
283/* -------------------------------------------------------------------- */
284/*      Open source raster file.                                        */
285/* -------------------------------------------------------------------- */
286    GDALRasterBandH hBand;
287
288    hSrcDS = GDALOpen( pszSrcFilename, GA_ReadOnly );
289    if( hSrcDS == NULL ){
290#ifndef ZOO_SERVICE
291        exit( 2 );
292#else
293        setMapInMaps(conf,"lenv","message","Unable to open the file");
294#endif
295    }
296    hBand = GDALGetRasterBand( hSrcDS, nBandIn );
297    if( hBand == NULL )
298    {
299#ifndef ZOO_SERVICE
300        CPLError( CE_Failure, CPLE_AppDefined, 
301                  "Band %d does not exist on dataset.", 
302                  nBandIn );
303        exit(2);
304#else
305        char tmp[1024];
306        sprintf(tmp,"Band %d does not exist on dataset.",nBandIn);
307        setMapInMaps(conf,"lenv","message",tmp);
308        return SERVICE_FAILED;
309#endif
310    }
311
312    if( !bNoDataSet && !bIgnoreNoData )
313        dfNoData = GDALGetRasterNoDataValue( hBand, &bNoDataSet );
314
315/* -------------------------------------------------------------------- */
316/*      Try to get a coordinate system from the raster.                 */
317/* -------------------------------------------------------------------- */
318    OGRSpatialReferenceH hSRS = NULL;
319
320    const char *pszWKT = GDALGetProjectionRef( hSrcDS );
321
322    if( pszWKT != NULL && strlen(pszWKT) != 0 )
323        hSRS = OSRNewSpatialReference( pszWKT );
324
325/* -------------------------------------------------------------------- */
326/*      Create the outputfile.                                          */
327/* -------------------------------------------------------------------- */
328    OGRDataSourceH hDS;
329    OGRSFDriverH hDriver = OGRGetDriverByName( pszFormat );
330    OGRFieldDefnH hFld;
331    OGRLayerH hLayer;
332
333    if( hDriver == NULL )
334    {
335#ifndef ZOO_SERVICE
336        fprintf( stderr, "Unable to find format driver named %s.\n", 
337                 pszFormat );
338        exit( 10 );
339#else
340        char tmp[1024];
341        sprintf( tmp, "Unable to find format driver named %s.\n", pszFormat ); 
342        setMapInMaps(conf,"lenv","message",tmp);
343        return SERVICE_FAILED;
344#endif
345    }
346
347    hDS = OGR_Dr_CreateDataSource( hDriver, pszDstFilename, papszDSCO );
348    if( hDS == NULL ){
349#ifndef ZOO_SERVICE
350        exit( 1 );
351#else
352        setMapInMaps(conf,"lenv","message","Unable to create the file");
353        return SERVICE_FAILED;
354#endif
355    }
356
357    hLayer = OGR_DS_CreateLayer( hDS, pszNewLayerName, hSRS, 
358                                 b3D ? wkbLineString25D : wkbLineString,
359                                 papszLCO );
360    if( hLayer == NULL )
361        exit( 1 );
362
363    hFld = OGR_Fld_Create( "ID", OFTInteger );
364    OGR_Fld_SetWidth( hFld, 8 );
365    OGR_L_CreateField( hLayer, hFld, FALSE );
366    OGR_Fld_Destroy( hFld );
367
368    if( pszElevAttrib )
369    {
370        hFld = OGR_Fld_Create( pszElevAttrib, OFTReal );
371        OGR_Fld_SetWidth( hFld, 12 );
372        OGR_Fld_SetPrecision( hFld, 3 );
373        OGR_L_CreateField( hLayer, hFld, FALSE );
374        OGR_Fld_Destroy( hFld );
375    }
376
377/* -------------------------------------------------------------------- */
378/*      Invoke.                                                         */
379/* -------------------------------------------------------------------- */
380    CPLErr eErr;
381   
382    eErr = GDALContourGenerate( hBand, dfInterval, dfOffset, 
383                         nFixedLevelCount, adfFixedLevels,
384                         bNoDataSet, dfNoData, hLayer, 
385                         OGR_FD_GetFieldIndex( OGR_L_GetLayerDefn( hLayer ), 
386                                               "ID" ), 
387                         (pszElevAttrib == NULL) ? -1 :
388                                 OGR_FD_GetFieldIndex( OGR_L_GetLayerDefn( hLayer ), 
389                                                       pszElevAttrib ), 
390                                (GDALProgressFunc)pfnProgress1, conf );
391
392    OGR_DS_Destroy( hDS );
393    GDALClose( hSrcDS );
394
395    if (hSRS)
396        OSRDestroySpatialReference( hSRS );
397
398#ifndef ZOO_SERVICE
399    CSLDestroy( argv );
400    CSLDestroy( papszDSCO );
401    CSLDestroy( papszLCO );
402    GDALDestroyDriverManager();
403    OGRCleanupAll();
404    return 0;
405#else
406    GDALDestroyDriverManager();
407    OGRCleanupAll();
408    char tmp[1024];
409    sprintf(tmp,"File %s successfully created.",pszDstFilename);
410    setMapInMaps(outputs,"Result","value",tmp);
411    return SERVICE_SUCCEEDED;
412#endif
413}
414
415#ifdef ZOO_SERVICE
416}
417#endif
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png