# 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.

## 1 Answer

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/.

See also this answer here.

Example

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:

An already classical example of measuring errors is the reach of missiles from North Korea. Years ago, the Economist published a map with wrong measurements. As you can see in the linked post on the second map, the correct (geodesic/ great circle line) distance from North Korea to northern New Zealand is 10.000 km.

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).

• So \$length can give a great circle distance directly, but length() is Cartesian? Commented May 20, 2021 at 22:24
• @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. Commented May 20, 2021 at 22:46
• Thanks, I got my measurements. Always thought this would be just an icon click or menu item to spit out the number. This is a bit more involved if I have to add a field to all my 150 separate trail layers. Commented May 21, 2021 at 15:29
• You can use field calculator in batch mode to automatize this Commented May 21, 2021 at 15:31