﻿{"id":6196,"date":"2018-08-31T07:33:02","date_gmt":"2018-08-31T05:33:02","guid":{"rendered":"http:\/\/www.sigterritoires.fr\/?p=6196"},"modified":"2018-10-17T12:05:09","modified_gmt":"2018-10-17T10:05:09","slug":"spatial-analysis-with-sql-5-aggregation-function-arcgis-union","status":"publish","type":"post","link":"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/","title":{"rendered":"Spatial analysis with SQL: 5- aggregation function (ArcGis Union)"},"content":{"rendered":"\r\n<p>In Article previous we have discussed some <a href=\"https:\/\/translate.google.com\/translate?hl=en&amp;prev=_t&amp;sl=auto&amp;tl=en&amp;u=https:\/\/www.sigterritoires.fr\/index.php\/lanalyse-spatiale-avec-sql-4-fonctions-de-traitement-spatial\">SQL functions for spatial processing<\/a> . In this article we will see a function a little more complicated to put at work: the aggregation function for two layers of polygons. This operation is the one called <strong><em>Union<\/em><\/strong> in ArcGis. The purpose of the operation is to obtain a layer with all the intersections of the entities of the two original layers, and, also, all non-intersecting entities.<\/p>\r\n\r\n\r\n\r\n<p>Let\u2019s use the following example. We have a first layer \u201crectangles\u201d<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"487\" data-attachment-id=\"6197\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/attachment\/51\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/51.png?fit=684%2C521&amp;ssl=1\" data-orig-size=\"684,521\" 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=\"51\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/51.png?fit=640%2C487&amp;ssl=1\" class=\"wp-image-6197\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/51.png?resize=640%2C487&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/51.png?w=684&amp;ssl=1 684w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/51.png?resize=300%2C229&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>A second layer \u201ccircles\u201d<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"532\" height=\"495\" data-attachment-id=\"6198\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/attachment\/52\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/52.png?fit=532%2C495&amp;ssl=1\" data-orig-size=\"532,495\" 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=\"52\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/52.png?fit=532%2C495&amp;ssl=1\" class=\"wp-image-6198\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/52.png?resize=532%2C495&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/52.png?w=532&amp;ssl=1 532w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/52.png?resize=300%2C279&amp;ssl=1 300w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>That overlaps spatially:<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"530\" data-attachment-id=\"6199\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/attachment\/53\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/53.png?fit=709%2C587&amp;ssl=1\" data-orig-size=\"709,587\" 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=\"53\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/53.png?fit=640%2C530&amp;ssl=1\" class=\"wp-image-6199\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/53.png?resize=640%2C530&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/53.png?w=709&amp;ssl=1 709w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/53.png?resize=300%2C248&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>What we want to obtain is a layer containing all the polygons of both layers but creating polygons to overlay areas:<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"533\" data-attachment-id=\"6200\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/attachment\/54\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/54.png?fit=685%2C570&amp;ssl=1\" data-orig-size=\"685,570\" 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=\"54\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/54.png?fit=640%2C533&amp;ssl=1\" class=\"wp-image-6200\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/54.png?resize=640%2C533&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/54.png?w=685&amp;ssl=1 685w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/54.png?resize=300%2C250&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>We will proceed by following the same method used in the previous article, i.e. by developing an SQL query including several sub queries.<\/p>\r\n\r\n\r\n\r\n<p>In the first place, we will develop a query that will build the polygons of the overlapping areas (the intersections) of both layers. These polygons appear in yellow in the next picture.<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"550\" data-attachment-id=\"6201\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/attachment\/55\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/55.png?fit=663%2C570&amp;ssl=1\" data-orig-size=\"663,570\" 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=\"55\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/55.png?fit=640%2C550&amp;ssl=1\" class=\"wp-image-6201\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/55.png?resize=640%2C550&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/55.png?w=663&amp;ssl=1 663w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/55.png?resize=300%2C258&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>We will resume the request used in the previous article:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"50\" height=\"50\" data-attachment-id=\"6208\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/tuerca-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/tuerca-3.png?fit=50%2C50&amp;ssl=1\" data-orig-size=\"50,50\" 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=\"tuerca-3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/tuerca-3.png?fit=50%2C50&amp;ssl=1\" class=\"wp-image-6208\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/tuerca-3.png?resize=50%2C50&#038;ssl=1\" alt=\"\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong><em>select<\/em><\/strong> <strong><em>circle.id as id1, rectangles.id as<\/em><\/strong> <strong><em>id2<\/em><\/strong>, <strong><em>st_intersection<\/em><\/strong> <strong><em>(<\/em><\/strong><strong>circle.geom<em>, rectangles.geom<\/em><\/strong> <strong><em>) as<\/em><\/strong> <strong><em>the_geom<\/em><\/strong> <br \/><strong><em>from<\/em><\/strong> <strong><em>circle, rectangles<\/em><\/strong> <br \/><strong><em>where<\/em><\/strong> <strong><em>st_intersects<\/em><\/strong> <strong><em>(<\/em><\/strong> <strong><em>circle.geom, rectangles.geom<\/em><\/strong> <strong><em>)<\/em><\/strong> <br \/><strong><em>group by<\/em><\/strong> <strong><em>circle.id, rectangles.id<\/em><\/strong><\/p>\r\n\r\n\r\n\r\n<p>You will notice that the select is built not just to retrieve intersected geometries, but also the identifiers of the original entities (<strong><em>circle.id<\/em><\/strong> <strong>and<\/strong> <strong><em>rectangles.id)<\/em><\/strong>. Of course, you can recover other attributes, or all by changing this part of the request.<\/p>\r\n\r\n\r\n\r\n<p>To these polygons, we will add the rest of the areas occupied by the rectangle layer (in yellow in the following picture)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"541\" data-attachment-id=\"6202\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/attachment\/57\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/57.png?fit=670%2C566&amp;ssl=1\" data-orig-size=\"670,566\" 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=\"57\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/57.png?fit=640%2C541&amp;ssl=1\" class=\"wp-image-6202\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/57.png?resize=640%2C541&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/57.png?w=670&amp;ssl=1 670w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/57.png?resize=300%2C253&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"50\" height=\"50\" data-attachment-id=\"6207\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/tuerca-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/tuerca-2.png?fit=50%2C50&amp;ssl=1\" data-orig-size=\"50,50\" 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=\"tuerca-2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/tuerca-2.png?fit=50%2C50&amp;ssl=1\" class=\"wp-image-6207\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/tuerca-2.png?resize=50%2C50&#038;ssl=1\" alt=\"\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong>select<\/strong> <strong><em>0 as id1, rectangles.id as<\/em><\/strong> <strong>id2<\/strong>, <strong>st_difference<\/strong> <strong>(<\/strong> <strong><em>rectangles.geom<\/em><\/strong>,<\/p>\r\n\r\n\r\n\r\n<p><strong>(select<\/strong> <strong>multi<\/strong> <strong>ST_<\/strong> <strong>(ST_Union<\/strong> <strong>(the_geom)) as<\/strong> <strong>the_geom<\/strong> <strong>from (<\/strong><br \/><strong>select<\/strong> <strong>st_intersection<\/strong> <strong>(<\/strong>circle.geom<strong><em>, rectangles.geom<\/em><\/strong> <strong>) as<\/strong> <strong>the_geom<\/strong> <br \/><strong>from<\/strong> <strong><em>circle, rectangles<\/em><\/strong> <br \/><strong>where<\/strong> <strong>st_intersects<\/strong> <strong>(<\/strong> <strong><em>circle.geom, rectangles.geom<\/em><\/strong> <strong>))<\/strong> <br \/><strong>as<\/strong> <strong>the_geom<\/strong>)<strong>)<\/strong> <strong>as<\/strong> <strong>the_geom<\/strong> <br \/><strong>from rectangles<\/strong><\/p>\r\n\r\n\r\n\r\n<p>What we get to do with this request is to extract all the intersection areas (as in the previous query) and convert them in a single multi-polygon. The request removes the difference of this multi- polygon with the layer of rectangles.<\/p>\r\n\r\n\r\n\r\n<p>Finally, these polygons, we are going to add the rest of the areas occupied by the layer circles to these polygons (yellow in the following image)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"526\" data-attachment-id=\"6203\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/attachment\/58\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/58.png?fit=708%2C582&amp;ssl=1\" data-orig-size=\"708,582\" 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=\"58\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/58.png?fit=640%2C526&amp;ssl=1\" class=\"wp-image-6203\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/58.png?resize=640%2C526&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/58.png?w=708&amp;ssl=1 708w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/58.png?resize=300%2C247&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>We will proceed likewise the rectangles, by using a multi- polygon of intersection areas and calculating the difference with the layer circles:<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"50\" height=\"50\" data-attachment-id=\"6206\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/tuerca-1\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/tuerca-1.png?fit=50%2C50&amp;ssl=1\" data-orig-size=\"50,50\" 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=\"tuerca-1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/tuerca-1.png?fit=50%2C50&amp;ssl=1\" class=\"wp-image-6206\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/tuerca-1.png?resize=50%2C50&#038;ssl=1\" alt=\"\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong>select<\/strong> <strong><em>circle.id as id1, 0 as<\/em><\/strong> <strong>id2<\/strong> <strong><em>,<\/em><\/strong> <strong>st_difference (<\/strong> <strong><em>circle.geom<\/em><\/strong> <strong>, (<\/strong> <br \/><strong>select<\/strong> <strong>st_multi<\/strong> <strong>(<\/strong> <strong>st_union<\/strong> <strong>(<\/strong> <strong>the_geom<\/strong> <strong>)) as<\/strong> <strong>the_geom<\/strong> <strong>from (<\/strong> <br \/><strong>select<\/strong> <strong>st_intersection<\/strong> <strong>(<\/strong> <strong><em>circle.geom, rectangles.geom<\/em><\/strong> <strong>) as<\/strong> <strong>the_geom<\/strong> <br \/><strong>from<\/strong> <strong><em>circle, rectangles<\/em><\/strong> <br \/><strong>where<\/strong> <strong>st_intersects<\/strong> <strong>(<\/strong> <strong><em>circle.geom, rectangles.geom<\/em><\/strong> <strong>))<\/strong> <br \/><strong>as<\/strong> <strong>the_geom<\/strong> <strong>)) as<\/strong> <strong>the_geom<\/strong> <br \/><strong>from<\/strong> <strong>circle<\/strong><\/p>\r\n\r\n\r\n\r\n<p>The union of these three requests produces the outcome layer we expected :<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"50\" height=\"50\" data-attachment-id=\"6205\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/tuerca\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/tuerca.png?fit=50%2C50&amp;ssl=1\" data-orig-size=\"50,50\" 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=\"tuerca\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/tuerca.png?fit=50%2C50&amp;ssl=1\" class=\"wp-image-6205\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/tuerca.png?resize=50%2C50&#038;ssl=1\" alt=\"\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong>select *<\/strong> <br \/><strong>from<\/strong> <br \/><strong>(select circle.id as id1, rectangles.id<\/strong> <strong>as<\/strong> <strong>id2, st_intersection (<\/strong> <strong>circle.geom, rectangles.geom<\/strong> <strong>) as<\/strong> <strong>the_geom<\/strong> <br \/><strong>from<\/strong> <strong>circle, rectangles<\/strong> <br \/><strong>where<\/strong> <strong>st_intersects<\/strong> <strong>(<\/strong> <strong>circle.geom, rectangles.geom<\/strong> <strong>)<\/strong> <br \/><strong>group by<\/strong> <strong>circle.id, rectangles.id<\/strong> <br \/><strong>union all<\/strong> <br \/><strong>select 0 as id1, rectangles.id as id2, st_difference (<\/strong> <strong>rectangles.geom<\/strong> <strong>, (<\/strong> <br \/><strong>select<\/strong> <strong>st_multi<\/strong> <strong>(<\/strong> <strong>st_union<\/strong> <strong>(<\/strong> <strong>the_geom<\/strong> <strong>)) as<\/strong> <strong>the_geom<\/strong> <strong>from (<\/strong> <br \/><strong>select<\/strong> <strong>st_intersection<\/strong> <strong>(<\/strong> <strong>circle.geom, rectangles.geom<\/strong> <strong>) as<\/strong> <strong>the_geom<\/strong> <br \/><strong>from<\/strong> <strong>circle, rectangles<\/strong> <br \/><strong>where<\/strong> <strong>st_intersects<\/strong> <strong>(<\/strong> <strong>circle.geom, rectangles.geom<\/strong> <strong>))<\/strong> <br \/><strong>as<\/strong> <strong>the_geom<\/strong> <strong>)) as<\/strong> <strong>the_geom<\/strong> <br \/><strong>from rectangles<\/strong> <br \/><strong>union all<\/strong> <br \/><strong>select circle.id as id1, 0 as id2, st_difference (<\/strong> <strong>circle.geom<\/strong> <strong>, (<\/strong> <br \/><strong>select<\/strong> <strong>st_multi<\/strong> <strong>(<\/strong> <strong>st_union<\/strong> <strong>(<\/strong> <strong>the_geom<\/strong> <strong>)) as<\/strong> <strong>the_geom<\/strong> <strong>from (<\/strong> <br \/><strong>select<\/strong> <strong>st_intersection<\/strong> <strong>(<\/strong> <strong>circle.geom, rectangles.geom<\/strong> <strong>) as<\/strong> <strong>the_geom<\/strong> <br \/><strong>from<\/strong> <strong>circle, rectangles<\/strong> <br \/><strong>where<\/strong> <strong>st_intersects<\/strong> <strong>(<\/strong> <strong>circle.geom, rectangles.geom<\/strong> <strong>))<\/strong> <br \/><strong>as<\/strong> <strong>the_geom<\/strong> <strong>)) as<\/strong> <strong>the_geom<\/strong> <br \/><strong>from<\/strong> <strong>circle<\/strong> <br \/><strong>) as T1<\/strong> \u00a0<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"534\" data-attachment-id=\"6204\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/spatial-analysis-with-sql-5-aggregation-function-arcgis-union\/attachment\/59\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/59.png?fit=684%2C571&amp;ssl=1\" data-orig-size=\"684,571\" 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=\"59\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/59.png?fit=640%2C534&amp;ssl=1\" class=\"wp-image-6204\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/59.png?resize=640%2C534&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/59.png?w=684&amp;ssl=1 684w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2018\/08\/59.png?resize=300%2C250&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\r\n","protected":false},"excerpt":{"rendered":"<p>In Article previous we have discussed some SQL functions for spatial processing . In this article we will see a function a little more complicated to put at work: the aggregation function for two layers of&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_post_was_ever_published":false,"_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":""},"categories":[1296,1338],"tags":[1443,1351,1386,1404,1353,1445],"class_list":["post-6196","post","type-post","status-publish","format-standard","hentry","category-posts-en","category-postgresql-en","tag-aggregation","tag-postgresql-en","tag-qgis-en","tag-spatial-analyzis","tag-sql-en","tag-union-en"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p6XU0A-1BW","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts\/6196","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=6196"}],"version-history":[{"count":0,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts\/6196\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/media?parent=6196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/categories?post=6196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/tags?post=6196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}