I tried to shorten a given LineGeometry with some Python code.
from qgis.utils import iface
layer = iface.activeLayer()
fs = layer.selectedFeatures()
for f in fs:
fg = f.geometry()
geomSingleType = QgsWkbTypes.isSingleType(fg.wkbType())
if fg.type() == QgsWkbTypes.LineGeometry:
if geomSingleType:
x = fg.asPolyline()
print("length: ", fg.length())
else:
x = fg.asMultiPolyline()
print("MultiLine: ", x, "length: ", fg.length())
newLine = fg.extendLine(0,(-2.5))
print("length: ", newLine .length())
That returns a line with an equal length:
length: 11.995677929842055
length: 11.995677929842055
When I use a value > 0 the length is updated:
length: 11.995677929842055
length: 14.495677929927327
So I'm able to make a line longer, but I want to make it shorter.
Does anyone have any advice for me?
The code is part of a bigger project - so solutions with QGIS UI will not help me.
extendLineintents only extend the lines not shrink. There is Line substring tool you may want to try out. – Nil Jan 11 '21 at 11:51extendLineusesextendbehind the scene. Andextenddoesn't work with negative values. https://i.stack.imgur.com/QbKlg.png – Kadir Şahbaz Jan 11 '21 at 12:05