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
Create a new attribute field with field calculator. Once calculated, it will be static and not change any more (you must explicitely update it).
Option to version 1: create a virtual field. It updates dynamically when you change the line.
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
From documentation:
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).
