QGIS has built-in functions to calculate various properties based on the geometry of the feature - such as length, area, perimeter etc. This tutorial will show how to use Field Calculator to add a column with a value representing length of each feature.
We will use a polyline shapefile of railroads in North America and try to determine the total length of railroads in the United States.
Natural Earth has a public domain railroads dataset. Download the North America supplement zip file from the portal.
For convenience, you may directly download a copy of the dataset from the link below:
ne_10m_railroads_north_america..zip
Data Source [NATURALEARTH]
ne_10m_railroads_north_america.zip file and click
OK.
ne_10m_railroads_north_america.shp layer.
"sov_a3" = 'USA'. Click Select followed by
Close.
ne_10m_railroads_north_america layer and select Save
Selection As….
usa_railroads.shp.
We also want to change the CRS of the layer. Click on Browse
next to CRS.Note
The built-in functions that use a feature’s geometry for calculation use the units of the layer’s CRS. Geographic Coordinate Reference System(CRS) such as EPSG:4326 have degrees as units - so the length of the feature would be in degrees and area in square degrees - which is meaningless. You need to use a Projected Coordinate Reference System with units of meters or feet to perform such calculations.
usa_railroads
loaded in QGIS. You can uncheck the box next to
ne_10m_railroads_north_america layer to turn it off as we don’t need it
anymore.
usa_railroads layer and select
Open Attribute Table.
$length / 1000 because our layer CRS is in meters
unit and we want the output in km. Click OK.
usa_railroads. Choose the
Target field as length_km and click OK. You
will see various statistics appear. The Sum value is the total
length of the railroads that we are looking to find.Note
This answer will vary slightly if a different projection is chosen.In practice, line lengths for roads and other linear features are measured on the ground and provided as attributes to the dataset. This method works in absence of such attribute and as an approximation of actual line lengths.
This work is licensed under a Creative Commons Attribution 4.0 International License