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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
|
/* $Id: gdal_tutorial.dox,v 1.9 2006/03/27 15:16:01 fwarmerdam Exp $ */
/*!
\page gdal_tutorial GDAL API Tutorial
\section gdal_tutorial_open Opening the File
Before opening a GDAL supported raster datastore it is necessary to
register drivers. There is a driver for each supported format. Normally
this is accomplished with the GDALAllRegister() function which attempts
to register all known drivers, including those auto-loaded from .so files
using GDALDriverManager::AutoLoadDrivers(). If for some applications it
is necessary to limit the set of drivers it may be helpful to review
the code from <a href="gdalallregister.cpp.html">gdalallregister.cpp</a>.
Once the drivers are registered, the application should call the free
standing GDALOpen() function to open a dataset, passing the name of the
dataset and the access desired (GA_ReadOnly or GA_Update).
In C++:
\code
#include "gdal_priv.h"
int main()
{
GDALDataset *poDataset;
GDALAllRegister();
poDataset = (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly );
if( poDataset == NULL )
{
...;
}
\endcode
In C:
\code
#include "gdal.h"
int main()
{
GDALDatasetH hDataset;
GDALAllRegister();
hDataset = GDALOpen( pszFilename, GA_ReadOnly );
if( hDataset == NULL )
{
...;
}
\endcode
In Python:
\code
import gdal
from gdalconst import *
dataset = gdal.Open( filename, GA_ReadOnly )
if dataset is None:
...
\endcode
Note that if GDALOpen() returns NULL it means the open failed, and that
an error messages will already have been emitted via CPLError(). If you want
to control how errors are reported to the user review the CPLError()
documentation. Generally speaking all of GDAL uses CPLError() for error
reporting. Also, note that pszFilename need not actually be the name
of a physical file (though it usually is). It's interpretation is driver
dependent, and it might be an URL, a filename with additional parameters
added at the end controlling the open or almost anything. Please try not
to limit GDAL file selection dialogs to only selecting physical files.
\section gdal_tutorial_dataset Getting Dataset Information
As described in the <a href="gdal_datamodel.html">GDAL Data Model</a>, a
GDALDataset contains a list of raster bands, all pertaining to the same
area, and having the same resolution. It also has metadata, a coordinate
system, a georeferencing transform, size of raster and various other
information.
\code
adfGeoTransform[0] /* top left x */
adfGeoTransform[1] /* w-e pixel resolution */
adfGeoTransform[2] /* rotation, 0 if image is "north up" */
adfGeoTransform[3] /* top left y */
adfGeoTransform[4] /* rotation, 0 if image is "north up" */
adfGeoTransform[5] /* n-s pixel resolution */
\endcode
If we wanted to print some general information about the
dataset we might do the following:
In C++:
\code
double adfGeoTransform[6];
printf( "Driver: %s/%s\n",
poDataset->GetDriver()->GetDescription(),
poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
printf( "Size is %dx%dx%d\n",
poDataset->GetRasterXSize(), poDataset->GetRasterYSize(),
poDataset->GetRasterCount() );
if( poDataset->GetProjectionRef() != NULL )
printf( "Projection is `%s'\n", poDataset->GetProjectionRef() );
if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None )
{
printf( "Origin = (%.6f,%.6f)\n",
adfGeoTransform[0], adfGeoTransform[3] );
printf( "Pixel Size = (%.6f,%.6f)\n",
adfGeoTransform[1], adfGeoTransform[5] );
}
\endcode
In C:
\code
GDALDriverH hDriver;
double adfGeoTransform[6];
hDriver = GDALGetDatasetDriver( hDataset );
printf( "Driver: %s/%s\n",
GDALGetDriverShortName( hDriver ),
GDALGetDriverLongName( hDriver ) );
printf( "Size is %dx%dx%d\n",
GDALGetRasterXSize( hDataset ),
GDALGetRasterYSize( hDataset ),
GDALGetRasterCount( hDataset ) );
if( GDALGetProjectionRef( hDataset ) != NULL )
printf( "Projection is `%s'\n", GDALGetProjectionRef( hDataset ) );
if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None )
{
printf( "Origin = (%.6f,%.6f)\n",
adfGeoTransform[0], adfGeoTransform[3] );
printf( "Pixel Size = (%.6f,%.6f)\n",
adfGeoTransform[1], adfGeoTransform[5] );
}
\endcode
In Python:
\code
print 'Driver: ', dataset.GetDriver().ShortName,'/', \
dataset.GetDriver().LongName
print 'Size is ',dataset.RasterXSize,'x',dataset.RasterYSize, \
'x',dataset.RasterCount
print 'Projection is ',dataset.GetProjection()
geotransform = dataset.GetGeoTransform()
if not geotransform is None:
print 'Origin = (',geotransform[0], ',',geotransform[3],')'
print 'Pixel Size = (',geotransform[1], ',',geotransform[5],')'
\endcode
\section gdal_tutorial_band Fetching a Raster Band
At this time access to raster data via GDAL is done one band at a time.
Also, there is metadata, blocksizes, color tables, and various other
information available on a band by band basis. The following codes fetches
a GDALRasterBand object from the dataset (numbered 1 through GetRasterCount())
and displays a little information about it.
In C++:
\code
GDALRasterBand *poBand;
int nBlockXSize, nBlockYSize;
int bGotMin, bGotMax;
double adfMinMax[2];
poBand = poDataset->GetRasterBand( 1 );
poBand->GetBlockSize( &nBlockXSize, &nBlockYSize );
printf( "Block=%dx%d Type=%s, ColorInterp=%s\n",
nBlockXSize, nBlockYSize,
GDALGetDataTypeName(poBand->GetRasterDataType()),
GDALGetColorInterpretationName(
poBand->GetColorInterpretation()) );
adfMinMax[0] = poBand->GetMinimum( &bGotMin );
adfMinMax[1] = poBand->GetMaximum( &bGotMax );
if( ! (bGotMin && bGotMax) )
GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);
printf( "Min=%.3fd, Max=%.3f\n", adfMinMax[0], adfMinMax[1] );
if( poBand->GetOverviewCount() > 0 )
printf( "Band has %d overviews.\n", poBand->GetOverviewCount() );
if( poBand->GetColorTable() != NULL )
printf( "Band has a color table with %d entries.\n",
poBand->GetColorTable()->GetColorEntryCount() );
\endcode
In C:
\code
GDALRasterBandH hBand;
int nBlockXSize, nBlockYSize;
int bGotMin, bGotMax;
double adfMinMax[2];
hBand = GDALGetRasterBand( hDataset, 1 );
GDALGetBlockSize( hBand, &nBlockXSize, &nBlockYSize );
printf( "Block=%dx%d Type=%s, ColorInterp=%s\n",
nBlockXSize, nBlockYSize,
GDALGetDataTypeName(GDALGetRasterDataType(hBand)),
GDALGetColorInterpretationName(
GDALGetRasterColorInterpretation(hBand)) );
adfMinMax[0] = GDALGetRasterMinimum( hBand, &bGotMin );
adfMinMax[1] = GDALGetRasterMaximum( hBand, &bGotMax );
if( ! (bGotMin && bGotMax) )
GDALComputeRasterMinMax( hBand, TRUE, adfMinMax );
printf( "Min=%.3fd, Max=%.3f\n", adfMinMax[0], adfMinMax[1] );
if( GDALGetOverviewCount(hBand) > 0 )
printf( "Band has %d overviews.\n", GDALGetOverviewCount(hBand));
if( GDALGetRasterColorTable( hBand ) != NULL )
printf( "Band has a color table with %d entries.\n",
GDALGetColorEntryCount(
GDALGetRasterColorTable( hBand ) ) );
\endcode
In Python (note several bindings are missing):
\code
band = dataset.GetRasterBand(1)
print 'Band Type=',gdal.GetDataTypeName(band.DataType)
min = band.GetMinimum()
max = band.GetMaximum()
if min is not None and max is not None:
(min,max) = ComputeRasterMinMax(1)
print 'Min=%.3f, Max=%.3f' % (min,max)
if band.GetOverviewCount() > 0:
print 'Band has ', band.GetOverviewCount(), ' overviews.'
if not band.GetRasterColorTable() is None:
print 'Band has a color table with ', \
band.GetRasterColorTable().GetCount(), ' entries.'
\endcode
\section gdal_tutorial_read Reading Raster Data
There are a few ways to read raster data, but the most common is via
the GDALRasterBand::RasterIO() method. This method will automatically take
care of data type conversion, up/down sampling and windowing. The following
code will read the first scanline of data into a similarly sized buffer,
converting it to floating point as part of the operation.
In C++:
\code
float *pafScanline;
int nXSize = poBand->GetXSize();
pafScanline = (float *) CPLMalloc(sizeof(float)*nXSize);
poBand->RasterIO( GF_Read, 0, 0, nXSize, 1,
pafScanline, nXSize, 1, GDT_Float32,
0, 0 );
\endcode
In C:
\code
float *pafScanline;
int nXSize = GDALGetRasterBandXSize( hBand );
pafScanline = (float *) CPLMalloc(sizeof(float)*nXSize);
GDALRasterIO( hBand, GF_Read, 0, 0, nXSize, 1,
pafScanline, nXSize, 1, GDT_Float32,
0, 0 );
\endcode
In Python:
\code
scanline = band.ReadRaster( 0, 0, band.XSize, 1, \
band.XSize, 1, GDT_Float32 )
\endcode
Note that the returned scanline is of type string, and contains
xsize*4 bytes of raw binary floating point data. This can be converted
to Python values using the <b>struct</b> module from the standard
library:
\code
import struct
tuple_of_floats = struct.unpack('f' * b2.XSize, scanline)
\endcode
The RasterIO call takes the following arguments.
\code
CPLErr GDALRasterBand::RasterIO( GDALRWFlag eRWFlag,
int nXOff, int nYOff, int nXSize, int nYSize,
void * pData, int nBufXSize, int nBufYSize,
GDALDataType eBufType,
int nPixelSpace,
int nLineSpace )
\endcode
Note that the same RasterIO() call is used to read, or write based on
the setting of eRWFlag (either GF_Read or GF_Write). The nXOff, nYOff,
nXSize, nYSize argument describe the window of raster data on disk to
read (or write). It doesn't have to fall on tile boundaries though access
may be more efficient if it does.
The pData is the memory buffer the data is read into, or written from. It's
real type must be whatever is passed as eBufType, such as GDT_Float32, or
GDT_Byte. The RasterIO() call will take care of converting between the
buffer's data type and the data type of the band. Note that when converting
floating point data to integer RasterIO() rounds down, and when converting
source values outside the legal range of the output the nearest legal value
is used. This implies, for instance, that 16bit data read into a GDT_Byte
buffer will map all values greater than 255 to 255, <b>the data is not
scaled!</b>
The nBufXSize and nBufYSize
values describe the size of the buffer. When loading data at full resolution
this would be the same as the window size. However, to load a reduced
resolution overview this could be set to smaller than the window on disk. In
this case the RasterIO() will utilize overviews to do the IO more efficiently
if the overviews are suitable.
The nPixelSpace, and nLineSpace are normally zero indicating that default
values should be used. However, they can be used to control access to
the memory data buffer, allowing reading into a buffer containing other
pixel interleaved data for instance.
\section gdal_tutorial_close Closing the Dataset
Please keep in mind that GDALRasterBand objects are <i>owned</i> by their
dataset, and they should never be destroyed with the C++ delete operator.
GDALDataset's can be closed either by calling GDALClose() or using the
delete operator on the GDALDataset. Either will result in proper cleanup,
and flushing of any pending writes.
\section gdal_tutorial_creation Techniques for Creating Files
New files in GDAL supported formats may be created if the format driver
supports creation. There are two general techniques for creating files,
using CreateCopy() and Create().
The CreateCopy method involves calling the CreateCopy() method on the
format driver, and passing in a source dataset that should be copied. The
Create method involves calling the Create() method on the driver, and then
explicitly writing all the metadata, and raster data with separate calls.
All drivers that support creating new files support the CreateCopy() method,
but only a few support the Create() method.
To determine if a particular format supports Create or CreateCopy
it is possible to check the DCAP_CREATE and DCAP_CREATECOPY metadata on the
format driver object. Ensure that GDALAllRegister() has been called before
calling GetDriverByName(). In this example we
In C++:
\code
#include "cpl_string.h"
...
const char *pszFormat = "GTiff";
GDALDriver *poDriver;
char **papszMetadata;
poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
if( poDriver == NULL )
exit( 1 );
papszMetadata = poDriver->GetMetadata();
if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ) )
printf( "Driver %s supports Create() method.\n", pszFormat );
if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATECOPY, FALSE ) )
printf( "Driver %s supports CreateCopy() method.\n", pszFormat );
\endcode
In C:
\code
#include "cpl_string.h"
...
const char *pszFormat = "GTiff";
GDALDriver hDriver = GDALGetDriverByName( pszFormat );
char **papszMetadata;
if( hDriver == NULL )
exit( 1 );
papszMetadata = GDALGetMetadata( hDriver, NULL );
if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ) )
printf( "Driver %s supports Create() method.\n", pszFormat );
if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATECOPY, FALSE ) )
printf( "Driver %s supports CreateCopy() method.\n", pszFormat );
\endcode
In Python:
\code
format = "GTiff"
driver = gdal.GetDriverByName( format )
metadata = driver.GetMetadata()
if metadata.has_key(gdal.DCAP_CREATE) \
and metadata[gdal.DCAP_CREATE] == 'YES':
print 'Driver %s supports Create() method.' % format
if metadata.has_key(gdal.DCAP_CREATECOPY) \
and metadata[gdal.DCAP_CREATECOPY] == 'YES':
print 'Driver %s supports CreateCopy() method.' % format
\endcode
Note that a number of drivers are read-only and won't support Create()
or CreateCopy().
\section gdal_tutorial_createcopy Using CreateCopy()
The GDALDriver::CreateCopy() method can be used fairly simply as most
information is collected from the source dataset. However, it includes
options for passing format specific creation options, and for reporting
progress to the user as a long dataset copy takes place. A simple copy
from the a file named pszSrcFilename, to a new file named pszDstFilename
using default options on a format whose driver was previously fetched might
look like this:
In C++:
\code
GDALDataset *poSrcDS =
(GDALDataset *) GDALOpen( pszSrcFilename, GA_ReadOnly );
GDALDataset *poDstDS;
poDstDS = poDriver->CreateCopy( pszDstFilename, poSrcDS, FALSE,
NULL, NULL, NULL );
if( poDstDS != NULL )
delete poDstDS;
\endcode
In C:
\code
GDALDatasetH hSrcDS = GDALOpen( pszSrcFilename, GA_ReadOnly );
GDALDatasetH hDstDS;
hDstDS = GDALCreateCopy( hDriver, pszDstFilename, hSrcDS, FALSE,
NULL, NULL, NULL );
if( hDstDS != NULL )
GDALClose( hDstDS );
\endcode
In Python:
\code
src_ds = gdal.Open( src_filename )
dst_ds = driver.CreateCopy( dst_filename, src_ds, 0 )
\endcode
Note that the CreateCopy() method returns a writeable dataset, and that it
must be closed properly to complete writing and flushing the dataset to disk.
In the Python case this occurs automatically when "dst_ds" goes out of scope.
The FALSE (or 0) value used for the bStrict option just after the destination
filename in the CreateCopy() call indicates that the CreateCopy() call
should proceed without a fatal error even if the destination dataset cannot
be created to exactly match the input dataset. This might be because the
output format does not support the pixel datatype of the input dataset, or
because the destination cannot support writing georeferencing for instance.
A more complex case might involve passing creation options, and using a
predefined progress monitor like this:
In C++:
\code
#include "cpl_string.h"
...
char **papszOptions = NULL;
papszOptions = CSLSetNameValue( papszOptions, "TILED", "YES" );
papszOptions = CSLSetNameValue( papszOptions, "COMPRESS", "PACKBITS" );
poDstDS = poDriver->CreateCopy( pszDstFilename, poSrcDS, FALSE,
papszOptions, GDALTermProgress, NULL );
if( poDstDS != NULL )
delete poDstDS;
\endcode
In C:
\code
#include "cpl_string.h"
...
char **papszOptions = NULL;
papszOptions = CSLSetNameValue( papszOptions, "TILED", "YES" );
papszOptions = CSLSetNameValue( papszOptions, "COMPRESS", "PACKBITS" );
hDstDS = GDALCreateCopy( hDriver, pszDstFilename, hSrcDS, FALSE,
papszOptions, GDALTermProgres, NULL );
if( hDstDS != NULL )
GDALClose( hDstDS );
\endcode
In Python:
\code
src_ds = gdal.Open( src_filename )
dst_ds = driver.CreateCopy( dst_filename, src_ds, 0,
[ 'TILED=YES', 'COMPRESS=PACKBITS' ] )
\endcode
\section gdal_tutorial_create Using Create()
For situations in which you are not just exporting an existing file to a new
file, it is generally necessary to use the GDALDriver::Create() method (though
some interesting options are possible through use of virtual files or in-memory
files). The Create() method takes an options list much like CreateCopy(),
but the image size, number of bands and band type must be provided explicitly.
<p>
In C++:
\code
GDALDataset *poDstDS;
char **papszOptions = NULL;
poDstDS = poDriver->Create( pszDstFilename, 512, 512, 1, GDT_Byte,
papszOptions );
\endcode
In C:
\code
GDALDatasetH hDstDS;
char **papszOptions = NULL;
hDstDS = GDALCreate( hDriver, pszDstFilename, 512, 512, 1, GDT_Byte,
papszOptions );
\endcode
In Python:
\code
dst_ds = driver.Create( dst_filename, 512, 512, 1, gdal.GDT_Byte )
\endcode
Once the dataset is successfully created, all appropriate metadata and raster
data must be written to the file. What this is will vary according to usage,
but a simple case with a projection, geotransform and raster data is covered
here.<p>
In C++:
\code
double adfGeoTransform[6] = { 444720, 30, 0, 3751320, 0, -30 };
OGRSpatialReference oSRS;
char *pszSRS_WKT = NULL;
GDALRasterBand *poBand;
GByte abyRaster[512*512];
poDstDS->SetGeoTransform( adfGeoTransform );
oSRS.SetUTM( 11, TRUE );
oSRS.SetWellKnownGeogCS( "NAD27" );
oSRS.exportToWkt( &pszSRS_WKT );
poDstDS->SetProjection( pszSRS_WKT );
CPLFree( pszSRS_WKT );
poBand = poDstDS->GetRasterBand(1);
poBand->RasterIO( GF_Write, 0, 0, 512, 512,
abyRaster, 512, 512, GDT_Byte, 0, 0 );
delete poDstDS;
\endcode
In C:
\code
double adfGeoTransform[6] = { 444720, 30, 0, 3751320, 0, -30 };
OGRSpatialReferenceH hSRS;
char *pszSRS_WKT = NULL;
GDALRasterBandH hBand;
GByte abyRaster[512*512];
GDALSetGeoTransform( hDstDS, adfGeoTransform );
hSRS = OSRNewSpatialReference( NULL );
OSRSetUTM( hSRS, 11, TRUE );
OSRSetWellKnownGeogCS( hSRS, "NAD27" );
OSRExportToWkt( hSRS, &pszSRS_WKT );
OSRDestroySpatialReference( hSRS );
GDALSetProjection( hDstDS, pszSRS_WKT );
CPLFree( pszSRS_WKT );
hBand = GDALGetRasterBand( hDstDS, 1 );
GDALRasterIO( hBand, GF_Write, 0, 0, 512, 512,
abyRaster, 512, 512, GDT_Byte, 0, 0 );
GDALClose( hDstDS );
\endcode
In Python:
\code
import Numeric, osr
dst_ds.SetGeoTransform( [ 444720, 30, 0, 3751320, 0, -30 ] )
srs = osr.SpatialReference()
srs.SetUTM( 11, 1 )
srs.SetWellKnownGeogCS( 'NAD27' )
dst_ds.SetProjection( srs.ExportToWkt() )
raster = Numeric.zeros( (512, 512) )
dst_ds.GetRasterBand(1).WriteArray( raster )
\endcode
*/
|