ogrlineref
What is ogrlineref?
ogrlineref is the GDAL utility for linear referencing on polyline data. It can segment a line feature into a series of "parts" each carrying begin and end measure values, locate a given distance along a line and emit the corresponding point, or extract a sub-part of a line between two measure values. It is the command-line answer to typical LRS workflows on roads, pipelines, rails, and rivers.
ogrlineref [options] <mode-flags>Main modes (pick one):
-create— build a parts layer by segmenting a line with a stations layer-get_pos— find the measure position of a point along a line-get_coord— find the point at a given measure along a line-get_subline— extract a sub-section between two measures
Commonly used options:
-src <path>/-src_layer <name>— source line dataset-spf <path>/-spf_layer <name>— stations (points) dataset for-create-dst <path>/-dst_layer <name>— output dataset-f <format>— output driver-s <step>— segment length (metres) for-create-mb <value>/-me <value>— begin/end measure for-get_subline-x <value>/-y <value>— point coordinates for-get_pos-m <value>— measure for-get_coord-progress— show progress
When would you use ogrlineref?
Use ogrlineref for LRS tasks that go beyond simple length calculations. Typical jobs: creating a parts layer that breaks a road centreline into 100-metre segments with begin_pos/end_pos measures for mapping accident hot-spots (ogrlineref -create -src road.gpkg -dst parts.gpkg -s 100), locating a specific kilometre post on a rail corridor (-get_coord -src rail.gpkg -m 45000), or clipping a pipeline to a section of interest (-get_subline -mb 1200 -me 1800).
For heavier LRS workflows — mile-post referencing, cross-section measurement, route event tables — consider PostGIS with its linear-referencing functions or dedicated transportation-GIS tools. ogrlineref covers the classic command-line needs but is not a full LRS engine.
FAQs
What units are the measures in?
Whatever the source line's CRS uses horizontally — metres in a metric projection, degrees in WGS84 (which is rarely what you want). Reproject to a metric CRS with ogr2ogr -t_srs EPSG:3857 or a local metric system before using ogrlineref if you want kilometres or miles to line up with reality.
How do I create 1-km mileposts along a corridor?
Use -create with -s 1000 (metres). The output parts layer will have evenly-spaced segments. To get point features at each kilometre mark rather than segments, post-process with ogr2ogr -sql "SELECT ST_StartPoint(geom) FROM parts" -dialect SQLite or use a dedicated milepost generator.
How do I find the chainage of an off-line point?
Use -get_pos -src line.gpkg -x <x> -y <y>. The tool projects the point onto the nearest line vertex (or segment) and returns the measure there. Note this is a simple projection onto the line — for multi-line networks or for points far from any line, results may be surprising.
When should I use ogrlineref vs PostGIS?
ogrlineref is convenient for file-based workflows and one-off jobs. PostGIS ST_LineLocatePoint, ST_LineSubstring, and the full LRS extension give you persistent, indexable, queryable LRS in the database — the right choice for any serious transportation or utility LRS deployment.