| 1 | = Map Algebra = |
| 2 | |
| 3 | '''NOTE: This document describes features that are still in development, and may not be available in the source tree.''' |
| 4 | |
| 5 | |
| 6 | == ST_MapAlgebra Variants == |
| 7 | |
| 8 | The specification for PostGIS Raster includes a definition for the function ST_MapAlgebra. This function has many variants, but the main differences between the variants are that one set of variants take an expression, and the other set of variants take a user function. |
| 9 | |
| 10 | '''Expression-accepting''' variants are very user-friendly, in that any SQL expression can be evaluated on a cell-by-cell or neighbor-by-neighbor basis. This ease of use comes at a cost of performance, as the expanded and evaluated expression is slower than creating a user function. '''User function''' variants require a bit more involvement, since users must create a pl/pgsql function first. This user function is evaluated on a cell-by-cell or neighbor-by-neighbor basis. |
| 11 | |
| 12 | The expression-accepting variants are: |
| 13 | {{{ |
| 14 | #!sql |
| 15 | ST_MapAlgebra(rast raster, band integer, pixeltype text, expression text, nodatavalueexpr text); |
| 16 | ST_MapAlgebra(rast raster, band integer, expression text); |
| 17 | ST_MapAlgebra(rast raster, pixeltype text, expression text); |
| 18 | ST_MapAlgebra(rast raster, expression text); |
| 19 | ST_MapAlgebra(rast raster, band integer, expression text, nodatavalueexpr text); |
| 20 | ST_MapAlgebra(rast raster, pixeltype text, expression text, nodatavalueexpr text); |
| 21 | }}} |
| 22 | |
| 23 | The user function variants are: |
| 24 | {{{ |
| 25 | #!sql |
| 26 | ST_MapAlgebra(rast raster, band integer, pixeltype text, userfunction regprocedure, variadic args text[]); |
| 27 | ST_MapAlgebra(rast raster, band integer, userfunction regprocedure); |
| 28 | ST_MapAlgebra(rast raster, pixeltype text, userfunction regprocedure); |
| 29 | ST_MapAlgebra(rast raster, userfunction regprocedure); |
| 30 | ST_MapAlgebra(rast raster, band integer, userfunction regprocedure, variadic args text[]); |
| 31 | ST_MapAlgebra(rast raster, pixeltype text, userfunction regprocedure, variadic args text[]); |
| 32 | }}} |
| 33 | |
| 34 | |
| 35 | === Performance === |
| 36 | |
| 37 | The performance differences between the two variants are described below: |
| 38 | |
| 39 | || || Expression (full debug) || User Function (full debug) || Hardcoded (full debug) || Expression || User Function || Hardcoded || |
| 40 | ||SQL|| 27.314 s || 26.511 s || 22.932 s || 90.0 ms || 61.0 ms || 35.0 ms || |
| 41 | || C || 1.557 s || 1.786 s || 1.338 s || 50.0 ms || 45.0 ms || 34.0 ms || |
| 42 | |
| 43 | The first test was performed with full debugging and logging turned on. This showed little difference between the user function and expression variants. The second test was performed in psql with debugging turned off, and times taken from the psql query timer. The datasets for these tests were a set of 25 rasters, each 10x10, 1 band, 16BUI. The tests showed an improvement in performance when using a user function, but the performance enhancements were reduced when testing the C versions of the functions versus the SQL prototype versions. |
| 44 | |
| 45 | Another test on a set of tiles with 1,000,000 total cells shows a 20x performance improvement (95 s for the expression vs. 4 s for the user function). This type of performance improvement was to be expected, moving away from the expression evaluation technique. |
| 46 | |
| 47 | |
| 48 | === Future Research === |
| 49 | |
| 50 | One thing the expression evaluation technique may excel at is caching query results, and improving value calculation over rasters with mostly uniform data. It is not known if an expression result is cached by the SPI_ interface at the C level, in which case, a raster with uniform values would approach 100% cache hits with this configuration. More investigation is required to determine if this is a viable path to pursue. |
| 51 | |
| 52 | |
| 53 | == User Functions == |
| 54 | |
| 55 | The functions that can be used as an user function must accept a float as the first parameter, and a variadic text array as the second parameter. This function can return any data type that can fit inside of the resulting raster. The following examples are user functions for the single-raster version of ST_MapAlgebra: |
| 56 | |
| 57 | {{{ |
| 58 | #!sql |
| 59 | CREATE OR REPLACE FUNCTION addconstant(cell FLOAT, VARIADIC args TEXT[]) |
| 60 | RETURNS FLOAT |
| 61 | AS $$ |
| 62 | BEGIN |
| 63 | return cell + 1; |
| 64 | END; |
| 65 | $$ LANGUAGE 'plpgsql'; |
| 66 | }}} |
| 67 | |
| 68 | This user function is pretty brain-dead. A more interesting user function would be: |
| 69 | |
| 70 | {{{ |
| 71 | #!sql |
| 72 | CREATE OR REPLACE FUNCTION polynomial(x FLOAT, VARIADIC args TEXT[]) |
| 73 | RETURNS FLOAT |
| 74 | AS $$ |
| 75 | DECLARE |
| 76 | m FLOAT; |
| 77 | b FLOAT; |
| 78 | BEGIN |
| 79 | m := args[1]::FLOAT; |
| 80 | b := args[2]::FLOAT; |
| 81 | return m * x + b; |
| 82 | END; |
| 83 | $$ LANGUAGE 'plpgsql'; |
| 84 | }}} |