# Splitting line by percentage length in QGIS?

I'm using QGIS 2.18.9.

How do I split a line by percentage?

In the case of a line, split it by percentage length.

My use-case is more for roads where I would like to split a segment (by %). So, I can use the line feature to determine the length and then create a buffer as shown. • Splitting a polygon by a percentage should be a different question to splitting a line by a percentage. I think you should focus this one on the former, and if the other is not already asked and answered on this site, then ask that separately. – PolyGeo Jul 11 '17 at 1:36
• Out of curiosity, what happened to the other 10%? ;) – Joseph Jul 13 '17 at 13:23
• you got my red handed @Joseph. :) its a mistake – Garry Aug 30 '17 at 3:25

You may run this simple code from the Python Console:

``````percentage = 0.6 # Set the percentage

layer = iface.activeLayer() # Load it as you want

# Create the output layer
crs = layer.crs().toWkt()
outLayer = QgsVectorLayer('Polygon?crs='+ crs, 'catchments_new' , 'memory')
prov = outLayer.dataProvider()
fields = layer.pendingFields() # Fields from the input layer
outLayer.updateFields()

for feat in layer.getFeatures():
attrs = feat.attributes()
line_geom = feat.geometry()
leng = line_geom.length()
print leng
dist = (percentage * leng)
center_pt = dist * 0.5
pt = line_geom.interpolate(center_pt)
buffered_geom = pt.buffer(center_pt, 2000) # set the second parameter sufficiently enough for avoiding creating odd behaviors
itx = line_geom.intersection(buffered_geom)
diff = line_geom.difference(buffered_geom)

outGeom_1 = QgsFeature()
outGeom_1.setAttributes(attrs)
outGeom_1.setGeometry(buffered_geom)