127
154
/************************************************************************/
129
156
static void ProcessLayer(
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 )
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 )
144
pszProjection = (char *) GDALGetProjectionRef( hDstDS );
146
hDstSRS = OSRNewSpatialReference(NULL);
147
if( OSRImportFromWkt( hDstSRS, &pszProjection ) != CE_None )
170
OGRSpatialReferenceH hDstSRS = NULL;
171
if( GDALGetProjectionRef( hDstDS ) != NULL )
175
pszProjection = (char *) GDALGetProjectionRef( hDstDS );
177
hDstSRS = OSRNewSpatialReference(NULL);
178
if( OSRImportFromWkt( hDstSRS, &pszProjection ) != CE_None )
180
OSRDestroySpatialReference(hDstSRS);
185
OGRSpatialReferenceH hSrcSRS = OGR_L_GetSpatialRef(hSrcLayer);
186
if( hDstSRS != NULL && hSrcSRS != NULL )
188
if( OSRIsSame(hSrcSRS, hDstSRS) == FALSE )
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");
195
else if( hDstSRS != NULL && hSrcSRS == NULL )
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");
201
else if( hDstSRS == NULL && hSrcSRS != NULL )
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");
208
if( hDstSRS != NULL )
149
210
OSRDestroySpatialReference(hDstSRS);
154
OGRSpatialReferenceH hSrcSRS = OGR_L_GetSpatialRef(hSrcLayer);
155
if( hDstSRS != NULL && hSrcSRS != NULL )
157
if( OSRIsSame(hSrcSRS, hDstSRS) == FALSE )
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");
164
else if( hDstSRS != NULL && hSrcSRS == NULL )
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");
170
else if( hDstSRS == NULL && hSrcSRS != NULL )
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");
177
if( hDstSRS != NULL )
179
OSRDestroySpatialReference(hDstSRS);
182
214
/* -------------------------------------------------------------------- */
277
309
/************************************************************************/
310
/* CreateOutputDataset() */
311
/************************************************************************/
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)
324
int bFirstLayer = TRUE;
326
GDALDatasetH hDstDS = NULL;
329
for( i = 0; i < ahLayers.size(); i++ )
331
OGRLayerH hLayer = ahLayers[i];
335
OGREnvelope sLayerEnvelop;
337
if (OGR_L_GetExtent(hLayer, &sLayerEnvelop, TRUE) != OGRERR_NONE)
339
fprintf(stderr, "Cannot get layer extent\n");
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)
349
sLayerEnvelop.MinX -= dfXRes / 2;
350
sLayerEnvelop.MaxX += dfXRes / 2;
351
sLayerEnvelop.MinY -= dfYRes / 2;
352
sLayerEnvelop.MaxY += dfYRes / 2;
357
sEnvelop.MinX = sLayerEnvelop.MinX;
358
sEnvelop.MinY = sLayerEnvelop.MinY;
359
sEnvelop.MaxX = sLayerEnvelop.MaxX;
360
sEnvelop.MaxY = sLayerEnvelop.MaxY;
363
hSRS = OGR_L_GetSpatialRef(hLayer);
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);
380
hSRS = OGR_L_GetSpatialRef(hLayer);
387
if (dfXRes == 0 && dfYRes == 0)
389
dfXRes = (sEnvelop.MaxX - sEnvelop.MinX) / nXSize;
390
dfYRes = (sEnvelop.MaxY - sEnvelop.MinY) / nYSize;
392
else if (bTargetAlignedPixels && dfXRes != 0 && dfYRes != 0)
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;
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;
408
if (nXSize == 0 && nYSize == 0)
410
nXSize = (int)(0.5 + (sEnvelop.MaxX - sEnvelop.MinX) / dfXRes);
411
nYSize = (int)(0.5 + (sEnvelop.MaxY - sEnvelop.MinY) / dfYRes);
414
hDstDS = GDALCreate(hDriver, pszDstFilename, nXSize, nYSize,
415
nBandCount, eOutputType, papszCreateOptions);
418
fprintf(stderr, "Cannot create %s\n", pszDstFilename);
422
GDALSetGeoTransform(hDstDS, adfProjection);
425
OSRExportToWkt(hSRS, &pszWKT);
427
GDALSetProjection(hDstDS, pszWKT);
431
/*if( nBandCount == 3 || nBandCount == 4 )
433
for(iBand = 0; iBand < nBandCount; iBand++)
435
GDALRasterBandH hBand = GDALGetRasterBand(hDstDS, iBand + 1);
436
GDALSetRasterColorInterpretation(hBand, (GDALColorInterp)(GCI_RedBand + iBand));
442
for(iBand = 0; iBand < nBandCount; iBand++)
444
GDALRasterBandH hBand = GDALGetRasterBand(hDstDS, iBand + 1);
445
GDALSetRasterNoDataValue(hBand, dfNoData);
449
if (adfInitVals.size() != 0)
451
for(iBand = 0; iBand < MIN(nBandCount,(int)adfInitVals.size()); iBand++)
453
GDALRasterBandH hBand = GDALGetRasterBand(hDstDS, iBand + 1);
454
GDALFillRaster(hBand, adfInitVals[iBand], 0);
461
/************************************************************************/
279
463
/************************************************************************/
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;
489
OGREnvelope sEnvelop;
490
int bGotBounds = FALSE;
491
int nXSize = 0, nYSize = 0;
493
GDALProgressFunc pfnProgress = GDALTermProgress;
494
OGRSpatialReferenceH hSRS = NULL;
495
int bTargetAlignedPixels = FALSE;
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"));
525
else if( EQUAL(argv[i],"-q") || EQUAL(argv[i],"-quiet") )
528
pfnProgress = GDALDummyProgress;
323
530
else if( EQUAL(argv[i],"-a") && i < argc-1 )
325
532
pszBurnAttribute = argv[++i];
327
534
else if( EQUAL(argv[i],"-b") && i < argc-1 )
329
anBandList.push_back( atoi(argv[++i]) );
536
if (strchr(argv[i+1], ' '))
538
char** papszTokens = CSLTokenizeString( argv[i+1] );
539
char** papszIter = papszTokens;
540
while(papszIter && *papszIter)
542
anBandList.push_back(atoi(*papszIter));
545
CSLDestroy(papszTokens);
550
while(i < argc-1 && ArgIsNumeric(argv[i+1]))
552
anBandList.push_back(atoi(argv[i+1]));
331
557
else if( EQUAL(argv[i],"-3d") )
360
605
pszSQL = argv[++i];
607
else if( EQUAL(argv[i],"-of") && i < argc-1 )
609
pszFormat = argv[++i];
610
bFormatExplicitelySet = TRUE;
611
bCreateOutput = TRUE;
613
else if( EQUAL(argv[i],"-init") && i < argc - 1 )
615
if (strchr(argv[i+1], ' '))
617
char** papszTokens = CSLTokenizeString( argv[i+1] );
618
char** papszIter = papszTokens;
619
while(papszIter && *papszIter)
621
adfInitVals.push_back(atof(*papszIter));
624
CSLDestroy(papszTokens);
629
while(i < argc-1 && ArgIsNumeric(argv[i+1]))
631
adfInitVals.push_back(atof(argv[i+1]));
635
bCreateOutput = TRUE;
637
else if( EQUAL(argv[i],"-a_nodata") && i < argc - 1 )
639
dfNoData = atof(argv[i+1]);
642
bCreateOutput = TRUE;
644
else if( EQUAL(argv[i],"-a_srs") && i < argc-1 )
646
hSRS = OSRNewSpatialReference( NULL );
648
if( OSRSetFromUserInput(hSRS, argv[i+1]) != OGRERR_NONE )
650
fprintf( stderr, "Failed to process SRS definition: %s\n",
656
bCreateOutput = TRUE;
659
else if( EQUAL(argv[i],"-te") && i < argc - 4 )
661
sEnvelop.MinX = atof(argv[++i]);
662
sEnvelop.MinY = atof(argv[++i]);
663
sEnvelop.MaxX = atof(argv[++i]);
664
sEnvelop.MaxY = atof(argv[++i]);
666
bCreateOutput = TRUE;
668
else if( EQUAL(argv[i],"-a_ullr") && i < argc - 4 )
670
sEnvelop.MinX = atof(argv[++i]);
671
sEnvelop.MaxY = atof(argv[++i]);
672
sEnvelop.MaxX = atof(argv[++i]);
673
sEnvelop.MinY = atof(argv[++i]);
675
bCreateOutput = TRUE;
677
else if( EQUAL(argv[i],"-co") && i < argc-1 )
679
papszCreateOptions = CSLAddString( papszCreateOptions, argv[++i] );
680
bCreateOutput = TRUE;
682
else if( EQUAL(argv[i],"-ot") && i < argc-1 )
686
for( iType = 1; iType < GDT_TypeCount; iType++ )
688
if( GDALGetDataTypeName((GDALDataType)iType) != NULL
689
&& EQUAL(GDALGetDataTypeName((GDALDataType)iType),
692
eOutputType = (GDALDataType) iType;
696
if( eOutputType == GDT_Unknown )
698
printf( "Unknown output pixel type: %s\n", argv[i+1] );
702
bCreateOutput = TRUE;
704
else if( (EQUAL(argv[i],"-ts") || EQUAL(argv[i],"-outsize")) && i < argc-2 )
706
nXSize = atoi(argv[++i]);
707
nYSize = atoi(argv[++i]);
708
if (nXSize <= 0 || nYSize <= 0)
710
printf( "Wrong value for -outsize parameters\n");
713
bCreateOutput = TRUE;
715
else if( EQUAL(argv[i],"-tr") && i < argc-2 )
717
dfXRes = atof(argv[++i]);
718
dfYRes = fabs(atof(argv[++i]));
719
if( dfXRes == 0 || dfYRes == 0 )
721
printf( "Wrong value for -tr parameters\n");
724
bCreateOutput = TRUE;
726
else if( EQUAL(argv[i],"-tap") )
728
bTargetAlignedPixels = TRUE;
729
bCreateOutput = TRUE;
362
731
else if( pszSrcFilename == NULL )
364
733
pszSrcFilename = argv[i];
392
if( anBandList.size() == 0 )
393
anBandList.push_back( 1 );
757
if( dfXRes == 0 && dfYRes == 0 && nXSize == 0 && nYSize == 0 )
759
fprintf( stderr, "'-tr xres yes' or '-ts xsize ysize' is required.\n\n" );
763
if (bTargetAlignedPixels && dfXRes == 0 && dfYRes == 0)
765
fprintf( stderr, "-tap option cannot be used without using -tr\n");
769
if( anBandList.size() != 0 )
771
fprintf( stderr, "-b option cannot be used when creating a GDAL dataset.\n\n" );
777
if (adfBurnValues.size() != 0)
778
nBandCount = adfBurnValues.size();
780
if ((int)adfInitVals.size() > nBandCount)
781
nBandCount = adfInitVals.size();
783
if (adfInitVals.size() == 1)
785
for(i=1;i<=nBandCount - 1;i++)
786
adfInitVals.push_back( adfInitVals[0] );
790
for(i=1;i<=nBandCount;i++)
791
anBandList.push_back( i );
795
if( anBandList.size() == 0 )
796
anBandList.push_back( 1 );
395
799
/* -------------------------------------------------------------------- */
396
800
/* Open source vector dataset. */
812
if( pszSQL == NULL && papszLayers == NULL )
814
if( OGR_DS_GetLayerCount(hSrcDS) == 1 )
816
papszLayers = CSLAddString(NULL, OGR_L_GetName(OGR_DS_GetLayer(hSrcDS, 0)));
820
fprintf( stderr, "At least one of -l or -sql required.\n\n" );
408
825
/* -------------------------------------------------------------------- */
409
826
/* Open target raster file. Eventually we will add optional */
411
828
/* -------------------------------------------------------------------- */
414
hDstDS = GDALOpen( pszDstFilename, GA_Update );
829
GDALDatasetH hDstDS = NULL;
833
/* -------------------------------------------------------------------- */
834
/* Find the output driver. */
835
/* -------------------------------------------------------------------- */
836
hDriver = GDALGetDriverByName( pszFormat );
838
|| GDALGetMetadataItem( hDriver, GDAL_DCAP_CREATE, NULL ) == NULL )
842
printf( "Output driver `%s' not recognised or does not support\n",
844
printf( "direct output file creation. The following format drivers are configured\n"
845
"and support direct output:\n" );
847
for( iDr = 0; iDr < GDALGetDriverCount(); iDr++ )
849
GDALDriverH hDriver = GDALGetDriver(iDr);
851
if( GDALGetMetadataItem( hDriver, GDAL_DCAP_CREATE, NULL) != NULL )
854
GDALGetDriverShortName( hDriver ),
855
GDALGetDriverLongName( hDriver ) );
862
if (!bQuiet && !bFormatExplicitelySet)
863
CheckExtensionConsistency(pszDstFilename, pszFormat);
867
hDstDS = GDALOpen( pszDstFilename, GA_Update );
418
872
/* -------------------------------------------------------------------- */
419
873
/* Process SQL request. */
425
879
hLayer = OGR_DS_ExecuteSQL( hSrcDS, pszSQL, NULL, NULL );
426
880
if( hLayer != NULL )
428
ProcessLayer( hLayer, hDstDS, anBandList,
884
std::vector<OGRLayerH> ahLayers;
885
ahLayers.push_back(hLayer);
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);
897
ProcessLayer( hLayer, hSRS != NULL, hDstDS, anBandList,
429
898
adfBurnValues, b3D, bInverse, pszBurnAttribute,
430
papszRasterizeOptions );
899
papszRasterizeOptions, pfnProgress, NULL );
901
OGR_DS_ReleaseResultSet( hSrcDS, hLayer );
905
/* -------------------------------------------------------------------- */
906
/* Create output file if necessary. */
907
/* -------------------------------------------------------------------- */
908
int nLayerCount = CSLCount(papszLayers);
910
if (bCreateOutput && hDstDS == NULL)
912
std::vector<OGRLayerH> ahLayers;
914
for( i = 0; i < nLayerCount; i++ )
916
OGRLayerH hLayer = OGR_DS_GetLayerByName( hSrcDS, papszLayers[i] );
921
ahLayers.push_back(hLayer);
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);
434
934
/* -------------------------------------------------------------------- */
435
935
/* Process each layer. */
436
936
/* -------------------------------------------------------------------- */
437
int nLayerCount = CSLCount(papszLayers);
438
938
for( i = 0; i < nLayerCount; i++ )
440
940
OGRLayerH hLayer = OGR_DS_GetLayerByName( hSrcDS, papszLayers[i] );