Calculate Geometry in QGIS: How to calculate line length, polygon area, and polygon perimeter


For many GIS analyses, it is necessary to calculate the length, area, or perimeter of line or polygon features (i.e. geometries). These operations are referred to as calculating geometry. The QGIS tool that calculates feature geometries is the Field Calculator tool. There are some nuances and potential pitfalls when calculating geometry in QGIS. This guide will walk you through the process in a few simple steps.

Add Data to QGIS

To begin, add some vector data into QGIS. Here I’m using a shapefile that displays the boundaries of US states. This shapefile can be obtained from the US Census Bureau. The shapefile (and a video demonstration of geometry calculation) is also part of the QGIS for Beginners course materials.

Geometry can be calculated for line and polygon features. Points do not have length or width dimensions so you will not be able to calculate geometry for point features.

Shapefile containing polygon boundaries for states of the United States

Calculate Feature Geometry

Once your data are added to QGIS, open the attribute table for the vector layer (right-click on the layer name in the table of contents and select ‘Open Attribute Table’). We will make a new column in the attribute table that gives values for the geometry of each feature. This can be done very easily with the Field Calculator tool.

1. Open Field Calculator

Open the field calculator by clicking the icon on the attribute table toolbar. The field calculator icon is indicated by an abacus. When you hover your cursor over the icon it will display the ‘Open field calculator’ help message.

Open the field calculator.

2. Define Name and Data Type for a New Field

After you click on the icon a new dialog window will open, similar to the one shown below. Here you will enter the information to create a new field and the expression to populate the field values. To start, set up the new field. Give it a name and a data type. For geometry calculations, it’s best to use decimal (real) numbers. Below, you can see that I’ve selected the option to create a new field named ‘area’ as a decimal data type.

Create a new field for the area calculation.

3. Enter an Expression to Calculate Geometry

Now, use the expression editor box to create an expression that will calculate geometry. I know this may sound a little difficult, but it’s really quite simple. The list of options in the middle of the screen (below the search bar) gives you all the variables, attributes, functions, etc. that can be used to build expressions.

Expand the ‘Geometry’ list. Select ‘$area’. In the function description, you will see that this calculates the area of a polygon object. If you scroll through the Geometry list you will also see ‘$perimeter’, which will calculate a polygon’s perimeter. Line features will have a $length option available.

Definition of $area function

Double-click $area to add it to the expression editor. Notice the preview below the expression box. This shows what the values for the new column will be for the specified feature. Here the area is shown in square meters. Now is a good time to do a quick check and make sure the answer is about what it should be. The reported area of Alabama is about 52,000 square miles. Converting the preview answer to square miles results in about 51,400. This is acceptable since there is going to be error associated with projections and coordinate systems.

Expression editor and preview.

4. Run the Expression

Once you’re satisfied that the preview value is correct, run the expression for all features in the vector layer. Simply click OK to run the expression. Depending on the number of features in your layer this may take a few minutes. When complete, you’ll have geometry calculations for each vector feature.

Results from the area calculation are shown in the far right column.

5. Turn off Editing Mode

QGIS turns on editing mode to complete the geometry calculation. To prevent yourself from inadvertently making changes to the vector layer, turn off editing mode once the calculation is complete. Simply click the pencil icon to toggle editing mode off.

Conclusion

You can follow this same approach to calculate the area or perimeter of polygon features or the length of line features. The steps are the same, just change the function used in the expression editor to meet your desired result. It’s important to know which units the results are presented in. In this case, the area was in units of square meters. Always check. It’s also a good idea to include the units in the name of the new field (something we didn’t do here) so that when you come back to do more analysis you don’t have to second guess.

For an introduction to geographic analysis with QGIS check out the QGIS for Beginners course, which contains over 5 hours of video instruction and data to complete all the course exercises. From the link, you can view several sample lessons to get a feel for the course and its material.

Konrad Hafen

Konrad is a natural resources scientist. He develops models and analysis workflows to predict and evaluate changes to landscapes and water resources.

Latest Tutorials