~ubuntu-branches/debian/sid/gdal/sid

« back to all changes in this revision

Viewing changes to apps/gdal_rasterize.cpp

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-05-07 15:04:42 UTC
  • mfrom: (5.5.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120507150442-2eks97loeh6rq005
Tags: 1.9.0-1
* Ready for sid, starting transition.
* All symfiles updated to latest builds.
* Added dh_numpy call in debian/rules to depend on numpy ABI.
* Policy bumped to 3.9.3, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/******************************************************************************
2
 
 * $Id: gdal_rasterize.cpp 20654 2010-09-20 14:40:53Z dron $
 
2
 * $Id: gdal_rasterize.cpp 23285 2011-10-27 21:39:19Z rouault $
3
3
 *
4
4
 * Project:  GDAL Utilities
5
5
 * Purpose:  Rasterize OGR shapes into a GDAL raster.
33
33
#include "ogr_api.h"
34
34
#include "ogr_srs_api.h"
35
35
#include "cpl_string.h"
 
36
#include "commonutils.h"
36
37
#include <vector>
37
38
 
38
 
CPL_CVSID("$Id: gdal_rasterize.cpp 20654 2010-09-20 14:40:53Z dron $");
 
39
CPL_CVSID("$Id: gdal_rasterize.cpp 23285 2011-10-27 21:39:19Z rouault $");
 
40
 
 
41
/************************************************************************/
 
42
/*                            ArgIsNumeric()                            */
 
43
/************************************************************************/
 
44
 
 
45
static int ArgIsNumeric( const char *pszArg )
 
46
 
 
47
{
 
48
    if( pszArg[0] == '-' )
 
49
        pszArg++;
 
50
 
 
51
    if( *pszArg == '\0' )
 
52
        return FALSE;
 
53
 
 
54
    while( *pszArg != '\0' )
 
55
    {
 
56
        if( (*pszArg < '0' || *pszArg > '9') && *pszArg != '.' )
 
57
            return FALSE;
 
58
        pszArg++;
 
59
    }
 
60
        
 
61
    return TRUE;
 
62
}
39
63
 
40
64
/************************************************************************/
41
65
/*                               Usage()                                */
45
69
 
46
70
{
47
71
    printf( 
48
 
        "Usage: gdal_rasterize [-b band] [-i] [-at]\n"
49
 
        "       [-burn value] | [-a attribute_name] [-3d]\n"
50
 
//      "       [-of format_driver] [-co key=value]\n"       
51
 
//      "       [-te xmin ymin xmax ymax] [-tr xres yres] [-ts width height]\n"
 
72
        "Usage: gdal_rasterize [-b band]* [-i] [-at]\n"
 
73
        "       [-burn value]* | [-a attribute_name] [-3d]\n"
52
74
        "       [-l layername]* [-where expression] [-sql select_statement]\n"
 
75
        "       [-of format] [-a_srs srs_def] [-co \"NAME=VALUE\"]*\n"
 
76
        "       [-a_nodata value] [-init value]*\n"
 
77
        "       [-te xmin ymin xmax ymax] [-tr xres yres] [-tap] [-ts width height]\n"
 
78
        "       [-ot {Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/\n"
 
79
        "             CInt16/CInt32/CFloat32/CFloat64}] [-q]\n"
53
80
        "       <src_datasource> <dst_filename>\n" );
54
81
    exit( 1 );
55
82
}
127
154
/************************************************************************/
128
155
 
129
156
static void ProcessLayer( 
130
 
    OGRLayerH hSrcLayer, 
 
157
    OGRLayerH hSrcLayer, int bSRSIsSet, 
131
158
    GDALDatasetH hDstDS, std::vector<int> anBandList,
132
159
    std::vector<double> &adfBurnValues, int b3D, int bInverse,
133
 
    const char *pszBurnAttribute, char **papszRasterizeOptions )
 
160
    const char *pszBurnAttribute, char **papszRasterizeOptions,
 
161
    GDALProgressFunc pfnProgress, void* pProgressData )
134
162
 
135
163
{
136
164
/* -------------------------------------------------------------------- */
137
165
/*      Checkout that SRS are the same.                                 */
 
166
/*      If -a_srs is specified, skip the test                           */
138
167
/* -------------------------------------------------------------------- */
139
 
    OGRSpatialReferenceH  hDstSRS = NULL;
140
 
    if( GDALGetProjectionRef( hDstDS ) != NULL )
 
168
    if (!bSRSIsSet)
141
169
    {
142
 
        char *pszProjection;
143
 
 
144
 
        pszProjection = (char *) GDALGetProjectionRef( hDstDS );
145
 
 
146
 
        hDstSRS = OSRNewSpatialReference(NULL);
147
 
        if( OSRImportFromWkt( hDstSRS, &pszProjection ) != CE_None )
 
170
        OGRSpatialReferenceH  hDstSRS = NULL;
 
171
        if( GDALGetProjectionRef( hDstDS ) != NULL )
 
172
        {
 
173
            char *pszProjection;
 
174
    
 
175
            pszProjection = (char *) GDALGetProjectionRef( hDstDS );
 
176
    
 
177
            hDstSRS = OSRNewSpatialReference(NULL);
 
178
            if( OSRImportFromWkt( hDstSRS, &pszProjection ) != CE_None )
 
179
            {
 
180
                OSRDestroySpatialReference(hDstSRS);
 
181
                hDstSRS = NULL;
 
182
            }
 
183
        }
 
184
    
 
185
        OGRSpatialReferenceH hSrcSRS = OGR_L_GetSpatialRef(hSrcLayer);
 
186
        if( hDstSRS != NULL && hSrcSRS != NULL )
 
187
        {
 
188
            if( OSRIsSame(hSrcSRS, hDstSRS) == FALSE )
 
189
            {
 
190
                fprintf(stderr,
 
191
                        "Warning : the output raster dataset and the input vector layer do not have the same SRS.\n"
 
192
                        "Results might be incorrect (no on-the-fly reprojection of input data).\n");
 
193
            }
 
194
        }
 
195
        else if( hDstSRS != NULL && hSrcSRS == NULL )
 
196
        {
 
197
            fprintf(stderr,
 
198
                    "Warning : the output raster dataset has a SRS, but the input vector layer SRS is unknown.\n"
 
199
                    "Ensure input vector has the same SRS, otherwise results might be incorrect.\n");
 
200
        }
 
201
        else if( hDstSRS == NULL && hSrcSRS != NULL )
 
202
        {
 
203
            fprintf(stderr,
 
204
                    "Warning : the input vector layer has a SRS, but the output raster dataset SRS is unknown.\n"
 
205
                    "Ensure output raster dataset has the same SRS, otherwise results might be incorrect.\n");
 
206
        }
 
207
    
 
208
        if( hDstSRS != NULL )
148
209
        {
149
210
            OSRDestroySpatialReference(hDstSRS);
150
 
            hDstSRS = NULL;
151
 
        }
152
 
    }
153
 
 
154
 
    OGRSpatialReferenceH hSrcSRS = OGR_L_GetSpatialRef(hSrcLayer);
155
 
    if( hDstSRS != NULL && hSrcSRS != NULL )
156
 
    {
157
 
        if( OSRIsSame(hSrcSRS, hDstSRS) == FALSE )
158
 
        {
159
 
            fprintf(stderr,
160
 
                    "Warning : the output raster dataset and the input vector layer do not have the same SRS.\n"
161
 
                    "Results might be incorrect (no on-the-fly reprojection of input data).\n");
162
 
        }
163
 
    }
164
 
    else if( hDstSRS != NULL && hSrcSRS == NULL )
165
 
    {
166
 
        fprintf(stderr,
167
 
                "Warning : the output raster dataset has a SRS, but the input vector layer SRS is unknown.\n"
168
 
                "Ensure input vector has the same SRS, otherwise results might be incorrect.\n");
169
 
    }
170
 
    else if( hDstSRS == NULL && hSrcSRS != NULL )
171
 
    {
172
 
        fprintf(stderr,
173
 
                "Warning : the input vector layer has a SRS, but the output raster dataset SRS is unknown.\n"
174
 
                "Ensure output raster dataset has the same SRS, otherwise results might be incorrect.\n");
175
 
    }
176
 
 
177
 
    if( hDstSRS != NULL )
178
 
    {
179
 
        OSRDestroySpatialReference(hDstSRS);
 
211
        }
180
212
    }
181
213
 
182
214
/* -------------------------------------------------------------------- */
249
281
/* -------------------------------------------------------------------- */
250
282
/*      If we are in inverse mode, we add one extra ring around the     */
251
283
/*      whole dataset to invert the concept of insideness and then      */
252
 
/*      merge everything into one geomtry collection.                   */
 
284
/*      merge everything into one geometry collection.                  */
253
285
/* -------------------------------------------------------------------- */
254
286
    if( bInverse )
255
287
    {
263
295
                             ahGeometries.size(), &(ahGeometries[0]), 
264
296
                             NULL, NULL, &(adfFullBurnValues[0]), 
265
297
                             papszRasterizeOptions,
266
 
                             GDALTermProgress, NULL );
 
298
                             pfnProgress, pProgressData );
267
299
 
268
300
/* -------------------------------------------------------------------- */
269
301
/*      Cleanup geometries.                                             */
275
307
}
276
308
 
277
309
/************************************************************************/
 
310
/*                  CreateOutputDataset()                               */
 
311
/************************************************************************/
 
312
 
 
313
static
 
314
GDALDatasetH CreateOutputDataset(std::vector<OGRLayerH> ahLayers,
 
315
                                 OGRSpatialReferenceH hSRS,
 
316
                                 int bGotBounds, OGREnvelope sEnvelop,
 
317
                                 GDALDriverH hDriver, const char* pszDstFilename,
 
318
                                 int nXSize, int nYSize, double dfXRes, double dfYRes,
 
319
                                 int bTargetAlignedPixels,
 
320
                                 int nBandCount, GDALDataType eOutputType,
 
321
                                 char** papszCreateOptions, std::vector<double> adfInitVals,
 
322
                                 int bNoDataSet, double dfNoData)
 
323
{
 
324
    int bFirstLayer = TRUE;
 
325
    char* pszWKT = NULL;
 
326
    GDALDatasetH hDstDS = NULL;
 
327
    unsigned int i;
 
328
 
 
329
    for( i = 0; i < ahLayers.size(); i++ )
 
330
    {
 
331
        OGRLayerH hLayer = ahLayers[i];
 
332
 
 
333
        if (!bGotBounds)
 
334
        {
 
335
            OGREnvelope sLayerEnvelop;
 
336
 
 
337
            if (OGR_L_GetExtent(hLayer, &sLayerEnvelop, TRUE) != OGRERR_NONE)
 
338
            {
 
339
                fprintf(stderr, "Cannot get layer extent\n");
 
340
                exit(2);
 
341
            }
 
342
 
 
343
            /* When rasterizing point layers and that the bounds have */
 
344
            /* not been explicitely set, voluntary increase the extent by */
 
345
            /* a half-pixel size to avoid missing points on the border */
 
346
            if (wkbFlatten(OGR_L_GetGeomType(hLayer)) == wkbPoint &&
 
347
                !bTargetAlignedPixels && dfXRes != 0 && dfYRes != 0)
 
348
            {
 
349
                sLayerEnvelop.MinX -= dfXRes / 2;
 
350
                sLayerEnvelop.MaxX += dfXRes / 2;
 
351
                sLayerEnvelop.MinY -= dfYRes / 2;
 
352
                sLayerEnvelop.MaxY += dfYRes / 2;
 
353
            }
 
354
 
 
355
            if (bFirstLayer)
 
356
            {
 
357
                sEnvelop.MinX = sLayerEnvelop.MinX;
 
358
                sEnvelop.MinY = sLayerEnvelop.MinY;
 
359
                sEnvelop.MaxX = sLayerEnvelop.MaxX;
 
360
                sEnvelop.MaxY = sLayerEnvelop.MaxY;
 
361
 
 
362
                if (hSRS == NULL)
 
363
                    hSRS = OGR_L_GetSpatialRef(hLayer);
 
364
 
 
365
                bFirstLayer = FALSE;
 
366
            }
 
367
            else
 
368
            {
 
369
                sEnvelop.MinX = MIN(sEnvelop.MinX, sLayerEnvelop.MinX);
 
370
                sEnvelop.MinY = MIN(sEnvelop.MinY, sLayerEnvelop.MinY);
 
371
                sEnvelop.MaxX = MAX(sEnvelop.MaxX, sLayerEnvelop.MaxX);
 
372
                sEnvelop.MaxY = MAX(sEnvelop.MaxY, sLayerEnvelop.MaxY);
 
373
            }
 
374
        }
 
375
        else
 
376
        {
 
377
            if (bFirstLayer)
 
378
            {
 
379
                if (hSRS == NULL)
 
380
                    hSRS = OGR_L_GetSpatialRef(hLayer);
 
381
 
 
382
                bFirstLayer = FALSE;
 
383
            }
 
384
        }
 
385
    }
 
386
 
 
387
    if (dfXRes == 0 && dfYRes == 0)
 
388
    {
 
389
        dfXRes = (sEnvelop.MaxX - sEnvelop.MinX) / nXSize;
 
390
        dfYRes = (sEnvelop.MaxY - sEnvelop.MinY) / nYSize;
 
391
    }
 
392
    else if (bTargetAlignedPixels && dfXRes != 0 && dfYRes != 0)
 
393
    {
 
394
        sEnvelop.MinX = floor(sEnvelop.MinX / dfXRes) * dfXRes;
 
395
        sEnvelop.MaxX = ceil(sEnvelop.MaxX / dfXRes) * dfXRes;
 
396
        sEnvelop.MinY = floor(sEnvelop.MinY / dfYRes) * dfYRes;
 
397
        sEnvelop.MaxY = ceil(sEnvelop.MaxY / dfYRes) * dfYRes;
 
398
    }
 
399
 
 
400
    double adfProjection[6];
 
401
    adfProjection[0] = sEnvelop.MinX;
 
402
    adfProjection[1] = dfXRes;
 
403
    adfProjection[2] = 0;
 
404
    adfProjection[3] = sEnvelop.MaxY;
 
405
    adfProjection[4] = 0;
 
406
    adfProjection[5] = -dfYRes;
 
407
 
 
408
    if (nXSize == 0 && nYSize == 0)
 
409
    {
 
410
        nXSize = (int)(0.5 + (sEnvelop.MaxX - sEnvelop.MinX) / dfXRes);
 
411
        nYSize = (int)(0.5 + (sEnvelop.MaxY - sEnvelop.MinY) / dfYRes);
 
412
    }
 
413
 
 
414
    hDstDS = GDALCreate(hDriver, pszDstFilename, nXSize, nYSize,
 
415
                        nBandCount, eOutputType, papszCreateOptions);
 
416
    if (hDstDS == NULL)
 
417
    {
 
418
        fprintf(stderr, "Cannot create %s\n", pszDstFilename);
 
419
        exit(2);
 
420
    }
 
421
 
 
422
    GDALSetGeoTransform(hDstDS, adfProjection);
 
423
 
 
424
    if (hSRS)
 
425
        OSRExportToWkt(hSRS, &pszWKT);
 
426
    if (pszWKT)
 
427
        GDALSetProjection(hDstDS, pszWKT);
 
428
    CPLFree(pszWKT);
 
429
 
 
430
    int iBand;
 
431
    /*if( nBandCount == 3 || nBandCount == 4 )
 
432
    {
 
433
        for(iBand = 0; iBand < nBandCount; iBand++)
 
434
        {
 
435
            GDALRasterBandH hBand = GDALGetRasterBand(hDstDS, iBand + 1);
 
436
            GDALSetRasterColorInterpretation(hBand, (GDALColorInterp)(GCI_RedBand + iBand));
 
437
        }
 
438
    }*/
 
439
 
 
440
    if (bNoDataSet)
 
441
    {
 
442
        for(iBand = 0; iBand < nBandCount; iBand++)
 
443
        {
 
444
            GDALRasterBandH hBand = GDALGetRasterBand(hDstDS, iBand + 1);
 
445
            GDALSetRasterNoDataValue(hBand, dfNoData);
 
446
        }
 
447
    }
 
448
 
 
449
    if (adfInitVals.size() != 0)
 
450
    {
 
451
        for(iBand = 0; iBand < MIN(nBandCount,(int)adfInitVals.size()); iBand++)
 
452
        {
 
453
            GDALRasterBandH hBand = GDALGetRasterBand(hDstDS, iBand + 1);
 
454
            GDALFillRaster(hBand, adfInitVals[iBand], 0);
 
455
        }
 
456
    }
 
457
 
 
458
    return hDstDS;
 
459
}
 
460
 
 
461
/************************************************************************/
278
462
/*                                main()                                */
279
463
/************************************************************************/
280
464
 
292
476
    std::vector<int> anBandList;
293
477
    std::vector<double> adfBurnValues;
294
478
    char **papszRasterizeOptions = NULL;
 
479
    double dfXRes = 0, dfYRes = 0;
 
480
    int bCreateOutput = FALSE;
 
481
    const char* pszFormat = "GTiff";
 
482
    int bFormatExplicitelySet = FALSE;
 
483
    char **papszCreateOptions = NULL;
 
484
    GDALDriverH hDriver = NULL;
 
485
    GDALDataType eOutputType = GDT_Float64;
 
486
    std::vector<double> adfInitVals;
 
487
    int bNoDataSet = FALSE;
 
488
    double dfNoData = 0;
 
489
    OGREnvelope sEnvelop;
 
490
    int bGotBounds = FALSE;
 
491
    int nXSize = 0, nYSize = 0;
 
492
    int bQuiet = FALSE;
 
493
    GDALProgressFunc pfnProgress = GDALTermProgress;
 
494
    OGRSpatialReferenceH hSRS = NULL;
 
495
    int bTargetAlignedPixels = FALSE;
 
496
    
295
497
 
296
498
    /* Check that we are running against at least GDAL 1.4 */
297
499
    /* Note to developers : if we use newer API, please change the requirement */
320
522
                   argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
321
523
            return 0;
322
524
        }
 
525
        else if( EQUAL(argv[i],"-q") || EQUAL(argv[i],"-quiet") )
 
526
        {
 
527
            bQuiet = TRUE;
 
528
            pfnProgress = GDALDummyProgress;
 
529
        }
323
530
        else if( EQUAL(argv[i],"-a") && i < argc-1 )
324
531
        {
325
532
            pszBurnAttribute = argv[++i];
326
533
        }
327
534
        else if( EQUAL(argv[i],"-b") && i < argc-1 )
328
535
        {
329
 
            anBandList.push_back( atoi(argv[++i]) );
 
536
            if (strchr(argv[i+1], ' '))
 
537
            {
 
538
                char** papszTokens = CSLTokenizeString( argv[i+1] );
 
539
                char** papszIter = papszTokens;
 
540
                while(papszIter && *papszIter)
 
541
                {
 
542
                    anBandList.push_back(atoi(*papszIter));
 
543
                    papszIter ++;
 
544
                }
 
545
                CSLDestroy(papszTokens);
 
546
                i += 1;
 
547
            }
 
548
            else
 
549
            {
 
550
                while(i < argc-1 && ArgIsNumeric(argv[i+1]))
 
551
                {
 
552
                    anBandList.push_back(atoi(argv[i+1]));
 
553
                    i += 1;
 
554
                }
 
555
            }
330
556
        }
331
557
        else if( EQUAL(argv[i],"-3d")  )
332
558
        {
345
571
        }
346
572
        else if( EQUAL(argv[i],"-burn") && i < argc-1 )
347
573
        {
348
 
            adfBurnValues.push_back( atof(argv[++i]) );
 
574
            if (strchr(argv[i+1], ' '))
 
575
            {
 
576
                char** papszTokens = CSLTokenizeString( argv[i+1] );
 
577
                char** papszIter = papszTokens;
 
578
                while(papszIter && *papszIter)
 
579
                {
 
580
                    adfBurnValues.push_back(atof(*papszIter));
 
581
                    papszIter ++;
 
582
                }
 
583
                CSLDestroy(papszTokens);
 
584
                i += 1;
 
585
            }
 
586
            else
 
587
            {
 
588
                while(i < argc-1 && ArgIsNumeric(argv[i+1]))
 
589
                {
 
590
                    adfBurnValues.push_back(atof(argv[i+1]));
 
591
                    i += 1;
 
592
                }
 
593
            }
349
594
        }
350
595
        else if( EQUAL(argv[i],"-where") && i < argc-1 )
351
596
        {
359
604
        {
360
605
            pszSQL = argv[++i];
361
606
        }
 
607
        else if( EQUAL(argv[i],"-of") && i < argc-1 )
 
608
        {
 
609
            pszFormat = argv[++i];
 
610
            bFormatExplicitelySet = TRUE;
 
611
            bCreateOutput = TRUE;
 
612
        }
 
613
        else if( EQUAL(argv[i],"-init") && i < argc - 1 )
 
614
        {
 
615
            if (strchr(argv[i+1], ' '))
 
616
            {
 
617
                char** papszTokens = CSLTokenizeString( argv[i+1] );
 
618
                char** papszIter = papszTokens;
 
619
                while(papszIter && *papszIter)
 
620
                {
 
621
                    adfInitVals.push_back(atof(*papszIter));
 
622
                    papszIter ++;
 
623
                }
 
624
                CSLDestroy(papszTokens);
 
625
                i += 1;
 
626
            }
 
627
            else
 
628
            {
 
629
                while(i < argc-1 && ArgIsNumeric(argv[i+1]))
 
630
                {
 
631
                    adfInitVals.push_back(atof(argv[i+1]));
 
632
                    i += 1;
 
633
                }
 
634
            }
 
635
            bCreateOutput = TRUE;
 
636
        }
 
637
        else if( EQUAL(argv[i],"-a_nodata") && i < argc - 1 )
 
638
        {
 
639
            dfNoData = atof(argv[i+1]);
 
640
            bNoDataSet = TRUE;
 
641
            i += 1;
 
642
            bCreateOutput = TRUE;
 
643
        }
 
644
        else if( EQUAL(argv[i],"-a_srs") && i < argc-1 )
 
645
        {
 
646
            hSRS = OSRNewSpatialReference( NULL );
 
647
 
 
648
            if( OSRSetFromUserInput(hSRS, argv[i+1]) != OGRERR_NONE )
 
649
            {
 
650
                fprintf( stderr, "Failed to process SRS definition: %s\n", 
 
651
                         argv[i+1] );
 
652
                exit( 1 );
 
653
            }
 
654
 
 
655
            i++;
 
656
            bCreateOutput = TRUE;
 
657
        }   
 
658
 
 
659
        else if( EQUAL(argv[i],"-te") && i < argc - 4 )
 
660
        {
 
661
            sEnvelop.MinX = atof(argv[++i]);
 
662
            sEnvelop.MinY = atof(argv[++i]);
 
663
            sEnvelop.MaxX = atof(argv[++i]);
 
664
            sEnvelop.MaxY = atof(argv[++i]);
 
665
            bGotBounds = TRUE;
 
666
            bCreateOutput = TRUE;
 
667
        }
 
668
        else if( EQUAL(argv[i],"-a_ullr") && i < argc - 4 )
 
669
        {
 
670
            sEnvelop.MinX = atof(argv[++i]);
 
671
            sEnvelop.MaxY = atof(argv[++i]);
 
672
            sEnvelop.MaxX = atof(argv[++i]);
 
673
            sEnvelop.MinY = atof(argv[++i]);
 
674
            bGotBounds = TRUE;
 
675
            bCreateOutput = TRUE;
 
676
        }
 
677
        else if( EQUAL(argv[i],"-co") && i < argc-1 )
 
678
        {
 
679
            papszCreateOptions = CSLAddString( papszCreateOptions, argv[++i] );
 
680
            bCreateOutput = TRUE;
 
681
        }
 
682
        else if( EQUAL(argv[i],"-ot") && i < argc-1 )
 
683
        {
 
684
            int iType;
 
685
            
 
686
            for( iType = 1; iType < GDT_TypeCount; iType++ )
 
687
            {
 
688
                if( GDALGetDataTypeName((GDALDataType)iType) != NULL
 
689
                    && EQUAL(GDALGetDataTypeName((GDALDataType)iType),
 
690
                             argv[i+1]) )
 
691
                {
 
692
                    eOutputType = (GDALDataType) iType;
 
693
                }
 
694
            }
 
695
 
 
696
            if( eOutputType == GDT_Unknown )
 
697
            {
 
698
                printf( "Unknown output pixel type: %s\n", argv[i+1] );
 
699
                Usage();
 
700
            }
 
701
            i++;
 
702
            bCreateOutput = TRUE;
 
703
        }
 
704
        else if( (EQUAL(argv[i],"-ts") || EQUAL(argv[i],"-outsize")) && i < argc-2 )
 
705
        {
 
706
            nXSize = atoi(argv[++i]);
 
707
            nYSize = atoi(argv[++i]);
 
708
            if (nXSize <= 0 || nYSize <= 0)
 
709
            {
 
710
                printf( "Wrong value for -outsize parameters\n");
 
711
                Usage();
 
712
            }
 
713
            bCreateOutput = TRUE;
 
714
        }
 
715
        else if( EQUAL(argv[i],"-tr") && i < argc-2 )
 
716
        {
 
717
            dfXRes = atof(argv[++i]);
 
718
            dfYRes = fabs(atof(argv[++i]));
 
719
            if( dfXRes == 0 || dfYRes == 0 )
 
720
            {
 
721
                printf( "Wrong value for -tr parameters\n");
 
722
                Usage();
 
723
            }
 
724
            bCreateOutput = TRUE;
 
725
        }
 
726
        else if( EQUAL(argv[i],"-tap") )
 
727
        {
 
728
            bTargetAlignedPixels = TRUE;
 
729
            bCreateOutput = TRUE;
 
730
        }
362
731
        else if( pszSrcFilename == NULL )
363
732
        {
364
733
            pszSrcFilename = argv[i];
376
745
        fprintf( stderr, "Missing source or destination.\n\n" );
377
746
        Usage();
378
747
    }
379
 
    
380
 
    if( pszSQL == NULL && papszLayers == NULL )
381
 
    {
382
 
        fprintf( stderr, "At least one of -l or -sql required.\n\n" );
383
 
        Usage();
384
 
    }
385
748
 
386
749
    if( adfBurnValues.size() == 0 && pszBurnAttribute == NULL && !b3D )
387
750
    {
389
752
        Usage();
390
753
    }
391
754
 
392
 
    if( anBandList.size() == 0 )
393
 
        anBandList.push_back( 1 );
 
755
    if( bCreateOutput )
 
756
    {
 
757
        if( dfXRes == 0 && dfYRes == 0 && nXSize == 0 && nYSize == 0 )
 
758
        {
 
759
            fprintf( stderr, "'-tr xres yes' or '-ts xsize ysize' is required.\n\n" );
 
760
            Usage();
 
761
        }
 
762
    
 
763
        if (bTargetAlignedPixels && dfXRes == 0 && dfYRes == 0)
 
764
        {
 
765
            fprintf( stderr, "-tap option cannot be used without using -tr\n");
 
766
            Usage();
 
767
        }
 
768
 
 
769
        if( anBandList.size() != 0 )
 
770
        {
 
771
            fprintf( stderr, "-b option cannot be used when creating a GDAL dataset.\n\n" );
 
772
            Usage();
 
773
        }
 
774
 
 
775
        int nBandCount = 1;
 
776
 
 
777
        if (adfBurnValues.size() != 0)
 
778
            nBandCount = adfBurnValues.size();
 
779
 
 
780
        if ((int)adfInitVals.size() > nBandCount)
 
781
            nBandCount = adfInitVals.size();
 
782
 
 
783
        if (adfInitVals.size() == 1)
 
784
        {
 
785
            for(i=1;i<=nBandCount - 1;i++)
 
786
                adfInitVals.push_back( adfInitVals[0] );
 
787
        }
 
788
 
 
789
        int i;
 
790
        for(i=1;i<=nBandCount;i++)
 
791
            anBandList.push_back( i );
 
792
    }
 
793
    else
 
794
    {
 
795
        if( anBandList.size() == 0 )
 
796
            anBandList.push_back( 1 );
 
797
    }
394
798
 
395
799
/* -------------------------------------------------------------------- */
396
800
/*      Open source vector dataset.                                     */
405
809
        exit( 1 );
406
810
    }
407
811
 
 
812
    if( pszSQL == NULL && papszLayers == NULL )
 
813
    {
 
814
        if( OGR_DS_GetLayerCount(hSrcDS) == 1 )
 
815
        {
 
816
            papszLayers = CSLAddString(NULL, OGR_L_GetName(OGR_DS_GetLayer(hSrcDS, 0)));
 
817
        }
 
818
        else
 
819
        {
 
820
            fprintf( stderr, "At least one of -l or -sql required.\n\n" );
 
821
            Usage();
 
822
        }
 
823
    }
 
824
 
408
825
/* -------------------------------------------------------------------- */
409
826
/*      Open target raster file.  Eventually we will add optional       */
410
827
/*      creation.                                                       */
411
828
/* -------------------------------------------------------------------- */
412
 
    GDALDatasetH hDstDS;
413
 
 
414
 
    hDstDS = GDALOpen( pszDstFilename, GA_Update );
415
 
    if( hDstDS == NULL )
416
 
        exit( 2 );
 
829
    GDALDatasetH hDstDS = NULL;
 
830
 
 
831
    if (bCreateOutput)
 
832
    {
 
833
/* -------------------------------------------------------------------- */
 
834
/*      Find the output driver.                                         */
 
835
/* -------------------------------------------------------------------- */
 
836
        hDriver = GDALGetDriverByName( pszFormat );
 
837
        if( hDriver == NULL 
 
838
            || GDALGetMetadataItem( hDriver, GDAL_DCAP_CREATE, NULL ) == NULL )
 
839
        {
 
840
            int iDr;
 
841
 
 
842
            printf( "Output driver `%s' not recognised or does not support\n", 
 
843
                    pszFormat );
 
844
            printf( "direct output file creation.  The following format drivers are configured\n"
 
845
                    "and support direct output:\n" );
 
846
 
 
847
            for( iDr = 0; iDr < GDALGetDriverCount(); iDr++ )
 
848
            {
 
849
                GDALDriverH hDriver = GDALGetDriver(iDr);
 
850
 
 
851
                if( GDALGetMetadataItem( hDriver, GDAL_DCAP_CREATE, NULL) != NULL )
 
852
                {
 
853
                    printf( "  %s: %s\n",
 
854
                            GDALGetDriverShortName( hDriver  ),
 
855
                            GDALGetDriverLongName( hDriver ) );
 
856
                }
 
857
            }
 
858
            printf( "\n" );
 
859
            exit( 1 );
 
860
        }
 
861
 
 
862
        if (!bQuiet && !bFormatExplicitelySet)
 
863
            CheckExtensionConsistency(pszDstFilename, pszFormat);
 
864
    }
 
865
    else
 
866
    {
 
867
        hDstDS = GDALOpen( pszDstFilename, GA_Update );
 
868
        if( hDstDS == NULL )
 
869
            exit( 2 );
 
870
    }
417
871
 
418
872
/* -------------------------------------------------------------------- */
419
873
/*      Process SQL request.                                            */
425
879
        hLayer = OGR_DS_ExecuteSQL( hSrcDS, pszSQL, NULL, NULL ); 
426
880
        if( hLayer != NULL )
427
881
        {
428
 
            ProcessLayer( hLayer, hDstDS, anBandList, 
 
882
            if (bCreateOutput)
 
883
            {
 
884
                std::vector<OGRLayerH> ahLayers;
 
885
                ahLayers.push_back(hLayer);
 
886
 
 
887
                hDstDS = CreateOutputDataset(ahLayers, hSRS,
 
888
                                 bGotBounds, sEnvelop,
 
889
                                 hDriver, pszDstFilename,
 
890
                                 nXSize, nYSize, dfXRes, dfYRes,
 
891
                                 bTargetAlignedPixels,
 
892
                                 anBandList.size(), eOutputType,
 
893
                                 papszCreateOptions, adfInitVals,
 
894
                                 bNoDataSet, dfNoData);
 
895
            }
 
896
 
 
897
            ProcessLayer( hLayer, hSRS != NULL, hDstDS, anBandList, 
429
898
                          adfBurnValues, b3D, bInverse, pszBurnAttribute,
430
 
                          papszRasterizeOptions );
431
 
        }
 
899
                          papszRasterizeOptions, pfnProgress, NULL );
 
900
 
 
901
            OGR_DS_ReleaseResultSet( hSrcDS, hLayer );
 
902
        }
 
903
    }
 
904
 
 
905
/* -------------------------------------------------------------------- */
 
906
/*      Create output file if necessary.                                */
 
907
/* -------------------------------------------------------------------- */
 
908
    int nLayerCount = CSLCount(papszLayers);
 
909
 
 
910
    if (bCreateOutput && hDstDS == NULL)
 
911
    {
 
912
        std::vector<OGRLayerH> ahLayers;
 
913
 
 
914
        for( i = 0; i < nLayerCount; i++ )
 
915
        {
 
916
            OGRLayerH hLayer = OGR_DS_GetLayerByName( hSrcDS, papszLayers[i] );
 
917
            if( hLayer == NULL )
 
918
            {
 
919
                continue;
 
920
            }
 
921
            ahLayers.push_back(hLayer);
 
922
        }
 
923
 
 
924
        hDstDS = CreateOutputDataset(ahLayers, hSRS,
 
925
                                bGotBounds, sEnvelop,
 
926
                                hDriver, pszDstFilename,
 
927
                                nXSize, nYSize, dfXRes, dfYRes,
 
928
                                bTargetAlignedPixels,
 
929
                                anBandList.size(), eOutputType,
 
930
                                papszCreateOptions, adfInitVals,
 
931
                                bNoDataSet, dfNoData);
432
932
    }
433
933
 
434
934
/* -------------------------------------------------------------------- */
435
935
/*      Process each layer.                                             */
436
936
/* -------------------------------------------------------------------- */
437
 
    int nLayerCount = CSLCount(papszLayers);
 
937
 
438
938
    for( i = 0; i < nLayerCount; i++ )
439
939
    {
440
940
        OGRLayerH hLayer = OGR_DS_GetLayerByName( hSrcDS, papszLayers[i] );
451
951
                break;
452
952
        }
453
953
 
454
 
        ProcessLayer( hLayer, hDstDS, anBandList, 
 
954
        void *pScaledProgress;
 
955
        pScaledProgress =
 
956
            GDALCreateScaledProgress( 0.0, 1.0 * (i + 1) / nLayerCount,
 
957
                                      pfnProgress, NULL );
 
958
 
 
959
        ProcessLayer( hLayer, hSRS != NULL, hDstDS, anBandList, 
455
960
                      adfBurnValues, b3D, bInverse, pszBurnAttribute,
456
 
                      papszRasterizeOptions );
 
961
                      papszRasterizeOptions, GDALScaledProgress, pScaledProgress );
 
962
 
 
963
        GDALDestroyScaledProgress( pScaledProgress );
457
964
    }
458
965
 
459
966
/* -------------------------------------------------------------------- */
463
970
    OGR_DS_Destroy( hSrcDS );
464
971
    GDALClose( hDstDS );
465
972
 
 
973
    OSRDestroySpatialReference(hSRS);
 
974
 
466
975
    CSLDestroy( argv );
467
976
    CSLDestroy( papszRasterizeOptions );
468
977
    CSLDestroy( papszLayers );
 
978
    CSLDestroy( papszCreateOptions );
469
979
    
470
980
    GDALDestroyDriverManager();
471
981
    OGRCleanupAll();