Site menu:

Cyclocross For Roadies: How and Why to Get Started - Buy Now!

Site search

Categories

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 gis.stackexchange.com) 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”

 

Comments

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.

Andreas

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 (http://postgis.net/docs/manual-2.1/ST_ClosestPoint.html)

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!!

Write a comment





Bookshelf 2.0 developed by revood.com

More in GIS (2 of 33 articles)


If you've ever had to create a point on a polygon surface, you fairly quickly realize that there's a lack ...