QGIS 2 Cookbook
上QQ阅读APP看书,第一时间看更新

Building virtual rasters (catalogs)

When you have a lot of rasters (instead of one big raster) that are all part of the same dataset (typically adjacent to each other), you don't want to load each file individually and then style it. It's much easier to load one file and treat it as one layer. This recipe lets you do this without actually creating a single monstrous raster, which can be difficult to work with.

Getting ready

You will need two or more raster files that have adjacent extents or only overlap partially around the edges and are in the same projection. Ideally, the files should be of the same type, such as all elevations, all air photos, and so on. For this recipe, the elevation rasters from the OSGeo EDU (North Carolina) dataset will work.

How to do it…

  1. (Optional) Load the elevation rasters to your current map.
  2. Go to Raster Menu | Miscellanous | Build Virtual Raster (Catalog).
  3. Check the Use visible raster layers checkbox or choose SELECT, browse to the example data, and select all four.
  4. SELECT and name an output file using the .vrt extension.
  5. (Optional) Check the Load into canvas when finished checkbox if you want to see the results immediately:
    How to do it…

    Tip

    GDAL command line equivalent: <command> <output.vrt> <list of inputs... space between each...>

    For example, gdalbuildvrt elevlid.vrt elevlid_D782_6.tif elevlid_D783_6m.tif elevlid_D792_6m.tif elevlid_D793_6m.tif.

How it works…

GDAL VRT format is an XML file that defines the location of each raster file relative to an anchor file. It uses the existing spatial extent information of the rasters to figure out their positions relative to each other and then anchors the set in the given coordinate system.

There's more…

Using a VRT is all about saving time. When you have hundreds of raster files for one particular dataset, you can combine them into a single file. However, this file could be gigantic in size and somewhat impossible to work with. This is a quick way to be able use the files as a seamless background layer. If you need to perform analysis, you'll likely need to either combine the layers or loop over them individually.

You could also generate Tile Index (also in the Miscellaneous menu), which makes a shapefile of the outlines of the rasters and puts the ID and path of the raster in the attribute table. This would allow you to figure out which image you want to load for a given map without having to load them all.

Finally, if you really want to make all of the files a single large file, use the context menu (right-click on the loaded VRT layer and choose Save As). If you have overlaps, more complicated situations, or want to merge without loading the files, first use the Merge tool (also in the Miscellaneous menu). This can be tricky if your files overlap, you'll need to decide how to handle the double data.

See also