Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Artifact ID: | ab5eedca812bd461b6aebe3d0fd1ca4149c767dd |
---|---|
Page Name: | Affine Transform |
Date: | 2015-05-02 01:11:07 |
Original User: | sandro |
Next | d58fb3a802e01efc38cef7d978be63a4b3361a63 |
Content
Affine Transformations
Starting since version 4.3.0 SpatiaLite supports several new SQL functions based on affine transformation; understanding the underlaying mathematics could easily be rather difficult if you have absolutely no familiarity with this kind of operazions.So we'll start smoothly by introducing first a very simple practical example based on kind of a joke about the geography of Italy.
Fancy Geograpy and amusing Math
PrefaceLooking to a map of Italy it appears obvious at first glance that Sicily is located in a very inconvenient position.It's dangerously close to north african shores and the Strait of Messina is exaggeratedly narrow, thus causing many troubles and severe security risks to the navigation of capital ships. Even worst, from time to time some crazy politician strongly advocates the very stupid idea to build an incredibly costly bridge crossing the Strait overlooking the very high seismical risk of this district and purposely ignoring that in 1908 both towns of Messina and Reggio Calabria were completely destroyed by an earthquake followed by a tsunami killing more than 100,000 peoples, one of the most frightening natural disasters registered in Europe during modern times. So we'll immediately start a case study intended to identify a possible alternative location for Sicily. Incidentally we'll use the new SQL functions based on Affine Transformations for this task. Let's go on. |
![]() |
Step #1: translating Sicily into a more convenient positionThere is plenty of unused free room in the lower Tyrrhenian Sea, so we start by applying a translation: this practically means adding (or subctracting) a constant value to the coordinates on both x and y axes.After a very quick examination going 150 km westewards and 150 km northwards seems to be an absolutely reasonable choice. So using the Affine Transformation SQL functions we'll write the following SQL statement: CREATE TABLE sicilia_1 AS SELECT cod_reg, ATM_Transform(geometry, ATM_CreateTranslate(-150000, 150000)) AS geom FROM sicilia_0; SELECT RecoverGeometryColumn('sicilia_1', 'geom', 32632, 'MULTIPOLYGON', 'XY'); |
![]() |
Step #2: rotating Sicily so to get a nice horizontal alignementAligning the southern shores of Sicily to an almost horizontal line will surely lead to a more nicely ordered layout: so we have now to apply a counterclockwise rotation of about 25.0 degrees.Thanks to Affine Transformations, we can combine both the previous translation and the current rotation into a single movemente (a so called rototranslation). We simply have to execute the following SQL statement: CREATE TABLE sicilia_2 AS SELECT cod_reg, ATM_Transform(geometry, ATM_Translate(-150000, 150000, ATM_Translate(cx, cy, ATM_Rotate(25, ATM_CreateTranslate(-cx, -cy))))) AS geom FROM (SELECT cod_reg, ST_X(centroid) AS cx, ST_Y(centroid) AS cy, geometry FROM (SELECT cod_reg, ST_Centroid(geometry) AS centroid, geometry FROM sicilia_0) AS g1 ) AS g2; SELECT RecoverGeometryColumn('sicilia_2', 'geom', 32632, 'MULTIPOLYGON', 'XY'); |
![]() |
Step #3: inflating and reshaping SicilyAn increased surface is surely welcome, because will automatically imply more cultivable lands, thus certanly impelling a strong productivity boost to the sicilian agricolture: on the other hand shortening a little bit the length of the southern coastline will surely facilitate mobility and communications.So we'll now apply a scaling transformation using two different values: sx=0.9 and sy=1.3. Once again, Affine Transformations enable us to combine altogether both translate, rotate and scale into a single transformation. This is the corresponding SQL statement: CREATE TABLE sicilia_2 AS CREATE TABLE sicilia_3 AS SELECT cod_reg, ATM_Transform(geometry, ATM_Translate(-150000, 150000, ATM_Translate(cx, cy, ATM_Scale(0.9, 1.3, ATM_Rotate(25, ATM_CreateTranslate(-cx, -cy)))))) AS geom FROM (SELECT cod_reg, ST_X(centroid) AS cx, ST_Y(centroid) AS cy, geometry FROM (SELECT cod_reg, ST_Centroid(geometry) AS centroid, geometry FROM sicilia_0) AS g1 ) AS g2; SELECT RecoverGeometryColumn('sicilia_3', 'geom', 32632, 'MULTIPOLYGON', 'XY'); |
![]() |
Step #4: final touch: reflecting SicilyAn increased surface is surely welcome, because will automatically imply more cultivable lands, thus certanly impelling a strong productivity boost to the sicilian agricolture: on the other hand shortening a little bit the length of the southern coastline will surely facilitate mobility and communications.So we'll now apply a scaling transformation using two different values: sx=0.9 and sy=1.3. Once again, Affine Transformations enable us to combine altogether both translate, rotate and scale into a single transformation. This is the corresponding SQL statement: CREATE TABLE sicilia_2 AS CREATE TABLE sicilia_4 AS SELECT cod_reg, ATM_Transform(geometry, ATM_Translate(-150000, 150000, ATM_Translate(cx, cy, ATM_YRoll(180, ATM_Scale(0.9, 1.3, ATM_Rotate(25, ATM_CreateTranslate(-cx, -cy))))))) AS geom FROM (SELECT cod_reg, ST_X(centroid) AS cx, ST_Y(centroid) AS cy, geometry FROM (SELECT cod_reg, ST_Centroid(geometry) AS centroid, geometry FROM sicilia_0) AS g1 ) AS g2; SELECT RecoverGeometryColumn('sicilia_4', 'geom', 32632, 'MULTIPOLYGON', 'XY'); |
![]() |
Conclusion and final considerations |
![]() |