Site search

Create Midpoints Along Lines with OGR

Today I found myself trying to create a ‘midpoint’ on a large shapefile line layer. QGIS doesn’t have a tool to do this, and because the layer was large, I really didn’t want to mess around converting file formats to stick it into a database. However, similar to my last post about Creating Points On A Surface Using OGR, I found a quick trick (via this question on that also uses the awesome ‘-dialect’ switch in OGR. It works like this:

ogr2ogr OutputPoints.shp InputLines.shp -dialect sqlite -sql “SELECT AsText(Line_Interpolate_Point(InputLines.geometry, 0.5)),OTHER,FIELDS,CAN,GO,HERE from InputLines”

Simply replace “OutputPoints” with the desired output filename, and “InputLines” with the name of the line file you wish to create the midpoints from. You can change the “0.5” to any number you wish – it represents how far along the line the point will fall, so if you want a midpoint, leave it at 0.5, but if you want the point 1/4 of the way along, change 0.5 to 0.25.  If don’t specify fields where I have “OTHER,FIELDS,CAN,GO,HERE” you will get only a point file with no attributes. If you include other field names from InputLines.shp (comma separated) after the closing bracket, OGR will also return those attributes from InputLines.shp.

Here’s a quick visual:

Lines With Midpoints

Lines With Midpoints

Bonus assignment: rather than making an intermediate point layer, let’s just write the lat/lon of the midpoint directly to the attribute table:

ogr2ogr OutputLines.shp InputLines.shp -dialect sqlite -sql “SELECT *, X(Line_Interpolate_Point(InputLines.geometry, 0.5)) as X, Y(Line_Interpolate_Point(InputLines.geometry, 0.5)) as Y from InputLines”



Comment from Andreas Neumann
Time January 29, 2014 at 7:57 am

Hi Darren,

QGIS already has a built-in method to visualize the mid-point of a line: use a marker line and multiple symbol levels. So no need to create these new geometries.


Comment from darrencope
Time January 29, 2014 at 8:08 am

Andreas; great point! You’re absolutely right; if you’re just looking to visualize, then you can do it as you suggest. However, there are cases where one might actually want the geometries or the coordinates written to a file, such as in my example.

Comment from Andreas Neumann
Time January 29, 2014 at 11:32 am

agreed. Sometimes you actually want these mid-points as features.

I was just a bit puzzled by the sentence “QGIS doesn’t have a tool to do this”.

Often people don’t know that a tool already exists in QGIS. Sometimes people even create a plugin for a functionality that already exists in QGIS.

Comment from Andreas Neumann
Time January 29, 2014 at 11:33 am

Maybe there should be a better “search” tool in QGIS – where functions could have keywords/tags and you could search for a keyword and QGIS would show the appropriate menu entry, dialogue or plugin.

Comment from darrencope
Time January 29, 2014 at 11:40 am

Hi Andreas; unless I’m mistaken, my statement still stands. QGIS doesn’t have a tool to either a) create a new point layer on the midpoint of a line, or b) write the coordinates of the midpoint to the attribute table.

I agree that a better ‘search’ would be nice, but would likely be difficult to do with something like a style as you suggested. A very interesting idea to think about though! (Perhaps an expansion of the “Processing” toolbar search, although that would confuse things in that it would no longer be only processing…

Comment from Andreas Neumann
Time January 29, 2014 at 11:54 am

Yes – it doesn’t have a tool to create these new midpoint features, but it has a tool to visualize these midpoints on the fly without having to create extra layers.

BTW: would be cool to expose the Line_Interpolate_Point functionality in QGIS processing. Along with other such stuff, like ClosestPoint (

Comment from darrencope
Time January 29, 2014 at 3:17 pm

It would be VERY cool to expose some of the spatial database functionality into Processing… I’m intrigued!!

Comment from Mat
Time January 24, 2017 at 3:53 am

I have a problem. When launch the script the output is:
“layer names ignored in combination with -sql.
ERROR 1: SQL: Missing keyword SELECT”
Where is the problem? Not found the problem.

Comment from darrencope
Time January 24, 2017 at 8:59 am

Hi Matteo. Make sure you’re following the syntax in my blog post exactly. You need the word SELECT in there–that’s what the error message is telling you.

Comment from Justin Walker
Time January 25, 2017 at 5:04 am

Hi Darren, it’s 3 years on from your original post and I’m wondering if this functionality did ever make it into QGIS? I can’t find it so I’m guessing not.

Comment from darrencope
Time January 29, 2017 at 7:51 pm

Hi Justin,

This functionality is in OGR, not QGIS directly. I am no aware of any plans to add it to QGIS.

Write a comment

Read previous post:
Search and Filter in Golden Cheetah v3

I put together a quick screencast showing the Search and Filter capabilities of Golden Cheetah v3. Check it out!