/*!
\page gdal_vrttut
GDAL Virtual Format Tutorial
Introduction
The VRT driver is a format driver for GDAL that allows a virtual GDAL dataset
to be composed from other GDAL datasets with repositioning, and algorithms
potentially applied as well as various kinds of metadata altered or added.
VRT descriptions of datasets can be saved in an XML format normally given the
extension .vrt.
An example of a simple .vrt file referring to a 512x512 dataset with one band
loaded from utm.tif might look like this:
\code
4.4072000000000000e+05, 6.0000000000000000e+01, 0.0000000000000000e+00, 3.7513200000000000e+06, 0.0000000000000000e+00, -6.0000000000000000e+01
Gray
utm.tif
1
\endcode
VRT files can be produced by translating to VRT format. The resulting file can
then be edited to modify mappings, add metadata or other purposes. VRT files
can also be produced programmatically by various means.
This tutorial will cover the .vrt file format (suitable for users editing
.vrt files), and how .vrt files may be created an manipulated programmatically
for developers.
.vrt Format
Virtual files stored on disk are kept in an XML format with the following
elements.
-
: This is the root element for the whole GDAL dataset.
It must have the attributes rasterXSize and rasterYSize describing the width
and height of the dataset in pixels. It may have SRS, GeoTransform,
GCPList, Metadata, and VRTRasterBand subelements.
\code
\endcode
- : This element contains the spatial reference system (coordinate
system) in OGC WKT format. Note that this must be appropriately escaped for
XML, so items like quotes will have the ampersand escape sequences substituted.
\code
PROJCS["NAD27 / UTM zone 11N",GEOGCS["NAD27",DATUM["North_American_Datum_1927",SPHEROID["Clarke 1866",6378206.4,294.9786982139006,AUTHORITY["EPSG","7008"]],AUTHORITY["EPSG","6267"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4267"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-117],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","26711"]]
\endcode
- : This element contains a six value affine
geotransformation for the dataset, mapping between pixel/line coordinates
and georeferenced coordinates. The list of values is the data of the SRS
element, and the values are separated by commas.
\code
440720.0, 60, 0.0, 3751320.0, 0.0, -60.0
\endcode
- : This element contains a list of metadata name/value
pairs associated with the VRTDataset as a whole, or a VRTRasterBand. It has
(metadata item) subelements which have a "key" attribute and the value
as the data of the element.
\code
Metadata value
\endcode
- : This represents one band of a dataset. It will
have a dataType attribute with the type of the pixel data associated with
this band (use names Byte, UInt16, Int16, UInt32, Int32, Float32, Float64,
CInt16, CInt32, CFloat32 or CFloat64) and the band this element represents
(1 based). This element may have Metadata, ColorInterp, NoDataValue,
ColorTable, and Description subelements as well as the various kinds of
source elements such as SimpleSource. A raster band may have many "sources"
indicating where the actual raster data should be fetched from, and how it
should be mapped into the raster bands pixel space.
- : The data of this element should be the name of
a color interpretation type. One of Gray, Palette, Red, Green, Blue, Alpha,
Hue, Saturation, Lightness, Cyan, Magenta, Yellow, Black, or Unknown.
\code
Gray:
\endcode
- : If this element exists a raster band has a
nodata value associated with, of the value given as data in the element.
\code
-100.0
\endcode
- : This element is parent to a set of Entry
elements defining the entries in a color table. Currently only RGBA
color tables are supported with c1 being red, c2 being green, c3 being
blue and c4 being alpha. The entries are ordered and will be assumed
to start from color table entry 0.
\code
\endcode
- : This element contains the optional description
of a raster band as it's text value.
\code
Crop Classification Layer
\endcode
- : The SimpleSource indicates that raster data
should be read from a separate dataset, indicating the dataset, and band to be
read from, and how the data should map into this bands raster space.
The SimpleSource may have the SourceFilename, SourceBand, SrcRect, and DstRect
subelements. The SrcRect element will indicate what rectangle on the indicated
source file should be read, and the DstRect element indicates how that
rectangle of source data should be mapped into the VRTRasterBands space.
\code
utm.tif
1
\endcode
- : This is a pixel source derived from the
Simple Source (so it shares the SourceFilename, SourceBand, SrcRect and
DestRect elements, but it also passes the data through a simple filtering
kernel specified with the Kernel element. The Kernel element should have
two child elements, Size and Coefs and optionally the boolean attribute
normalized (defaults to false=0). The size must always be an odd number,
and the Coefs must have Size * Size entries separated by spaces.
\code
/debian/home/warmerda/openev/utm.tif
1
3
0.11111111 0.11111111 0.11111111 0.11111111 0.11111111 0.11111111 0.11111111 0.11111111 0.11111111
\endcode
Programatic Creation of VRT Datasets
The VRT driver supports several methods of creating VRT datasets. As of
GDAL 1.2.0 the vrtdataset.h include file should be installed with the core
GDAL include files, allowing direct access to the VRT classes. However,
even without that most capabilities remain available through standard GDAL
interfaces.
To create a VRT dataset that is a clone of an existing dataset use the
CreateCopy() method. For example to clone utm.tif into a wrk.vrt file in
C++ the following could be used:
\code
GDALDriver *poDriver = (GDALDriver *) GDALGetDriverByName( "VRT" );
GDALDataset *poSrcDS, *poVRTDS;
poSrcDS = (GDALDataset *) GDALOpenShared( "utm.tif", GA_ReadOnly );
poVRTDS =
poDriver->CreateCopy( "wrk.vrt", poSrcDS, FALSE, NULL, NULL, NULL );
delete poVRTDS;
delete poSrcDS;
\endcode
To create a virtual copy of a dataset with some attributes added or changed
such as metadata or coordinate system that are often hard to change on other
formats, you might do the following. In this case, the virtual dataset is
created "in memory" only by virtual of creating it with an empty filename, and
then used as a modified source to pass to a CreateCopy() written out in TIFF
format.
\code
poVRTDS = poDriver->CreateCopy( "", poSrcDS, FALSE, NULL, NULL, NULL );
poVRTDS->SetMetadataItem( "SourceAgency", "United States Geological Survey");
poVRTDS->SetMetadataItem( "SourceDate", "July 21, 2003" );
poVRTDS->GetRasterBand( 1 )->SetNoDataValue( -999.0 );
GDALDriver *poTIFFDriver = (GDALDriver *) GDALGetDriverByName( "GTiff" );
GDALDataset *poTiffDS;
poTiffDS =
poTIFFDriver->CreateCopy( "wrk.tif", poVRTDS, FALSE, NULL, NULL, NULL );
delete poTiffDS;
\endcode
In this example a virtual dataset is created with the Create() method, and
adding bands and sources programmatically, but still via the "generic" API.
A special attribute of VRT datasets is that sources can be added to the bands
by passing the XML describing the source into SetMetadata() on the special
domain target "new_vrt_sources". The domain target "vrt_sources" may also be
used, in which case any existing sources will be discarded before adding the
new ones. In this example we construct a simple averaging filter source
instead of using the simple source.
\code
// construct XML for simple 3x3 average filter kernel source.
const char *pszFilterSourceXML =
""
" utm.tif11"
" "
" 3"
" 0.111 0.111 0.111 0.111 0.111 0.111 0.111 0.111 0.111"
" "
"";
// Create the virtual dataset.
poVRTDS = poDriver->Create( "", 512, 512, 1, GDT_Byte, NULL );
poVRTDS->GetRasterBand(1)->SetMetadataItem("source_0",pszFilterSourceXML",
"new_vrt_sources");
\endcode
A more general form of this that will produce a 3x3 average filtered clone
of any input datasource might look like the following. In this case we
deliberately set the filtered datasource as in the "vrt_sources" domain
to override the SimpleSource created by the CreateCopy() method. The fact
that we used CreateCopy() ensures that all the other metadata, georeferencing
and so forth is preserved from the source dataset ... the only thing we are
changing is the data source for each band.
\code
int nBand;
GDALDriver *poDriver = (GDALDriver *) GDALGetDriverByName( "VRT" );
GDALDataset *poSrcDS, *poVRTDS;
poSrcDS = (GDALDataset *) GDALOpenShared( pszSourceFilename, GA_ReadOnly );
poVRTDS = poDriver->CreateCopy( "", poSrcDS, FALSE, NULL, NULL, NULL );
for( nBand = 1; nBand <= poVRTDS->GetRasterCount(); nBand++ )
{
char szFilterSourceXML[10000];
GDALRasterBand *poBand = poVRTDS->GetRasterBand( nBand );
sprintf( szFilterSourceXML,
""
" %s1%d"
" "
" 3"
" 0.111 0.111 0.111 0.111 0.111 0.111 0.111 0.111 0.111"
" "
"",
pszSourceFilename, nBand );
poBand->SetMetadataItem( "source_0", szFilterSourceXML, "vrt_sources" );
}
\endcode
*/