Virtual Layers in Context

“Virtual layers provide a way to use data that is computed 'on the fly' as the basis for a layer”

In this module, we look at how you can use virtual layers to minimise data duplication and dynamically compute new information from a base dataset.

You try:

Goal: To learn how to create a virtual polygon layer based on buffered points.

  • Start a new project.
  • Add a satellite base map using the Quick Map Service plugin or using the XYZ tile from the browser panel.
  • Zoom in to an area of interest.
  • Create a new layer ('trees'), adding attributes and setting the CRS as indicated in the table
  • Create 5 or so points on the layer entering appropriate values for the height, canopy radius, species. Just make up values as needed. Remember not to create the points so close to each other.
  • Add a new virtual layer and import 'trees'
  • Use the expression as provided in the table to define the data for your virtual layer.

Check your results:
Once your virtual layer is created, it should show a buffered (to the distance of canopy diameter) polygon around each tree point.

Name Expectation

CRS

UTM 34S, WSG84

Attribute: species

Text, length 255

Attribute: canopy

Decimal number, length 10, precision 3

Attribute: height

Decimal number, length 20, precision 3

Virtual layer query:

select id, buffer(geometry, canopy) as geometry, canopy, height, species from trees

More about

The virtual layers are dynamic - whenever you update the base layers that the virtual layer is defined from, the virtual layer will update too. This can save a lot of overhead in terms of file space and data duplication since you can have the virtual layer derive extra data from the base layers without saving anything to disk.

You can use SQL like constructs to define your virtual layer. Because the virtual layer is dependent on other layers / data-sources, you need to be careful not to rename/ move/ delete the base layers.

There can be a performance overhead when using virtual layers - especially when the virtual layer is defined with a complex query or does complex geometry operations.

When adding/ changing data in the base layers, you will need to refresh the map display (e.g. pan/zoom) in order to see the virtual layer dynamically update.

Check your knowledge:

  1. A virtual layer is:

    1. A layer that can be viewed using a virtual reality headset
    2. A layer that is dynamically generated from one or more other layers
    3. Something virtually nobody uses
  2. What do you think this virtual layer will do when based on our trees example:

    1. Updates the trees layer to add a new field called 'ratio'
    2. Creates a virtual layer based on trees
    3. Creates a virtual layer where the geometry is based on the ratio of height to canopy