# Measure distance in km of polyline in QGIS

As much as I have looked numerous times for how to measure vector polyline in QGIS I can not find the way. This seems like an easy thing to do but the answer eludes me. Dah!

After I have traced out a route as a vector polyline on a map layer I sure would like to know how long in kilometres it is. The actual total line length with many points winding along a path.

And not by taking a Ruler tool to measure crudely the distance as you see on Google Maps.

Create a new field with field calculator and this expression `\$length/1000`.

This was the short answer. More details in what follows, as things are a bit more complicated.

How to calculate length? Use QGIS expressions

You can use QGIS expressions for that: `length(\$geometry)` or `\$length`. If unsure, use the second one (see below for more information).

To get km from m, divide by 1000: `length(\$geometry) / 1000` or `\$length / 1000`.

Where to apply the epxression? Add an attribute field or a label

1. Create a new attribute field with field calculator. Once calculated, it will be static and not change any more (you must explicitely update it).

2. Option to version 1: create a virtual field. It updates dynamically when you change the line.

3. Set a label for visualization purpose. If you use an expression instead of a field as label source, it will update dynamically when you change the line. This case is shown in the screenshot below.

What is the difference between the two expressions? Planimetric vs. ellipsoidal measurement

`length (\$geometry)`: Calculations are always planimetric in the Spatial Reference System (SRS) of this geometry, and the units of the returned length will match the units for the SRS. This differs from the calculations performed by the `\$length` function, which will perform ellipsoidal calculations based on the project’s ellipsoid and distance unit settings.

That means: if unsure about projection (see next point), use `\$length` as it measures great circle distances (based on the ellipsoid defined in the CRS). `length(\$geometry)` however measures on the distorted map projection (flat canvas).

Important remark: mind projection!

Be aware of projection or Coordinat Reference System (CRS, same as SRS): For your line-layer, select a CRS with units in meters (projected CRS) and one where measurements make sense (e.g. not `EPSG:3857`, this extremely distorts lengths and areas). Which CRS to use depends on the location and extent of your study area. See here a reference list of CRS definitions or also have a look at https://epsg.io/.

Even with `EPSG:3857`, you can make more or less "accurate" measurements with using `\$length`, based on the ellipsoid used for the CRS. So you should still consider to get a local CRS that is fit best to your study area - `WGS84` ellipsoid used for `EPSG:3857`is not the best bet. But if accuracy does not play a major role, you could still use it as follows:
Now drawing a line with layer CRS `EPSG:3857` (an project CRS the same, but relevant for measuring is layer CRS) from North Korea to northern New Zealand. Aplly a label with the expression `\$length/1000` to this line and you get the correct value, see screenshot. If I change the label expression to `length (\$geometry)/1000`, it returns the (wrong) value 10.927 km).
• @wingnut: that's true. So if you measure in a CRS like `EPSG:3857`, than `\$length` still returns a more or less correct value. `length(\$geometry)`, however, is meaningless. Will update the answer accordingly. May 20, 2021 at 22:46