5

I'm looking for a way to collect the mid-point coordinates of my LineString type Shapefile using the Field Calculator.

What I've already tried was x(centroid($geometry)), but this calculates the mean position of the line.

The Geometry panel from the Field Calculator has an end_point and start_point option. Is there a mid_point function that I'm missing?

6
  • Do you want the coordinate of the point on the line at half the line length?
    – BERA
    Jun 27 at 17:43
  • 1
    try with line_interpolate_point($geometry, $length/2) will draw the midpoint along the line
    – pigreco
    Jun 27 at 17:43
  • Yes, what i want is the coordinate values at half the line length. I tried your option @pigreco, but I'm using Field Calculator from the Attribute Table. Could I use $length/2 for this same purpose? Jun 27 at 17:55
  • 1
    yes, $length calculates the length of the line respecting the project settings. For corrdinate just write x (line_interpolate_point ($geometry, $length / 2)), same thing for y
    – pigreco
    Jun 27 at 18:01
  • 1
    Thanks @pigreco! It worked just fine! Now I'm just curious. In order to draw the midpoint along the line do I have to create a whole new layer? Or the point would be embedded into my lines? Virtual layers are used for something like this? Jun 27 at 18:13

1 Answer 1

7

To plot the midpoint of a polyline using the field calc:

line_interpolate_point ($geometry, $length / 2)

To visualize the point, add a style layer with the geometry generator:

enter image description here

$length: Returns the length of a linestring. If you need the length of a border of a polygon, use $perimeter instead. The length calculated by this function respects both the current project's ellipsoid setting and distance unit settings. For example, if an ellipsoid has been set for the project then the calculated length will be ellipsoidal, and if no ellipsoid is set then the calculated length will be planimetric.

https://docs.qgis.org/3.22/en/docs/user_manual/expressions/functions_list.html#length

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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