6

Provided a set of lines with various lengths, I would like to change their lengths (hence, changing their geometry) to a specific value, using their centroid as base point.

I have checked the algorithms and whereas there is a similar one called Rotate, I haven't seen anything like what I am looking for.

Is there any way to achieve this?

I assume I could do it with python console, but I do not have a clue on what to type in order to achieve it - I am not familiar with python yet.

6

You can do this with expressions, creating new attribute table columns and then creating a line from the coordinates.

First create four new columns using the expression engine in the attribute table editor. The names and expressions are below, depending on a numeric attribute called outlen which is how long you want the output lines to be. If you want them to all be 1km or whatever put that constant in place in the expressions. Make sure you create "Decimal Number" (and not integer) attributes. Here's one:

enter image description here

here's the name and expression for all four - cut and paste each expression into the box - the differences are subtle:

x0: x(centroid($geometry)) - 
    (outlen/2) * sin(radians(angle_at_vertex($geometry, 0)))
y0: y(centroid($geometry)) - 
    (outlen/2) * cos(radians(angle_at_vertex($geometry, 0)))
x1: x(centroid($geometry)) + 
    (outlen/2) * sin(radians(angle_at_vertex($geometry, 0)))
y1: y(centroid($geometry)) + 
    (outlen/2) * cos(radians(angle_at_vertex($geometry, 0)))

With that done, your attribute table should now look like this:

enter image description here

The lines you want to create go from (x0,y0) to (x1,y1).

Then use the expression editor one final time to update the <geometry> "field" to this:

 geom_from_wkt(format('LINESTRING (%1 %2) (%3 %4)',"x0","y0","x1","y1"))

when you save the changes, your lines should all have changed length! Work on a copy if you need the original!

  • Thanks for such a quick response. I am still looking at it, as (not without shame) I have to admit that I am a little bit lost with the maths behind. – ccamara Sep 25 '18 at 13:42
  • 1
    update: now you just add four new fields (all expressions given) and then modify the <geometry> field (didnt know about that! thx @kazuhito) by constructing a WKT. BOSH! – Spacedman Sep 25 '18 at 21:32
  • Thanks again for such detailed explanation. I will test in a while. I find it a little bit more complex than Kazuhito's answer, but their answer does not work if I want to shrink lines, and yours seems do so. – ccamara Sep 26 '18 at 9:24
  • I just tried to combine it into one long expression so you don't have to make four new columns, but it wasn't working properly, creating features with 0 coordinates. Not sure why, possibly the expression is too long, or some other bug. – Spacedman Sep 26 '18 at 9:32
  • I do agree with @ccamara. Yours is the only workable solution. Btw I am glad you find modifying the <geometry> helpful. I think I leaned it in XY points not showing up on map after adding new rows to attribute table answered by @ndawson. – Kazuhito Sep 26 '18 at 9:33
6

QGIS 3.2

Another option is extend() function which is available in QGIS 3.2.

If you have two attribute fields:

  • len1: original length (m)
  • len2: target length (m)

Then the expression would be extend($geometry, ("len2"-"len1")/2, ("len2"-"len1")/2 )


[Usage] Select Update existing field and then <<geometry>>

enter image description here

  • Interesting! sounds a relatively simple approach. I am not sure, though, where should I use the expression you mentioned. – ccamara Sep 25 '18 at 13:40
  • @Kazuhito, smartly done, I like it! – Taras Sep 25 '18 at 13:53
  • @ccamara My apologies... added a screenshot. – Kazuhito Sep 25 '18 at 13:57
  • 2
    I see what my problem is... If I want to extend the line (this is, make it longer) it works great, but if I want to make it shorter, it doesn't. I have also tried with absolute numbers with no results. My scenario: I have a line with length=5m. If I extend it 2 m it works fine, but if I want to reduce -2m per side in order to make it 1m length, it does not. – ccamara Sep 25 '18 at 14:38
  • 1
    FYI: I created this bug report, should anyone can confirm it or add information: issues.qgis.org/issues/19943 – ccamara Sep 25 '18 at 15:10
2

Just for the sake of documenting: After Kazuhito's answer I realized that there is an algorithm in the processing toolbox called Extend lines which seems to do what I wanted to achieve (visit documentation here):

enter image description here

When used within the graphical modeller, the Start distance and End Distance parameters can be calculated using Field calculator, hence being able to add a formula like Kazuhito's.

Unfortunately, this does not solve my problem since I want to be able to extend lines (works great) but also shrink them (lines cannot be shrunk). I have created the following issue but currently has been rejected (hope developers reconsider their position)

PS: all credits go for @Kazuhito

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.