---
title: 'LRS, Linear Reference System in QGIS with Virtual Layers'
url: 'https://www.faunalia.eu/en/blog/2019-05-14_lrs'
markdown: 'https://www.faunalia.eu/en/blog/2019-05-14_lrs.md'
lang: en
date: '2019-05-14'
description: 'LRS, Linear Reference System in QGIS with Virtual Layers'
taxonomy:
  category:
    - blog
  tag:
    - qgis
    - lrs
  archives_month:
    - may_2019
  archives_year:
    - '2019'
---

[ Faunalia ](https://www.faunalia.eu/en)     [ Blog ](https://www.faunalia.eu/en/blog)      LRS, Linear Reference System in QGIS with Virtual Layers    

## [LRS, Linear Reference System in QGIS with Virtual Layers](https://www.faunalia.eu/en/blog/2019-05-14_lrs)

    14th May 2019

  [qgis](https://www.faunalia.eu/en/blog/tag:qgis) [lrs](https://www.faunalia.eu/en/blog/tag:lrs)  

# Basic concepts

LRS (Linear Reference System) is a spatial reference method where the places along a path have distance measures compared to some specific points, like milestones of a road.

Imagine the following scenario:

- linear river layer
- non-spatial table with the description of the operation type (pruning, cleaning, etc.) done *for each* river

You want to automatically create 3 different layer **for each** new operation added to the table, that is, when you add a line to the table you want that these 3 layers update accordingly. These 3 layers are:

1. point layer of the beginning of the operation
2. point layer of the end of the operation
3. line layer that links the beginning and the end of each operation

![LRS concepts in QGIS](https://www.faunalia.eu/user/pages/08.blog/2019-05-14_lrs/layout.png)

## Data structure

The base layers are only `rivers` and `operation_table`.

It is important that these 2 tables have a common field that identifies the river, in this example `river_name` for the layer `rivers` and `route` for the table `operation_table`.

![LRS Attribute Tables](https://www.faunalia.eu/user/pages/08.blog/2019-05-14_lrs/tables.png)

## Creation of Virtual Layers

Virtual layers have been introduced long time ago in QGIS and allow you to run *real* SQL queries on each data type. So if you have shapefiles as input, you can create virtual layers from them without experiencing any problem.

The main advantage? Virtual layers behave like [database views](https://www.postgresql.org/docs/9.2/sql-createview.html): when the source layer values change, views update accordingly.

Another big advantage? Views (and therefore virtual layers) are *in place queries* of existing data, so nothing is written on the disk.

By using a combination of [ST\_Line\_Interpolate\_Point](https://postgis.net/docs/manual-1.5/ST_Line_Interpolate_Point.html)and [ST\_Length](https://postgis.net/docs/ST_Length.html) functions we can get the 3 layers that we were looking for.

Moreover, with a smart customization of the QGIS forms we can add a line to the attribute table of `operation_table` (so, a new operation made on rivers) and the layers will update continuously. Have a look at the small video:

 [  Previous Post ](https://www.faunalia.eu/en/blog/2019-05-01-dataplotly_extended) [Next Post  ](https://www.faunalia.eu/en/blog/2019-05-23_a_bug_story)

#### Related Posts

- 100 

     [QGIS Print Layouts Graphs and Charts — Beta Out Now!](https://www.faunalia.eu/en/blog/2019-09-26-dataplotly-beta "QGIS Print Layouts Graphs and Charts — Beta Out Now!")
- 100 

     [QGIS Print Layouts Graphs and Charts Campaign](https://www.faunalia.eu/en/blog/2019-03-13_dataplotly "QGIS Print Layouts Graphs and Charts Campaign")
- 90 

     [A Bug's Death](https://www.faunalia.eu/en/blog/2019-05-23_a_bug_story "A Bug's Death")
- 80 

     [DataPlotly target reached!](https://www.faunalia.eu/en/blog/2019-06-03_dataplotly_target_reached "DataPlotly target reached!")
- 70 

     [Adding a custom widget to a QGIS Processing script](https://www.faunalia.eu/en/blog/2019-07-02-custom-processing-widget "Adding a custom widget to a QGIS Processing script")

#### Popular Tags

  [qgis](https://www.faunalia.eu/en/blog/tag:qgis) [dataplotly](https://www.faunalia.eu/en/blog/tag:dataplotly) [crowdfunding](https://www.faunalia.eu/en/blog/tag:crowdfunding) [python](https://www.faunalia.eu/en/blog/tag:python) [lrs](https://www.faunalia.eu/en/blog/tag:lrs) [ecw](https://www.faunalia.eu/en/blog/tag:ecw) [bug](https://www.faunalia.eu/en/blog/tag:bug) [processing](https://www.faunalia.eu/en/blog/tag:processing) [grib](https://www.faunalia.eu/en/blog/tag:grib) [plotly](https://www.faunalia.eu/en/blog/tag:plotly) [flask](https://www.faunalia.eu/en/blog/tag:flask) [dash](https://www.faunalia.eu/en/blog/tag:dash) [remote](https://www.faunalia.eu/en/blog/tag:remote) [open source](https://www.faunalia.eu/en/blog/tag:open%20source) [online](https://www.faunalia.eu/en/blog/tag:online) [plugin](https://www.faunalia.eu/en/blog/tag:plugin)  

#### Archives

- [ 1 August 2022  ](https://www.faunalia.eu/en/blog/archives_month:aug_2022)
- [ 1 February 2021  ](https://www.faunalia.eu/en/blog/archives_month:feb_2021)
- [ 1 December 2020  ](https://www.faunalia.eu/en/blog/archives_month:dec_2020)
- [ 1 April 2020  ](https://www.faunalia.eu/en/blog/archives_month:apr_2020)
- [ 1 March 2020  ](https://www.faunalia.eu/en/blog/archives_month:mar_2020)
- [ 1 January 2020  ](https://www.faunalia.eu/en/blog/archives_month:jan_2020)
- [ 1 October 2019  ](https://www.faunalia.eu/en/blog/archives_month:oct_2019)
- [ 2 September 2019  ](https://www.faunalia.eu/en/blog/archives_month:sep_2019)
- [ 1 July 2019  ](https://www.faunalia.eu/en/blog/archives_month:jul_2019)
- [ 1 June 2019  ](https://www.faunalia.eu/en/blog/archives_month:jun_2019)
- [ 3 May 2019  ](https://www.faunalia.eu/en/blog/archives_month:may_2019)
- [ 1 April 2019  ](https://www.faunalia.eu/en/blog/archives_month:apr_2019)

---

## Navigation

- Parent: [Blog](https://www.faunalia.eu/en/blog.md)
- Previous: [A Bug's Death](https://www.faunalia.eu/en/blog/2019-05-23_a_bug_story.md)
- Next: [QGIS Print Layouts Graphs and Charts Campaign Extended!](https://www.faunalia.eu/en/blog/2019-05-01-dataplotly_extended.md)
