Virtual Format
OGR Virtual Format is a driver that transforms features read from other
drivers based on criteria specified in an XML control file. It is primarily
used to derive spatial layers from flat tables with spatial information in
attribute columns. It can also be used to associate coordinate system
information with a datasource, merge layers from different datasources into
a single data source, or even just to provide an anchor file for access to
non-file oriented datasources.
The virtual files are currently normally prepared by hand.
Creation Issues
Before GDAL 1.7.0, the OGR VRT driver was read-only.
Since GDAL 1.7.0, the CreateFeature(), SetFeature() and DeleteFeature() operations
are supported on a layer of a VRT dataset, if the following conditions are met :
- the VRT dataset is opened in update mode
- the underlying source layer supports those operations
- the SrcLayer element is used (as opposed to the SrcSQL element)
- the FID of the VRT features is the same as the FID of the source features, that
is to say, the FID element is not specified
Virtual File Format
The root element of the XML control file is OGRVRTDataSource. It has
an OGRVRTLayer child for each layer in the virtual datasource. That
element should have a name attribute with the layer name, and may have the
following subelements:
- SrcDataSource (mandatory): The value is the name of the datasource
that this layer will be derived from. The element may optionally have
a relativeToVRT attribute which defaults to "0", but if "1" indicates
that the source datasource should be interpreted as relative to the virtual
file. This can be any OGR supported dataset, including ODBC, CSV, etc.
The element may also have a shared attribute to control whether the
datasource should be opened in shared mode. Defaults to OFF for SrcLayer use and ON for SrcSQL use.
- SrcLayer (optional): The value is the name of the layer on the
source data source from which this virtual layer should be derived. If this
element isn't provided, then the SrcSQL element must be provided.
- SrcSQL (optional): An SQL statement to execute to generate the
desired layer result. This should be provided instead of the SrcLayer for
statement derived results. Some limitations may apply for SQL derived
layers.
- FID (optional): Name of the attribute column from which the
FID of features should be derived. If not provided, the FID of the source
features will be used directly.
- Style (optional): Name of the attribute column from which the
style of features should be derived. If not provided, the style of the source
features will be used directly.
- GeometryType (optional): The geometry type to be assigned to the
layer.
If not provided it will be taken from the source layer. The value should
be one of "wkbNone", "wkbUnknown", "wkbPoint", "wkbLineString", "wkbPolygon",
"wkbMultiPoint",
"wkbMultiLineString", "wkbMultiPolygon", or "wkbGeometryCollection".
Optionally "25D" may be appended to mark it as including Z coordinates.
Defaults to "wkbUnknown" indicating that any geometry type is allowed.
- LayerSRS (optional): The value of this element is the spatial
reference to use for the layer. If not provided, it is inherited from the
source layer. The value may be WKT or any other input that is accepted
by the OGRSpatialReference::SetUserInput() method. If the value is NULL, then
no SRS will be used for the layer.
- GeometryField (optional): This element is used to define
how the geometry for features should be derived.
If not provided the geometry of the source feature is copied directly.
The type of geometry encoding is indicated with the encoding attribute which may have the
value "WKT", "WKB" or "PointFromColumns".
If the encoding is "WKT" or "WKB" then the field attribute will have
the name of the field containing the WKT or WKB geometry.
If the encoding is "PointFromColumns" then the x, y and z attributes
will have the names of the columns to be used for the X, Y and Z coordinates.
The z attribute is optional.
Starting with GDAL 1.7.0, the optional reportSrcColumn attribute can be used to specify
whether the source geometry fields (the fields set in the field, x, x
or z attributes) should be reported as fields of the VRT layer. It defaults to TRUE.
If set to FALSE, the source geometry fields will only be used to build the geometry of the features of the VRT layer.
- SrcRegion (optionnal, from GDAL 1.7.0) : This element is used
to define an initial spatial filter for the source features. This spatial
filter will be combined with any spatial filter explicitely set on the VRT
layer with the SetSpatialFilter() method. The value of the element must be
a valid WKT string defining a polygon. An optional clip attribute
can be set to "TRUE" to clip the geometries to the source region, otherwise
the source geometries are not modified.
- Field (optional, from GDAL 1.7.0): One or more attribute fields
may be defined with Field elements. If no Field elements are defined, the
fields of the source layer/sql will be defined on the VRT layer. The Field
may have the following attributes:
- name (required): the name of the field.
- type: the field type, one of "Integer", "IntegerList",
"Real", "RealList", "String", "StringList", "Binary", "Date", "Time",
or "DateTime" - defaults to "String".
- width: the field width, defaults to unknown.
- precision: the field width, defaults to zero.
- src: the name of the source field to be copied to this one. By
default defaults to the value of "name".
Example: ODBC Point Layer
In the following example (disease.ovf) the worms table from the ODBC
database DISEASE is used to form a spatial layer. The virtual file
uses the "x" and "y" columns to get the spatial location. It also marks
the layer as a point layer, and as being in the WGS84 coordinate system.
<OGRVRTDataSource>
<OGRVRTLayer name="worms">
<SrcDataSource>ODBC:DISEASE,worms</SrcDataSource>
<SrcLayer>worms</SrcLayer>
<GeometryType>wkbPoint</GeometryType>
<LayerSRS>WGS84</LayerSRS>
<GeometryField encoding="PointFromColumns" x="x" y="y"/>
</OGRVRTLayer>
</OGRVRTDataSource>
Example: Renaming attributes
It can be usefull in some circumstances to be able to rename the field names
from a source layer to other names. This is particularly true when you want
to transcode to a format whose schema is fixed, such as GPX
(<name>, <desc>, etc.). This can be accomplished using
SQL this way:
<OGRVRTDataSource>
<OGRVRTLayer name="remapped_layer">
<SrcDataSource>your_source.shp</SrcDataSource>
<SrcSQL>SELECT src_field_1 AS name, src_field_2 AS desc FROM your_source_layer_name</SrcSQL>
</OGRVRTLayer>
</OGRVRTDataSource>
This can also be accomplished (from GDAL 1.7.0) using explicit field
definitions:
<OGRVRTDataSource>
<OGRVRTLayer name="remapped_layer">
<SrcDataSource>your_source.shp</SrcDataSource>
<SrcLayer>your_source</SrcSQL>
<Field name="name" src="src_field_1" />
<Field name="desc" src="src_field_2" type="String" width="45" />
</OGRVRTLayer>
</OGRVRTDataSource>
Example: Transparent spatial filtering (GDAL >= 1.7.0)
The following example will only return features from the source layer that
intersect the (0,40)-(10,50) region. Furthermore, returned geometries will be clipped
to fit into that region.
<OGRVRTDataSource>
<OGRVRTLayer name="source">
<SrcDataSource>source.shp</SrcDataSource>
<SrcRegion clip="true">POLYGON((0 40,10 40,10 50,0 50,0 40))</SrcRegion>
</OGRVRTLayer>
</OGRVRTDataSource>
Other Notes
- When the GeometryField is "WKT" spatial filtering is applied
after extracting all rows from the source datasource. Essentially that means
there is no fast spatial filtering on WKT derived geometries.
- When the GeometryField is "PointFromColumns", and a SrcLayer (as opposed
to SrcSQL) is used, and a spatial filter is in effect on the virtual layer
then the spatial filter will be internally translated into an attribute filter
on the X and Y columns in the SrcLayer. In cases where fast spatial filtering
is important it can be helpful to index the X and Y columns in the source
datastore, if that is possible. For instance if the source is an RDBMS.
You can turn off that feature by setting the useSpatialSubquery attribute
of the GeometryField element to FALSE.
- Normally the SrcDataSource is a non-spatial tabular format (such as
MySQL, SQLite, CSV, OCI, or ODBC) but it can also be a spatial database in which
case the geometry can be directly copied over.