LCOV - code coverage report
Current view: directory - apps - gdal_contour.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 117 90 76.9 %
Date: 2012-04-28 Functions: 3 2 66.7 %

       1                 : /******************************************************************************
       2                 :  * $Id: gdal_contour.cpp 21191 2010-12-03 20:02:34Z rouault $
       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                 : 
      37                 : CPL_CVSID("$Id: gdal_contour.cpp 21191 2010-12-03 20:02:34Z rouault $");
      38                 : 
      39                 : /************************************************************************/
      40                 : /*                            ArgIsNumeric()                            */
      41                 : /************************************************************************/
      42                 : 
      43               8 : static int ArgIsNumeric( const char *pszArg )
      44                 : 
      45                 : {
      46               8 :     if( pszArg[0] == '-' )
      47               2 :         pszArg++;
      48                 : 
      49               8 :     if( *pszArg == '\0' )
      50               0 :         return FALSE;
      51                 : 
      52              30 :     while( *pszArg != '\0' )
      53                 :     {
      54              16 :         if( (*pszArg < '0' || *pszArg > '9') && *pszArg != '.' )
      55               2 :             return FALSE;
      56              14 :         pszArg++;
      57                 :     }
      58                 : 
      59               6 :     return TRUE;
      60                 : }
      61                 : 
      62                 : /************************************************************************/
      63                 : /*                               Usage()                                */
      64                 : /************************************************************************/
      65                 : 
      66               0 : static void Usage()
      67                 : 
      68                 : {
      69                 :     printf( 
      70                 :         "Usage: gdal_contour [-b <band>] [-a <attribute_name>] [-3d] [-inodata]\n"
      71                 :         "                    [-snodata n] [-f <formatname>] [-i <interval>]\n"
      72                 :         "                    [-off <offset>] [-fl <level> <level>...]\n" 
      73                 :         "                    [-nln <outlayername>] [-q]\n"
      74               0 :         "                    <src_filename> <dst_filename>\n" );
      75               0 :     exit( 1 );
      76                 : }
      77                 : 
      78                 : /************************************************************************/
      79                 : /*                                main()                                */
      80                 : /************************************************************************/
      81                 : 
      82              14 : int main( int argc, char ** argv )
      83                 : 
      84                 : {
      85                 :     GDALDatasetH  hSrcDS;
      86              14 :     int i, b3D = FALSE, bNoDataSet = FALSE, bIgnoreNoData = FALSE;
      87              14 :     int nBandIn = 1;
      88              14 :     double dfInterval = 0.0, dfNoData = 0.0, dfOffset = 0.0;
      89              14 :     const char *pszSrcFilename = NULL;
      90              14 :     const char *pszDstFilename = NULL;
      91              14 :     const char *pszElevAttrib = NULL;
      92              14 :     const char *pszFormat = "ESRI Shapefile";
      93                 :     double adfFixedLevels[1000];
      94              14 :     int    nFixedLevelCount = 0;
      95              14 :     const char *pszNewLayerName = "contour";
      96              14 :     int bQuiet = FALSE;
      97              14 :     GDALProgressFunc pfnProgress = NULL;
      98                 : 
      99                 :     /* Check that we are running against at least GDAL 1.4 */
     100                 :     /* Note to developers : if we use newer API, please change the requirement */
     101              14 :     if (atoi(GDALVersionInfo("VERSION_NUM")) < 1400)
     102                 :     {
     103                 :         fprintf(stderr, "At least, GDAL >= 1.4.0 is required for this version of %s, "
     104               0 :                 "which was compiled against GDAL %s\n", argv[0], GDAL_RELEASE_NAME);
     105               0 :         exit(1);
     106                 :     }
     107                 : 
     108              14 :     GDALAllRegister();
     109              14 :     OGRRegisterAll();
     110                 : 
     111              14 :     argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 );
     112                 : 
     113                 : /* -------------------------------------------------------------------- */
     114                 : /*      Parse arguments.                                                */
     115                 : /* -------------------------------------------------------------------- */
     116              64 :     for( i = 1; i < argc; i++ )
     117                 :     {
     118              52 :         if( EQUAL(argv[i], "--utility_version") )
     119                 :         {
     120                 :             printf("%s was compiled against GDAL %s and is running against GDAL %s\n",
     121               2 :                    argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
     122               2 :             return 0;
     123                 :         }
     124              60 :         else if( EQUAL(argv[i],"-a") && i < argc-1 )
     125                 :         {
     126              10 :             pszElevAttrib = argv[++i];
     127                 :         }
     128              40 :         else if( EQUAL(argv[i],"-off") && i < argc-1 )
     129                 :         {
     130               0 :             dfOffset = atof(argv[++i]);
     131                 :         }
     132              50 :         else if( EQUAL(argv[i],"-i") && i < argc-1 )
     133                 :         {
     134              10 :             dfInterval = atof(argv[++i]);
     135                 :         }
     136              32 :         else if( EQUAL(argv[i],"-fl") && i < argc-1 )
     137                 :         {
     138              18 :             while( i < argc-1 
     139                 :                    && nFixedLevelCount 
     140                 :                              < (int)(sizeof(adfFixedLevels)/sizeof(double))
     141               8 :                    && ArgIsNumeric(argv[i+1]) )
     142               6 :                 adfFixedLevels[nFixedLevelCount++] = atof(argv[++i]);
     143                 :         }
     144              28 :         else if( EQUAL(argv[i],"-b") && i < argc-1 )
     145                 :         {
     146               0 :             nBandIn = atoi(argv[++i]);
     147                 :         }
     148              28 :         else if( EQUAL(argv[i],"-f") && i < argc-1 )
     149                 :         {
     150               0 :             pszFormat = argv[++i];
     151                 :         }
     152              28 :         else if( EQUAL(argv[i],"-3d")  )
     153                 :         {
     154               4 :             b3D = TRUE;
     155                 :         }
     156              24 :         else if( EQUAL(argv[i],"-snodata")  && i < argc-1 )
     157                 :         {
     158               0 :             bNoDataSet = TRUE;
     159               0 :             dfNoData = atof(argv[++i]);
     160                 :         }
     161              24 :         else if( EQUAL(argv[i],"-nln")  && i < argc-1 )
     162                 :         {
     163               0 :             pszNewLayerName = argv[++i];
     164                 :         }
     165              24 :         else if( EQUAL(argv[i],"-inodata") )
     166                 :         {
     167               0 :             bIgnoreNoData = TRUE;
     168                 :         }
     169              24 :         else if ( EQUAL(argv[i],"-q") || EQUAL(argv[i],"-quiet") )
     170                 :         {
     171               0 :             bQuiet = TRUE;
     172                 :         }
     173              24 :         else if( pszSrcFilename == NULL )
     174                 :         {
     175              12 :             pszSrcFilename = argv[i];
     176                 :         }
     177              12 :         else if( pszDstFilename == NULL )
     178                 :         {
     179              12 :             pszDstFilename = argv[i];
     180                 :         }
     181                 :         else
     182               0 :             Usage();
     183                 :     }
     184                 : 
     185              12 :     if( dfInterval == 0.0 && nFixedLevelCount == 0 )
     186                 :     {
     187               0 :         Usage();
     188                 :     }
     189                 : 
     190              12 :     if (pszSrcFilename == NULL)
     191                 :     {
     192               0 :         fprintf(stderr, "Missing source filename.\n");
     193               0 :         Usage();
     194                 :     }
     195                 : 
     196              12 :     if (pszDstFilename == NULL)
     197                 :     {
     198               0 :         fprintf(stderr, "Missing destination filename.\n");
     199               0 :         Usage();
     200                 :     }
     201                 :     
     202              12 :     if (!bQuiet)
     203              12 :         pfnProgress = GDALTermProgress;
     204                 : 
     205                 : /* -------------------------------------------------------------------- */
     206                 : /*      Open source raster file.                                        */
     207                 : /* -------------------------------------------------------------------- */
     208                 :     GDALRasterBandH hBand;
     209                 : 
     210              12 :     hSrcDS = GDALOpen( pszSrcFilename, GA_ReadOnly );
     211              12 :     if( hSrcDS == NULL )
     212               0 :         exit( 2 );
     213                 : 
     214              12 :     hBand = GDALGetRasterBand( hSrcDS, nBandIn );
     215              12 :     if( hBand == NULL )
     216                 :     {
     217                 :         CPLError( CE_Failure, CPLE_AppDefined, 
     218                 :                   "Band %d does not exist on dataset.", 
     219               0 :                   nBandIn );
     220               0 :         exit(2);
     221                 :     }
     222                 : 
     223              12 :     if( !bNoDataSet && !bIgnoreNoData )
     224              12 :         dfNoData = GDALGetRasterNoDataValue( hBand, &bNoDataSet );
     225                 : 
     226                 : /* -------------------------------------------------------------------- */
     227                 : /*      Try to get a coordinate system from the raster.                 */
     228                 : /* -------------------------------------------------------------------- */
     229              12 :     OGRSpatialReferenceH hSRS = NULL;
     230                 : 
     231              12 :     const char *pszWKT = GDALGetProjectionRef( hSrcDS );
     232                 : 
     233              12 :     if( pszWKT != NULL && strlen(pszWKT) != 0 )
     234              10 :         hSRS = OSRNewSpatialReference( pszWKT );
     235                 : 
     236                 : /* -------------------------------------------------------------------- */
     237                 : /*      Create the outputfile.                                          */
     238                 : /* -------------------------------------------------------------------- */
     239                 :     OGRDataSourceH hDS;
     240              12 :     OGRSFDriverH hDriver = OGRGetDriverByName( pszFormat );
     241                 :     OGRFieldDefnH hFld;
     242                 :     OGRLayerH hLayer;
     243                 : 
     244              12 :     if( hDriver == NULL )
     245                 :     {
     246                 :         fprintf( stderr, "Unable to find format driver named %s.\n", 
     247               0 :                  pszFormat );
     248               0 :         exit( 10 );
     249                 :     }
     250                 : 
     251              12 :     hDS = OGR_Dr_CreateDataSource( hDriver, pszDstFilename, NULL );
     252              12 :     if( hDS == NULL )
     253               0 :         exit( 1 );
     254                 : 
     255                 :     hLayer = OGR_DS_CreateLayer( hDS, pszNewLayerName, hSRS, 
     256                 :                                  b3D ? wkbLineString25D : wkbLineString,
     257              12 :                                  NULL );
     258              12 :     if( hLayer == NULL )
     259               0 :         exit( 1 );
     260                 : 
     261              12 :     hFld = OGR_Fld_Create( "ID", OFTInteger );
     262              12 :     OGR_Fld_SetWidth( hFld, 8 );
     263              12 :     OGR_L_CreateField( hLayer, hFld, FALSE );
     264              12 :     OGR_Fld_Destroy( hFld );
     265                 : 
     266              12 :     if( pszElevAttrib )
     267                 :     {
     268              10 :         hFld = OGR_Fld_Create( pszElevAttrib, OFTReal );
     269              10 :         OGR_Fld_SetWidth( hFld, 12 );
     270              10 :         OGR_Fld_SetPrecision( hFld, 3 );
     271              10 :         OGR_L_CreateField( hLayer, hFld, FALSE );
     272              10 :         OGR_Fld_Destroy( hFld );
     273                 :     }
     274                 : 
     275                 : /* -------------------------------------------------------------------- */
     276                 : /*      Invoke.                                                         */
     277                 : /* -------------------------------------------------------------------- */
     278                 :     CPLErr eErr;
     279                 :     
     280                 :     eErr = GDALContourGenerate( hBand, dfInterval, dfOffset, 
     281                 :                          nFixedLevelCount, adfFixedLevels,
     282                 :                          bNoDataSet, dfNoData, hLayer, 
     283                 :                          OGR_FD_GetFieldIndex( OGR_L_GetLayerDefn( hLayer ), 
     284                 :                                                "ID" ), 
     285                 :                          (pszElevAttrib == NULL) ? -1 :
     286                 :                                  OGR_FD_GetFieldIndex( OGR_L_GetLayerDefn( hLayer ), 
     287                 :                                                        pszElevAttrib ), 
     288              12 :                          pfnProgress, NULL );
     289                 : 
     290              12 :     OGR_DS_Destroy( hDS );
     291              12 :     GDALClose( hSrcDS );
     292                 : 
     293              12 :     if (hSRS)
     294              10 :         OSRDestroySpatialReference( hSRS );
     295                 : 
     296              12 :     CSLDestroy( argv );
     297              12 :     GDALDestroyDriverManager();
     298              12 :     OGRCleanupAll();
     299                 : 
     300              12 :     return 0;
     301                 : }

Generated by: LCOV version 1.7