﻿{"id":6062,"date":"2018-08-22T00:01:59","date_gmt":"2018-08-21T22:01:59","guid":{"rendered":"http:\/\/www.sigterritoires.fr\/?p=6062"},"modified":"2018-10-17T12:06:13","modified_gmt":"2018-10-17T10:06:13","slug":"how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis","status":"publish","type":"post","link":"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/","title":{"rendered":"How to calculate local averages in the Vorono\u00ef polygons with Qgis"},"content":{"rendered":"\n<p>The Voronoi maps are built based on a series of polygons created around the location of each sampling point.<\/p>\n\n\n\n<p>The Vorono\u00ef 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 .<\/p>\n\n\n\n<p><a href=\"https:\/\/www.sigterritoires.fr\/index.php\/analyse-exploratoire-des-donnees-pour-la-geostatistiqueles-diagrammes-de-voronoi\/\">In the article regarding the Exploratory data analysis<\/a>\u00a0, we discussed how to use the ArcGIS Geostatistical Analyst to build and analyse the Voronoi polygons.<\/p>\n\n\n\n<p><p>In this article we will discuss how to get the same results with QGis.<!--more--><\/p><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>For example, if we choose the option \u00ab\u00a0average\u201d, the colour assigned to each polygon will be a function of the average of a column (\u201cAttribute\u00a0\u00bb)<\/p>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"561\" height=\"759\" data-attachment-id=\"6064\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/poly1\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly1.png?fit=561%2C759&amp;ssl=1\" data-orig-size=\"561,759\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"poly1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly1.png?fit=561%2C759&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly1.png?resize=561%2C759&#038;ssl=1\" alt=\"\" class=\"wp-image-6064\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly1.png?w=561&amp;ssl=1 561w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly1.png?resize=222%2C300&amp;ssl=1 222w\" sizes=\"auto, (max-width: 561px) 100vw, 561px\" \/><\/figure>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<p>In QGis, the Vorono\u00ef 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.<\/p>\n\n\n\n<p>It is possible to do it using different methods. Here, we will follow one of the available procedures; we will use a SQL query.<\/p>\n\n\n\n<p>This procedure consists in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Calculate the Voronoi polygons with QGis processing tools<\/li>\n<li>Import the result in a database (Sqlite or Postgres)<\/li>\n<li>Add a field to include the desired local statistic<\/li>\n<li>Perform a SQL query to fill this field<\/li>\n<li>Display the layer with a defined theme in this field<\/li>\n<\/ul>\n\n\n\n<p><h2><strong>Calculate<\/strong> <strong>the<\/strong> <strong>Voronoi<\/strong> <strong>polygons<\/strong> <strong>with<\/strong> <strong>QGis processing<\/strong> <strong>tools<\/strong><\/h2><\/p>\n\n\n\n<p>NOTE: the approach is the same as this with either 2.18 or 3.2 QAGis<\/p>\n\n\n\n<p>You have two tools that allows you to calculate the Vorono\u00ef polygons in the QGis treatment:<\/p>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"307\" height=\"249\" data-attachment-id=\"6065\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/poly2\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly2.png?fit=307%2C249&amp;ssl=1\" data-orig-size=\"307,249\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"poly2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly2.png?fit=307%2C249&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly2.png?resize=307%2C249&#038;ssl=1\" alt=\"\" class=\"wp-image-6065\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly2.png?w=307&amp;ssl=1 307w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly2.png?resize=300%2C243&amp;ssl=1 300w\" sizes=\"auto, (max-width: 307px) 100vw, 307px\" \/><\/figure>\n\n\n\n<p>One of the tools belongs to the group \u201cVectorial Geometry\u201d, the other is an integral part of GRASS geoprocessing.<\/p>\n\n\n\n<p>You can use them indifferently, but I do prefer GRASS.<\/p>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"351\" data-attachment-id=\"6066\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/poly3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly3.png?fit=840%2C461&amp;ssl=1\" data-orig-size=\"840,461\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"poly3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly3.png?fit=640%2C351&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly3.png?resize=640%2C351&#038;ssl=1\" alt=\"\" class=\"wp-image-6066\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly3.png?w=840&amp;ssl=1 840w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly3.png?resize=300%2C165&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly3.png?resize=768%2C421&amp;ssl=1 768w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p>Save the result as a shape file. This will allow you to load more easily the file in your database.<\/p>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"402\" data-attachment-id=\"6067\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/poly4\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly4.png?fit=840%2C528&amp;ssl=1\" data-orig-size=\"840,528\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"poly4\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly4.png?fit=640%2C402&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly4.png?resize=640%2C402&#038;ssl=1\" alt=\"\" class=\"wp-image-6067\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly4.png?w=840&amp;ssl=1 840w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly4.png?resize=300%2C189&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly4.png?resize=768%2C483&amp;ssl=1 768w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p><h2><strong>How to import the<\/strong> <strong>result<\/strong> <strong>in<\/strong> <strong>a<\/strong> <strong>database<\/strong> <strong>(Sqlite<\/strong> <strong>or<\/strong> <strong>Postgres)<\/strong><\/h2><\/p>\n\n\n\n<p>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 \/ <a href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/starting-with-postgres-postgis\/\">Postgis<\/a> database.<\/p>\n\n\n\n<p>For importing , you have to follow the same process independently of the chosen database.<\/p>\n\n\n\n<p>Note : To create a Sqlite database , go to QGis <strong><em>Explorer<\/em><\/strong> panel, right click on <strong>Spatialite<\/strong> , then on <strong>Create Database<\/strong><\/p>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"363\" height=\"419\" data-attachment-id=\"6068\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/poly5\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly5.png?fit=363%2C419&amp;ssl=1\" data-orig-size=\"363,419\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"poly5\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly5.png?fit=363%2C419&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly5.png?resize=363%2C419&#038;ssl=1\" alt=\"\" class=\"wp-image-6068\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly5.png?w=363&amp;ssl=1 363w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly5.png?resize=260%2C300&amp;ssl=1 260w\" sizes=\"auto, (max-width: 363px) 100vw, 363px\" \/><\/figure>\n\n\n\n<p>To import your shape file with Vorono\u00ef polygons you will use the database Manager:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>go to <strong>Database<\/strong> menu <strong>-> DB Manager<\/strong> to open the window of the database manager.<\/li>\n<li>Login to the selected database<\/li>\n<li>Click on the <strong>Import<\/strong> <strong>a<\/strong> <strong>layer<\/strong> <strong>\/<\/strong> <strong>file<\/strong> tool<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"497\" data-attachment-id=\"6069\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/poly6\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly6.png?fit=840%2C652&amp;ssl=1\" data-orig-size=\"840,652\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"poly6\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly6.png?fit=640%2C497&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly6.png?resize=640%2C497&#038;ssl=1\" alt=\"\" class=\"wp-image-6069\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly6.png?w=840&amp;ssl=1 840w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly6.png?resize=300%2C233&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly6.png?resize=768%2C596&amp;ssl=1 768w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p>Tick your shape file and name to the table you want to create. Click OK.<\/p>\n\n\n\n<p>A message will appear indicating that the import has been performed.<\/p>\n\n\n\n<p><h2><strong>Add<\/strong> <strong>a<\/strong> <strong>field to<\/strong> <strong>include<\/strong> <strong>the<\/strong> <strong>desired<\/strong> <strong>local<\/strong> <strong>statistics<\/strong><\/h2><\/p>\n\n\n\n<p>While remaining in the database manager, select the window \u201c<strong>providers<\/strong>\u201d, the table you just created, and then go to <strong>Table-> Edit Table <\/strong>in the manager menu<strong>.<\/strong><\/p>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"492\" data-attachment-id=\"6070\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/poly7\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly7.png?fit=693%2C533&amp;ssl=1\" data-orig-size=\"693,533\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"poly7\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly7.png?fit=640%2C492&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly7.png?resize=640%2C492&#038;ssl=1\" alt=\"\" class=\"wp-image-6070\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly7.png?w=693&amp;ssl=1 693w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly7.png?resize=300%2C231&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p>Click on the <strong>Add<\/strong> <strong>a<\/strong> <strong>column<\/strong> button and fill in the characteristics of the field you want to calculate.<\/p>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"463\" data-attachment-id=\"6071\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/poly8\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly8.png?fit=739%2C535&amp;ssl=1\" data-orig-size=\"739,535\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"poly8\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly8.png?fit=640%2C463&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly8.png?resize=640%2C463&#038;ssl=1\" alt=\"\" class=\"wp-image-6071\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly8.png?w=739&amp;ssl=1 739w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly8.png?resize=300%2C217&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p>In our example we will calculate the average of each polygon and its immediate neighbours.<\/p>\n\n\n\n<p><h2><strong>How to perform<\/strong> <strong>a<\/strong> <strong>SQL<\/strong> <strong>query<\/strong> <strong>to<\/strong> <strong>fill up<\/strong> <strong>this<\/strong> <strong>field<\/strong><\/h2><\/p>\n\n\n\n<p>Before discussing the syntax of the query, let&rsquo;s see what we want to calculate ;<\/p>\n\n\n\n<p>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).<\/p>\n\n\n\n<p>For a Postgesql \/ postgis base, the query is as follows<\/p>\n\n\n\n<p><strong>update<\/strong> <strong><em>voronoi<\/em><\/strong> <strong>vor<\/strong> <br\/><strong>set<\/strong> <strong>mean<\/strong> <strong>= (SELECT sum (a.<\/strong> <strong><em>parapen)<\/em> :: real \/ count (a.<\/strong> <strong><em>parapen)<\/em> :: real<\/strong> <br\/><strong>FROM<\/strong> <strong>voronoi<\/strong> <strong>as a<\/strong> <br\/><strong>JOIN<\/strong> <strong>voronoi<\/strong> <strong>as b<\/strong> <br\/><strong>ON<\/strong> <strong><a href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/postgis-st-intersects-vs-st-within\/\">st_intersects<\/a><\/strong> <strong>((<\/strong> <strong>st_buffer<\/strong> <strong>(a.geom, 0.00001)),<\/strong> <strong>b.geom<\/strong> <strong>)<\/strong> <br\/><strong>where b.id = vor.id)<\/strong><\/p>\n\n\n\n<p>where <strong><em>voronoi<\/em><\/strong> is the name of the table, <strong><em>parapen<\/em><\/strong> is the name of the attribute which we want to calculate the local average and <strong><em>geom<\/em><\/strong> is the name of the geometry field of the table.<\/p>\n\n\n\n<p>Notice the operators <strong><em>::<\/em><\/strong> <strong><em>real<\/em><\/strong>, 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.<\/p>\n\n\n\n<p>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 ):<\/p>\n\n\n\n<p><strong>update<\/strong> <strong>voronoi<\/strong> <br\/><strong>average<\/strong> <strong>set<\/strong> <strong>= (<\/strong> <br\/><strong>SELECT sum (cast (<\/strong> <strong>a.parapen<\/strong> <strong>as real)) \/ count (<\/strong> <strong>a.parapen<\/strong> <strong>)<\/strong> <br\/><strong>FROM<\/strong> <strong>voronoi<\/strong> <strong>as a<\/strong> <br\/><strong>JOIN<\/strong> <strong>voronoi<\/strong> <strong>as b<\/strong> <br\/><strong>ON<\/strong> <strong>st_intersects<\/strong> <strong>((<\/strong> <strong>st_buffer<\/strong> <strong>(a.geom, 0.00001)),<\/strong> <strong>b.geom<\/strong> <strong>)<\/strong> <br\/><strong>where b.id = voronoi.id);<\/strong><\/p>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"483\" data-attachment-id=\"6072\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/poly9\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly9.png?fit=840%2C634&amp;ssl=1\" data-orig-size=\"840,634\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"poly9\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly9.png?fit=640%2C483&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly9.png?resize=640%2C483&#038;ssl=1\" alt=\"\" class=\"wp-image-6072\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly9.png?w=840&amp;ssl=1 840w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly9.png?resize=300%2C226&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly9.png?resize=768%2C580&amp;ssl=1 768w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p>When opening the table you will find that the field <strong><em>Average<\/em><\/strong> is now filled up.<\/p>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"483\" data-attachment-id=\"6073\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/poly10\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly10.png?fit=840%2C634&amp;ssl=1\" data-orig-size=\"840,634\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"poly10\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly10.png?fit=640%2C483&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly10.png?resize=640%2C483&#038;ssl=1\" alt=\"\" class=\"wp-image-6073\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly10.png?w=840&amp;ssl=1 840w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly10.png?resize=300%2C226&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly10.png?resize=768%2C580&amp;ssl=1 768w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p><strong>How to display<\/strong> <strong>the<\/strong> <strong>layer<\/strong> <strong>with<\/strong> <strong>a<\/strong> <strong>defined<\/strong> <strong>theme<\/strong> <strong>in<\/strong> <strong>this<\/strong> <strong>field<\/strong><\/p>\n\n\n\n<p>If you have selected the option Load as a new layer , you should see the new layer calculated on your Mapping window\u00a0<\/p>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"368\" data-attachment-id=\"6074\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/poly11\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly11.png?fit=840%2C483&amp;ssl=1\" data-orig-size=\"840,483\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"poly11\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly11.png?fit=640%2C368&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly11.png?resize=640%2C368&#038;ssl=1\" alt=\"\" class=\"wp-image-6074\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly11.png?w=840&amp;ssl=1 840w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly11.png?resize=300%2C173&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly11.png?resize=768%2C442&amp;ssl=1 768w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p>Lastly, you must open the properties window of the layer and set the symbology that you want to represent the <strong>Middle<\/strong> field \u00a0<\/p>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"393\" data-attachment-id=\"6075\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/how-to-calculate-local-averages-in-the-voronoi-polygons-with-qgis\/poly12\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly12.png?fit=840%2C516&amp;ssl=1\" data-orig-size=\"840,516\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"poly12\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly12.png?fit=640%2C393&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly12.png?resize=640%2C393&#038;ssl=1\" alt=\"\" class=\"wp-image-6075\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly12.png?w=840&amp;ssl=1 840w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly12.png?resize=300%2C184&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/poly12.png?resize=768%2C472&amp;ssl=1 768w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p>Obviously, for each type of desired local statistic, the query sql will have to be modified accordingly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Voronoi maps are built based on a series of polygons created around the location of each sampling point. The Vorono\u00ef polygons are created in such a way that each location in a polygon is closer&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"give_campaign_id":0,"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sfsi_plus_gutenberg_text_before_share":"","sfsi_plus_gutenberg_show_text_before_share":"","sfsi_plus_gutenberg_icon_type":"","sfsi_plus_gutenberg_icon_alignemt":"","sfsi_plus_gutenburg_max_per_row":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1260],"tags":[],"class_list":["post-6062","post","type-post","status-publish","format-standard","hentry","category-non-classe-en"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p6XU0A-1zM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts\/6062","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/comments?post=6062"}],"version-history":[{"count":0,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts\/6062\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/media?parent=6062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/categories?post=6062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/tags?post=6062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}