Skip to content

African Surveyors Connect | GeoConnect

The geospatial platform for visionaries and those who dare to dream

Menu
  • Posts
    • News
    • General
    • Educational
    • Spotlight
  • Jobs
  • Events
  • More
    • Learn-GIS
    • Magazine
    • Academy
Menu

Calculating Line Lengths and Statistics (QGIS3)

Posted on April 12, 2021 by KUMBIRAI MATINGO

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 filter features.
  • Using the Statistics panel to compute and view statistics on columns.

Get the data

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]

Procedure

  1. Locate the downloaded 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.
../../_images/120.png
  1. 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.
../../_images/218.png
  1. Search for and locate the Vector geometry ‣ Add geometry attributes algorithm. Double-click to launch it.
../../_images/316.png
  1. 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 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.
../../_images/47.png

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.

  1. 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.
../../_images/57.png

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.

  1. 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.
../../_images/67.png
  1. Now that we have lengths of individual railroad line segments, we can add them up to find the total length of railroads. 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 filter the layer. Right-click the Added geom info layer and select Filter.
../../_images/77.png
  1. In the Query Builder dialog, enter the following expression and click OK.
"sov_a3" = 'USA'

../../_images/87.png

  1. You will see a Filter icon appear next to the 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.
../../_images/97.png
  1. A new Statistics panel will open. Select Added geom info layer and length column.
../../_images/107.png
  1. 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 fields drop-down menu in the Statistics panel.
../../_images/1111.png
  1. Enter the following expression in the Expression Dialog that converts the length to kilometers.
length / 1000

../../_images/127.png

  1. The Sum value displayed is the total length of railroads in USA.
../../_images/137.png
KUMBIRAI MATINGO
KUMBIRAI MATINGO

Kumbirai is a GIS & MEAL specialist using geospatial analytics to advance global health and social impact. A certified Data Protection Officer (DPO), an open-data advocate and self-taught software developer, he builds web GIS tools that turn field data into decisions. He lectures in GIS/Remote Sensing and mentors emerging practitioners. Founder of a geospatial startup and nonprofit, he believes, “Real geospatial innovation happens when we empower communities with the right tools and knowledge.” Open to consulting and collaborations.

Share this:

  • Tweet
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print

Related Articles You Might Like

Search for Articles

Latest Posts

  • Deploying a GeoDjango App on Heroku
  • The Truth About Self-Taught Skills No One Tells You
  • Transformative Mapping Event Launches in Chinhoyi: A Testament to Youth Engagement and Community Collaboration 
  • GeoConnect Awarded Grant for Transformative OpenStreetMap Project in Chinhoyi, Zimbabwe
  • Unlocking Data Treasure Troves: A Guide to Accessing Zimbabwe’s Geospatial Datasets

In today’s digital age, a website is the face of your brand, and having a well-designed and functional website is essential for businesses to succeed online. Our web development services combine creativity and technical expertise to build custom websites that not only look great but also deliver a seamless user experience across all devices.

From responsive design to e-commerce integration, our team of web developers has the skills and experience to bring your vision to life and help your business thrive in the online world. Let us help you create a website that showcases your brand and drives growth for your business. Get in touch via WhatsApp on +263-77-6-887-606 or send me an Email on matingonk@gmail.com

While we have renamed and re-branded to GeoConnect, we have dedicated this platform to continue our work in sharing and exchanging knowledge with the community that has helped us become what we are today.

The journey and legacy continues. Learn more about Geo-Connect.

Quick Links / Resources

  • Legal
  • About Us
© 2025 African Surveyors Connect | GeoConnect | Developed for the profession with support from KM-Spatial