Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
Python
QGIS
Uncategorized

Beginners guide to the QGIS Field Calculator

The Field Calculator in QGIS

The Field Calculator in QGIS is a powerful tool that allows you to perform calculations on the attributes of a vector layer. Here are a few examples of things you can do with the Field Calculator in QGIS:

Learn more about QGIS here!

Perform basic arithmetic calculations:

  • Add two fields together: "field1" + "field2"
  • Calculate the difference between two fields: "field1" - "field2"
  • Multiply two fields: "field1" * "field2"
  • Divide two fields: "field1" / "field2"

Use functions to perform more complex calculations:

  • Calculate the square root of a field: sqrt("field")
  • Calculate the sine of an angle field (in degrees): sin(degrees("field"))
  • Calculate the length of a line feature: $length
  • Calculate the area of a polygon feature: $area

Use variables to reference other fields or constants:

  • Add a fixed value to a field: "field" + 10
  • Calculate the ratio of two fields: "field1" / "field2"
  • Calculate the percentage of a field: "field" / sum("field") * 100

Here is a beginner’s guide to using the Field Calculator:

In order to modify the attributes of a vector layer in QGIS, you need to be in “edit” mode. This means that you need to turn on the “Edit” button in the attribute table window or on the main toolbar.

To enter edit mode, follow these steps:

  1. Open the attribute table of the layer you want to modify.
  2. Click the “Toggle Editing” button in the attribute table window or on the main toolbar.
  3. Make sure that the “Edit” button is highlighted, indicating that you are in edit mode.

Once you are in edit mode, you can use tools such as the Field Calculator to modify the attributes of the layer. When you are finished, you can save your changes by clicking the “Save Edits” button.

To start working with the Field Calculator, follow these steps

  1. Open the attribute table of the layer you want to modify.
  2. Click the Field Calculator button in the attribute table window.
  3. In the Field Calculator dialog, select the field that you want to calculate in the “Field” dropdown.
  4. In the “Output field” name field, enter a name for the new field that will store the calculated values.
  5. In the “Formula” field, enter the calculation that you want to perform. You can use standard arithmetic operators (e.g. +, -, *, /), as well as functions and variables.
  6. If you want to update the values of the existing field, check the “Update existing field” box.
  7. Click the “OK” button to execute the calculation.

Examples of Field Calculator expressions that you can use in QGIS:

  • Calculate the length of a line feature in meters: $length
  • Calculate the area of a polygon feature in square meters: $area
  • Calculate the total population of a district by summing the population of each census block: sum("population")
  • Calculate the population density of a district by dividing the total population by the area: "population" / $area
  • Calculate the percentage of a field by dividing the field by the sum of all values and multiplying by 100: "field" / sum("field") * 100
  • Convert a field containing angles in degrees to radians: radians("degrees")
  • Calculate the average value of a field by dividing the sum of the field by the number of features: sum("field") / count()

Use the Field Calculator to add strings together, insert dates and times, and use values from different fields in your calculations. Here are a few examples:

  • Concatenate two string fields: "field1" || ' ' || "field2"
  • Insert the current date: now()
  • Insert the current time: time()
  • Extract the year from a date field: year("date_field")
  • Calculate the difference between two dates in days: "date_field1" - "date_field2"
  • Calculate the total cost of an order by multiplying the quantity by the price: "quantity" * "price"

Examples of Field Calculator expressions that you can use to work with geometries in QGIS:

  • Calculate the centroid of a polygon feature: centroid($geometry)
  • Calculate the bounding box of a feature: bounds($geometry)
  • Calculate the distance between two points: distance(point_a, point_b)
  • Calculate the area of a polygon feature in square kilometers: $area / 1000000
  • Calculate the perimeter of a polygon feature in kilometers: $perimeter / 1000
  • Calculate the azimuth (bearing) between two points: azimuth(point_a, point_b)

You can use these expressions as part of a larger formula, or you can use them to create new fields that store the calculated values.

Here are a few tips for using the Field Calculator:

  • You can use the “Insert Expression” button to select from a list of available functions and variables.
  • You can use the “Sample” button to preview the calculated values for a few selected features.
  • You can use the “Test” button to check the syntax of your formula.
About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.

Leave a Reply