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 (or OGRVRTWarpedLayer or OGRVRTUnionLayer starting with GDAL 1.10.0) child for each layer in the virtual datasource.

A XML schema of the OGR VRT format is available. Starting with GDAL 1.11, when GDAL is configured with libXML2 support, that schema will be used to validate the VRT documents. Non-conformities will be reported only as warnings. That validation can be disabled by setting the GDAL_XML_VALIDATION configuration option to NO.

A OGRVRTLayer element should have a name attribute with the layer name, and may have the following subelements:

A OGRVRTWarpedLayer element (GDAL >= 1.10.0) is used to do on-the-fly reprojection of a source layer. It may have the following subelements:

A OGRVRTUnionLayer element (GDAL >= 1.10.0) is used to concatenate the content of source layers. It should have a 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 useful 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>

Example: Reprojected layer (GDAL >= 1.10.0)

The following example will return the source.shp layer reprojected to EPSG:4326.

<OGRVRTDataSource>
    <OGRVRTWarpedLayer>
        <OGRVRTLayer name="source">
            <SrcDataSource>source.shp</SrcDataSource>
        </OGRVRTLayer>
        <TargetSRS>EPSG:4326</TargetSRS>
    </OGRVRTWarpedLayer>
</OGRVRTDataSource>

Example: Union layer (GDAL >= 1.10.0)

The following example will return a layer that is the concatenation of source1.shp and source2.shp.

<OGRVRTDataSource>
    <OGRVRTUnionLayer name="unionLayer">
        <OGRVRTLayer name="source1">
            <SrcDataSource>source1.shp</SrcDataSource>
        </OGRVRTLayer>
        <OGRVRTLayer name="source2">
            <SrcDataSource>source2.shp</SrcDataSource>
        </OGRVRTLayer>
    </OGRVRTUnionLayer>
</OGRVRTDataSource>

Example: SQLite/Spatialite SQL dialect (GDAL >=1.10.0)

The following example will return four different layers which are generated in a fly from the same polygon shapefile. The first one is the shapefile layer as it stands. The second layer gives simplified polygons by applying Spatialite function "Simplify" with parameter tolerance=10. In the third layer the original geometries are replaced by their convace hulls. In the fourth layer Spatialite function PointOnSurface is used for replacing the original geometries by points which are inside the corresponding source polygons. Note that for using the last three layers of this VRT file GDAL must be compiled with SQLite and Spatialite.

<OGRVRTDataSource>
    <OGRVRTLayer name="polygons">
        <SrcDataSource>polygons.shp</SrcDataSource>
        </OGRVRTLayer>
    <OGRVRTLayer name="polygons_as_simplified">
        <SrcDataSource>polygons.shp</SrcDataSource>
        <SrcSQL dialect="sqlite">SELECT Simplify(geometry,10) from polygons</SrcSQL>
    </OGRVRTLayer>
    <OGRVRTLayer name="polygons_as_hulls">
        <SrcDataSource>polygons.shp</SrcDataSource>
        <SrcSQL dialect="sqlite">SELECT ConvexHull(geometry) from polygons</SrcSQL>
    </OGRVRTLayer>
    <OGRVRTLayer name="polygons_as_points">
        <SrcDataSource>polygons.shp</SrcDataSource>
        <SrcSQL dialect="sqlite">SELECT PointOnSurface(geometry) from polygons</SrcSQL>
    </OGRVRTLayer>
</OGRVRTDataSource>

Example: Multiple geometry fields (GDAL >= 1.11)

The following example will expose all the attribute and geometry fields of the source layer:

<OGRVRTDataSource>
    <OGRVRTLayer name="test">
        <SrcDataSource>PG:dbname=testdb</SrcDataSource>
    </OGRVRTLayer>
</OGRVRTDataSource>

To expose only part (or all!) of the fields:

<OGRVRTDataSource>
    <OGRVRTLayer name="other_test">
        <SrcDataSource>PG:dbname=testdb</SrcDataSource>
        <SrcLayer>test</SrcLayer>
        <GeometryField name="pg_geom_field_1" />
        <GeometryField name="vrt_geom_field_2" field="pg_geom_field_2">
            <GeometryType>wkbPolygon</GeometryType>
            <SRS>EPSG:4326</SRS>
            <ExtentXMin>-180</ExtentXMin>
            <ExtentYMin>-90</ExtentYMin>
            <ExtentXMax>180</ExtentXMax>
            <ExtentYMax>90</ExtentYMax>
        </GeometryField>
        <Field name="vrt_field_1" src="src_field_1" />
    </OGRVRTLayer>w
</OGRVRTDataSource>
To reproject the 'pg_geom_field_2' geometry field to EPSG:4326:

<OGRVRTDataSource>
    <OGRVRTWarpedLayer>
        <OGRVRTLayer name="other_test">
            <SrcDataSource>PG:dbname=testdb</SrcDataSource>
        </OGRVRTLayer>
        <WarpedGeomFieldName>pg_geom_field_2</WarpedGeomFieldName>
        <TargetSRS>EPSG:32631</TargetSRS>
    </OGRVRTWarpedLayer>
</OGRVRTDataSource>
To make the union of several multi-geometry layers and keep only a few of them:

<OGRVRTDataSource>
    <OGRVRTUnionLayer name="unionLayer">
        <OGRVRTLayer name="source1">
            <SrcDataSource>PG:dbname=testdb</SrcDataSource>
        </OGRVRTLayer>
        <OGRVRTLayer name="source2">
            <SrcDataSource>PG:dbname=testdb</SrcDataSource>
        </OGRVRTLayer>
        <GeometryField name="pg_geom_field_2">
            <GeometryType>wkbPolygon</GeometryType>
            <SRS>EPSG:4326</SRS>
            <ExtentXMin>-180</ExtentXMin>
            <ExtentYMin>-90</ExtentYMin>
            <ExtentXMax>180</ExtentXMax>
            <ExtentYMax>90</ExtentYMax>
        </GeometryField>
        <GeometryField name="pg_geom_field_3" />
        <Field name="src_field_1" />
    </OGRVRTUnionLayer>
</OGRVRTDataSource>

Other Notes