LCOV - code coverage report
Current view: directory - apps - gdal_contour.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 109 83 76.1 %
Date: 2012-12-26 Functions: 3 2 66.7 %

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

Generated by: LCOV version 1.7