Skip to main content
Commonmark migration
Source Link

You can do this with geometry generator in QGIS 3.x using the collect aggregate within the aggregate() function to extract the collected geometry of another layer and use it in an expression.

Here are the expressions I used to generate this example - the line layer was called Line; you just need to replace that with the name of your line layer.

enter image description here

Geometry generator (on point layer):

shortest_line($geometry,aggregate('Line','collect',$geometry))

Label:

round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)

Label placement (Label -> Placement -> Data defined):

Coordinate X - x(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Coordinate Y - y(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Rotation - line_interpolate_angle(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2)+90

Label alignment horizontal - 'Center'


#UPDATE QGIS 3.8

UPDATE QGIS 3.8

Label placement

From QGIS 3.8 onwards you can use Geometry Generator to place labels. So instead of using 3 expressions above for X/Y/Rotation, go to the Geometry Generator under the Placement tab in Label settings, change the geometry type to Linestring, and paste the same expression used to generate the lines as above in the text box.

Now you can configure your label placement as if it were actually on a line and set distance from the line, label repetition, etc. without using an expression to set X and Y coordinates.

enter image description here

You can do this with geometry generator in QGIS 3.x using the collect aggregate within the aggregate() function to extract the collected geometry of another layer and use it in an expression.

Here are the expressions I used to generate this example - the line layer was called Line; you just need to replace that with the name of your line layer.

enter image description here

Geometry generator (on point layer):

shortest_line($geometry,aggregate('Line','collect',$geometry))

Label:

round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)

Label placement (Label -> Placement -> Data defined):

Coordinate X - x(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Coordinate Y - y(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Rotation - line_interpolate_angle(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2)+90

Label alignment horizontal - 'Center'


#UPDATE QGIS 3.8

Label placement

From QGIS 3.8 onwards you can use Geometry Generator to place labels. So instead of using 3 expressions above for X/Y/Rotation, go to the Geometry Generator under the Placement tab in Label settings, change the geometry type to Linestring, and paste the same expression used to generate the lines as above in the text box.

Now you can configure your label placement as if it were actually on a line and set distance from the line, label repetition, etc. without using an expression to set X and Y coordinates.

enter image description here

You can do this with geometry generator in QGIS 3.x using the collect aggregate within the aggregate() function to extract the collected geometry of another layer and use it in an expression.

Here are the expressions I used to generate this example - the line layer was called Line; you just need to replace that with the name of your line layer.

enter image description here

Geometry generator (on point layer):

shortest_line($geometry,aggregate('Line','collect',$geometry))

Label:

round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)

Label placement (Label -> Placement -> Data defined):

Coordinate X - x(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Coordinate Y - y(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Rotation - line_interpolate_angle(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2)+90

Label alignment horizontal - 'Center'


UPDATE QGIS 3.8

Label placement

From QGIS 3.8 onwards you can use Geometry Generator to place labels. So instead of using 3 expressions above for X/Y/Rotation, go to the Geometry Generator under the Placement tab in Label settings, change the geometry type to Linestring, and paste the same expression used to generate the lines as above in the text box.

Now you can configure your label placement as if it were actually on a line and set distance from the line, label repetition, etc. without using an expression to set X and Y coordinates.

enter image description here

QGIS 3.8 update
Source Link
she_weeds
  • 13k
  • 1
  • 32
  • 64

You can do this with geometry generator in QGIS 3.x using the collect aggregate within the aggregate() function to extract the collected geometry of another layer and use it in an expression.

Here are the expressions I used to generate this example - the line layer was called Line; you just need to replace that with the name of your line layer.

enter image description here

Geometry generator (on point layer):

shortest_line($geometry,aggregate('Line','collect',$geometry))

Label:

round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)

Label placement (Label -> Placement -> Data defined):

Coordinate X - x(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Coordinate Y - y(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Rotation - line_interpolate_angle(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2)+90

Label alignment horizontal - 'Center'


#UPDATE QGIS 3.8

Label placement

From QGIS 3.8 onwards you can use Geometry Generator to place labels. So instead of using 3 expressions above for X/Y/Rotation, go to the Geometry Generator under the Placement tab in Label settings, change the geometry type to Linestring, and paste the same expression used to generate the lines as above in the text box.

Now you can configure your label placement as if it were actually on a line and set distance from the line, label repetition, etc. without using an expression to set X and Y coordinates.

enter image description here

You can do this with geometry generator in QGIS 3.x using the collect aggregate within the aggregate() function to extract the collected geometry of another layer and use it in an expression.

Here are the expressions I used to generate this example - the line layer was called Line; you just need to replace that with the name of your line layer.

enter image description here

Geometry generator (on point layer):

shortest_line($geometry,aggregate('Line','collect',$geometry))

Label:

round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)

Label placement (Label -> Placement -> Data defined):

Coordinate X - x(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Coordinate Y - y(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Rotation - line_interpolate_angle(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2)+90

Label alignment horizontal - 'Center'

You can do this with geometry generator in QGIS 3.x using the collect aggregate within the aggregate() function to extract the collected geometry of another layer and use it in an expression.

Here are the expressions I used to generate this example - the line layer was called Line; you just need to replace that with the name of your line layer.

enter image description here

Geometry generator (on point layer):

shortest_line($geometry,aggregate('Line','collect',$geometry))

Label:

round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)

Label placement (Label -> Placement -> Data defined):

Coordinate X - x(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Coordinate Y - y(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Rotation - line_interpolate_angle(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2)+90

Label alignment horizontal - 'Center'


#UPDATE QGIS 3.8

Label placement

From QGIS 3.8 onwards you can use Geometry Generator to place labels. So instead of using 3 expressions above for X/Y/Rotation, go to the Geometry Generator under the Placement tab in Label settings, change the geometry type to Linestring, and paste the same expression used to generate the lines as above in the text box.

Now you can configure your label placement as if it were actually on a line and set distance from the line, label repetition, etc. without using an expression to set X and Y coordinates.

enter image description here

Source Link
she_weeds
  • 13k
  • 1
  • 32
  • 64

You can do this with geometry generator in QGIS 3.x using the collect aggregate within the aggregate() function to extract the collected geometry of another layer and use it in an expression.

Here are the expressions I used to generate this example - the line layer was called Line; you just need to replace that with the name of your line layer.

enter image description here

Geometry generator (on point layer):

shortest_line($geometry,aggregate('Line','collect',$geometry))

Label:

round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)

Label placement (Label -> Placement -> Data defined):

Coordinate X - x(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Coordinate Y - y(line_interpolate_point(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2))

Rotation - line_interpolate_angle(shortest_line($geometry,aggregate('Line','collect',$geometry)),round(length(shortest_line($geometry,aggregate('Line','collect',$geometry))),2)/2)+90

Label alignment horizontal - 'Center'