Forms in QGIS. Dynamic update and behavior of spatial entities.
Luis Eduardo Perez Graterol
Especialista en Sistemas de Información Geográfica en entornos comerciales y de libres. Desarrollador de plugins (QGIS) y script tools (ArcGIS). Integración de modelos de Inteligencia Artificial a datos geoespaciales.
This article is a continuation of the article Forms in QGIS. Here we will see another capability of the forms and examples of its application.
It is important to note that this feature was explained by Mauricio Marquez Goa in his article Function aggregate in QGIS 3.8. It is a recommended reading if you want to learn other applications besides the ones we will see below.
Another must read are the articles by Ujaval Ghandi, aggregate and spatial filters in QGIS, Advanced aggregate expressions to automate quality control in QGIS.
The objective of this article is to analyze the implications and potential of the forms in various data management systems, such as cadastral, supply networks, environmental, as well as to explain in a simple way how to use this function.
Advanced users can refer to the final part of the article where more complex procedures are applied.
Introduction
GIS uses a layered structure to differentiate and represent the elements of the territory. As we commented in QGIS Forms, the vector format allows the most accurate representation.
However, a further step is to achieve a behavior of the entities according to the element they represent. Through the expressions in the forms we can approach this goal.
What do we mean by behavior of spatial entities?
Each type of entity or phenomenon that we want to represent has its own peculiarities, for example: drainage, transport, electricity, water supply networks are totally different from each other, but all of them can be represented in a GIS by the same type of geometry (lines).
These representations are models of a reality that we want to simulate and study, if we can get the layers to generate or update their data according to the reality they are trying to represent, we will get closer to our goal.
A simple example is the case of the hydrographic network, the flow of water takes one direction, in the direction of the steepest slope and the volume of water accumulates downstream to the mouth. However, other networks, for example supply networks, have other particularities.
In the case of a cadastral system, the lot of land has some attributes of interest whose determination can be automated, for example geometric properties: area calculations, perimeter, front, back, up to those related to other layers, such as political, administrative location, inverse geocoding.
A more ambitious aspect would be to achieve an estimation (preliminary or final) of the value of the land, (if a lot of land is digitized), or of the construction, (infrastructure). In both cases the calculation would be made considering a series of criteria represented by additional layers, for example: the average value for the area(s) where it is contained, proximity to main and secondary roads, services, as well as elements that could decrease the value.
In the case of a network, for example supply, digitizing a pipe should recognize the diameter, material and other aspects of the pipe to which it is connected or extended.
Clearly, providing behavior through dynamic updating is an undertaking that has varying degrees of complexity and processing requirements. The attribute of a layer can be calculated directly or depend on the relationship to other layers.
Form configuration for dynamic data update
To display the forms select a layer in the layers panel, then right click properties.
In the layer properties dialog box select the Form tab, you will notice two panels, the one on the left corresponds to the fields of our table of attributes and controls, on the other hand the one on the left shows the configuration options.
Geometric properties
This is the simplest case, because geometric properties, such as area, perimeter, length, centroid, can be extracted directly from the entity we are digitizing.
1.- Coordinates:
For a layer of points representing, for example: a hydrant, a natural element (a tree) the occurrence of an event, such as a crime etc. We can require that when digitizing it on screen, the coordinates are immediately loaded into the attribute table.
To do this, the first requirement is to add to the attribute table of the layer the numeric fields where the values of the pair of coordinates (north and east) will be stored.
Then display the attribute table in the forms option, in the left panel we select the North field, when the right panel is activated we go to the Defaults options.
In the default value option, enter the expression to obtain the Y coordinate of the point, considering the ellipsoid $y or planimetric y($geometry). The procedure is repeated for the East (or X) coordinate by entering $x or x($geometry).
Finally activate the option Apply default value when updating.
2.- Area, perimeter and length
For the other geometry types, lines and polygons, the procedure is similar, only the expression varies. For example, for a line layer we can determine the area with $area, the perimeter with $perimeter. Likewise for line layers we can determine the length with $length.
What is the benefit of this procedure?
After this simple configuration when creating a new entity (polygon, line or point) the previously configured data is automatically filled in,
Relative Location
Referencing entities is a common aspect when consolidating spatial data attributes. That is to say, to specify in which country, state, province, department, neighborhood, block a certain property is located.
If we have the polygonal areas we can automate the process. This case is a spatial query that will determine within which "municipality" is located an infrastructure represented by a point. For this we will use an aggregate expression.
What is an aggregate expression?
The term aggregate is equivalent to group, or more understandable summarize, totalize./
When you display the expressions dialog, you will notice that the central panel shows the available expressions, the first category being aggregates.
Most of these expressions return a statistic of a data set, for example the mean, median, interquartile range (iqr), range, sum, standard deviation (stdev), first quartile (q1), third quartile (q3).
On the other hand, if we want to perform a query that returns multiple results, we can use concatenate expressions that allow us to join the texts returned by the expression. Also, we can use array_agg that returns a list of results.
How do expressions work?
Each expression requires us to specify the inputs and certain configuration options. Selecting an expression displays a help, a description of how it works and how to use it.
Each expression requires us to specify inputs and certain configuration options. Selecting an expression displays a help, a description of how it works and how to use it.
For example, as shown in the image, if you select the expression mean and enter as argument the name of a field with double quotes, it will return the average value of this field for all records in the table. Similar to using the statistics panel.
Options to refine the query
The previous case is the simplest, if we look at the expression help we will notice that it has options to refine the query. For example if we calculate the average population of a layer of countries we can enter an expression that restricts the calculation of the average only those countries with a population less than xxx value, using the filter option. We can also configure a spatial filter as we will see later.
mean( ----Expression aggregate 'paises', ----layer countries "poblacion", ----field filter:="poblacion"< 30000000) ----filter
On the other hand, it is possible to calculate the average by grouping the countries by continents with the group_by option.
mean( ----Expression aggregate 'paises', ----layer "poblacion", ----field group_by:="continente") ----grouping by
Expression context
The vast majority of expressions act on the layer from which we call the Expression Dialog, for example: if we activate it from the parcel layer, we can only perform calculations on the attributes of that layer.
But there is a special type of expression, called aggregate, we can catalog it as a more versatile and complex function, which allows us to consider other layers in our analysis, this will allow us to perform queries that evaluate whether the entities of our layer of interest have a spatial relationship with another layer (they are close, they touch, they overlap).
Aggregate function:
The power of this function has motivated leading specialists to produce the articles cited at the beginning.
This function allows us to apply an aggregate function between two layers. Let's see an example to automatically fill the location of a point.
For a series of infrastructures represented by the red circles in the image, we wish that when a new point is created, the municipality (administrative political division) where it is contained is filled in the attribute table.
aggregate ( 'municipio', ---Additional layer 'concatenate', ---Expression aggregate "Nb_municip", ---Field filter:=intersects(geometry(@parent), $geometry) ---Spatial filter )
We will notice that it requires the name of an additional layer, in this case 'municipio' (municipality), then the added expression that we will use 'concatenate', which serves to join texts.
If we expect to get several results, we can specify a separator for the texts with 'concatenator', but as we are working with dots it can only be contained in a municipality. The next argument ("Nb_municip") is the field with the names of the municipalities.
Finally we apply a filter (filter:=intersects(geometry(@parent), $geometry)) that will determine in which municipality the point is contained. In this case intersects is an expression that requires two geometries and evaluates if the first intersects the second. The geometry of the main layer (infrastructure) that we access with geometry(@parent) and the geometry of the polygon of the municipality, through the variable $geometry.
How does the aggregate expression work?
For each point of the main layer (infrastructure) performs a traversal of all polygons of the municipality layer, evaluates the filter, that is, if that point is contained in that polygon (municipality), if the result is false continues with the next polygon. On the other hand, if it is true it returns the value of the field "Nb_municip", then continues with the rest of polygons looking for other matches.
Spatial query and geoprocessing
The next case is of a higher level of complexity, involving spatial query and geoprocessing operations.
We are performing the characterization of a watershed and its sub-basins, for this we have the polygon layer with the watershed divide. It is important to note that not all possible sub-basins have been defined and we wish to automate the characterization process as the remaining sub-basins are digitized.
On the other hand, for the same study area, there is a layer of polygons with vegetation cover. A total of seventeen vegetation/use categories have been defined.
The objective of the study:
To automatically determine the vegetation types and the area they occupy, within each subwatershed. This result should be written in the attribute table and plotted on the form.
Note: the usual procedure to solve this problem would be to intersect the two layers and then request a statistical summary by category. Then write the result in the attribute table and configure the form.
If we want to know which vegetation types are present for each watershed we can use the expression:
aggregate( 'vegetacion', ---Vegetation layer 'concatenate', ---Expression aggregate "categoria", ---Field with the type of vegetation concatenator:=' , ', ---Character for separating texts filter:=intersects(geometry(@parent), $geometry) --Filter )
In the image above, at the bottom (red box) we can see that our expression works but returns repeated results, this is due to the presence of several polygons of the same type of vegetation within the watershed.
To solve this we replace the aggregate expression with 'concatenate_unique'.
For the case of the surface of the vegetation types the expression is more complex.
aggregate( 'vegetacion', ---Vegetation layer 'concatenate', ---Expression aggregate to_string( ---Convert to text round( ---Rounding decimals area( ---Calculate area intersection(geometry(@parent),$geometry) ---Determine intersection ),2) ---Rounding decimals ), concatenator:=', ', ---Character for separating texts filter:=intersects(geometry(@parent),$geometry)--Filter )
You may notice some new features, the most outstanding is the intersection(geometry(@parent),$geometry) portion which returns the geometry resulting from the intersection of the two input geometries. Then we determine the surface of that geometry with area(), round the decimals with round(value,2) and finally convert it to text with to_string().
Limitations of the expressions
Although we can obtain a list of vegetation types and the areas they occupy, we need to total them as follows: Gallery forest: 300.22, Shrublands: 120.45, .....
Expressions is one of the fastest improving features in QGIS, however, for the QGIS 3.10.12 A Coru?a version, I did not find a combination of expressions that would allow me to obtain the totalization.
However, this will not prevent us from achieving our goal - we can create our own expressions!
The expression dialog box has a tab called function editor where we can write an expression using Python, then integrate it with the rest of the available expressions and use it for our analysis.
In our case we do not require a very complex expression, just take both lists, with the types of vegetation and surfaces product of the intersection, then total them.
We can now test the performance of our new expression by drawing a polygon in the watershed layer:
Final notes:
Expressions are a very extensive topic, some aspects were covered briefly, it is recommended to read the cited articles as well as the official QGIS documentation.
Going into more details about the intricacies of expressions and how to create them is beyond the scope of this article, but this can be addressed in detail in the course presented in the previous article.
I am also considering the possibility of creating an article dedicated to the expressions and their implications.
On this topic, the videos commented are pending, first of them, the one on watersheds used in the articles.
--
3 年Very educational. THX for sharing
Expert geomaticien Analyst data GIS, SIG, Geospatial, BD, Alumni Fellow DCDJ, Mail:[email protected] +2250709096707
3 年Amalaman Habib Baikoro
Wildlife Research Assistant at Cameroon Biodiversity Association (CAMBIO) , R, GIS and Remote
3 年Hi thanks for this article, how can I have a copy to follow offline?
Project manager- GIS and spatial data specialist- Ms. Urban planner- Designer- Coastal Manager.
3 年Love this
Chef de projet MOE SI & SIG
3 年bravo