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

Function: st_resize

 

 

Schema

public

 

Owner

albenard

 

Descriptions

args: rast, width, height, algorithm=NearestNeighbor, maxerr=0.125 - Resize a raster to a new width/height

 

Options

Option

Value

Returns

public.raster

Language

plpgsql

Parameters

rast public.raster

width text

height text

algorithm text = 'NearestNeighbour'::text

maxerr double precision = 0.125

 

Definition

CREATE OR REPLACE FUNCTION public.st_resize (
 rast public.raster,
 width text,
 height text,
 algorithm text = 'NearestNeighbour'::text,
 maxerr double precision = 0.125
)
RETURNS public.raster AS
$span$
DECLARE

i integer;

wh text[2];

whi integer[2];
whd double precision[2];

_width integer;
_height integer;
BEGIN
wh[1] := trim(both from $2);
wh[2] := trim(both from $3);

-- see if width and height are percentages
FOR i IN 1..2 LOOP
IF position('%' in wh[i]) > 0 THEN
BEGIN

wh[i] := (regexp_matches(wh[i], E'^(\\d*.?\\d*)%{1}$'))[1];
IF length(wh[i]) < 1 THEN
RAISE
invalid_parameter_value;
END IF;

whd[i] := wh[i]::double precision * 0.01;
EXCEPTION WHEN OTHERS THEN -- TODO: WHEN invalid_parameter_value !
RAISE EXCEPTION 'Invalid percentage value provided for width/height';
RETURN NULL;
END;
ELSE
BEGIN

whi[i] := abs(wh[i]::integer);
EXCEPTION WHEN OTHERS THEN -- TODO: only handle appropriate SQLSTATE
RAISE EXCEPTION 'Non-integer value provided for width/height';
RETURN NULL;
END;
END IF;
END LOOP;

IF whd[1] IS NOT NULL OR whd[2] IS NOT NULL THEN
SELECT
foo.width, foo.height INTO _width, _height FROM public.ST_Metadata($1) AS foo;

IF whd[1] IS NOT NULL THEN
whi[1] := round(_width::double precision * whd[1])::integer;
END IF;

IF whd[2] IS NOT NULL THEN
whi[2] := round(_height::double precision * whd[2])::integer;
END IF;

END IF;

-- should NEVER be here
IF whi[1] IS NULL OR whi[2] IS NULL THEN
RAISE EXCEPTION
'Unable to determine appropriate width or height';
RETURN NULL;
END IF;

FOR i IN 1..2 LOOP
IF whi[i] < 1 THEN
whi[i] = 1;
END IF;
END LOOP;

RETURN public._ST_gdalwarp(
$1,
$4, $5,
NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
whi[1], whi[2]
);
END;
$span$
LANGUAGE
'plpgsql'
STABLE
RETURNS NULL ON NULL INPUT
SECURITY INVOKER
COST
100;

COMMENT ON FUNCTION public.st_resize(rast public.raster, width text, height text, algorithm text, maxerr double precision)
IS 'args: rast, width, height, algorithm=NearestNeighbor, maxerr=0.125 - Resize a raster to a new width/height';

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