Since this question was asked, an interpolate function has been added to PyQGIS on the QgsGeometry object.
Here is a quick example of how to use this in the Python console inside of QGIS (Plugins -> Python Console).
layer = iface.activeLayer() #layer selected in your layers panel
feature = layer.selectedFeatures()[0] #the first feature of selected features
geom = feature.geometry() #QgsGeometry representing your line
length = geom.length() #length of geometry in the layer CRS. If EPSG:4326 this will be degrees
point = geom.interpolate(length/2.0) #QgsGeometry representing the mid point
x = point.geometry().x() #X coordinate in layer CRS
y = point.geometry().y() #Y coordinate in layer CRS
An even easier option...
In the processing toolbox there is a QGIS function under Vector Geometry
that is called Interpolate point on line
that is most likely using the interpolate function described above.
For your input layer select the line layer you want to find the midpoints
Warning! Make sure you are using a projected coordinate system appropriate for the area where you are finding midpoints. Geographic coordinate systems like EPSG 4326 (WGS 84) are not accurate for measuring distances, and a world wide projected coordinate system like EPSG 3857 (WGS 84 / Pseudo-Mercator) will not be as accurate as a projected coordinate system for a specific region.
For distance, this is the distance to be interpolated along the line by the given units. If you give a distance of 10 and meters is selected, then a point will be created 10 meters along the line for every line in your layer. What we want here is a calculated value, not a static value like 10. To calculate a value for distance, click on the drop down to the far right (shown circled in red below) and select edit...
![Distance input](https://i.stack.imgur.com/VooKk.png)
In the expression box, put in the expression $length / 2.0
This will calculate half the length of the given line feature in the coordinate reference system for that layer (hence the warning above). Select OK.
Run the Interpolate Point on Line
function, and it will then create a point layer with a point at the midpoint of every line feature in the selected layer, and each midpoint will have the attributes of the original line feature copied over to it.
Well i want did the same but, i can´t find... this the way i solve it. Maybe can help to anybody
from qgis.core import *
import qgis.utils
canvas = qgis.utils.iface.mapCanvas();
layer = canvas.layer(1)
maxArea = 0
maxId = 0
selection =[]
uniquevalues = [];
dP= layer.dataProvider()
id = dP.fields().indexFromName('myColumn')
#i have the unique values from some column
uniquevalues = dP.uniqueValues( id )
#then i go for each value that is equal to my Unique value
for uv in uniquevalues:
exprec = QgsExpression( 'myColumn = ' + uv )
exprec.prepare( layer.pendingFields() )
for feature in layer.getFeatures():
value = exprec.evaluate( feature )
if bool( value ):
#areaDegs is the column what i want get the max value
if feature['areaDegs'] > maxArea :
maxArea = feature['areaDegs']
maxId = feature.id()
#here i store de Id´s width de Max values and unique values from myColumn
selection.append( maxId )
maxId =0
maxArea = 0
layer.setSelectedFeatures( selection )
Maybe there are a best way to do it, but for now, this is the way i do it.
Best Answer
You can workaround by using a marker line with a triangle marker:
For Midpoint
Choose "on central point"
For percentage of length
choose "on first vertex only". Next to "Offset along line" switch to "Map units" and then choose "Edit..." in the corresponding context menu.
In the Expression String Builder type
$length * 0.2
and then press "Ok". Replace "0.2" by the desired percentage.This is the result: