LCOV - code coverage report
Current view: directory - apps - gdal_contour.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 108 82 75.9 %
Date: 2010-01-09 Functions: 2 1 50.0 %

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

Generated by: LCOV version 1.7