Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active June 26, 2020 10:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/fc612be255c2626997a0b9a923bdd071 to your computer and use it in GitHub Desktop.
Save ThomasG77/fc612be255c2626997a0b9a923bdd071 to your computer and use it in GitHub Desktop.

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)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment