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.
Given a polyline layer of railroads in North America, we will 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 in the Browser panel and expand it. Drag the ne_10m_railroads_north_america.shp
file to the canvas.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 .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 better 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 finishes, 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.
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 different set of attributes depending on whether the input layer is points, lines or polygons. See QGIS documentation for more details.
Added geom info
layer and select Filter."sov_a3" = 'USA'
Added geom info
layer in the Layers panel indicating that a filter is applied to the layer. You can also visually confirm 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.Added geom info
layer and length
column.length
column - meters. Let’s change the computation to use kilometers instead. Click the Expression icon next to the fields drop-down menu in the Statistics panel.length / 1000
This work is licensed under a Creative Commons Attribution 4.0 International License