Virtual Format for GDAL/OGR

TLDR; I use VRTs for everything that I can.

VRT format is developed by the GDAL/OGR group and is an XML file format that is used by the GDAL/OGR programs and libraries to transform source GIS datasets on the fly.

VRT is not an anacronym (I couldn’t find an anacronym anyway), but instead it seems to be letters taken from the word ViRTual. It can operate on either rasters or vector datasets without identification in the extension which is always ‘.vrt’. You cannot mix raster and vector data sources in the same VRT file. The source datasets can be any format that the local install of GDAL/OGR can read.

When necessary or expedient you can chain VRTs, for example setting up a raster mosaicing VRT that uses other VRTs as source datasets.

Raster VRT Start

An easy way to setup a foundation VRT for raster datasets is to use the “gdalbuildvrt” command line tool that is a part of any GDAL install. The “gdalbuildvrt” tool extracts necessary information from the raster source datasets for inclusion into the VRT. Once setup you can add or modify the VRT as needed.

Raster Look Up Table (LUT)

The complete VRT I use in this example is vrts_are_amazing/agwo_2000_03.vrt

An operation unique to a raster VRT is the ability to display or work with values from a look up table. In my work this allows a single integer raster of land cover codes or soil types to be the source for thousands of VRTs that represent some characteristic of the source raster through a look up table.

For example, this look up table (incomplete and shown here as an example of the structure):


will take this integer land cover raster:

../_images/lue.png ../_images/lue_legend.png

and make the following float raster of baseflow estimates for 2000-03 on the fly.


You would also have to set the type of the VRT to accomodate the real numbers in the look up table.:

<VRTRasterBand dataType="Float32" band="1">

Vector SQL

VRTs support SQL statements for vector formats that can access and make calculations against any item in the source dataset. There are two SQL dialects. The GDAL/OGR dialect is available no matter what, but the SQLite dialect is only available if the GDAL/OGR libraries where compiled to support it. Go to for information on the OGR SQL, and for information on the SQLite dialect. Also if Spatialite support is included when compiling the OGR libraries additional spatial functions are available with the SQLite dialect.

An example vector VRT that is a mosaic of shapefiles along with development of a unique ID is vrts_are_amazing/Final_wsheds_mosaic.vrt

Vector Selection by Attributes

A nice use is to create subsets of the source dataset.:


Calculation on Attributes of Vector Features

Convert “Depth_ft” to a sane system of measures.:

<SrcSQL>SELECT Depth_ft*0.3048 AS Depth_m FROM SOURCETABLE</SrcSQL>

If Spatialite support was included in the OGR libraries you can even have the SQL command calculate the area.:

<SrcSQL dialect="SQLITE">SELECT *,ST_Area(GEOMETRY)*0.00024710439 AS Area_Acre FROM SOURCETABLE</SrcSQL>

Or dissolve polygons based on attribute values.:


Create VRT from a CSV File

This is a very simple point VRT using longitude and latitude and attributes from a CSV file and projecting to the Florida Albers projection (EPSG:3087).:

    <OGRVRTLayer name="NWIS_Daily_Values_flow_stations">
      <SrcDataSource relativeToVRT="1">NWIS_Daily_Values_flow_stations.csv</SrcDataSource>
      <GeometryField encoding="PointFromColumns" x="Longitude" y="Latitude"/>

That is the entire file. I think that is really cool.

Vector Layer Creation From Other Layers Using SQL

The following VRT will create a new polygon layer called “joined” using Spatiallite ST_INTERSECTS from the other layers within the VRT. It also creates a new attribute “total_capacity” which is the total number of beds in all of the nursing homes in each borough. Note that this VRT refers to itself (“input.vrt”) it order to access the data in the other layers.:

    <OGRVRTLayer name="boroughs">
    <OGRVRTLayer name="nursinghomes">
    <OGRVRTLayer name="joined">
        <SrcSQL dialect="SQLITE">
            SELECT b.BoroName, sum(n.Capacity) as total_capacity from
            boroughs b, nursinghomes n WHERE ST_INTERSECTS(b.geometry,
            n.geometry) group by b.BoroName


You can reproject both raster and vector source datasets.:


The SourceSRS and TargetSRS codes come from, but also can be defined completely with OGC Well Known Text (for example


You can mosaic multiple raster or vector source datasets. The easiest way to do this for raster data is to use “gdalbuildvrt” command line tool.

The file vrts_are_amazing/Final_wsheds_mosaic.vrt has an example of how to mosaic vector datasets.


comments powered by Disqus