The Voronoi maps are built based on a series of polygons created around the location of each sampling point.
The Voronoï polygons are created in such a way that each location in a polygon is closer to the sampling point present in this polygon than any other sampling point .
In the article regarding the Exploratory data analysis , we discussed how to use the ArcGIS Geostatistical Analyst to build and analyse the Voronoi polygons.
In this article we will discuss how to get the same results with QGis.
The main difference between the two software (ArcGis and QGis) lies in the fact that in Geostatistical Analyst the tool calculates the Voronoi polygons and offers their display with local statistics. The result is a thematic map where the polygons are displayed with a colour gradient in function of the local value of the chosen statistic.
For example, if we choose the option “average”, the colour assigned to each polygon will be a function of the average of a column (“Attribute”)
In QGis, the Voronoï tools are limited to the calculation of polygons. There is no option to calculate the local statistics simultaneously. Each polygon will have as attributes; the concerning attributes of the point therefore in order to calculate the local statistics extra work is required.
It is possible to do it using different methods. Here, we will follow one of the available procedures; we will use a SQL query.
This procedure consists in:
- Calculate the Voronoi polygons with QGis processing tools
- Import the result in a database (Sqlite or Postgres)
- Add a field to include the desired local statistic
- Perform a SQL query to fill this field
- Display the layer with a defined theme in this field
Calculate the Voronoi polygons with QGis processing tools
NOTE: the approach is the same as this with either 2.18 or 3.2 QAGis
You have two tools that allows you to calculate the Voronoï polygons in the QGis treatment:
One of the tools belongs to the group “Vectorial Geometry”, the other is an integral part of GRASS geoprocessing.
You can use them indifferently, but I do prefer GRASS.
Save the result as a shape file. This will allow you to load more easily the file in your database.
How to import the result in a database (Sqlite or Postgres)
In order to use the spatial commands in your layer we will load it into a database allowing this type of operations. You can always create and load the shape file into a Sqlite database, provided by QGis. Or you could, if you have installed Postgresql on your computer, use a Postgresql / Postgis database.
For importing , you have to follow the same process independently of the chosen database.
Note : To create a Sqlite database , go to QGis Explorer panel, right click on Spatialite , then on Create Database
To import your shape file with Voronoï polygons you will use the database Manager:
- go to Database menu -> DB Manager to open the window of the database manager.
- Login to the selected database
- Click on the Import a layer / file tool
Tick your shape file and name to the table you want to create. Click OK.
A message will appear indicating that the import has been performed.
Add a field to include the desired local statistics
While remaining in the database manager, select the window “providers”, the table you just created, and then go to Table-> Edit Table in the manager menu.
Click on the Add a column button and fill in the characteristics of the field you want to calculate.
In our example we will calculate the average of each polygon and its immediate neighbours.
How to perform a SQL query to fill up this field
Before discussing the syntax of the query, let’s see what we want to calculate ;
For each polygon we want to find the polygons that have a shared side with the polygon into consideration, then to calculate the average of an attribute for this set (the value of the central polygon plus the values of the adjacent polygons).
For a Postgesql / postgis base, the query is as follows
update voronoi vor
set mean = (SELECT sum (a. parapen) :: real / count (a. parapen) :: real
FROM voronoi as a
JOIN voronoi as b
ON st_intersects (( st_buffer (a.geom, 0.00001)), b.geom )
where b.id = vor.id)
where voronoi is the name of the table, parapen is the name of the attribute which we want to calculate the local average and geom is the name of the geometry field of the table.
Notice the operators :: real, essential for the result (the average ) has to be a real number. Right here as attribute parapen is an integer field, by default the result is an integer without a decimal part.
If you use a Sqlite base, the syntax has to be adapted since this is not the same SQL as postgres ( :: real must be replaced by the cast command ):
update voronoi
average set = (
SELECT sum (cast ( a.parapen as real)) / count ( a.parapen )
FROM voronoi as a
JOIN voronoi as b
ON st_intersects (( st_buffer (a.geom, 0.00001)), b.geom )
where b.id = voronoi.id);
When opening the table you will find that the field Average is now filled up.
How to display the layer with a defined theme in this field
If you have selected the option Load as a new layer , you should see the new layer calculated on your Mapping window
Lastly, you must open the properties window of the layer and set the symbology that you want to represent the Middle field
Obviously, for each type of desired local statistic, the query sql will have to be modified accordingly.