Schema
Owner
postgres
Descriptions
There is no description for function st_mindist4ma
Options
Option | Value |
---|---|
Returns |
double precision |
Language |
|
Parameters |
value double precision [] pos integer [] variadic userargs text [] = NULL::text[] |
Definition
CREATE OR REPLACE FUNCTION public.st_mindist4ma (
value double precision [],
pos integer [],
variadic userargs text [] = NULL::text[]
)
RETURNS double precision AS
$span$
DECLARE
_value double precision[][][];
ndims int;
d double precision DEFAULT NULL;
_d double precision;
z integer;
x integer;
y integer;
cx integer;
cy integer;
cv double precision;
w integer;
h integer;
max_dx double precision;
max_dy double precision;
BEGIN
ndims := array_ndims(value);
-- add a third dimension if 2-dimension
IF ndims = 2 THEN
_value := _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);
-- width and height (0-based)
h := array_upper(_value, 2) - array_lower(_value, 2);
w := array_upper(_value, 3) - array_lower(_value, 3);
-- max distance from center pixel
max_dx := w / 2;
max_dy := h / 2;
-- correct width and height (1-based)
w := w + 1;
h := h + 1;
-- width and height should be odd numbers
IF w % 2. != 1 THEN
RAISE EXCEPTION 'Width of neighborhood array does not permit for a center pixel';
END IF;
IF h % 2. != 1 THEN
RAISE EXCEPTION 'Height of neighborhood array does not permit for a center pixel';
END IF;
-- center pixel's coordinates
cy := max_dy + array_lower(_value, 2);
cx := max_dx + array_lower(_value, 3);
-- center pixel value
cv := _value[z][cy][cx];
-- check to see if center pixel has value
IF cv IS NOT NULL THEN
RETURN 0.;
END IF;
FOR y IN array_lower(_value, 2)..array_upper(_value, 2) LOOP
FOR x IN array_lower(_value, 3)..array_upper(_value, 3) LOOP
-- skip NODATA values and center pixel
IF _value[z][y][x] IS NULL OR (x = cx AND y = cy) THEN
CONTINUE;
END IF;
-- use pythagorean theorem
_d := sqrt(power(cx - x, 2) + power(cy - y, 2));
-- RAISE NOTICE 'distance = %', _d;
IF d IS NULL OR _d < d THEN
d := _d;
END IF;
END LOOP;
END LOOP;
-- RAISE NOTICE 'd = %', d;
RETURN d;
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 13/03/2014 13:23 |