3

I want to use the Processing Algorithm, pointsalonglines, in the expression function used in Geometry Generator.

This is because I want to create points "on the fly" along my line and in a equal distance.

The final reason why, is because I want to show the distance of a track every X meters or kilometers.

All is explained in this two links:

Hatching features in layer using QGIS

Anita Graser help me in this link:

https://anitagraser.com/movement-data-in-gis/#comment-21044

enter image description here

RBenet
  • 780
  • 4
  • 15

1 Answers1

6

You can use the following expression to create the points along the line every n distance (here 5000 considering the layer use meters)

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)
))

Alternative if you need to deal with layer projection (needed if your layer is not using meters in particular) considering EPSG:4326 is your layer projection and your local projection is EPSG:25831

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

Although it works for a part and I set the distance value in the M part of the geometry, I don't really see how I can then reuse the M in the labels part as you want in the second part of your requirements. Using Font marker can be another way e.g https://gis.stackexchange.com/a/314789/638 with expression to_string((@geometry_part_num - 1) * 5) || ' Km'

Edit note: comment from @RBenet made me change the second part of the answer

ThomasG77
  • 30,725
  • 1
  • 53
  • 93
  • Thomas this is what I'm looking for. Thank you so much!!! For show the km I will use the option Font Marker and in the expression I put this (@geometry_part_num-1)*5 || 'Km' – RBenet Jun 25 '20 at 12:27
  • Tested with to_string((@geometry_part_num - 1) * 5) || ' Km' due to your suggestion (had cast issue when no to_string) – ThomasG77 Jun 25 '20 at 12:40
  • Sorry, you have reason. I write like this to to try explain better. I have all explained in this link. Thank you again for your help: https://gis.stackexchange.com/questions/364736/hatching-features-in-layer-using-qgis/365988#365988 – RBenet Jun 25 '20 at 13:02
  • No problem, we both learn something :) Not aware how to use well Font marker and you how to generate points along line – ThomasG77 Jun 25 '20 at 13:30
  • Thomas, my original data is a GPX Track so is in Geodesic Coordinates. If I transform the CRS to a Cartesian Coordinates, your script works perfect. But If I want to make transformation "on the fly" QGIS BREAK with a Unexpectedly ended. This is my code: collect_geometries(array_foreach( generate_series(0, length(transform($geometry,'EPSG:4326','EPSG:25831')),step:=25000), make_point_m( x(line_interpolate_point(transform($geometry,'EPSG:4326','EPSG:25831'), @element)), y(line_interpolate_point(transform($geometry,'EPSG:4326','EPSG:25831'), @element)), @element) )) – RBenet Jun 26 '20 at 07:33
  • I also try to put distance in degrees (0.101341795 are 10km): collect_geometries(array_foreach( generate_series(0, length($geometry),step:=0.101341795), make_point_m( x(line_interpolate_point($geometry, @element)), y(line_interpolate_point($geometry, @element)), @element) )) . The result is: 10km is showed in a correct position, but the other shows in a incorrect position.... Any idea?? Open a Question with this??? – RBenet Jun 26 '20 at 07:38
  • Try to wrap with a transform expression the make_point_m. It seems you reproject the input geometry but not the output. – ThomasG77 Jun 26 '20 at 09:14
  • I try with this code and not works. I see in Expression Dialog a error that says: Eval Error: Cannot convert to geometry. This is my code: collect_geometries(array_foreach( generate_series(0, length(transform($geometry,'EPSG:4326','EPSG:25831')),step:=50000), transform(make_point_m( x(line_interpolate_point(transform($geometry,'EPSG:4326','EPSG:25831'), @element)), y(line_interpolate_point(transform($geometry,'EPSG:4326','EPSG:25831'), @element)), @element),'EPSG:4326','EPSG:25831') )) – RBenet Jun 26 '20 at 15:51
  • 1
    Try collect_geometries(array_foreach( generate_series(0, length(transform($geometry,'EPSG:4326', 'EPSG:25831')),step:=5000), transform(make_point_m(x(line_interpolate_point(transform($geometry,'EPSG:4326', 'EPSG:25831'), @element)),y(line_interpolate_point(transform($geometry, 'EPSG:4326', 'EPSG:25831'), @element)), @element), 'EPSG:25831', 'EPSG:4326') )) I've tested (and it works) using points with EPSG 4326 coordinates and calculation using my local projection (EPSG:2154) and replaced the working expression with your EPSG code 25831. – ThomasG77 Jun 26 '20 at 16:04
  • It's incredible!!! Works Perfect!!! For me you are the best!!!! Thank you so much!!!! – RBenet Jun 26 '20 at 16:13
  • Your issue was you were converting WGS84 to your local projection and then converting again the coordinates from WGS84 to your local projection (but your coordinates were not in this projection) whereas the second time the conversion should have been from your local projection to WGS84 – ThomasG77 Jun 26 '20 at 16:29