| 1 |
/*! |
|---|
| 2 |
\page gdal_drivertut |
|---|
| 3 |
|
|---|
| 4 |
<center> |
|---|
| 5 |
<title>GDAL Driver Implementation Tutorial</title> |
|---|
| 6 |
</center> |
|---|
| 7 |
|
|---|
| 8 |
<h2>Overall Approach</h2> |
|---|
| 9 |
|
|---|
| 10 |
In general new formats are added to GDAL by implementing format specific |
|---|
| 11 |
drivers as subclasses of GDALDataset, and band accessors as subclasses |
|---|
| 12 |
of GDALRasterBand. As well, a GDALDriver instance is created for the |
|---|
| 13 |
format, and registered with the GDALDriverManager, to ensure that the system |
|---|
| 14 |
<i>knows</i> about the format. |
|---|
| 15 |
|
|---|
| 16 |
This tutorial will start with implementing a simple read-only driver |
|---|
| 17 |
(based on the JDEM driver), and then proceed to utilizing the RawRasterBand |
|---|
| 18 |
helper class, implementing creatable and updatable formats, and some |
|---|
| 19 |
esoteric issues. |
|---|
| 20 |
|
|---|
| 21 |
It is strongly advised that the <a href="gdal_datamodel.html">GDAL Data Model |
|---|
| 22 |
</a> description be reviewed and understood before attempting to implement a |
|---|
| 23 |
GDAL driver. |
|---|
| 24 |
|
|---|
| 25 |
<h2>Contents</h2> |
|---|
| 26 |
|
|---|
| 27 |
<ol> |
|---|
| 28 |
<li> <a href="#dataset">Implementing the Dataset</a> |
|---|
| 29 |
<li> <a href="#rasterband">Implementing the RasterBand</a> |
|---|
| 30 |
<li> <a href="#driver">The Driver</a> |
|---|
| 31 |
<li> <a href="#addingdriver">Adding Driver to GDAL Tree</a> |
|---|
| 32 |
<li> <a href="#georef">Adding Georeferencing</a> |
|---|
| 33 |
<li> <a href="#overviews">Overviews</a> |
|---|
| 34 |
<li> <a href="#creation">File Creation</a> |
|---|
| 35 |
<li> <a href="#raw">RawDataset/RawRasterBand Helper Classes</a> |
|---|
| 36 |
<li> <a href="#metadata">Metadata, and Other Exotic Extensions</a> |
|---|
| 37 |
</ol> |
|---|
| 38 |
|
|---|
| 39 |
<h2><a name="dataset">Implementing the Dataset</a></h2> |
|---|
| 40 |
|
|---|
| 41 |
We will start showing minimal implementation of a read-only driver for |
|---|
| 42 |
the Japanese DEM format (<a href="jdemdataset.cpp.html">jdemdataset.cpp</a>). |
|---|
| 43 |
First we declare a format |
|---|
| 44 |
specific dataset class, JDEMDataset in this case. |
|---|
| 45 |
|
|---|
| 46 |
\code |
|---|
| 47 |
class JDEMDataset : public GDALDataset |
|---|
| 48 |
{ |
|---|
| 49 |
FILE *fp; |
|---|
| 50 |
GByte abyHeader[1012]; |
|---|
| 51 |
|
|---|
| 52 |
public: |
|---|
| 53 |
~JDEMDataset(); |
|---|
| 54 |
|
|---|
| 55 |
static GDALDataset *Open( GDALOpenInfo * ); |
|---|
| 56 |
}; |
|---|
| 57 |
\endcode |
|---|
| 58 |
|
|---|
| 59 |
In general we provide capabilities for a driver, by overriding the various |
|---|
| 60 |
virtual methods on the GDALDataset base class. However, the Open() method |
|---|
| 61 |
is special. This is not a virtual method on the baseclass, and we will |
|---|
| 62 |
need a freestanding function for this operation, so we declare it static. |
|---|
| 63 |
Implementing it as a method in the JDEMDataset class is convenient because |
|---|
| 64 |
we have priveledged access to modify the contents of the database object. |
|---|
| 65 |
|
|---|
| 66 |
The open method itself may look something like this: |
|---|
| 67 |
|
|---|
| 68 |
\verbatim |
|---|
| 69 |
GDALDataset *JDEMDataset::Open( GDALOpenInfo * poOpenInfo ) |
|---|
| 70 |
|
|---|
| 71 |
{ |
|---|
| 72 |
// -------------------------------------------------------------------- |
|---|
| 73 |
// Before trying JDEMOpen() we first verify that there is at |
|---|
| 74 |
// least one "\n#keyword" type signature in the first chunk of |
|---|
| 75 |
// the file. |
|---|
| 76 |
// -------------------------------------------------------------------- |
|---|
| 77 |
if( poOpenInfo->fp == NULL || poOpenInfo->nHeaderBytes < 50 ) |
|---|
| 78 |
return NULL; |
|---|
| 79 |
|
|---|
| 80 |
// check if century values seem reasonable |
|---|
| 81 |
if( (!EQUALN((char *)poOpenInfo->pabyHeader+11,"19",2) |
|---|
| 82 |
&& !EQUALN((char *)poOpenInfo->pabyHeader+11,"20",2)) |
|---|
| 83 |
|| (!EQUALN((char *)poOpenInfo->pabyHeader+15,"19",2) |
|---|
| 84 |
&& !EQUALN((char *)poOpenInfo->pabyHeader+15,"20",2)) |
|---|
| 85 |
|| (!EQUALN((char *)poOpenInfo->pabyHeader+19,"19",2) |
|---|
| 86 |
&& !EQUALN((char *)poOpenInfo->pabyHeader+19,"20",2)) ) |
|---|
| 87 |
{ |
|---|
| 88 |
return NULL; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
// -------------------------------------------------------------------- |
|---|
| 92 |
// Create a corresponding GDALDataset. |
|---|
| 93 |
// -------------------------------------------------------------------- |
|---|
| 94 |
JDEMDataset *poDS; |
|---|
| 95 |
|
|---|
| 96 |
poDS = new JDEMDataset(); |
|---|
| 97 |
|
|---|
| 98 |
poDS->fp = poOpenInfo->fp; |
|---|
| 99 |
poOpenInfo->fp = NULL; |
|---|
| 100 |
|
|---|
| 101 |
// -------------------------------------------------------------------- |
|---|
| 102 |
// Read the header. |
|---|
| 103 |
// -------------------------------------------------------------------- |
|---|
| 104 |
VSIFSeek( poDS->fp, 0, SEEK_SET ); |
|---|
| 105 |
VSIFRead( poDS->abyHeader, 1, 1012, poDS->fp ); |
|---|
| 106 |
|
|---|
| 107 |
poDS->nRasterXSize = JDEMGetField( (char *) poDS->abyHeader + 23, 3 ); |
|---|
| 108 |
poDS->nRasterYSize = JDEMGetField( (char *) poDS->abyHeader + 26, 3 ); |
|---|
| 109 |
|
|---|
| 110 |
// -------------------------------------------------------------------- |
|---|
| 111 |
// Create band information objects. |
|---|
| 112 |
// -------------------------------------------------------------------- |
|---|
| 113 |
poDS->nBands = 1; |
|---|
| 114 |
poDS->SetBand( 1, new JDEMRasterBand( poDS, 1 )); |
|---|
| 115 |
|
|---|
| 116 |
return( poDS ); |
|---|
| 117 |
} |
|---|
| 118 |
\endverbatim |
|---|
| 119 |
|
|---|
| 120 |
The first step in any database Open function is to verify that the file |
|---|
| 121 |
being passed is in fact of the type this driver is for. It is important |
|---|
| 122 |
to realize that each driver's Open function is called in turn till one |
|---|
| 123 |
succeeds. Drivers must quitly return NULL if the passed file is not of |
|---|
| 124 |
their format. They should only produce an error if the file does appear to |
|---|
| 125 |
be of their supported format, but is for some reason unsupported or corrupt. |
|---|
| 126 |
|
|---|
| 127 |
The information on the file to be opened is passed in contained in a |
|---|
| 128 |
GDALOpenInfo object. The GDALOpenInfo includes the following public |
|---|
| 129 |
data members: |
|---|
| 130 |
|
|---|
| 131 |
\code |
|---|
| 132 |
char *pszFilename; |
|---|
| 133 |
|
|---|
| 134 |
GDALAccess eAccess; // GA_ReadOnly or GA_Update |
|---|
| 135 |
|
|---|
| 136 |
GBool bStatOK; |
|---|
| 137 |
VSIStatBuf sStat; |
|---|
| 138 |
|
|---|
| 139 |
FILE *fp; |
|---|
| 140 |
|
|---|
| 141 |
int nHeaderBytes; |
|---|
| 142 |
GByte *pabyHeader; |
|---|
| 143 |
\endcode |
|---|
| 144 |
|
|---|
| 145 |
The driver can inspect these to establish if the file is supported. If the |
|---|
| 146 |
pszFilename refers to an object in the file system, the <b>bStatOK</b> flag |
|---|
| 147 |
will be set, and the <b>sStat</b> structure will contain normal stat() |
|---|
| 148 |
information about the object (be it directory, file, device). If the object |
|---|
| 149 |
is a regular readable file, the <b>fp</b> will be non-NULL, and can be used |
|---|
| 150 |
for reads on the file (please use the VSI stdio functions from |
|---|
| 151 |
cpl_vsi.h). As well, if the file was successfully opened, the first kilobyte |
|---|
| 152 |
or so is read in, and put in <b>pabyHeader</b>, with the exact size in |
|---|
| 153 |
<b>nHeaderBytes</b>. |
|---|
| 154 |
|
|---|
| 155 |
In this typical testing example it is verified that the file was successfully |
|---|
| 156 |
opened, that we have at least enough header information to perform our test, |
|---|
| 157 |
and that various parts of the header are as expected for this format. In |
|---|
| 158 |
this case, there are no <i>magic</i> numbers for JDEM format so we check |
|---|
| 159 |
various date fields to ensure they have reasonable century values. If the |
|---|
| 160 |
test fails, we quietly return NULL indicating this file isn't of our supported |
|---|
| 161 |
format. |
|---|
| 162 |
|
|---|
| 163 |
\code |
|---|
| 164 |
if( poOpenInfo->fp == NULL || poOpenInfo->nHeaderBytes < 50 ) |
|---|
| 165 |
return NULL; |
|---|
| 166 |
|
|---|
| 167 |
// check if century values seem reasonable |
|---|
| 168 |
if( (!EQUALN((char *)poOpenInfo->pabyHeader+11,"19",2) |
|---|
| 169 |
&& !EQUALN((char *)poOpenInfo->pabyHeader+11,"20",2)) |
|---|
| 170 |
|| (!EQUALN((char *)poOpenInfo->pabyHeader+15,"19",2) |
|---|
| 171 |
&& !EQUALN((char *)poOpenInfo->pabyHeader+15,"20",2)) |
|---|
| 172 |
|| (!EQUALN((char *)poOpenInfo->pabyHeader+19,"19",2) |
|---|
| 173 |
&& !EQUALN((char *)poOpenInfo->pabyHeader+19,"20",2)) ) |
|---|
| 174 |
{ |
|---|
| 175 |
return NULL; |
|---|
| 176 |
} |
|---|
| 177 |
\endcode |
|---|
| 178 |
|
|---|
| 179 |
It is important to make the <i>is this my format</i> test as stringent as |
|---|
| 180 |
possible. In this particular case the test is weak, and a file that happened |
|---|
| 181 |
to have 19s or 20s at a few locations could be erroneously recognised as |
|---|
| 182 |
JDEM format, causing it to not be handled properly. |
|---|
| 183 |
|
|---|
| 184 |
Once we are satisfied that the file is of our format, we need to create |
|---|
| 185 |
an instance of the database class in which we will set various information |
|---|
| 186 |
of interest. |
|---|
| 187 |
|
|---|
| 188 |
\code |
|---|
| 189 |
JDEMDataset *poDS; |
|---|
| 190 |
|
|---|
| 191 |
poDS = new JDEMDataset(); |
|---|
| 192 |
|
|---|
| 193 |
poDS->fp = poOpenInfo->fp; |
|---|
| 194 |
poOpenInfo->fp = NULL; |
|---|
| 195 |
\endcode |
|---|
| 196 |
|
|---|
| 197 |
Generally at this point we would open the file, to acquire a file handle |
|---|
| 198 |
for the dataset; however, if read-only access is sufficient it is permitted |
|---|
| 199 |
to <b>assume ownership</b> of the FILE * from the GDALOpenInfo object. |
|---|
| 200 |
Just ensure that it is set to NULL in the GDALOpenInfo to avoid having it |
|---|
| 201 |
get closed twice. It is also important to note that the state of the |
|---|
| 202 |
FILE * adopted is indeterminate. Ensure that the current location is reset |
|---|
| 203 |
with VSIFSeek() before assuming you can read from it. This is accomplished |
|---|
| 204 |
in the following statements which reset the file and read the header. |
|---|
| 205 |
|
|---|
| 206 |
\code |
|---|
| 207 |
VSIFSeek( poDS->fp, 0, SEEK_SET ); |
|---|
| 208 |
VSIFRead( poDS->abyHeader, 1, 1012, poDS->fp ); |
|---|
| 209 |
\endcode |
|---|
| 210 |
|
|---|
| 211 |
Next the X and Y size are extracted from the header. The nRasterXSize and |
|---|
| 212 |
nRasterYSize are data fields inherited from the GDALDataset base class, and |
|---|
| 213 |
must be set by the Open() method. |
|---|
| 214 |
|
|---|
| 215 |
\code |
|---|
| 216 |
poDS->nRasterXSize = JDEMGetField( (char *) poDS->abyHeader + 23, 3 ); |
|---|
| 217 |
poDS->nRasterYSize = JDEMGetField( (char *) poDS->abyHeader + 26, 3 ); |
|---|
| 218 |
\endcode |
|---|
| 219 |
|
|---|
| 220 |
Finally, all the bands related to this dataset must be attached using |
|---|
| 221 |
the SetBand() method. We will explore the JDEMRasterBand() class shortly. |
|---|
| 222 |
|
|---|
| 223 |
\code |
|---|
| 224 |
poDS->SetBand( 1, new JDEMRasterBand( poDS, 1 )); |
|---|
| 225 |
|
|---|
| 226 |
return( poDS ); |
|---|
| 227 |
\endcode |
|---|
| 228 |
|
|---|
| 229 |
<h2><a name="rasterband">Implementing the RasterBand</a></h2> |
|---|
| 230 |
|
|---|
| 231 |
Similar to the customized JDEMDataset class subclassed from GDALDataset, |
|---|
| 232 |
we also need to declare and implement a customized JDEMRasterBand derived |
|---|
| 233 |
from GDALRasterBand for access to the band(s) of the JDEM file. For |
|---|
| 234 |
JDEMRasterBand the declaration looks like this: |
|---|
| 235 |
|
|---|
| 236 |
\code |
|---|
| 237 |
class JDEMRasterBand : public GDALRasterBand |
|---|
| 238 |
{ |
|---|
| 239 |
public: |
|---|
| 240 |
JDEMRasterBand( JDEMDataset *, int ); |
|---|
| 241 |
virtual CPLErr IReadBlock( int, int, void * ); |
|---|
| 242 |
}; |
|---|
| 243 |
\endcode |
|---|
| 244 |
|
|---|
| 245 |
The constructor may have any signature, and is only called from the Open() |
|---|
| 246 |
method. Other virtual methods, such as IReadBlock() must be exactly |
|---|
| 247 |
matched to the method signature in gdal_priv.h. |
|---|
| 248 |
|
|---|
| 249 |
The constructor implementation looks like this: |
|---|
| 250 |
|
|---|
| 251 |
\code |
|---|
| 252 |
JDEMRasterBand::JDEMRasterBand( JDEMDataset *poDS, int nBand ) |
|---|
| 253 |
|
|---|
| 254 |
{ |
|---|
| 255 |
this->poDS = poDS; |
|---|
| 256 |
this->nBand = nBand; |
|---|
| 257 |
|
|---|
| 258 |
eDataType = GDT_Float32; |
|---|
| 259 |
|
|---|
| 260 |
nBlockXSize = poDS->GetRasterXSize(); |
|---|
| 261 |
nBlockYSize = 1; |
|---|
| 262 |
} |
|---|
| 263 |
\endcode |
|---|
| 264 |
|
|---|
| 265 |
The following data members are inherited from GDALRasterBand, and should |
|---|
| 266 |
generally be set in the band constructor. |
|---|
| 267 |
|
|---|
| 268 |
<ul> |
|---|
| 269 |
<li> <b>poDS</b>: Pointer to the parent GDALDataset. |
|---|
| 270 |
<li> <b>nBand</b>: The band number within the dataset. |
|---|
| 271 |
<li> <b>eDataType</b>: The data type of pixels in this band. |
|---|
| 272 |
<li> <b>nBlockXSize</b>: The width of one block in this band. |
|---|
| 273 |
<li> <b>nBlockYSize</b>: The height of one block in this band. |
|---|
| 274 |
</ul> |
|---|
| 275 |
|
|---|
| 276 |
The full set of possible GDALDataType values are declared in gdal.h, and |
|---|
| 277 |
include GDT_Byte, GDT_UInt16, GDT_Int16, and GDT_Float32. The block size is |
|---|
| 278 |
used to establish a <i>natural</i> or efficient block size to access the data |
|---|
| 279 |
with. For tiled datasets this will be the size of a tile, while for most |
|---|
| 280 |
other datasets it will be one scanline, as in this case. |
|---|
| 281 |
|
|---|
| 282 |
Next we see the implementation of the code that actually reads the image |
|---|
| 283 |
data, IReadBlock(). |
|---|
| 284 |
|
|---|
| 285 |
\code |
|---|
| 286 |
CPLErr JDEMRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff, |
|---|
| 287 |
void * pImage ) |
|---|
| 288 |
|
|---|
| 289 |
{ |
|---|
| 290 |
JDEMDataset *poGDS = (JDEMDataset *) poDS; |
|---|
| 291 |
char *pszRecord; |
|---|
| 292 |
int nRecordSize = nBlockXSize*5 + 9 + 2; |
|---|
| 293 |
int i; |
|---|
| 294 |
|
|---|
| 295 |
VSIFSeek( poGDS->fp, 1011 + nRecordSize*nBlockYOff, SEEK_SET ); |
|---|
| 296 |
|
|---|
| 297 |
pszRecord = (char *) CPLMalloc(nRecordSize); |
|---|
| 298 |
VSIFRead( pszRecord, 1, nRecordSize, poGDS->fp ); |
|---|
| 299 |
|
|---|
| 300 |
if( !EQUALN((char *) poGDS->abyHeader,pszRecord,6) ) |
|---|
| 301 |
{ |
|---|
| 302 |
CPLFree( pszRecord ); |
|---|
| 303 |
|
|---|
| 304 |
CPLError( CE_Failure, CPLE_AppDefined, |
|---|
| 305 |
"JDEM Scanline corrupt. Perhaps file was not transferred\n" |
|---|
| 306 |
"in binary mode?" ); |
|---|
| 307 |
return CE_Failure; |
|---|
| 308 |
} |
|---|
| 309 |
|
|---|
| 310 |
if( JDEMGetField( pszRecord + 6, 3 ) != nBlockYOff + 1 ) |
|---|
| 311 |
{ |
|---|
| 312 |
CPLFree( pszRecord ); |
|---|
| 313 |
|
|---|
| 314 |
CPLError( CE_Failure, CPLE_AppDefined, |
|---|
| 315 |
"JDEM scanline out of order, JDEM driver does not\n" |
|---|
| 316 |
"currently support partial datasets." ); |
|---|
| 317 |
return CE_Failure; |
|---|
| 318 |
} |
|---|
| 319 |
|
|---|
| 320 |
for( i = 0; i < nBlockXSize; i++ ) |
|---|
| 321 |
((float *) pImage)[i] = JDEMGetField( pszRecord + 9 + 5 * i, 5) * 0.1; |
|---|
| 322 |
|
|---|
| 323 |
return CE_None; |
|---|
| 324 |
} |
|---|
| 325 |
\endcode |
|---|
| 326 |
|
|---|
| 327 |
Key items to note are: |
|---|
| 328 |
|
|---|
| 329 |
<ul> |
|---|
| 330 |
<li> It is typical to cast the GDALRasterBand::poDS member to the derived |
|---|
| 331 |
type of the owning dataset. If your RasterBand class will need priveledged |
|---|
| 332 |
access to the owning dataset object, ensure it is declared as a friend (omitted |
|---|
| 333 |
above for brevity). |
|---|
| 334 |
|
|---|
| 335 |
<li> If an error occurs, report it with CPLError(), and return CE_Failure. |
|---|
| 336 |
Otherwise return CE_None. |
|---|
| 337 |
|
|---|
| 338 |
<li> The pImage buffer should be filled with one block of data. The block |
|---|
| 339 |
is the size declared in nBlockXSize and nBlockYSize for the raster band. The |
|---|
| 340 |
type of the data within pImage should match the type declared in |
|---|
| 341 |
eDataType in the raster band object. |
|---|
| 342 |
|
|---|
| 343 |
<li> The nBlockXOff and nBlockYOff are block offsets, so with 128x128 tiled |
|---|
| 344 |
datasets values of 1 and 1 would indicate the block going from (128,128) to |
|---|
| 345 |
(255,255) should be loaded. |
|---|
| 346 |
|
|---|
| 347 |
</ul> |
|---|
| 348 |
|
|---|
| 349 |
<h2><a name="driver">The Driver</a></h2> |
|---|
| 350 |
|
|---|
| 351 |
While the JDEMDataset and JDEMRasterBand are now ready to use to read image |
|---|
| 352 |
data, it still isn't clear how the GDAL system knows about the new driver. |
|---|
| 353 |
This is accomplished via the GDALDriverManager. To register our format we |
|---|
| 354 |
implement a registration function: |
|---|
| 355 |
|
|---|
| 356 |
\code |
|---|
| 357 |
CPL_C_START |
|---|
| 358 |
void GDALRegister_JDEM(void); |
|---|
| 359 |
CPL_C_END |
|---|
| 360 |
|
|---|
| 361 |
... |
|---|
| 362 |
|
|---|
| 363 |
void GDALRegister_JDEM() |
|---|
| 364 |
|
|---|
| 365 |
{ |
|---|
| 366 |
GDALDriver *poDriver; |
|---|
| 367 |
|
|---|
| 368 |
if( GDALGetDriverByName( "JDEM" ) == NULL ) |
|---|
| 369 |
{ |
|---|
| 370 |
poDriver = new GDALDriver(); |
|---|
| 371 |
|
|---|
| 372 |
poDriver->SetDescription( "JDEM" ); |
|---|
| 373 |
poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, |
|---|
| 374 |
"Japanese DEM (.mem)" ); |
|---|
| 375 |
poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, |
|---|
| 376 |
"frmt_various.html#JDEM" ); |
|---|
| 377 |
poDriver->SetMetadataItem( GDAL_DMD_EXTENSION, "mem" ); |
|---|
| 378 |
|
|---|
| 379 |
poDriver->pfnOpen = JDEMDataset::Open; |
|---|
| 380 |
|
|---|
| 381 |
GetGDALDriverManager()->RegisterDriver( poDriver ); |
|---|
| 382 |
} |
|---|
| 383 |
} |
|---|
| 384 |
\endcode |
|---|
| 385 |
|
|---|
| 386 |
The registration function will create an instance of a GDALDriver object |
|---|
| 387 |
when first called, and register it with the GDALDriverManager. The |
|---|
| 388 |
following fields can be set in the driver before |
|---|
| 389 |
registering it with the GDALDriverManager(). |
|---|
| 390 |
|
|---|
| 391 |
<ul> |
|---|
| 392 |
<li> The description is the short name for the format. This is a unique |
|---|
| 393 |
name for this format, often used to identity the driver in scripts and |
|---|
| 394 |
commandline programs. Normally 3-5 characters in length, and matching the |
|---|
| 395 |
prefix of the format classes. (manditory) |
|---|
| 396 |
|
|---|
| 397 |
<li> GDAL_DMD_LONGNAME: A longer descriptive name for the file format, |
|---|
| 398 |
but still no longer than 50-60 characters. (manditory) |
|---|
| 399 |
|
|---|
| 400 |
<li> GDAL_DMD_HELPTOPIC: The name of a help topic to display for this driver, |
|---|
| 401 |
if any. In this case JDEM format is contained within the various format |
|---|
| 402 |
web page held in gdal/html. (optional) |
|---|
| 403 |
|
|---|
| 404 |
<li> GDAL_DMD_EXTENSION: The extension used for files of this type. If more |
|---|
| 405 |
than one pick the primary extension, or none at all. (optional) |
|---|
| 406 |
|
|---|
| 407 |
<li> GDAL_DMD_MIMETYPE: The standard mime type for this file format, such as |
|---|
| 408 |
"image/png". (optional) |
|---|
| 409 |
|
|---|
| 410 |
<li> GDAL_DMD_CREATIONOPTIONLIST: There is evolving work on mechanisms |
|---|
| 411 |
to describe creation options. See the geotiff driver for an example of |
|---|
| 412 |
this. (optional) |
|---|
| 413 |
|
|---|
| 414 |
<li> GDAL_DMD_CREATIONDATATYPES: A list of space separated data types |
|---|
| 415 |
supported by this create when creating new datasets. If a Create() method |
|---|
| 416 |
exists, these will be will supported. If a CreateCopy() method exists, this |
|---|
| 417 |
will be a list of types that can be losslessly exported but it may include |
|---|
| 418 |
weaker datatypes than the type eventually written. For instance, a format |
|---|
| 419 |
with a CreateCopy() method, and that always writes Float32 might also list |
|---|
| 420 |
Byte, Int16, and UInt16 since they can losslessly translated to Float32. An |
|---|
| 421 |
example value might be "Byte Int16 UInt16". (required - if creation supported) |
|---|
| 422 |
|
|---|
| 423 |
<li> pfnOpen: The function to call to try opening files of this format. |
|---|
| 424 |
(optional) |
|---|
| 425 |
|
|---|
| 426 |
<li> pfnCreate: The function to call to create new updatable datasets of this |
|---|
| 427 |
format. (optional) |
|---|
| 428 |
|
|---|
| 429 |
<li> pfnCreateCopy: The function to call to create a new dataset of this format |
|---|
| 430 |
copied from another source, but not necessary updatable. (optional) |
|---|
| 431 |
|
|---|
| 432 |
<li> pfnDelete: The function to call to delete a dataset of this format. |
|---|
| 433 |
(optional) |
|---|
| 434 |
|
|---|
| 435 |
<li> pfnUnloadDriver: A function called only when the driver is destroyed. |
|---|
| 436 |
Could be used to cleanup data at the driver level. Rarely used. (optional) |
|---|
| 437 |
|
|---|
| 438 |
</ul> |
|---|
| 439 |
|
|---|
| 440 |
<h2><a name="addingdriver">Adding Driver to GDAL Tree</a></h2> |
|---|
| 441 |
|
|---|
| 442 |
Note that the GDALRegister_JDEM() method must be called by the higher |
|---|
| 443 |
level program in order to have access to the JDEM driver. Normal practice |
|---|
| 444 |
when writing new drivers is to: |
|---|
| 445 |
|
|---|
| 446 |
<ol> |
|---|
| 447 |
<li> Add a driver directory under gdal/frmts, with the directory name the same |
|---|
| 448 |
as the short name. |
|---|
| 449 |
|
|---|
| 450 |
<li> Add a GNUmakefile and makefile.vc in that directory modelled on those |
|---|
| 451 |
from other similar directories (ie. the jdem directory). |
|---|
| 452 |
|
|---|
| 453 |
<li> Add the module with the dataset, and rasterband implementation. |
|---|
| 454 |
Generally this is called <short_name>dataset.cpp, with all the GDAL specifc |
|---|
| 455 |
code in one file, though that is not required. |
|---|
| 456 |
|
|---|
| 457 |
<li> Add the registration entry point declaration (ie. GDALRegister_JDEM()) to |
|---|
| 458 |
gdal/gcore/gdal_frmts.h. |
|---|
| 459 |
|
|---|
| 460 |
<li> Add a call to the registration function to frmts/gdalallregister.c, |
|---|
| 461 |
protected by an appropriate #ifdef. |
|---|
| 462 |
|
|---|
| 463 |
<li> Add the format short name to the GDAL_FORMATS macro in |
|---|
| 464 |
GDALmake.opt.in (and to GDALmake.opt). |
|---|
| 465 |
|
|---|
| 466 |
<li> Add a format specific item to the EXTRAFLAGS macro in frmts/makefile.vc. |
|---|
| 467 |
</ol> |
|---|
| 468 |
|
|---|
| 469 |
Once this is all done, it should be possible to rebuild GDAL, and have |
|---|
| 470 |
the new format available in all the utilities. The gdalinfo utility can be |
|---|
| 471 |
used to test that opening and reporting on the format is working, and the |
|---|
| 472 |
gdal_translate utility can be used to test image reading. |
|---|
| 473 |
|
|---|
| 474 |
<h2><a name="georef">Adding Georeferencing</a></h2> |
|---|
| 475 |
|
|---|
| 476 |
Now we will take the example a step forward, adding georeferencing support. |
|---|
| 477 |
We add the following two virtual method overrides to JDEMDataset, taking |
|---|
| 478 |
care to exactly match the signature of the method on the GDALRasterDataset |
|---|
| 479 |
base class. |
|---|
| 480 |
|
|---|
| 481 |
\code |
|---|
| 482 |
CPLErr GetGeoTransform( double * padfTransform ); |
|---|
| 483 |
const char *GetProjectionRef(); |
|---|
| 484 |
\endcode |
|---|
| 485 |
|
|---|
| 486 |
The implementation of GetGeoTransform() just copies the usual geotransform |
|---|
| 487 |
matrix into the supplied buffer. Note that GetGeoTransform() may be called |
|---|
| 488 |
alot, so it isn't generally wise to do alot of computation in it. In many |
|---|
| 489 |
cases the Open() will collect the geotransform, and this method will just |
|---|
| 490 |
copy it over. Also note that the geotransform return is based on an |
|---|
| 491 |
anchor point at the top left corner of the top left pixel, not the center |
|---|
| 492 |
of pixel approach used in some packages. |
|---|
| 493 |
|
|---|
| 494 |
\code |
|---|
| 495 |
CPLErr JDEMDataset::GetGeoTransform( double * padfTransform ) |
|---|
| 496 |
|
|---|
| 497 |
{ |
|---|
| 498 |
double dfLLLat, dfLLLong, dfURLat, dfURLong; |
|---|
| 499 |
|
|---|
| 500 |
dfLLLat = JDEMGetAngle( (char *) abyHeader + 29 ); |
|---|
| 501 |
dfLLLong = JDEMGetAngle( (char *) abyHeader + 36 ); |
|---|
| 502 |
dfURLat = JDEMGetAngle( (char *) abyHeader + 43 ); |
|---|
| 503 |
dfURLong = JDEMGetAngle( (char *) abyHeader + 50 ); |
|---|
| 504 |
|
|---|
| 505 |
padfTransform[0] = dfLLLong; |
|---|
| 506 |
padfTransform[3] = dfURLat; |
|---|
| 507 |
padfTransform[1] = (dfURLong - dfLLLong) / GetRasterXSize(); |
|---|
| 508 |
padfTransform[2] = 0.0; |
|---|
| 509 |
|
|---|
| 510 |
padfTransform[4] = 0.0; |
|---|
| 511 |
padfTransform[5] = -1 * (dfURLat - dfLLLat) / GetRasterYSize(); |
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
return CE_None; |
|---|
| 515 |
} |
|---|
| 516 |
\endcode |
|---|
| 517 |
|
|---|
| 518 |
The GetProjectionRef() method returns a pointer to an internal string |
|---|
| 519 |
containing a coordinate system definition in OGC WKT format. In this case |
|---|
| 520 |
the coordinate system is fixed for all files of this format, but in more |
|---|
| 521 |
complex cases a definition may need to be composed on the fly, in which case |
|---|
| 522 |
it may be helpful to use the OGRSpatialReference class to help build the |
|---|
| 523 |
definition. |
|---|
| 524 |
|
|---|
| 525 |
\code |
|---|
| 526 |
const char *JDEMDataset::GetProjectionRef() |
|---|
| 527 |
|
|---|
| 528 |
{ |
|---|
| 529 |
return( "GEOGCS[\"Tokyo\",DATUM[\"Tokyo\",SPHEROID[\"Bessel 1841\"," |
|---|
| 530 |
"6377397.155,299.1528128,AUTHORITY[\"EPSG\",7004]],TOWGS84[-148," |
|---|
| 531 |
"507,685,0,0,0,0],AUTHORITY[\"EPSG\",6301]],PRIMEM[\"Greenwich\"," |
|---|
| 532 |
"0,AUTHORITY[\"EPSG\",8901]],UNIT[\"DMSH\",0.0174532925199433," |
|---|
| 533 |
"AUTHORITY[\"EPSG\",9108]],AXIS[\"Lat\",NORTH],AXIS[\"Long\",EAST]," |
|---|
| 534 |
"AUTHORITY[\"EPSG\",4301]]" ); |
|---|
| 535 |
} |
|---|
| 536 |
\endcode |
|---|
| 537 |
|
|---|
| 538 |
This completes explanation of the features of the JDEM driver. The full |
|---|
| 539 |
source for <a href="jdemdataset.cpp.html">jdemdataset.cpp</a> can be reviewed |
|---|
| 540 |
as needed. |
|---|
| 541 |
|
|---|
| 542 |
<h2><a name="overviews">Overviews</a></h2> |
|---|
| 543 |
|
|---|
| 544 |
GDAL allows file formats to make pre-built overviews available to applications |
|---|
| 545 |
via the GDALRasterBand::GetOverview() and related methods. However, |
|---|
| 546 |
implementing this is pretty involved, and goes beyond the scope of this |
|---|
| 547 |
document for now. The GeoTIFF driver (gdal/frmts/gtiff/geotiff.cpp) and |
|---|
| 548 |
related source can be reviewed for an example of a file format implementing |
|---|
| 549 |
overview reporting and creation support. |
|---|
| 550 |
|
|---|
| 551 |
Formats can also report that they have arbitrary overviews, by overriding |
|---|
| 552 |
the HasArbitraryOverviews() method on the GDALRasterBand, returning TRUE. |
|---|
| 553 |
In this case the raster band object is expected to override the RasterIO() |
|---|
| 554 |
method itself, to implement efficient access to imagery with resampling. |
|---|
| 555 |
This is also involved, and there are alot of requirements for correct |
|---|
| 556 |
implementation of the RasterIO() method. An example of this can be found |
|---|
| 557 |
in the ogdi and ecw formats. |
|---|
| 558 |
|
|---|
| 559 |
However, by far the most common approach to implementing overviews is to |
|---|
| 560 |
use the default support in GDAL for external overviews stored in TIFF files |
|---|
| 561 |
with the same name as the dataset, but the extension .ovr appended. In |
|---|
| 562 |
order to enable reading and creation of this style of overviews it is necessary |
|---|
| 563 |
for the GDALDataset to initialize the oOvManager object within itself. This |
|---|
| 564 |
is typically accomplished with a call like the following near the end of the |
|---|
| 565 |
Open() method. |
|---|
| 566 |
|
|---|
| 567 |
\code |
|---|
| 568 |
poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename ); |
|---|
| 569 |
\endcode |
|---|
| 570 |
|
|---|
| 571 |
This will enable default implementations for reading and creating overviews for |
|---|
| 572 |
the format. It is advised that this be enabled for all simple file system |
|---|
| 573 |
based formats unless there is a custom overview mechanism to be tied into. |
|---|
| 574 |
|
|---|
| 575 |
<h2><a name="creation">File Creation</a></h2> |
|---|
| 576 |
|
|---|
| 577 |
There are two approaches to file creation. The first method is called the |
|---|
| 578 |
CreateCopy() method, and involves implementing a function that can write a |
|---|
| 579 |
file in the output format, pulling all imagery and other information needed |
|---|
| 580 |
from a source GDALDataset. The second method, the dynamic creation method, |
|---|
| 581 |
involves implementing a Create method to create the shell of the file, and |
|---|
| 582 |
then the application writes various information by calls to set methods. |
|---|
| 583 |
|
|---|
| 584 |
The benefits of the first method are that that all the information is available |
|---|
| 585 |
at the point the output file is being created. This can be especially |
|---|
| 586 |
important when implementing file formats using external libraries which |
|---|
| 587 |
require information like colormaps, and georeferencing information at the |
|---|
| 588 |
point the file is created. The other advantage of this method is that the |
|---|
| 589 |
CreateCopy() method can read some kinds of information, such as min/max, |
|---|
| 590 |
scaling, description and GCPs for which there are no equivelent set methods. |
|---|
| 591 |
|
|---|
| 592 |
The benefits of the second method are that applications can create an |
|---|
| 593 |
empty new file, and write results to it as they become available. A complete |
|---|
| 594 |
image of the desired data does not have to be available in advance. |
|---|
| 595 |
|
|---|
| 596 |
For very important formats both methods may be implemented, otherwise do |
|---|
| 597 |
whichever is simpler, or provides the required capabilities. |
|---|
| 598 |
|
|---|
| 599 |
<h3>CreateCopy</h3> |
|---|
| 600 |
|
|---|
| 601 |
The GDALDriver::CreateCopy() method call is passed through directly, so |
|---|
| 602 |
that method should be consulted for details of arguments. However, some |
|---|
| 603 |
things to keep in mind are: |
|---|
| 604 |
|
|---|
| 605 |
<ul> |
|---|
| 606 |
<li> If the bStrict flag is FALSE the driver should try to do something |
|---|
| 607 |
reasonable when it cannot exactly represent the source dataset, transforming |
|---|
| 608 |
data types on the fly, droping georeferencing and so forth. |
|---|
| 609 |
|
|---|
| 610 |
<li> Implementing progress reporting correctly is somewhat involved. The |
|---|
| 611 |
return result of the progress function needs always to be checked for |
|---|
| 612 |
cancellation, and progress should be reported at reasonable intervals. The |
|---|
| 613 |
JPEGCreateCopy() method demonstrates good handling of the progress function. |
|---|
| 614 |
|
|---|
| 615 |
<li> Special creation options should be documented in the online help. |
|---|
| 616 |
If the options take the format "NAME=VALUE" the papszOptions list can be |
|---|
| 617 |
manipulated with CPLFetchNameValue() as demonstrated in the handling of |
|---|
| 618 |
the QUALITY and PROGRESSIVE flags for JPEGCreateCopy(). |
|---|
| 619 |
|
|---|
| 620 |
<li> The returned GDALDataset handle can be in ReadOnly or Update mode. |
|---|
| 621 |
Return it in Update mode if practical, otherwise in ReadOnly mode is fine. |
|---|
| 622 |
|
|---|
| 623 |
</ul> |
|---|
| 624 |
|
|---|
| 625 |
The full implementation of the CreateCopy function for JPEG (which is |
|---|
| 626 |
assigned to pfnCreateCopy in the GDALDriver object) is here. |
|---|
| 627 |
|
|---|
| 628 |
\verbatim |
|---|
| 629 |
static GDALDataset * |
|---|
| 630 |
JPEGCreateCopy( const char * pszFilename, GDALDataset *poSrcDS, |
|---|
| 631 |
int bStrict, char ** papszOptions, |
|---|
| 632 |
GDALProgressFunc pfnProgress, void * pProgressData ) |
|---|
| 633 |
|
|---|
| 634 |
{ |
|---|
| 635 |
int nBands = poSrcDS->GetRasterCount(); |
|---|
| 636 |
int nXSize = poSrcDS->GetRasterXSize(); |
|---|
| 637 |
int nYSize = poSrcDS->GetRasterYSize(); |
|---|
| 638 |
int nQuality = 75; |
|---|
| 639 |
int bProgressive = FALSE; |
|---|
| 640 |
|
|---|
| 641 |
// -------------------------------------------------------------------- |
|---|
| 642 |
// Some some rudimentary checks |
|---|
| 643 |
// -------------------------------------------------------------------- |
|---|
| 644 |
if( nBands != 1 && nBands != 3 ) |
|---|
| 645 |
{ |
|---|
| 646 |
CPLError( CE_Failure, CPLE_NotSupported, |
|---|
| 647 |
"JPEG driver doesn't support %d bands. Must be 1 (grey) " |
|---|
| 648 |
"or 3 (RGB) bands.\n", nBands ); |
|---|
| 649 |
|
|---|
| 650 |
return NULL; |
|---|
| 651 |
} |
|---|
| 652 |
|
|---|
| 653 |
if( poSrcDS->GetRasterBand(1)->GetRasterDataType() != GDT_Byte && bStrict ) |
|---|
| 654 |
{ |
|---|
| 655 |
CPLError( CE_Failure, CPLE_NotSupported, |
|---|
| 656 |
"JPEG driver doesn't support data type %s. " |
|---|
| 657 |
"Only eight bit byte bands supported.\n", |
|---|
| 658 |
GDALGetDataTypeName( |
|---|
| 659 |
poSrcDS->GetRasterBand(1)->GetRasterDataType()) ); |
|---|
| 660 |
|
|---|
| 661 |
return NULL; |
|---|
| 662 |
} |
|---|
| 663 |
|
|---|
| 664 |
// -------------------------------------------------------------------- |
|---|
| 665 |
// What options has the user selected? |
|---|
| 666 |
// -------------------------------------------------------------------- |
|---|
| 667 |
if( CSLFetchNameValue(papszOptions,"QUALITY") != NULL ) |
|---|
| 668 |
{ |
|---|
| 669 |
nQuality = atoi(CSLFetchNameValue(papszOptions,"QUALITY")); |
|---|
| 670 |
if( nQuality < 10 || nQuality > 100 ) |
|---|
| 671 |
{ |
|---|
| 672 |
CPLError( CE_Failure, CPLE_IllegalArg, |
|---|
| 673 |
"QUALITY=%s is not a legal value in the range 10-100.", |
|---|
| 674 |
CSLFetchNameValue(papszOptions,"QUALITY") ); |
|---|
| 675 |
return NULL; |
|---|
| 676 |
} |
|---|
| 677 |
} |
|---|
| 678 |
|
|---|
| 679 |
if( CSLFetchNameValue(papszOptions,"PROGRESSIVE") != NULL ) |
|---|
| 680 |
{ |
|---|
| 681 |
bProgressive = TRUE; |
|---|
| 682 |
} |
|---|
| 683 |
|
|---|
| 684 |
// -------------------------------------------------------------------- |
|---|
| 685 |
// Create the dataset. |
|---|
| 686 |
// -------------------------------------------------------------------- |
|---|
| 687 |
FILE *fpImage; |
|---|
| 688 |
|
|---|
| 689 |
fpImage = VSIFOpen( pszFilename, "wb" ); |
|---|
| 690 |
if( fpImage == NULL ) |
|---|
| 691 |
{ |
|---|
| 692 |
CPLError( CE_Failure, CPLE_OpenFailed, |
|---|
| 693 |
"Unable to create jpeg file %s.\n", |
|---|
| 694 |
pszFilename ); |
|---|
| 695 |
return NULL; |
|---|
| 696 |
} |
|---|
| 697 |
|
|---|
| 698 |
// -------------------------------------------------------------------- |
|---|
| 699 |
// Initialize JPG access to the file. |
|---|
| 700 |
// -------------------------------------------------------------------- |
|---|
| 701 |
struct jpeg_compress_struct sCInfo; |
|---|
| 702 |
struct jpeg_error_mgr sJErr; |
|---|
| 703 |
|
|---|
| 704 |
sCInfo.err = jpeg_std_error( &sJErr ); |
|---|
| 705 |
jpeg_create_compress( &sCInfo ); |
|---|
| 706 |
|
|---|
| 707 |
jpeg_stdio_dest( &sCInfo, fpImage ); |
|---|
| 708 |
|
|---|
| 709 |
sCInfo.image_width = nXSize; |
|---|
| 710 |
sCInfo.image_height = nYSize; |
|---|
| 711 |
sCInfo.input_components = nBands; |
|---|
| 712 |
|
|---|
| 713 |
if( nBands == 1 ) |
|---|
| 714 |
{ |
|---|
| 715 |
sCInfo.in_color_space = JCS_GRAYSCALE; |
|---|
| 716 |
} |
|---|
| 717 |
else |
|---|
| 718 |
{ |
|---|
| 719 |
sCInfo.in_color_space = JCS_RGB; |
|---|
| 720 |
} |
|---|
| 721 |
|
|---|
| 722 |
jpeg_set_defaults( &sCInfo ); |
|---|
| 723 |
|
|---|
| 724 |
jpeg_set_quality( &sCInfo, nQuality, TRUE ); |
|---|
| 725 |
|
|---|
| 726 |
if( bProgressive ) |
|---|
| 727 |
jpeg_simple_progression( &sCInfo ); |
|---|
| 728 |
|
|---|
| 729 |
jpeg_start_compress( &sCInfo, TRUE ); |
|---|
| 730 |
|
|---|
| 731 |
// -------------------------------------------------------------------- |
|---|
| 732 |
// Loop over image, copying image data. |
|---|
| 733 |
// -------------------------------------------------------------------- |
|---|
| 734 |
GByte *pabyScanline; |
|---|
| 735 |
CPLErr eErr; |
|---|
| 736 |
|
|---|
| 737 |
pabyScanline = (GByte *) CPLMalloc( nBands * nXSize ); |
|---|
| 738 |
|
|---|
| 739 |
for( int iLine = 0; iLine < nYSize; iLine++ ) |
|---|
| 740 |
{ |
|---|
| 741 |
JSAMPLE *ppSamples; |
|---|
| 742 |
|
|---|
| 743 |
for( int iBand = 0; iBand < nBands; iBand++ ) |
|---|
| 744 |
{ |
|---|
| 745 |
GDALRasterBand * poBand = poSrcDS->GetRasterBand( iBand+1 ); |
|---|
| 746 |
eErr = poBand->RasterIO( GF_Read, 0, iLine, nXSize, 1, |
|---|
| 747 |
pabyScanline + iBand, nXSize, 1, GDT_Byte, |
|---|
| 748 |
nBands, nBands * nXSize ); |
|---|
| 749 |
} |
|---|
| 750 |
|
|---|
| 751 |
ppSamples = pabyScanline; |
|---|
| 752 |
jpeg_write_scanlines( &sCInfo, &ppSamples, 1 ); |
|---|
| 753 |
} |
|---|
| 754 |
|
|---|
| 755 |
CPLFree( pabyScanline ); |
|---|
| 756 |
|
|---|
| 757 |
jpeg_finish_compress( &sCInfo ); |
|---|
| 758 |
jpeg_destroy_compress( &sCInfo ); |
|---|
| 759 |
|
|---|
| 760 |
VSIFClose( fpImage ); |
|---|
| 761 |
|
|---|
| 762 |
return (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly ); |
|---|
| 763 |
} |
|---|
| 764 |
\endverbatim |
|---|
| 765 |
|
|---|
| 766 |
<h3>Dynamic Creation</h3> |
|---|
| 767 |
|
|---|
| 768 |
In the case of dynamic creation, there is no source dataset. Instead the |
|---|
| 769 |
size, number of bands, and pixel data type of the desired file is provided |
|---|
| 770 |
but other information (such as georeferencing, and imagery data) would be |
|---|
| 771 |
supplied later via other method calls on the resulting GDALDataset. |
|---|
| 772 |
|
|---|
| 773 |
The following sample implement PCI .aux labelled raw raster creation. It |
|---|
| 774 |
follows a common approach of creating a blank, but valid file using non-GDAL |
|---|
| 775 |
calls, and then calling GDALOpen(,GA_Update) at the end to return a writable |
|---|
| 776 |
file handle. This avoids having to duplicate the various setup actions in |
|---|
| 777 |
the Open() function. |
|---|
| 778 |
|
|---|
| 779 |
\verbatim |
|---|
| 780 |
GDALDataset *PAuxDataset::Create( const char * pszFilename, |
|---|
| 781 |
int nXSize, int nYSize, int nBands, |
|---|
| 782 |
GDALDataType eType, |
|---|
| 783 |
char ** // papszParmList ) |
|---|
| 784 |
|
|---|
| 785 |
{ |
|---|
| 786 |
char *pszAuxFilename; |
|---|
| 787 |
|
|---|
| 788 |
// -------------------------------------------------------------------- |
|---|
| 789 |
// Verify input options. |
|---|
| 790 |
// -------------------------------------------------------------------- |
|---|
| 791 |
if( eType != GDT_Byte && eType != GDT_Float32 && eType != GDT_UInt16 |
|---|
| 792 |
&& eType != GDT_Int16 ) |
|---|
| 793 |
{ |
|---|
| 794 |
CPLError( CE_Failure, CPLE_AppDefined, |
|---|
| 795 |
"Attempt to create PCI .Aux labelled dataset with an illegal\n" |
|---|
| 796 |
"data type (%s).\n", |
|---|
| 797 |
GDALGetDataTypeName(eType) ); |
|---|
| 798 |
|
|---|
| 799 |
return NULL; |
|---|
| 800 |
} |
|---|
| 801 |
|
|---|
| 802 |
// -------------------------------------------------------------------- |
|---|
| 803 |
// Try to create the file. |
|---|
| 804 |
// -------------------------------------------------------------------- |
|---|
| 805 |
FILE *fp; |
|---|
| 806 |
|
|---|
| 807 |
fp = VSIFOpen( pszFilename, "w" ); |
|---|
| 808 |
|
|---|
| 809 |
if( fp == NULL ) |
|---|
| 810 |
{ |
|---|
| 811 |
CPLError( CE_Failure, CPLE_OpenFailed, |
|---|
| 812 |
"Attempt to create file `%s' failed.\n", |
|---|
| 813 |
pszFilename ); |
|---|
| 814 |
return NULL; |
|---|
| 815 |
} |
|---|
| 816 |
|
|---|
| 817 |
// -------------------------------------------------------------------- |
|---|
| 818 |
// Just write out a couple of bytes to establish the binary |
|---|
| 819 |
// file, and then close it. |
|---|
| 820 |
// -------------------------------------------------------------------- |
|---|
| 821 |
VSIFWrite( (void *) "\0\0", 2, 1, fp ); |
|---|
| 822 |
VSIFClose( fp ); |
|---|
| 823 |
|
|---|
| 824 |
// -------------------------------------------------------------------- |
|---|
| 825 |
// Create the aux filename. |
|---|
| 826 |
// -------------------------------------------------------------------- |
|---|
| 827 |
pszAuxFilename = (char *) CPLMalloc(strlen(pszFilename)+5); |
|---|
| 828 |
strcpy( pszAuxFilename, pszFilename );; |
|---|
| 829 |
|
|---|
| 830 |
for( int i = strlen(pszAuxFilename)-1; i > 0; i-- ) |
|---|
| 831 |
{ |
|---|
| 832 |
if( pszAuxFilename[i] == '.' ) |
|---|
| 833 |
{ |
|---|
| 834 |
pszAuxFilename[i] = '\0'; |
|---|
| 835 |
break; |
|---|
| 836 |
} |
|---|
| 837 |
} |
|---|
| 838 |
|
|---|
| 839 |
strcat( pszAuxFilename, ".aux" ); |
|---|
| 840 |
|
|---|
| 841 |
// -------------------------------------------------------------------- |
|---|
| 842 |
// Open the file. |
|---|
| 843 |
// -------------------------------------------------------------------- |
|---|
| 844 |
fp = VSIFOpen( pszAuxFilename, "wt" ); |
|---|
| 845 |
if( fp == NULL ) |
|---|
| 846 |
{ |
|---|
| 847 |
CPLError( CE_Failure, CPLE_OpenFailed, |
|---|
| 848 |
"Attempt to create file `%s' failed.\n", |
|---|
| 849 |
pszAuxFilename ); |
|---|
| 850 |
return NULL; |
|---|
| 851 |
} |
|---|
| 852 |
|
|---|
| 853 |
// -------------------------------------------------------------------- |
|---|
| 854 |
// We need to write out the original filename but without any |
|---|
| 855 |
// path components in the AuxilaryTarget line. Do so now. |
|---|
| 856 |
// -------------------------------------------------------------------- |
|---|
| 857 |
int iStart; |
|---|
| 858 |
|
|---|
| 859 |
iStart = strlen(pszFilename)-1; |
|---|
| 860 |
while( iStart > 0 && pszFilename[iStart-1] != '/' |
|---|
| 861 |
&& pszFilename[iStart-1] != '\\' ) |
|---|
| 862 |
iStart--; |
|---|
| 863 |
|
|---|
| 864 |
VSIFPrintf( fp, "AuxilaryTarget: %s\n", pszFilename + iStart ); |
|---|
| 865 |
|
|---|
| 866 |
// -------------------------------------------------------------------- |
|---|
| 867 |
// Write out the raw definition for the dataset as a whole. |
|---|
| 868 |
// -------------------------------------------------------------------- |
|---|
| 869 |
VSIFPrintf( fp, "RawDefinition: %d %d %d\n", |
|---|
| 870 |
nXSize, nYSize, nBands ); |
|---|
| 871 |
|
|---|
| 872 |
// -------------------------------------------------------------------- |
|---|
| 873 |
// Write out a definition for each band. We always write band |
|---|
| 874 |
// sequential files for now as these are pretty efficiently |
|---|
| 875 |
// handled by GDAL. |
|---|
| 876 |
// -------------------------------------------------------------------- |
|---|
| 877 |
int nImgOffset = 0; |
|---|
| 878 |
|
|---|
| 879 |
for( int iBand = 0; iBand < nBands; iBand++ ) |
|---|
| 880 |
{ |
|---|
| 881 |
const char * pszTypeName; |
|---|
| 882 |
int nPixelOffset; |
|---|
| 883 |
int nLineOffset; |
|---|
| 884 |
|
|---|
| 885 |
nPixelOffset = GDALGetDataTypeSize(eType)/8; |
|---|
| 886 |
nLineOffset = nXSize * nPixelOffset; |
|---|
| 887 |
|
|---|
| 888 |
if( eType == GDT_Float32 ) |
|---|
| 889 |
pszTypeName = "32R"; |
|---|
| 890 |
else if( eType == GDT_Int16 ) |
|---|
| 891 |
pszTypeName = "16S"; |
|---|
| 892 |
else if( eType == GDT_UInt16 ) |
|---|
| 893 |
pszTypeName = "16U"; |
|---|
| 894 |
else |
|---|
| 895 |
pszTypeName = "8U"; |
|---|
| 896 |
|
|---|
| 897 |
VSIFPrintf( fp, "ChanDefinition-%d: %s %d %d %d %s\n", |
|---|
| 898 |
iBand+1, pszTypeName, |
|---|
| 899 |
nImgOffset, nPixelOffset, nLineOffset, |
|---|
| 900 |
#ifdef CPL_LSB |
|---|
| 901 |
"Swapped" |
|---|
| 902 |
#else |
|---|
| 903 |
"Unswapped" |
|---|
| 904 |
#endif |
|---|
| 905 |
); |
|---|
| 906 |
|
|---|
| 907 |
nImgOffset += nYSize * nLineOffset; |
|---|
| 908 |
} |
|---|
| 909 |
|
|---|
| 910 |
// -------------------------------------------------------------------- |
|---|
| 911 |
// Cleanup |
|---|
| 912 |
// -------------------------------------------------------------------- |
|---|
| 913 |
VSIFClose( fp ); |
|---|
| 914 |
|
|---|
| 915 |
return (GDALDataset *) GDALOpen( pszFilename, GA_Update ); |
|---|
| 916 |
} |
|---|
| 917 |
\endverbatim |
|---|
| 918 |
|
|---|
| 919 |
File formats supporting dynamic creation, or even just update-in-place |
|---|
| 920 |
access also need to implement an IWriteBlock() method |
|---|
| 921 |
on the raster band class. It has semantics similar to IReadBlock(). |
|---|
| 922 |
As well, for various esoteric reasons, it is critical that a FlushCache() |
|---|
| 923 |
method be implemented in the raster band destructor. This is to ensure that |
|---|
| 924 |
any write cache blocks for the band be flushed out before the destructor |
|---|
| 925 |
is called. |
|---|
| 926 |
|
|---|
| 927 |
<h2><a name="raw">RawDataset/RawRasterBand Helper Classes</a></h2> |
|---|
| 928 |
|
|---|
| 929 |
Many file formats have the actual imagery data stored in a regular, |
|---|
| 930 |
binary, scanline oriented format. Rather than re-implement the access |
|---|
| 931 |
semantics for this for each formats, there are provided RawDataset and |
|---|
| 932 |
RawRasterBand classes declared in gdal/frmts/raw that can be utilized to |
|---|
| 933 |
implement efficient and convenient access. |
|---|
| 934 |
|
|---|
| 935 |
In these cases the format specific band class may not be required, or if |
|---|
| 936 |
required it can be derived from RawRasterBand. The dataset class should |
|---|
| 937 |
be derived from RawDataset. |
|---|
| 938 |
|
|---|
| 939 |
The Open() method for the dataset then instantiates raster bands passing |
|---|
| 940 |
all the layout information to the constructor. For instance, the PNM driver |
|---|
| 941 |
uses the following calls to create it's raster bands. |
|---|
| 942 |
|
|---|
| 943 |
\code |
|---|
| 944 |
if( poOpenInfo->pabyHeader[1] == '5' ) |
|---|
| 945 |
{ |
|---|
| 946 |
poDS->SetBand( |
|---|
| 947 |
1, new RawRasterBand( poDS, 1, poDS->fpImage, |
|---|
| 948 |
iIn, 1, nWidth, GDT_Byte, TRUE )); |
|---|
| 949 |
} |
|---|
| 950 |
else |
|---|
| 951 |
{ |
|---|
| 952 |
poDS->SetBand( |
|---|
| 953 |
1, new RawRasterBand( poDS, 1, poDS->fpImage, |
|---|
| 954 |
iIn, 3, nWidth*3, GDT_Byte, TRUE )); |
|---|
| 955 |
poDS->SetBand( |
|---|
| 956 |
2, new RawRasterBand( poDS, 2, poDS->fpImage, |
|---|
| 957 |
iIn+1, 3, nWidth*3, GDT_Byte, TRUE )); |
|---|
| 958 |
poDS->SetBand( |
|---|
| 959 |
3, new RawRasterBand( poDS, 3, poDS->fpImage, |
|---|
| 960 |
iIn+2, 3, nWidth*3, GDT_Byte, TRUE )); |
|---|
| 961 |
} |
|---|
| 962 |
\endcode |
|---|
| 963 |
|
|---|
| 964 |
The RawRasterBand takes the following arguments. |
|---|
| 965 |
|
|---|
| 966 |
<ul> |
|---|
| 967 |
<li> <b>poDS</b>: The GDALDataset this band will be a child of. This |
|---|
| 968 |
dataset must be of a class derived from RawRasterDataset. |
|---|
| 969 |
<li> <b>nBand</b>: The band it is on that dataset, 1 based. |
|---|
| 970 |
<li> <b>fpRaw</b>: The FILE * handle to the file containing the raster data. |
|---|
| 971 |
<li> <b>nImgOffset</b>: The byte offset to the first pixel of raster data for |
|---|
| 972 |
the first scanline. |
|---|
| 973 |
<li> <b>nPixelOffset</b>: The byte offset from the start of one pixel to the |
|---|
| 974 |
start of the next within the scanline. |
|---|
| 975 |
<li> <b>nLineOffset</b>: The byte offset from the start of one scanline to |
|---|
| 976 |
the start of the next. |
|---|
| 977 |
<li> <b>eDataType</b>: The GDALDataType code for the type of the data on disk. |
|---|
| 978 |
<li> <b>bNativeOrder</b>: FALSE if the data is not in the same endianness as |
|---|
| 979 |
the machine GDAL is running on. The data will be automatically byte swapped. |
|---|
| 980 |
</ul> |
|---|
| 981 |
|
|---|
| 982 |
Simple file formats utilizing the Raw services are normally placed all within |
|---|
| 983 |
one file in the gdal/frmts/raw directory. There are numerous examples there |
|---|
| 984 |
of format implementation.<p> |
|---|
| 985 |
|
|---|
| 986 |
<h2><a name="metadata">Metadata, and Other Exotic Extensions</a></h2> |
|---|
| 987 |
|
|---|
| 988 |
There are various other items in the GDAL data model, for which virtual |
|---|
| 989 |
methods exist on the GDALDataset and GDALRasterBand. They include: |
|---|
| 990 |
|
|---|
| 991 |
<ul> |
|---|
| 992 |
<li> <b>Metadata</b>: Name/value text values about a dataset or band. The |
|---|
| 993 |
GDALMajorObject (base class for GDALRasterBand and GDALDataset) has builtin |
|---|
| 994 |
support for holding metadata, so for read access it only needs to be |
|---|
| 995 |
set with calls to SetMetadataItem() during the Open(). The SAR_CEOS |
|---|
| 996 |
(frmts/ceos2/sar_ceosdataset.cpp) and GeoTIFF drivers are examples of drivers |
|---|
| 997 |
implementing readable metadata. |
|---|
| 998 |
|
|---|
| 999 |
<li> <b>ColorTables</b>: GDT_Byte raster bands can have color tables associated |
|---|
| 1000 |
with them. The frmts/png/pngdataset.cpp driver contains an example of a |
|---|
| 1001 |
format that supports colortables. |
|---|
| 1002 |
|
|---|
| 1003 |
<li> <b>ColorInterpretation</b>: The PNG driver contains an example of a |
|---|
| 1004 |
driver that returns an indication of whether a band should be treated as |
|---|
| 1005 |
a Red, Green, Blue, Alpha or Greyscale band. |
|---|
| 1006 |
|
|---|
| 1007 |
<li> <b>GCPs</b>: GDALDatasets can have a set of ground control points |
|---|
| 1008 |
associated with them (as opposed to an explicit affine transform returned by |
|---|
| 1009 |
GetGeotransform()) relating the raster to georeferenced coordinates. The |
|---|
| 1010 |
MFF2 (gdal/frmts/raw/hkvdataset.cpp) format is a simple example of a format |
|---|
| 1011 |
supporting GCPs. |
|---|
| 1012 |
|
|---|
| 1013 |
<li> <b>NoDataValue</b>: Bands with known "nodata" values can implement |
|---|
| 1014 |
the GetNoDataValue() method. See the PAux (frmts/raw/pauxdataset.cpp) for |
|---|
| 1015 |
an example of this. |
|---|
| 1016 |
|
|---|
| 1017 |
<li> <b>Category Names</b>: Classified images with names for each class can |
|---|
| 1018 |
return them using the GetCategoryNames() method though no formats currently |
|---|
| 1019 |
implement this. |
|---|
| 1020 |
|
|---|
| 1021 |
</ul> |
|---|
| 1022 |
|
|---|
| 1023 |
*/ |
|---|