Aggregate functions in QGIS explained with simplicity - Part One

Aggregate functions in QGIS explained with simplicity - Part One

Leer en espa?ol

Introduction

This is the first of a series of articles focused on the most powerful but complex expression of QGIS, the aggregate function. The objective is to explain its use and syntax in a gradual, simple and didactic way.

QGIS is a powerful software that allows the processing of information accurately located on the Earth's surface, using a series of functions and expressions through which geoprocesses can be executed, as well as advanced configuration of elements such as labeling, symbology and cartographic output (Atlas).

Expressions

QGIS is configured to work with two programming languages C++ and Python, a situation that facilitates developers and users to create expressions with which they can execute predefined processes, create new elements and manipulate data structures, also allows access to global and context variables, references to objects, among others.?

Expressions are fully integrated in QGIS. With each new version, the number of available expressions increases. Expressions are one of the fastest developing aspects of the software, due to the need to provide greater integration and ease of use.

By means of simple and complex expressions it is possible to perform a great variety of processes, which will facilitate the analysis and cartographic visualization, among them we have:?

  • Select by attributes or geometries.
  • Query and modify attributes
  • Spatial queries
  • Query geometric properties
  • Geoprocesses.
  • Dynamically change geometry style.?
  • Modify label content or position,?
  • Many others....

How are the expressions organized?

No hay texto alternativo para esta imagen

As you can see in the image, the Expression Dialog is made up of three (03) panels, on the left the space where we will write the expression, in the middle the list of expressions and on the right a description of the selected expression.

An important aspect of the structure of the expressions is that they are generally made up of three (03) basic elements:

  1. The function or variable is counted with a large variety organized by categories (central panel of the image above).
  2. The argument or input required by the function, generally a layer or field. Advanced expressions require multiple arguments so we must specify them using key terms, for example: overlay_intersects(layer:='regions', filter:= population > 10000).
  3. Operators these allow to link and combine the various elements of the expressions, among them: +,-,=,*,/,<,>,(),||,^,/n.?

How do expressions work?

There are two fundamental aspects about the behavior of expressions:

1.- Expressions, whether variables or functions are linked to the active layer:

No hay texto alternativo para esta imagen

2.- Expressions that execute functions, for example: the calculation of area ($area or area()), perform a sequential path from the first feature to the last one.

No hay texto alternativo para esta imagen

What is a feature?

As shown in the image above, this term encapsulates the geometry and its corresponding attribute.

No hay texto alternativo para esta imagen

Aggregates Expressions

The first category of the Expression Dialog is the Aggregates. These functions allow you to group, summarize, totalize values and geometries.?

The simplest use is to generate statistics of a numeric field of a layer, for example: the average population of countries: mean("Population").?

The expressions for calculating statistics are:

  • Count
  • Interquartile range (iqr)
  • Mean (mean)
  • Median (median)
  • Maximum value (maximum)
  • Minimum value (minimum)
  • First quartile (q1)
  • Third quartile (q3)
  • Range (range)
  • Standard deviation (stdev)
  • Sum (sum)

The Aggregates functions have clauses, similar to those of the SQL query language, by means of which we can refine the expressions, optimizing the queries or calculations performed.?Among them:

  • Group_by allows to group a query by a categorical field for example: get the average population grouped by region: mean(expression:="Population",group_by:="Region").?
  • Filter with it you can refine the previous query, so that for example it considers only countries with a GDP>20 mean(expression:="Population",group_by:="Region",filter:="GDP" > 20)?
  • Order_by can organize the records or results obtained taking into consideration the criteria issued by the user.

The aggregate function a very special expression

One of the most important functions within the Aggregates is the aggregate function, which allows implementing all the other aggregate expressions in a single expression.

By using it, users can obtain great advantages since it can be used to perform spatial and/or attribute operations involving more than one layer.

Function syntax:

aggregate(layer:= ,??aggregate:= ,??expression:= ,??order_by:= , filter:= )??

You can read the continuation of the article with the development of the aggregate function in the following link: aggregate function

要查看或添加评论,请登录

Luis Eduardo Perez Graterol的更多文章

社区洞察

其他会员也浏览了