1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/******************************************************************************
* $Id: gdal_contour.cpp,v 1.10 2005/02/03 17:28:37 fwarmerdam Exp $
*
* Project: Contour Generator
* Purpose: Contour Generator mainline.
* Author: Frank Warmerdam <warmerdam@pobox.com>
*
******************************************************************************
* Copyright (c) 2003, Applied Coherent Technology (www.actgate.com).
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************
*
* $Log: gdal_contour.cpp,v $
* Revision 1.10 2005/02/03 17:28:37 fwarmerdam
* Removed <interval> from end of usage message, use -i instead.
*
* Revision 1.9 2004/09/03 14:36:28 warmerda
* fix warning about exit value
*
* Revision 1.8 2004/04/02 17:33:22 warmerda
* added GDALGeneralCmdLineProcessor()
*
* Revision 1.7 2003/10/16 16:54:23 warmerda
* added support for fixed levels
*
* Revision 1.6 2003/10/15 20:58:43 warmerda
* ensure dfNoData is initialized
*
* Revision 1.5 2003/10/14 15:26:03 warmerda
* added format setting
*
* Revision 1.4 2003/10/10 19:05:46 warmerda
* implementation migrated to gdal/alg
*
* Revision 1.3 2003/10/09 20:08:38 warmerda
* complete nodata support
*
* Revision 1.2 2003/10/09 18:29:27 warmerda
* basics working now
*
* Revision 1.1 2003/09/30 18:11:33 warmerda
* New
*
*/
#include "gdal.h"
#include "gdal_alg.h"
#include "cpl_conv.h"
#include "ogr_api.h"
#include "ogr_srs_api.h"
CPL_CVSID("$Id: gdal_contour.cpp,v 1.10 2005/02/03 17:28:37 fwarmerdam Exp $");
/************************************************************************/
/* Usage() */
/************************************************************************/
static void Usage()
{
printf(
"Usage: gdal_contour [-b <band>] [-a <attribute_name>] [-3d] [-inodata]\n"
" [-snodata n] [-f <formatname>] [-i <interval>]\n"
" [-off <offset>] [-fl <level> <level>...]\n"
" <src_filename> <dst_filename>\n" );
exit( 1 );
}
/************************************************************************/
/* main() */
/************************************************************************/
int main( int argc, char ** argv )
{
GDALDatasetH hSrcDS;
int i, b3D = FALSE, bNoDataSet = FALSE, bIgnoreNoData = FALSE;
int nBandIn = 1;
double dfInterval = 0.0, dfNoData = 0.0, dfOffset = 0.0;
const char *pszSrcFilename = NULL;
const char *pszDstFilename = NULL;
const char *pszElevAttrib = NULL;
const char *pszFormat = "ESRI Shapefile";
double adfFixedLevels[1000];
int nFixedLevelCount = 0;
GDALAllRegister();
OGRRegisterAll();
argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 );
if( argc < 1 )
exit( -argc );
/* -------------------------------------------------------------------- */
/* Parse arguments. */
/* -------------------------------------------------------------------- */
for( i = 1; i < argc; i++ )
{
if( EQUAL(argv[i],"-a") && i < argc-1 )
{
pszElevAttrib = argv[++i];
}
else if( EQUAL(argv[i],"-off") && i < argc-1 )
{
dfOffset = atof(argv[++i]);
}
else if( EQUAL(argv[i],"-i") && i < argc-1 )
{
dfInterval = atof(argv[++i]);
}
else if( EQUAL(argv[i],"-fl") && i < argc-1 )
{
while( i < argc-1
&& nFixedLevelCount
< (int)(sizeof(adfFixedLevels)/sizeof(double))
&& atof(argv[i+1]) != 0 || EQUAL(argv[i+1],"0") )
adfFixedLevels[nFixedLevelCount++] = atof(argv[++i]);
}
else if( EQUAL(argv[i],"-b") && i < argc-1 )
{
nBandIn = atoi(argv[++i]);
}
else if( EQUAL(argv[i],"-f") && i < argc-1 )
{
pszFormat = argv[++i];
}
else if( EQUAL(argv[i],"-3d") )
{
b3D = TRUE;
}
else if( EQUAL(argv[i],"-snodata") && i < argc-1 )
{
bNoDataSet = TRUE;
dfNoData = atof(argv[++i]);
}
else if( EQUAL(argv[i],"-inodata") )
{
bIgnoreNoData = TRUE;
}
else if( pszSrcFilename == NULL )
{
pszSrcFilename = argv[i];
}
else if( pszDstFilename == NULL )
{
pszDstFilename = argv[i];
}
else
Usage();
}
if( dfInterval == 0.0 && nFixedLevelCount == 0 )
{
Usage();
}
/* -------------------------------------------------------------------- */
/* Open source raster file. */
/* -------------------------------------------------------------------- */
GDALRasterBandH hBand;
hSrcDS = GDALOpen( pszSrcFilename, GA_ReadOnly );
if( hSrcDS == NULL )
exit( 2 );
hBand = GDALGetRasterBand( hSrcDS, nBandIn );
if( hBand == NULL )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Band %d does not exist on dataset.",
nBandIn );
}
if( !bNoDataSet && !bIgnoreNoData )
dfNoData = GDALGetRasterNoDataValue( hBand, &bNoDataSet );
/* -------------------------------------------------------------------- */
/* Try to get a coordinate system from the raster. */
/* -------------------------------------------------------------------- */
OGRSpatialReferenceH hSRS = NULL;
const char *pszWKT = GDALGetProjectionRef( hBand );
if( pszWKT != NULL && strlen(pszWKT) != 0 )
hSRS = OSRNewSpatialReference( pszWKT );
/* -------------------------------------------------------------------- */
/* Create the outputfile. */
/* -------------------------------------------------------------------- */
OGRDataSourceH hDS;
OGRSFDriverH hDriver = OGRGetDriverByName( pszFormat );
OGRFieldDefnH hFld;
OGRLayerH hLayer;
int nElevField = -1;
if( hDriver == NULL )
{
fprintf( stderr, "Unable to find format driver named %s.\n",
pszFormat );
exit( 10 );
}
hDS = OGR_Dr_CreateDataSource( hDriver, pszDstFilename, NULL );
if( hDS == NULL )
exit( 1 );
hLayer = OGR_DS_CreateLayer( hDS, "contour", hSRS,
b3D ? wkbLineString25D : wkbLineString,
NULL );
if( hLayer == NULL )
exit( 1 );
hFld = OGR_Fld_Create( "ID", OFTInteger );
OGR_Fld_SetWidth( hFld, 8 );
OGR_L_CreateField( hLayer, hFld, FALSE );
OGR_Fld_Destroy( hFld );
if( pszElevAttrib )
{
hFld = OGR_Fld_Create( pszElevAttrib, OFTReal );
OGR_Fld_SetWidth( hFld, 12 );
OGR_Fld_SetPrecision( hFld, 3 );
OGR_L_CreateField( hLayer, hFld, FALSE );
OGR_Fld_Destroy( hFld );
nElevField = 1;
}
/* -------------------------------------------------------------------- */
/* Invoke. */
/* -------------------------------------------------------------------- */
CPLErr eErr;
eErr = GDALContourGenerate( hBand, dfInterval, dfOffset,
nFixedLevelCount, adfFixedLevels,
bNoDataSet, dfNoData,
hLayer, 0, nElevField,
GDALTermProgress, NULL );
OGR_DS_Destroy( hDS );
GDALClose( hSrcDS );
return 0;
}
|