Changes between Initial Version and Version 1 of WKTRaster/MapAlgebra


Ignore:
Timestamp:
Mar 18, 2011, 10:50:56 AM (13 years ago)
Author:
dzwarg
Comment:

Created

Legend:

Unmodified
Added
Removed
Modified
  • WKTRaster/MapAlgebra

    v1 v1  
     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
     8The 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
     12The expression-accepting variants are:
     13{{{
     14#!sql
     15ST_MapAlgebra(rast raster, band integer, pixeltype text, expression text, nodatavalueexpr text);
     16ST_MapAlgebra(rast raster, band integer, expression text);
     17ST_MapAlgebra(rast raster, pixeltype text, expression text);
     18ST_MapAlgebra(rast raster, expression text);
     19ST_MapAlgebra(rast raster, band integer, expression text, nodatavalueexpr text);
     20ST_MapAlgebra(rast raster, pixeltype text, expression text, nodatavalueexpr text);
     21}}}
     22
     23The user function variants are:
     24{{{
     25#!sql
     26ST_MapAlgebra(rast raster, band integer, pixeltype text, userfunction regprocedure, variadic args text[]);
     27ST_MapAlgebra(rast raster, band integer, userfunction regprocedure);
     28ST_MapAlgebra(rast raster, pixeltype text, userfunction regprocedure);
     29ST_MapAlgebra(rast raster, userfunction regprocedure);
     30ST_MapAlgebra(rast raster, band integer, userfunction regprocedure, variadic args text[]);
     31ST_MapAlgebra(rast raster, pixeltype text, userfunction regprocedure, variadic args text[]);
     32}}}
     33
     34
     35=== Performance ===
     36
     37The 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
     43The 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
     45Another 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
     50One 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
     55The 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
     59CREATE OR REPLACE FUNCTION addconstant(cell FLOAT, VARIADIC args TEXT[])
     60RETURNS FLOAT
     61AS $$
     62BEGIN
     63    return cell + 1;
     64END;
     65$$ LANGUAGE 'plpgsql';
     66}}}
     67
     68This user function is pretty brain-dead. A more interesting user function would be:
     69
     70{{{
     71#!sql
     72CREATE OR REPLACE FUNCTION polynomial(x FLOAT, VARIADIC args TEXT[])
     73RETURNS FLOAT
     74AS $$
     75DECLARE
     76    m FLOAT;
     77    b FLOAT;
     78BEGIN
     79    m := args[1]::FLOAT;
     80    b := args[2]::FLOAT;
     81    return m * x + b;
     82END;
     83$$ LANGUAGE 'plpgsql';
     84}}}