Geoscience Reference
In-Depth Information
For this example, the GDAL developer guidelines
16
are followed. Memory is
allocated and freed via the CPL functions
CPLMalloc
and
CPLFree
rather than
the more C++ like
new
and
delete
. Alternatively, you can also use
vector
from
the standard template library (STL), if you prefer generic programming. Though
GDAL does not support template types directly, you can pass a pointer to the first
vector element as an argument to
RasterIO
(after including
vector
in your
source file).
17
std::vector<
unsigned short
> vbuffer(ncol);
poBand->RasterIO(GF_Read, nXOff, nYoff, buffer.size(), 1,
→
&(buffer[0]), buffer.size(), 1, GDT_UInt16, 0, 0);
14.3.4 Create and Write Raster Files
Creating and writing raster files is a bit more complex than reading them. When you
open a raster file using the GDAL API, all relevant information from the raster file is
automatically available: projection, metadata, image format, data type and number
of bands, lines and columns. When you open a raster file for writing, this information
must be provided by you. Through the GDAL API, much of the internals of how to
create the different image formats is hidden from you as a programmer. However,
there are a few restrictions you must take into account.
One of the restrictions is that not all formats can be created in a similar way.
Some can be created using
Create
, whereas others can only be created using
CreateCopy
. Both are methods from the
GDALDriver
class. The first method,
Create
, allows you to create a new dataset from scratch. You explicitly provide all
relevant information of the dataset: image filename, format, size, number of bands
and data type. Some of the information is optional and format specific: metadata,
band encoding, compression, etc. However, only a limited set of drivers support this
Create
method.
18
Formats such as JPEG, PNG and GIF require the
CreateCopy
method. In this case you must use a source dataset as a template to create your new
target dataset. If not available, you can first create a virtual dataset, which does not
contain any real raster data values, and use that as a template. In Sect.
14.3.6
examples
are provided on how to create datasets using the
CreateCopy
method.
To find out whether or not a particular driver supports the
Create
method, you
can first call a function
CSLFetchBoolean
. It is good practice to test this function
before attempting a
Create
.
17
The API offered by pktools has built a wrapper around the GDAL API which supports the STL
vector.
18
All formats indicating
rw+
when listed with
gdalinfo—-formats
.
Search WWH ::
Custom Search