FunctionCallInfoData cbdata;
Datum tmpnewval;
char * strFromText = NULL;
+ int k = 0;
POSTGIS_RT_DEBUG(2, "RASTER_mapAlgebraFct: STARTING...");
PG_RETURN_NULL();
}
/* function should have correct # of args */
- else if (cbinfo.fn_nargs != 2) {
- elog(ERROR, "RASTER_mapAlgebraFct: Function does not have two input parameters");
+ else if (cbinfo.fn_nargs < 2 || cbinfo.fn_nargs > 3) {
+ elog(ERROR, "RASTER_mapAlgebraFct: Function does not have two or three input parameters");
rt_raster_destroy(raster);
rt_raster_destroy(newrast);
PG_RETURN_NULL();
}
+ if (cbinfo.fn_nargs == 2)
+ k = 1;
+ else
+ k = 2;
+
if (func_volatile(oid) == 'v') {
elog(NOTICE, "Function provided is VOLATILE. Unless required and for best performance, function should be IMMUTABLE or STABLE");
}
#else
InitFunctionCallInfoData(cbdata, &cbinfo, 2, InvalidOid, NULL, NULL);
#endif
- memset(cbdata.argnull, FALSE, 2);
+ memset(cbdata.argnull, FALSE, cbinfo.fn_nargs);
/* check that the function isn't strict if the args are null. */
if (PG_ARGISNULL(4)) {
PG_RETURN_NULL();
}
- cbdata.arg[1] = (Datum)NULL;
- cbdata.argnull[1] = TRUE;
+ cbdata.arg[k] = (Datum)NULL;
+ cbdata.argnull[k] = TRUE;
}
else {
- cbdata.arg[1] = PG_GETARG_DATUM(4);
+ cbdata.arg[k] = PG_GETARG_DATUM(4);
}
/**
cbdata.arg[0] = Float8GetDatum(r);
}
+ /* Add pixel positions if callback has proper # of args */
+ if (cbinfo.fn_nargs == 3) {
+ Datum d[2];
+ ArrayType *a;
+
+ d[0] = Int32GetDatum(x+1);
+ d[1] = Int32GetDatum(y+1);
+
+ a = construct_array(d, 2, INT4OID, sizeof(int4), true, 'i');
+
+ cbdata.argnull[1] = FALSE;
+ cbdata.arg[1] = PointerGetDatum(a);
+ }
+
POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: (%dx%d), r = %f",
x, y, r);
END;
$$
LANGUAGE 'plpgsql' IMMUTABLE;
+
+ CREATE OR REPLACE FUNCTION raster_x_plus_arg(pixel FLOAT, pos INT[], VARIADIC args TEXT[])
+ RETURNS FLOAT AS
+ $$
+ DECLARE
+ x float := 0;
+ BEGIN
+ IF NOT args[1] IS NULL THEN
+ x := args[1]::float;
+ END IF;
+ RETURN pixel + pos[1] + x;
+ END;
+ $$
+ LANGUAGE 'plpgsql' IMMUTABLE;
+
+ CREATE OR REPLACE FUNCTION raster_y_plus_arg(pixel FLOAT, pos INT[], VARIADIC args TEXT[])
+ RETURNS FLOAT AS
+ $$
+ DECLARE
+ x float := 0;
+ BEGIN
+ IF NOT args[1] IS NULL THEN
+ x := args[1]::float;
+ END IF;
+ RETURN pixel + pos[2] + x;
+ END;
+ $$
+ LANGUAGE 'plpgsql' IMMUTABLE;
-- Test null return from a user function = NODATA cell value
SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraFct(rast, 1, NULL, 'raster_nullage(float, text[])'::regprocedure), 1, 1) FROM ST_TestRaster(0, 0, 100) AS rast;
+
+SELECT ST_Value(rast, 3, 8) + 13 + 3, ST_Value(ST_MapAlgebraFct(rast, 1, NULL, 'raster_x_plus_arg(float, int[], text[])'::regprocedure, '13'), 3, 8) FROM ST_TestRaster(0, 0, 100) AS rast;
+SELECT ST_Value(rast, 3, 8) + 13 + 8, ST_Value(ST_MapAlgebraFct(rast, 1, NULL, 'raster_y_plus_arg(float, int[], text[])'::regprocedure, '13'), 3, 8) FROM ST_TestRaster(0, 0, 100) AS rast;