CREATE OR REPLACE FUNCTION _ST_WEDGE(fillvalue double precision, internalRadius integer, externalRadius integer, thetaStart double precision, thetaEnd double precision) 
RETURNS double precision [][] AS 
$BODY$
DECLARE
	xS integer;
	yS integer;
	theta double precision;
	xC integer;
	yC integer;
	x integer;
	y integer;
	
	thetaStep double precision;
	output double precision[][];
BEGIN
	xS := 2*externalRadius+1;
	yS := xS;
	xC := externalRadius+1;
	yC := externalRadius+1;
	thetaStep := (atan2(xC,yC)-atan2(xC-1,yC));
	output := ARRAY[array_fill(0.0::FLOAT8,ARRAY[yS])::FLOAT8[]];
	for x in 2..xS LOOP
		output := array_cat(output,array_fill(0.0::FLOAT8,ARRAY[yS])::FLOAT8[]);
	END LOOP;

	theta :=0;
	
	While theta <= 2*pi() loop
		for r in internalRadius..externalRadius loop
			if theta >= thetaStart and theta <= thetaEnd then 
				x := round(xC::double precision + r::double precision * sin(theta) )::integer;
				y := round(yC::double precision + r::double precision * cos(theta))::integer;
				output[x][y] = fillvalue;
			end if;
		end loop;
	theta := theta + thetaStep;
	end loop;
	RETURN output;
END;
$BODY$ 
LANGUAGE plpgsql IMMUTABLE;