Skip to main content
100%

QGIS tips

✓ Published0🌍 Public
TThomasG77
Last edited Jun 26, 2020
Created on Mar 26, 2020

QGIS tips

Expressions

Insert scale as an expression

In QGIS composer, set for the map an "Item ID" with value main_map and then in an expression in a HTML box or a Text box, you can insert the following expressions

[%'1 : '  || round(map_get(item_variables('main_map'), 'map_scale'),0)%]

Build HTML table from a layer

Insert in a box with HTML rendering the following

[%
concat(
  '<table>',
  '<thead><tr>Col 1 header</tr><tr>Col 2 header</tr></thead>',
  aggregate(
    layer:='my_layer',
    aggregate:='concatenate',
    expression:=concat(
      '<tr><td>',
      "col1",
      '<td><td>',
      "col2",
      '<td></tr>'),
    concatenator:=''
  ),
  '</table>')
%]

Do the same with filtering

[%
concat(
  '<table>',
  '<thead><tr>Col 1 header</tr><tr>Col 2 header</tr></thead>',
  aggregate(
    layer:='my_layer',
    aggregate:='concatenate',
    filter:="col1"='myvalue',
    expression:=concat(
      '<tr><td>',
      "col1",
      '<td><td>',
      "col2",
      '<td></tr>'),
    concatenator:=''
  ),
  '</table>')
%]

Do the same as first example but with ordering

Not aware of the way to order DESC, only with ASC behaviour

[%
concat(
  '<table>',
  '<thead><tr>Col 1 header</tr><tr>Col 2 header</tr></thead>',
  aggregate(
    layer:='my_layer',
    aggregate:='concatenate',
    expression:=concat(
      '<tr><td>',
      "col1",
      '<td><td>',
      "col2",
      '<td></tr>'),
    concatenator:='',
    order_by:=col1
  ),
  '</table>')
%]

Create points along line every n distance

This expression is intended to works at geometry generator level to avoid creating a second layer just to put intermediat points on top of a line layer

We use as distance of 5000 considering the layer use meters and is a line

collect_geometries(array_foreach(
generate_series(0, length($geometry),step:=5000),
make_point_m(
  x(line_interpolate_point($geometry, @element)),
  y(line_interpolate_point($geometry, @element)),
  @element)
))