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 :

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:

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