# How to calculate the length of a line in QGIS

Calculating the length of a line in QGIS is a straightforward process that can be done using the “Field Calculator.” Here’s how you can do it:

**Working with QGIS? You should be listening to our podcast!**

**Open your QGIS project**: Launch QGIS and open the project containing the line layer for which you want to calculate lengths.**Select the line layer**: In the “Layers” panel, click on the line layer to highlight it. Ensure it’s the active layer.**Open the Attribute Table**: Right-click on the line layer in the “Layers” panel and select “Open Attribute Table” from the context menu.**Open the Field Calculator**: In the Attribute Table window, look for the “Field Calculator” button. It’s usually represented by an abacus icon. Click on it to open the Field Calculator.**Create a new field for the length**: In the Field Calculator window, you’ll have the option to create a new field or update an existing one. To create a new field:

- Check the “Create a new field” option.
- Give your new field a name, e.g., “Length”.
- Choose the field type that suits your needs, typically “Decimal number (real)” for length.
- Specify the field width and precision according to your needs.

**Enter the expression to calculate length**: In the “Expression” box at the bottom of the Field Calculator window, you need to enter an expression to calculate the length. The expression varies depending on the coordinate system of your layer:

- For layers in a geographic coordinate system (lat/lon), use:
`$length * 111319.488`

- For layers in a projected coordinate system (meters, feet, etc.), simply use:
`$length`

This expression calculates the length of each line feature in the layer. The number`111319.488`

is used to convert degrees to meters at the equator, which is a rough estimation. For more accurate length measurements in geographic coordinate systems, consider reprojecting your layer to a projected coordinate system first.

**Execute the calculation**: After entering the expression, click “OK.” QGIS will calculate the lengths for each line feature in your layer and store the results in the new field you created.**Check the results**: Close the Field Calculator and the Attribute Table. If you reopen the Attribute Table, you will see the new field with the calculated lengths for each line feature.

Remember, the accuracy of your length calculations will depend on the coordinate system of your layer. For precise measurements, especially over large distances, using a projected coordinate system that’s appropriate for your region of interest is recommended.

## Frequently asked questions with detailed explanations and instructions.

### 1. What is the difference between $length and length($geometry)?

**$length**is a shorthand function in QGIS that returns the length of the current feature’s geometry. It is context-sensitive and directly accesses the geometry of the feature being evaluated, making it a quick and convenient way to refer to the feature’s length in expressions.**length($geometry)**, on the other hand, explicitly calculates the length of a given geometry.`$geometry`

is a variable representing the geometry of the current feature. This form is more flexible since you can replace`$geometry`

with any other geometry expression, allowing for more complex calculations and operations.

### 2. How can I convert the length units (e.g., from meters to kilometers or miles)?

When you calculate the length of features in QGIS, the units of the result depend on the layer’s coordinate system. For projected coordinate systems that use meters, converting to kilometers or miles involves a simple mathematical operation:

**To kilometers**: Divide the length in meters by 1,000. For example,`$length / 1000`

.**To miles**: Divide the length in meters by 1,609.34. For example,`$length / 1609.34`

.

### 3. How do I calculate the length of lines in a geographic coordinate system accurately?

Calculating lengths in a geographic coordinate system can lead to inaccuracies due to the Earth’s curvature. To achieve accurate results, it’s recommended to reproject your data to a suitable projected coordinate system before calculation or use QGIS’s built-in functions that account for spherical geometry. For on-the-fly conversions, consider using the `transform`

function in your expression to project geometries to an appropriate CRS (Coordinate Reference System) that uses meters before calculating lengths.

### 4. Can I calculate the total length of multiple lines?

Yes, you can calculate the total length of multiple lines using the “Aggregate” function in the Field Calculator:

- Use an expression like
`sum($length)`

to calculate the total length of all features in the layer. - If you need to calculate the total length of a subset of features, use a conditional expression within the
`sum`

function, like`sum($length, "condition" = 'value')`

, replacing`"condition" = 'value'`

with your actual criteria.

### 5. How do I update the length automatically when a line is modified?

To ensure length calculations update automatically with changes in geometry, you can use Virtual Fields:

- When creating a new field in the Field Calculator, check the “Virtual Field” option. Virtual fields are recalculated every time a feature is added or modified, keeping your length data up to date.

### 6. Why does the length of my line not match the expected real-world distance?

This discrepancy is usually due to the coordinate system used. Distortions inherent to certain map projections can affect distance measurements. For accurate real-world distances, ensure your layer is in a projected coordinate system that minimizes distortion for your area of interest. UTM (Universal Transverse Mercator) projections are often a good choice for many regions.

### 7. How can I calculate lengths for only a selection of features?

To calculate lengths for a selection of features:

- Select the features of interest in your layer using any of the selection tools in QGIS.
- Open the Field Calculator and ensure the “Only update selected features” option is checked before running your length calculation. This will apply the calculation only to your selected features.

### 8. Is it possible to calculate line lengths directly in the Print Layout or on a map?

While direct calculations in the Print Layout are not supported, you can display the lengths of lines on your map by creating labels:

- Go to the layer’s Properties -> Labels.
- Select an expression for your label like
`to_string($length) + ' m'`

(for meters) or perform conversions as needed. - These labels can then be styled and displayed alongside your features on the map and included in the Print Layout.

Further reading