Can QGIS read layers in OGR's virtual vector format? It seems to work with VRT raster files, but it reports my layer is an invalid data source for vectors.


I'm running QGIS 2.6 on Windows with GDAL 1.11.0 (which supports virtual vectors).

I've created a virtual layer as follows, which concatenates 4 shapefile layers.

The reason for wanting to do this is that the data is too big to fit into a single shapefile. I could use another format, but it would be more convenient if I could just use a virtual raster. I'm sure there a plenty of valid reasons for wanting to do this.

    <OGRVRTUnionLayer name="unionLayer">
        <OGRVRTLayer name="OSMM_SPNE">
        <OGRVRTLayer name="OSMM_SPNW">
        <OGRVRTLayer name="OSMM_SPSE">
        <OGRVRTLayer name="OSMM_SPSW">

The ogrinfo tool can read the layer correctly:

$ ogrinfo -al -so virtual.xml
INFO: Open of `virtual.xml'
      using driver `VRT' successful.

Layer name: unionLayer
Geometry: Polygon
Feature Count: 1469256
Extent: (398123.820000, 196969.360000) - (501938.600000, 260658.500000)
Layer SRS WKT:
PROJCS["British National Grid",
    GEOGCS["Ordnance Survey Great Brit",
        DATUM["Ordnance Survey Great Brit",
            SPHEROID["Airy 1830",6377563.396,299.3249646]],
TOID: String (20.0)
FEATCODE: Integer (9.0)
VERSION: Integer (9.0)
VERDATE: Date (10.0)
THEME: String (80.0)
CALCAREA: Real (19.11)
CHANGE: String (80.0)
DESCGROUP: String (150.0)
DESCTERM: String (150.0)
MAKE: String (20.0)
PHYSLEVEL: Integer (9.0)
PHYSPRES: String (20.0)
BROKEN: Integer (4.0)
LOADDATE: Date (10.0)
AREA: Real (19.11)
LEN: Real (19.11)
SYMBOLCODE: Integer (4.0)
SHAPE_Leng: Real (19.11)
SHAPE_Area: Real (19.11)

You need to either add relativeToVRT="1" to the SrcDataSource element as it defaults to an absolute path or use an absolute path to the vector files.

From the VRT driver page:

The element may optionally have a relativeToVRT attribute which defaults to "0"

Assuming virtual.xml is in /tmp/test (or C:\Temp\test if running on Windows). When you're running ogrinfo from the command line, your VRT will work if you're running the command from the same directory as the VRT (the test sub directory) - ogrinfo virtual.xml. This is because OSMM_SPNE.shp is a relative path (i.e relative to the current directory).

If you ran ogrinfo test/virtual.xml from /tmp the command would fail as ogrinfo will look for OSMM_SP*.shp in /tmp and those shapefiles are not there, they're in /tmp/test. When you're using QGIS, the current/working directory is unlikely be the directory your VRT and vector files are in, so QGIS (OGR) doesn't know where to find them.

The following VRT works in QGIS:

    <OGRVRTUnionLayer name="unionLayer">
        <OGRVRTLayer name="test1">
            <SrcDataSource relativeToVRT="1">test1.shp</SrcDataSource>
        <OGRVRTLayer name="test2">
            <SrcDataSource relativeToVRT="1">test2.shp</SrcDataSource>

So does this:

    <OGRVRTUnionLayer name="unionLayer">
        <OGRVRTLayer name="test1">
        <OGRVRTLayer name="test2">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.