0% found this document useful (0 votes)
174 views

Calculating Line Lengths and Statistics (QGIS3) - QGIS Tutorials and Tips

This document provides a tutorial for using QGIS to calculate line lengths and statistics from a polyline layer. The tutorial uses a railroads dataset for North America and shows how to: 1) Add a column with the length of each line feature using the Add Geometry Attributes tool. 2) Filter the layer to include only lines in the US using the country code attribute. 3) View statistics like total length in the Statistics panel, converting the values to kilometers.

Uploaded by

mp dc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views

Calculating Line Lengths and Statistics (QGIS3) - QGIS Tutorials and Tips

This document provides a tutorial for using QGIS to calculate line lengths and statistics from a polyline layer. The tutorial uses a railroads dataset for North America and shows how to: 1) Add a column with the length of each line feature using the Add Geometry Attributes tool. 2) Filter the layer to include only lines in the US using the country code attribute. 3) View statistics like total length in the Statistics panel, converting the values to kilometers.

Uploaded by

mp dc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Author

Ujaval Gandhi

Follow @spatialthoughts

Check out more QGIS learning materials, including free full-length QGIS courses from Spatial Thoughts Academy
(https://spatialthoughts.com/resources).

Subscribe to my list

Email address

Subscribe

Change Language

English

Calculating Line Lengths and Statistics


(QGIS3)
QGIS has built-in functions and algorithms to calculate various properties based on the geometry of the feature - such as
length, area, perimeter etc. This tutorial will show how to use the Add geometry attributess tool to add a column with a
value representing length of each feature.

Overview of the task


Given a polyline layer of railroads in North America, we will determine the total length of railroads in the United States.

Other skills you will learn


Using expressions to lter features.
Using the Statistics panel to compute and view statistics on columns.

Get the data


Natural Earth (http://www.naturalearthdata.com/downloads/10m-cultural-vectors/railroads/) has a public domain rail-
roads dataset.

Download the North America supplement


(http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/ne_10m_railroads_north_america.zip)
zip le from the portal.

For convenience, you may directly download a copy of the dataset from the link below:

ne_10m_railroads_north_america..zip (http://www.qgistutorials.com/downloads/ne_10m_railroads_north_america.zip)

Data Source [NATURALEARTH] (../credits.html#naturalearth)


Procedure
1. Locate the downloaded ne_10m_railroads_north_america.zip le in the Browser panel and expand it. Drag the
ne_10m_railroads_north_america.shp le to the canvas.

2. You will see a new layer ne_10m_railroads_north_america loaded in the Layers panel. You will see that the layer
has lines representing railroads for all of North America. Now, let’s calculate the lengths of each line feature. Go to
Processing ‣ Toolbox.
3. Search for and locate the Vector geometry ‣ Add geometry attributes algorithm. Double-click to launch it.
4. In the Add Geometry Attributes dialog, select ne_10m_railroads_north_america as the Input layer. The input layer’s
Coordinate Reference System (CRS) is EPSG:4326 WGS84. This is a Geographic CRS with Latitude and Longitude as
coordinates, WGS84 as ellipsoid and degrees as units. Because latitude and longitude don’t have a standard length,
you can’t measure distances or areas accurately using planar geometry functions. Fortunately, QGIS provides a bet-
ter way to compute distances using ellipsoidal geometry, which is the most accurate method for layers spanning
large areas such as this. Pick Ellipsoidal as the Calculate using option. Click Run. Once the process nishes, click
Close.

Note
If your input layer is in a Projected CRS, you may choose Layer CRS option for calculation. Local or Regional projected
coordinate systems are designed to minimize distortions over their region of interest, so are more accurate for such
computation.

5. You will see a new layer Added geom info loaded in the Layers panel. This is a copy of the input layer with a new
column added for distance. Right-click the Added geom info layer and select Open Attribute Table.
Note
The Add Geometry Attribute tool adds di erent set of attributes depending on whether the input layer is points, lines
or polygons. See QGIS documentation
(https://docs.qgis.org/testing/en/docs/user_manual/processing_algs/qgis/vectorgeometry.html#add-geometry-attrib-
utes) for more details.

6. In the Attribute Table, you will see a new column called distance. This contains the length of each line feature in
meters. Also note that the sov_a3 attribute which contains the country code for each feature. Close the Attribute
Table window.
7. Now that we have lengths of individual railroad line segments, we can add them up to nd the total length of rail-
roads. But as the problem statement demands we need total railroad length in the United States, we must use only
the segments contained within USA. We can use the country code value in the sov_a3 column to lter the layer.
Right-click the Added geom info layer and select Filter.
8. In the Query Builder dialog, enter the following expression and click OK.
"sov_a3" = 'USA'

9. You will see a Filter icon appear next to the Added geom info layer in the Layers panel indicating that a lter is ap-
plied to the layer. You can also visually con rm that the layer now contains line segments only for United States.
Now we are ready to calculate the sum. Click the Show statistical summary button on the Attributes Toolbar.
10. A new Statistics panel will open. Select Added geom info layer and length column.

11. You will see various statistics displayed in the panel. The unit of the statistics is the same as the units of length
column - meters. Let’s change the computation to use kilometers instead. Click the Expression icon next to the
elds drop-down menu in the Statistics panel.
12. Enter the following expression in the Expression Dialog that converts the length to kilometers.
length / 1000

13. The Sum value displayed is the total length of railroads in USA.
16 Comments QGIS Tutorials and Tips 🔒 Disqus' Privacy Policy 
1 Login

 Recommend 5 t Tweet f Share Sort by Best

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Lowell Larsson • 2 years ago


I am just starting to learn QGIS and could not figure out how to calculate the areas of polygons
in a layer. I found this tutorial helpful in understanding the steps necessary to preform
calculations on a layer. Thank you for sharing it.
2△ ▽ • Reply • Share ›

Yadvi • 4 months ago


Hi, I just started learning qgis. How do I know what units are used in the attribute table? In this
example, length is in meters but where can I find that information?
△ ▽ • Reply • Share ›

Ujaval Gandhi Mod > Yadvi • 4 months ago


If you use 'Ellipsoid' calculation, the units are always meters. But if you use 'Layer
CRS' , the units will depend on the CRS (projection) of the layer. Most CRSs have
meters as units, some CRSs such as EPSG:4326 will have degrees as units, others
such as EPSG:2227 will have units as feet. So check the CRS of your layer to know
what are the units. https://spatialreference.org/ is a good resource for that.
△ ▽ • Reply • Share ›

Itscom Plicated • 6 months ago

You might also like