For people not familiar with the Spatial SQL, I post this quick sample showing its beauty and simplicity at the same time.
We will go using PostGIS, but this could be performed in a similiar way with any GIS Database compliant with OGC Simple Feature Access - SQL Option.
The geomunion function
The geomunion Open GIS function make it possible to combine two geometries and getting from these a single geometry.
It is very easy to generate a geoprocessed layer from an original layer, making an union of its polygons based on a common attribute.
I will show you how to use the geomunion PostGIS function with a quick sample.
We will start from a MULTIPOLYGON layer, named polygon1, composed of 7 geometries with different values for the CODE field (1,2,3). In the following picture you can take a look at the polygon1 layer:
We will use the geomunion OpenGIS function to generate another MULTIPOLYGON PostGIS layer, named polygon1_union, composed of the polygons of polygon1 layer merged for the common value of CODE field.
We will get the polygon1_union layer, as in the picture:
Creation of the original PostGIS layer (polygon1)
To create polygon1 layer, just execute in your PostGIS environment the following SLQ code:
Creation of the geoprocessed PostGIS layer (polygon1_union)
Now we need to create the PostgreSQL table that will contain the geoprocessed layer, polygon1_union.
First we create the PostgreSQL table with the following sql code (note that we use the same fields from the original layer, polygon1)
Now we add the PostGIS spatial column to this layer using the OpenGIS AddGeometryColumn function:
Finally we insert in the polygon1_union PostGIS layer the polygons from the polygon1 PostGIS layer merged on common values from the CODE field. Note how we have to use the multi PostGIS function in order to avoid the downcasting of multipolygon geometries to polygon geometries.
It is easy to realize that from an original PostGIS layer composed of 7 multipolygons (polygon1) we derived a new PostGIS layer (polygon1_union) composed of 3 multipolygons.
The original PostGIS layer, polygon1, is composed of 7 multipolygons, as you can realize querying your database:
In these 7 multipolygons there are 3 ones with a code value of 2, 3 ones with a code value of 3, and only one with a code value of 1.
After geoprocessing polygon1 in polygon1_union PostGIS layer with the geomunion function, there are only 3 multipolygons, one for each value of the code field.
The interesting thing about PostGIS (like for Oracle Spatial and MySLQ Spatial) is that you can query, edit and geoprocess geometries just with plain SQL, without the need of commercial software and API.