GDAL
GDAL Driver Implementation Tutorial

Overall Approach

In general new formats are added to GDAL by implementing format specific drivers as subclasses of GDALDataset, and band accessors as subclasses of GDALRasterBand. As well, a GDALDriver instance is created for the format, and registered with the GDALDriverManager, to ensure that the system knows about the format.

This tutorial will start with implementing a simple read-only driver (based on the JDEM driver), and then proceed to utilizing the RawRasterBand helper class, implementing creatable and updatable formats, and some esoteric issues.

It is strongly advised that the GDAL Data Model description be reviewed and understood before attempting to implement a GDAL driver.

Contents

  1. Implementing the Dataset
  2. Implementing the RasterBand
  3. The Driver
  4. Adding Driver to GDAL Tree
  5. Adding Georeferencing
  6. Overviews
  7. File Creation
  8. RawDataset/RawRasterBand Helper Classes
  9. Metadata, and Other Exotic Extensions

Implementing the Dataset

We will start showing minimal implementation of a read-only driver for the Japanese DEM format (jdemdataset.cpp). First we declare a format specific dataset class, JDEMDataset in this case.

class JDEMDataset : public GDALPamDataset
{
friend class JDEMRasterBand;
FILE *fp;
GByte abyHeader[1012];
public:
~JDEMDataset();
static int Identify( GDALOpenInfo * );
CPLErr GetGeoTransform( double * padfTransform );
const char *GetProjectionRef();
};

In general we provide capabilities for a driver, by overriding the various virtual methods on the GDALDataset base class. However, the Open() method is special. This is not a virtual method on the base class, and we will need a freestanding function for this operation, so we declare it static. Implementing it as a method in the JDEMDataset class is convenient because we have privileged access to modify the contents of the database object.

The open method itself may look something like this:

GDALDataset *JDEMDataset::Open( GDALOpenInfo *poOpenInfo )
{
// Confirm that the header is compatible with a JDEM dataset.
if( !Identify(poOpenInfo) )
return NULL;
// Confirm the requested access is supported.
if( poOpenInfo->eAccess == GA_Update )
{
"The JDEM driver does not support update access to existing "
"datasets.");
return NULL;
}
// Check that the file pointer from GDALOpenInfo* is available
if( poOpenInfo->fpL == NULL )
{
return NULL;
}
// Create a corresponding GDALDataset.
JDEMDataset *poDS = new JDEMDataset();
// Borrow the file pointer from GDALOpenInfo*.
poDS->fp = poOpenInfo->fpL;
poOpenInfo->fpL = NULL;
// Read the header.
VSIFReadL(poDS->abyHeader, 1, 1012, poDS->fp);
poDS->nRasterXSize =
JDEMGetField(reinterpret_cast<char *>(poDS->abyHeader) + 23, 3);
poDS->nRasterYSize =
JDEMGetField(reinterpret_cast<char *>(poDS->abyHeader) + 26, 3);
if( poDS->nRasterXSize <= 0 || poDS->nRasterYSize <= 0 )
{
"Invalid dimensions : %d x %d",
poDS->nRasterXSize, poDS->nRasterYSize);
delete poDS;
return NULL;
}
// Create band information objects.
poDS->SetBand(1, new JDEMRasterBand(poDS, 1));
// Initialize any PAM information.
poDS->SetDescription(poOpenInfo->pszFilename);
poDS->TryLoadXML();
// Initialize default overviews.
poDS->oOvManager.Initialize(poDS, poOpenInfo->pszFilename);
return poDS;
}
\code
The first step in any database Open function is to verify that the file
being passed is in fact of the type this driver is for. It is important
to realize that each driver's Open function is called in turn till one
succeeds. Drivers must quietly return NULL if the passed file is not of
their format. They should only produce an error if the file does appear to
be of their supported format, but is for some reason unsupported or corrupt.
The information on the file to be opened is passed in contained in a
GDALOpenInfo object. The GDALOpenInfo includes the following public
data members:
\code
char *pszFilename;
char** papszOpenOptions;
GDALAccess eAccess; // GA_ReadOnly or GA_Update
int nOpenFlags;
int bStatOK;
int bIsDirectory;
VSILFILE *fpL;
int nHeaderBytes;
GByte *pabyHeader;

The driver can inspect these to establish if the file is supported. If the pszFilename refers to an object in the file system, the bStatOK flag will be set to TRUE. As well, if the file was successfully opened, the first kilobyte or so is read in, and put in pabyHeader, with the exact size in nHeaderBytes.

In this typical testing example it is verified that the file was successfully opened, that we have at least enough header information to perform our test, and that various parts of the header are as expected for this format. In this case, there are no magic numbers for JDEM format so we check various date fields to ensure they have reasonable century values. If the test fails, we quietly return NULL indicating this file isn't of our supported format.

The identification is in fact delegated to a Identify() static function :

/************************************************************************/
/* Identify() */
/************************************************************************/
int JDEMDataset::Identify( GDALOpenInfo * poOpenInfo )
{
// Confirm that the header has what appears to be dates in the
// expected locations. Sadly this is a relatively weak test.
if( poOpenInfo->nHeaderBytes < 50 )
return FALSE;
// Check if century values seem reasonable.
const char *psHeader = reinterpret_cast<char *>(poOpenInfo->pabyHeader);
if( (!EQUALN(psHeader + 11, "19", 2) &&
!EQUALN(psHeader + 11, "20", 2)) ||
(!EQUALN(psHeader + 15, "19", 2) &&
!EQUALN(psHeader + 15, "20", 2)) ||
(!EQUALN(psHeader + 19, "19", 2) &&
!EQUALN(psHeader + 19, "20", 2)) )
{
return FALSE;
}
return TRUE;
}
\code
It is important to make the <i>is this my format</i> test as stringent as
possible. In this particular case the test is weak, and a file that happened
to have 19s or 20s at a few locations could be erroneously recognized as
JDEM format, causing it to not be handled properly.
Once we are satisfied that the file is of our format, we can do any other
tests that are necessary to validate the file is usable, and in particular
that we can provide the level of access desired. Since the JDEM driver does
not provide update support, error out in that case.
\code
if( poOpenInfo->eAccess == GA_Update )
{
"The JDEM driver does not support update access to existing "
"datasets.");
return NULL;
}

Next we need to create an instance of the database class in which we will set various information of interest.

// Check that the file pointer from GDALOpenInfo* is available.
if( poOpenInfo->fpL == NULL )
{
return NULL;
}
JDEMDataset *poDS = new JDEMDataset();
// Borrow the file pointer from GDALOpenInfo*.
poDS->fp = poOpenInfo->fpL;
poOpenInfo->fpL = NULL;

At this point we "borrow" the file handle that was held by GDALOpenInfo*. This file pointer uses the VSI*L GDAL API to access files on disk. This virtualized POSIX-style API allows some special capabilities like supporting large files, in-memory files and zipped files.

Next the X and Y size are extracted from the header. The nRasterXSize and nRasterYSize are data fields inherited from the GDALDataset base class, and must be set by the Open() method.

VSIFReadL(poDS->abyHeader, 1, 1012, poDS->fp);
poDS->nRasterXSize =
JDEMGetField(reinterpret_cast<char *>(poDS->abyHeader) + 23, 3);
poDS->nRasterYSize =
JDEMGetField(reinterpret_cast<char *>(poDS->abyHeader) + 26, 3);
if (poDS->nRasterXSize <= 0 || poDS->nRasterYSize <= 0 )
{
"Invalid dimensions : %d x %d",
poDS->nRasterXSize, poDS->nRasterYSize);
delete poDS;
return NULL;
}

All the bands related to this dataset must be created and attached using the SetBand() method. We will explore the JDEMRasterBand() class shortly.

// Create band information objects.
poDS->SetBand(1, new JDEMRasterBand(poDS, 1));

Finally we assign a name to the dataset object, and call the GDALPamDataset TryLoadXML() method which can initialize auxiliary information from an .aux.xml file if available. For more details on these services review the GDALPamDataset and related classes.

// Initialize any PAM information.
poDS->SetDescription( poOpenInfo->pszFilename );
poDS->TryLoadXML();
return poDS;
}

Implementing the RasterBand

Similar to the customized JDEMDataset class subclassed from GDALDataset, we also need to declare and implement a customized JDEMRasterBand derived from GDALRasterBand for access to the band(s) of the JDEM file. For JDEMRasterBand the declaration looks like this:

class JDEMRasterBand : public GDALPamRasterBand
{
public:
JDEMRasterBand( JDEMDataset *, int );
virtual CPLErr IReadBlock( int, int, void * );
};

The constructor may have any signature, and is only called from the Open() method. Other virtual methods, such as IReadBlock() must be exactly matched to the method signature in gdal_priv.h.

The constructor implementation looks like this:

JDEMRasterBand::JDEMRasterBand( JDEMDataset *poDSIn, int nBandIn )
{
poDS = poDSIn;
nBand = nBandIn;
eDataType = GDT_Float32;
nBlockXSize = poDS->GetRasterXSize();
nBlockYSize = 1;
}

The following data members are inherited from GDALRasterBand, and should generally be set in the band constructor.

The full set of possible GDALDataType values are declared in gdal.h, and include GDT_Byte, GDT_UInt16, GDT_Int16, and GDT_Float32. The block size is used to establish a natural or efficient block size to access the data with. For tiled datasets this will be the size of a tile, while for most other datasets it will be one scanline, as in this case.

Next we see the implementation of the code that actually reads the image data, IReadBlock().

CPLErr JDEMRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff,
void * pImage )
{
JDEMDataset *poGDS = static_cast<JDEMDataset *>(poDS);
int nRecordSize = nBlockXSize * 5 + 9 + 2;
VSIFSeekL(poGDS->fp, 1011 + nRecordSize*nBlockYOff, SEEK_SET);
char *pszRecord = static_cast<char *>(CPLMalloc(nRecordSize));
VSIFReadL(pszRecord, 1, nRecordSize, poGDS->fp);
if( !EQUALN(reinterpret_cast<char *>(poGDS->abyHeader), pszRecord, 6) )
{
CPLFree(pszRecord);
"JDEM Scanline corrupt. Perhaps file was not transferred "
"in binary mode?");
return CE_Failure;
}
if( JDEMGetField(pszRecord + 6, 3) != nBlockYOff + 1 )
{
CPLFree(pszRecord);
"JDEM scanline out of order, JDEM driver does not "
"currently support partial datasets.");
return CE_Failure;
}
for( int i = 0; i < nBlockXSize; i++ )
((float *) pImage)[i] = JDEMGetField(pszRecord + 9 + 5 * i, 5) * 0.1;
return CE_None;
}

Key items to note are:

The Driver

While the JDEMDataset and JDEMRasterBand are now ready to use to read image data, it still isn't clear how the GDAL system knows about the new driver. This is accomplished via the GDALDriverManager. To register our format we implement a registration function. The declaration goes in gcore/gdal_frmts.h:

void CPL_DLL GDALRegister_JDEM(void);

The definition in the driver file is:

void GDALRegister_JDEM()
{
if( !GDAL_CHECK_VERSION("JDEM") )
return;
if( GDALGetDriverByName("JDEM") != NULL )
return;
GDALDriver *poDriver = new GDALDriver();
poDriver->SetDescription("JDEM");
poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
"Japanese DEM (.mem)");
"frmt_various.html#JDEM");
poDriver->pfnOpen = JDEMDataset::Open;
poDriver->pfnIdentify = JDEMDataset::Identify;
}

Note the use of GDAL_CHECK_VERSION macro (starting with GDAL 1.5.0). This is an optional macro for drivers inside GDAL tree that don't depend on external libraries, but that can be very useful if you compile your driver as a plugin (that is to say, an out-of-tree driver). As the GDAL C++ ABI may, and will, change between GDAL releases (for example from GDAL 1.5.0 to 1.6.0), it may be necessary to recompile your driver against the header files of the GDAL version with which you want to make it work. The GDAL_CHECK_VERSION macro will check that the GDAL version with which the driver was compiled and the version against which it is running are compatible.

The registration function will create an instance of a GDALDriver object when first called, and register it with the GDALDriverManager. The following fields can be set in the driver before registering it with the GDALDriverManager().

Adding Driver to GDAL Tree

Note that the GDALRegister_JDEM() method must be called by the higher level program in order to have access to the JDEM driver. Normal practice when writing new drivers is to:

  1. Add a driver directory under gdal/frmts, with the directory name the same as the short name.

  2. Add a GNUmakefile and makefile.vc in that directory modeled on those from other similar directories (i.e. the jdem directory).

  3. Add the module with the dataset, and rasterband implementation. Generally this is called <short_name>dataset.cpp, with all the GDAL specific code in one file, though that is not required.

  4. Add the registration entry point declaration (i.e. GDALRegister_JDEM()) to gdal/gcore/gdal_frmts.h.

  5. Add a call to the registration function to frmts/gdalallregister.cpp, protected by an appropriate #ifdef.

  6. Add the format short name to the GDAL_FORMATS macro in GDALmake.opt.in (and to GDALmake.opt).

  7. Add a format specific item to the EXTRAFLAGS macro in frmts/makefile.vc.

Once this is all done, it should be possible to rebuild GDAL, and have the new format available in all the utilities. The gdalinfo utility can be used to test that opening and reporting on the format is working, and the gdal_translate utility can be used to test image reading.

Adding Georeferencing

Now we will take the example a step forward, adding georeferencing support. We add the following two virtual method overrides to JDEMDataset, taking care to exactly match the signature of the method on the GDALRasterDataset base class.

CPLErr GetGeoTransform( double * padfTransform );
const char *GetProjectionRef();

The implementation of GetGeoTransform() just copies the usual geotransform matrix into the supplied buffer. Note that GetGeoTransform() may be called a lot, so it isn't generally wise to do a lot of computation in it. In many cases the Open() will collect the geotransform, and this method will just copy it over. Also note that the geotransform return is based on an anchor point at the top left corner of the top left pixel, not the center of pixel approach used in some packages.

CPLErr JDEMDataset::GetGeoTransform( double * padfTransform )
{
const char *psHeader = reinterpret_cast<char *>(abyHeader);
const double dfLLLat = JDEMGetAngle(psHeader + 29);
const double dfLLLong = JDEMGetAngle(psHeader + 36);
const double dfURLat = JDEMGetAngle(psHeader + 43);
const double dfURLong = JDEMGetAngle(psHeader + 50);
padfTransform[0] = dfLLLong;
padfTransform[3] = dfURLat;
padfTransform[1] = (dfURLong - dfLLLong) / GetRasterXSize();
padfTransform[2] = 0.0;
padfTransform[4] = 0.0;
padfTransform[5] = -1 * (dfURLat - dfLLLat) / GetRasterYSize();
return CE_None;
}

The GetProjectionRef() method returns a pointer to an internal string containing a coordinate system definition in OGC WKT format. In this case the coordinate system is fixed for all files of this format, but in more complex cases a definition may need to be composed on the fly, in which case it may be helpful to use the OGRSpatialReference class to help build the definition.

const char *JDEMDataset::GetProjectionRef()
{
return
"GEOGCS[\"Tokyo\",DATUM[\"Tokyo\",SPHEROID[\"Bessel 1841\","
"6377397.155,299.1528128,AUTHORITY[\"EPSG\",7004]],TOWGS84[-148,"
"507,685,0,0,0,0],AUTHORITY[\"EPSG\",6301]],PRIMEM[\"Greenwich\","
"0,AUTHORITY[\"EPSG\",8901]],UNIT[\"DMSH\",0.0174532925199433,"
"AUTHORITY[\"EPSG\",9108]],AXIS[\"Lat\",NORTH],AXIS[\"Long\",EAST],"
"AUTHORITY[\"EPSG\",4301]]";
}

This completes explanation of the features of the JDEM driver. The full source for jdemdataset.cpp can be reviewed as needed.

Overviews

GDAL allows file formats to make pre-built overviews available to applications via the GDALRasterBand::GetOverview() and related methods. However, implementing this is pretty involved, and goes beyond the scope of this document for now. The GeoTIFF driver (gdal/frmts/gtiff/geotiff.cpp) and related source can be reviewed for an example of a file format implementing overview reporting and creation support.

Formats can also report that they have arbitrary overviews, by overriding the HasArbitraryOverviews() method on the GDALRasterBand, returning TRUE. In this case the raster band object is expected to override the RasterIO() method itself, to implement efficient access to imagery with resampling. This is also involved, and there are a lot of requirements for correct implementation of the RasterIO() method. An example of this can be found in the OGDI and ECW formats.

However, by far the most common approach to implementing overviews is to use the default support in GDAL for external overviews stored in TIFF files with the same name as the dataset, but the extension .ovr appended. In order to enable reading and creation of this style of overviews it is necessary for the GDALDataset to initialize the oOvManager object within itself. This is typically accomplished with a call like the following near the end of the Open() method (after the PAM TryLoadXML()).

poDS->oOvManager.Initialize(poDS, poOpenInfo->pszFilename);

This will enable default implementations for reading and creating overviews for the format. It is advised that this be enabled for all simple file system based formats unless there is a custom overview mechanism to be tied into.

File Creation

There are two approaches to file creation. The first method is called the CreateCopy() method, and involves implementing a function that can write a file in the output format, pulling all imagery and other information needed from a source GDALDataset. The second method, the dynamic creation method, involves implementing a Create method to create the shell of the file, and then the application writes various information by calls to set methods.

The benefits of the first method are that that all the information is available at the point the output file is being created. This can be especially important when implementing file formats using external libraries which require information like color maps, and georeferencing information at the point the file is created. The other advantage of this method is that the CreateCopy() method can read some kinds of information, such as min/max, scaling, description and GCPs for which there are no equivalent set methods.

The benefits of the second method are that applications can create an empty new file, and write results to it as they become available. A complete image of the desired data does not have to be available in advance.

For very important formats both methods may be implemented, otherwise do whichever is simpler, or provides the required capabilities.

CreateCopy

The GDALDriver::CreateCopy() method call is passed through directly, so that method should be consulted for details of arguments. However, some things to keep in mind are:

The full implementation of the CreateCopy function for JPEG (which is assigned to pfnCreateCopy in the GDALDriver object) is here.

static GDALDataset *
JPEGCreateCopy( const char * pszFilename, GDALDataset *poSrcDS,
int bStrict, char ** papszOptions,
GDALProgressFunc pfnProgress, void * pProgressData )
{
const int nBands = poSrcDS->GetRasterCount();
const int nXSize = poSrcDS->GetRasterXSize();
const int nYSize = poSrcDS->GetRasterYSize();
// Some some rudimentary checks
if( nBands != 1 && nBands != 3 )
{
"JPEG driver doesn't support %d bands. Must be 1 (grey) "
"or 3 (RGB) bands.", nBands);
return NULL;
}
if( poSrcDS->GetRasterBand(1)->GetRasterDataType() != GDT_Byte && bStrict )
{
"JPEG driver doesn't support data type %s. "
"Only eight bit byte bands supported.",
poSrcDS->GetRasterBand(1)->GetRasterDataType()));
return NULL;
}
// What options has the user selected?
int nQuality = 75;
if( CSLFetchNameValue(papszOptions, "QUALITY") != NULL )
{
nQuality = atoi(CSLFetchNameValue(papszOptions, "QUALITY"));
if( nQuality < 10 || nQuality > 100 )
{
"QUALITY=%s is not a legal value in the range 10 - 100.",
CSLFetchNameValue(papszOptions, "QUALITY"));
return NULL;
}
}
bool bProgressive = false;
if( CSLFetchNameValue(papszOptions, "PROGRESSIVE") != NULL )
{
bProgressive = true;
}
// Create the dataset.
VSILFILE *fpImage = VSIFOpenL(pszFilename, "wb");
if( fpImage == NULL )
{
"Unable to create jpeg file %s.",
pszFilename);
return NULL;
}
// Initialize JPG access to the file.
struct jpeg_compress_struct sCInfo;
struct jpeg_error_mgr sJErr;
sCInfo.err = jpeg_std_error(&sJErr);
jpeg_create_compress(&sCInfo);
jpeg_stdio_dest(&sCInfo, fpImage);
sCInfo.image_width = nXSize;
sCInfo.image_height = nYSize;
sCInfo.input_components = nBands;
if( nBands == 1 )
{
sCInfo.in_color_space = JCS_GRAYSCALE;
}
else
{
sCInfo.in_color_space = JCS_RGB;
}
jpeg_set_defaults(&sCInfo);
jpeg_set_quality(&sCInfo, nQuality, TRUE);
if( bProgressive )
jpeg_simple_progression(&sCInfo);
jpeg_start_compress(&sCInfo, TRUE);
// Loop over image, copying image data.
GByte *pabyScanline = static_cast<GByte *>(CPLMalloc(nBands * nXSize));
for( int iLine = 0; iLine < nYSize; iLine++ )
{
for( int iBand = 0; iBand < nBands; iBand++ )
{
GDALRasterBand * poBand = poSrcDS->GetRasterBand(iBand + 1);
const CPLErr eErr =
poBand->RasterIO(GF_Read, 0, iLine, nXSize, 1,
pabyScanline + iBand, nXSize, 1, GDT_Byte,
nBands, nBands * nXSize);
// TODO: Handle error.
}
JSAMPLE *ppSamples = pabyScanline;
jpeg_write_scanlines(&sCInfo, &ppSamples, 1);
}
CPLFree(pabyScanline);
jpeg_finish_compress(&sCInfo);
jpeg_destroy_compress(&sCInfo);
VSIFCloseL(fpImage);
return static_cast<GDALDataset *>(GDALOpen(pszFilename, GA_ReadOnly));
}

Dynamic Creation

In the case of dynamic creation, there is no source dataset. Instead the size, number of bands, and pixel data type of the desired file is provided but other information (such as georeferencing, and imagery data) would be supplied later via other method calls on the resulting GDALDataset.

The following sample implement PCI .aux labeled raw raster creation. It follows a common approach of creating a blank, but valid file using non-GDAL calls, and then calling GDALOpen(,GA_Update) at the end to return a writable file handle. This avoids having to duplicate the various setup actions in the Open() function.

GDALDataset *PAuxDataset::Create( const char * pszFilename,
int nXSize, int nYSize, int nBands,
GDALDataType eType,
char ** /* papszParmList */ )
{
// Verify input options.
if( eType != GDT_Byte && eType != GDT_Float32 &&
eType != GDT_UInt16 && eType != GDT_Int16 )
{
CE_Failure, CPLE_AppDefined,
"Attempt to create PCI .Aux labeled dataset with an illegal "
"data type (%s).",
return NULL;
}
// Try to create the file.
FILE *fp = VSIFOpen(pszFilename, "w");
if( fp == NULL )
{
"Attempt to create file `%s' failed.",
pszFilename);
return NULL;
}
// Just write out a couple of bytes to establish the binary
// file, and then close it.
VSIFWrite("\0\0", 2, 1, fp);
VSIFClose(fp);
// Create the aux filename.
char *pszAuxFilename = static_cast<char *>(CPLMalloc(strlen(pszFilename) + 5));
strcpy(pszAuxFilename, pszFilename);;
for( int i = strlen(pszAuxFilename) - 1; i > 0; i-- )
{
if( pszAuxFilename[i] == '.' )
{
pszAuxFilename[i] = '\0';
break;
}
}
strcat(pszAuxFilename, ".aux");
// Open the file.
fp = VSIFOpen(pszAuxFilename, "wt");
if( fp == NULL )
{
"Attempt to create file `%s' failed.",
pszAuxFilename);
return NULL;
}
// We need to write out the original filename but without any
// path components in the AuxiliaryTarget line. Do so now.
int iStart = strlen(pszFilename) - 1;
while( iStart > 0 && pszFilename[iStart - 1] != '/' &&
pszFilename[iStart - 1] != '\\' )
iStart--;
VSIFPrintf(fp, "AuxilaryTarget: %s\n", pszFilename + iStart);
// Write out the raw definition for the dataset as a whole.
VSIFPrintf(fp, "RawDefinition: %d %d %d\n",
nXSize, nYSize, nBands);
// Write out a definition for each band. We always write band
// sequential files for now as these are pretty efficiently
// handled by GDAL.
int nImgOffset = 0;
for( int iBand = 0; iBand < nBands; iBand++ )
{
const int nPixelOffset = GDALGetDataTypeSize(eType)/8;
const int nLineOffset = nXSize * nPixelOffset;
const char *pszTypeName = NULL;
if( eType == GDT_Float32 )
pszTypeName = "32R";
else if( eType == GDT_Int16 )
pszTypeName = "16S";
else if( eType == GDT_UInt16 )
pszTypeName = "16U";
else
pszTypeName = "8U";
VSIFPrintf( fp, "ChanDefinition-%d: %s %d %d %d %s\n",
iBand + 1, pszTypeName,
nImgOffset, nPixelOffset, nLineOffset,
#ifdef CPL_LSB
"Swapped"
#else
"Unswapped"
#endif
);
nImgOffset += nYSize * nLineOffset;
}
// Cleanup.
VSIFClose(fp);
return static_cast<GDALDataset *>(GDALOpen(pszFilename, GA_Update));
}

File formats supporting dynamic creation, or even just update-in-place access also need to implement an IWriteBlock() method on the raster band class. It has semantics similar to IReadBlock(). As well, for various esoteric reasons, it is critical that a FlushCache() method be implemented in the raster band destructor. This is to ensure that any write cache blocks for the band be flushed out before the destructor is called.

RawDataset/RawRasterBand Helper Classes

Many file formats have the actual imagery data stored in a regular, binary, scanline oriented format. Rather than re-implement the access semantics for this for each formats, there are provided RawDataset and RawRasterBand classes declared in gdal/frmts/raw that can be utilized to implement efficient and convenient access.

In these cases the format specific band class may not be required, or if required it can be derived from RawRasterBand. The dataset class should be derived from RawDataset.

The Open() method for the dataset then instantiates raster bands passing all the layout information to the constructor. For instance, the PNM driver uses the following calls to create it's raster bands.

if( poOpenInfo->pabyHeader[1] == '5' )
{
poDS->SetBand(
1, new RawRasterBand(poDS, 1, poDS->fpImage,
iIn, 1, nWidth, GDT_Byte, TRUE));
}
else
{
poDS->SetBand(
1, new RawRasterBand(poDS, 1, poDS->fpImage,
iIn, 3, nWidth*3, GDT_Byte, TRUE));
poDS->SetBand(
2, new RawRasterBand(poDS, 2, poDS->fpImage,
iIn+1, 3, nWidth*3, GDT_Byte, TRUE));
poDS->SetBand(
3, new RawRasterBand(poDS, 3, poDS->fpImage,
iIn+2, 3, nWidth*3, GDT_Byte, TRUE));
}

The RawRasterBand takes the following arguments.

Simple file formats utilizing the Raw services are normally placed all within one file in the gdal/frmts/raw directory. There are numerous examples there of format implementation.

Metadata, and Other Exotic Extensions

There are various other items in the GDAL data model, for which virtual methods exist on the GDALDataset and GDALRasterBand. They include:

$Id: gdal_drivertut.dox 8f1e8a485501eb2f9f3a35805f12ff8e221c1333 2017-03-02 13:02:20Z Kurt Schwehr $


Generated for GDAL by doxygen 1.8.13.