pggeodb.nancy.inra.fr/db_robot - db_robot on pggeodb.nancy.inra.fr
Previous topic Chapter index Next topic

Function: _st_slope4ma

 

 

Schema

public

 

Owner

albenard

 

Descriptions

There is no description for function _st_slope4ma

 

Options

Option

Value

Returns

double precision

Language

plpgsql

Parameters

value double precision []

pos integer []

variadic userargs text [] = NULL::text[]

 

Definition

CREATE OR REPLACE FUNCTION public._st_slope4ma (
 value double precision [],
 pos integer [],
 variadic userargs text [] = NULL::text[]
)
RETURNS double precision AS
$span$
DECLARE

x integer;
y integer;
z integer;

_pixwidth double precision;
_pixheight double precision;
_width double precision;
_height double precision;
_units text;
_scale double precision;

dz_dx double precision;
dz_dy double precision;

slope double precision;

_value double precision[][][];
ndims int;
BEGIN

ndims := array_ndims(value);
-- add a third dimension if 2-dimension
IF ndims = 2 THEN
_value := public._ST_convertarray4ma(value);
ELSEIF ndims != 3 THEN
RAISE EXCEPTION
'First parameter of function must be a 3-dimension array';
ELSE
_value := value;
END IF;

-- only use the first raster passed to this function
IF array_length(_value, 1) > 1 THEN
RAISE NOTICE
'Only using the values from the first raster';
END IF;
z := array_lower(_value, 1);

IF (
array_lower(_value, 2) != 1 OR array_upper(_value, 2) != 3 OR
array_lower(_value, 3) != 1 OR array_upper(_value, 3) != 3
) THEN
RAISE EXCEPTION
'First parameter of function must be a 1x3x3 array with each of the lower bounds starting from 1';
END IF;

IF array_length(userargs, 1) < 6 THEN
RAISE EXCEPTION
'At least six elements must be provided for the third parameter';
END IF;

_pixwidth := userargs[1]::double precision;
_pixheight := userargs[2]::double precision;
_width := userargs[3]::double precision;
_height := userargs[4]::double precision;
_units := userargs[5];
_scale := userargs[6]::double precision;


-- check that center pixel isn't NODATA
IF _value[z][2][2] IS NULL THEN
RETURN NULL
;
-- substitute center pixel for any neighbor pixels that are NODATA
ELSE
FOR
y IN 1..3 LOOP
FOR x IN 1..3 LOOP
IF _value[z][y][x] IS NULL THEN
_value[z][y][x] = _value[z][2][2];
END IF;
END LOOP;
END LOOP;
END IF;

dz_dy := ((_value[z][3][1] + _value[z][3][2] + _value[z][3][2] + _value[z][3][3]) -
(_value[z][1][1] + _value[z][1][2] + _value[z][1][2] + _value[z][1][3])) / _pixheight;
dz_dx := ((_value[z][1][3] + _value[z][2][3] + _value[z][2][3] + _value[z][3][3]) -
(_value[z][1][1] + _value[z][2][1] + _value[z][2][1] + _value[z][3][1])) / _pixwidth;

slope := sqrt(dz_dx * dz_dx + dz_dy * dz_dy) / (8 * _scale);

-- output depends on user preference
CASE substring(upper(trim(leading from _units)) for 3)
-- percentages
WHEN 'PER' THEN
slope := 100.0 * slope;
-- radians
WHEN 'rad' THEN
slope := atan(slope);
-- degrees (default)
ELSE
slope := degrees(atan(slope));
END CASE;

RETURN slope;
END;
$span$
LANGUAGE
'plpgsql'
IMMUTABLE
CALLED ON NULL INPUT
SECURITY INVOKER
COST
100;

This file was generated with SQL Manager for PostgreSQL (www.pgsqlmanager.com) at 26/02/2014 11:51
Previous topic Chapter index Next topic