The real power of VRT files comes into play when you want create virtual representations of features as well. In this case, you can virtually tile together many individual layers as one. At the present time you cannot do this with a single command line but it only takes adding two simple lines to the VRT XML file to make it start working.
Here we want to create a virtual vector layer from all the files containing lines in the ne/10m_cultural folder.
First, to keep it simple, create a folder and copy in only the files we are interested in:
mkdir ne/all_lines cp ne/10m_cultural/*lines* ne/all_lines
Then we can create our VRT file using ogr2vrt as shown in the previous example:
python ogr2vrt.py -relative ne/all_lines all_lines.vrt
If added to QGIS at this point, it will merely present a list of four layers to select to load. This is not what we want.
So next we edit the resulting all_lines.vrt file and add a line that tells GDAL/OGR that the contents are to be presented as a unioned layer with a given name (i.e. “UnionedLines”).
The added line is the second one below, along with the closing line second from the end:
<OGRVRTDataSource> <OGRVRTUnionLayer name="UnionedLines"> <OGRVRTLayer name="ne_10m_admin_0_boundary_lines_disputed_areas"> <SrcDataSource relativeToVRT="1" shared="1"> ... <Field name="note" type="String" src="note" width="200"/> </OGRVRTLayer> </OGRVRTUnionLayer> </OGRVRTDataSource>
Now loading it into QGIS automatically loads it as a single layer but, behind the scenes, it is a virtual representation of all four source layers.
On the map in Figure 5.8 the unionedLines layer is drawn on top using red lines, whereas all the source files (that I manually loaded) are shown with a light shading. This shows that the new virtual layer covers all the source layer features.
Geospatial Power Tools is 350 pages long – 100 of those pages cover these kinds of workflow topic examples. Each copy includes a complete (edited!) set of the GDAL/OGR command line documentation as well as the following topics/examples:
Workflow Table of Contents
- Report Raster Information – gdalinfo 23
- Web Services – Retrieving Rasters (WMS) 29
- Report Vector Information – ogrinfo 35
- Web Services – Retrieving Vectors (WFS) 45
- Translate Rasters – gdal_translate 49
- Translate Vectors – ogr2ogr 63
- Transform Rasters – gdalwarp 71
- Create Raster Overviews – gdaladdo 75
- Create Tile Map Structure – gdal2tiles 79
- MapServer Raster Tileindex – gdaltindex 85
- MapServer Vector Tileindex – ogrtindex 89
- Virtual Raster Format – gdalbuildvrt 93
- Virtual Vector Format – ogr2vrt 97
- Raster Mosaics – gdal_merge 107