5

I Need to get a midpoint from a line layer. Property layer -> Style -> Simple symbol -> Generator geometry (line)

I have code #1:

x(line_interpolate_point($geometry, $length / 2)

this code return NULL

I have code #2:

point = $geometry.interpolate($geometry.length() / 2).asPoint()
make_line(make_point(point.x(), point.y()),
make_point( "auxiliary_storage_labeling_positionx", "auxiliary_storage_labeling_positiony"))

this code return NULL

What is my mistake? I am using QGIS 3.8.3

Answer eurojam: Edit code result:

 make_line(
 make_point(x(transform(line_interpolate_point(transform($geometry,'EPSG: my geographic','EPSG: my project')),'EPSG: my project','EPSG: my geographic')), y(transform(line_interpolate_point(transform($geometry,'EPSG: my geographic','EPSG: my project')),'EPSG: my project','EPSG: my geographic'))),
 make_point( "auxiliary_storage_labeling_positionx", 
 "auxiliary_storage_labeling_positiony")
 )
0

1 Answer 1

5

I suppose that your data CRS (linelayer) is 4326 (WGS84), so $length will result something in Meters and line_interpolate expects something in lat/lon values because your $geometry object is in WGS84. You can use a transform within your statement:

line_interpolate_point(transform($geometry,'EPSG:4326', 'EPSG:3857'),$length/2)

This will transform your line geometry first in something cartesic, so the line_interpolation will result in a valid point. At the end you have to transform your point back into your original CRS:

    transform(line_interpolate_point(transform($geometry,'EPSG:4326', 'EPSG:3857'),$length/2), 'EPSG:3857', 'EPSG:4326')
1
  • Thank you very much! Transform helped! CRS: geographic to project, and project to geographic Commented Jul 30, 2020 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.