Datum RASTER_lib_version(PG_FUNCTION_ARGS);
Datum RASTER_lib_build_date(PG_FUNCTION_ARGS);
Datum RASTER_gdal_version(PG_FUNCTION_ARGS);
+Datum RASTER_minPossibleValue(PG_FUNCTION_ARGS);
/* Input/output and format conversions */
Datum RASTER_in(PG_FUNCTION_ARGS);
PG_RETURN_POINTER(result);
}
+PG_FUNCTION_INFO_V1(RASTER_minPossibleValue);
+Datum RASTER_minPossibleValue(PG_FUNCTION_ARGS)
+{
+ text *pixeltypetext = NULL;
+ char *pixeltypechar = NULL;
+ rt_pixtype pixtype = PT_END;
+ double pixsize = 0;
+
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+
+ pixeltypetext = PG_GETARG_TEXT_P(0);
+ pixeltypechar = text_to_cstring(pixeltypetext);
+
+ pixtype = rt_pixtype_index_from_name(pixeltypechar);
+ if (pixtype == PT_END) {
+ elog(ERROR, "RASTER_minPossibleValue: Invalid pixel type: %s", pixeltypechar);
+ PG_RETURN_NULL();
+ }
+
+ pixsize = rt_pixtype_get_min_value(pixtype);
+
+ /*
+ correct pixsize of unsigned pixel types
+ example: for PT_8BUI, the value is CHAR_MIN but if char is signed,
+ the value returned is -127 instead of 0.
+ */
+ switch (pixtype) {
+ case PT_1BB:
+ case PT_2BUI:
+ case PT_4BUI:
+ case PT_8BUI:
+ case PT_16BUI:
+ case PT_32BUI:
+ pixsize = 0;
+ break;
+ default:
+ break;
+ }
+
+ PG_RETURN_FLOAT8(pixsize);
+}
+
/**
* Input is a string with hex chars in it.
* Convert to binary and put in the result
LANGUAGE 'plpgsql' IMMUTABLE STRICT;
-----------------------------------------------------------------------
--- ST_MinPossibleVal(pixeltype text)
+-- ST_MinPossibleValue(pixeltype text)
-- Return the smallest value for a given pixeltyp.
-- Should be called like this:
--- SELECT ST_MinPossibleVal(ST_BandPixelType(rast, band))
+-- SELECT ST_MinPossibleValue(ST_BandPixelType(rast, band))
-----------------------------------------------------------------------
-CREATE OR REPLACE FUNCTION ST_MinPossibleVal(pixeltype text)
- RETURNS float8 AS
- $$
- DECLARE
- newval int := 0;
- BEGIN
- newval := CASE
- WHEN pixeltype = '1BB' THEN 0
- WHEN pixeltype = '2BUI' THEN 0
- WHEN pixeltype = '4BUI' THEN 0
- WHEN pixeltype = '8BUI' THEN 0
- WHEN pixeltype = '8BSI' THEN -128
- WHEN pixeltype = '16BUI' THEN 0
- WHEN pixeltype = '16BSI' THEN -32768
- WHEN pixeltype = '32BUI' THEN 0
- WHEN pixeltype = '32BSI' THEN -2147483648
- WHEN pixeltype = '32BF' THEN -2147483648 -- Could not find a function returning the smallest real yet
- WHEN pixeltype = '64BF' THEN -2147483648 -- Could not find a function returning the smallest float8 yet
- END;
- RETURN newval;
- END;
- $$
- LANGUAGE 'plpgsql';
+
+CREATE OR REPLACE FUNCTION st_minpossiblevalue(pixeltype text)
+ RETURNS double precision
+ AS 'MODULE_PATHNAME', 'RASTER_minPossibleValue'
+ LANGUAGE 'C' IMMUTABLE STRICT;
-----------------------------------------------------------------------
-- Raster Outputs
bandend := band;
END IF;
newpixtype := ST_BandPixelType(rast, bandstart);
- newnodata := coalesce(nodata, ST_BandNodataValue(rast, bandstart), ST_MinPossibleVal(newpixtype));
+ newnodata := coalesce(nodata, ST_BandNodataValue(rast, bandstart), ST_MinPossibleValue(newpixtype));
newextent := CASE WHEN trimraster THEN 'INTERSECTION' ELSE 'FIRST' END;
--RAISE NOTICE 'newextent=%', newextent;
--RAISE NOTICE 'bandi=%', bandi;
-- for each band we must determine the nodata value
newpixtype := ST_BandPixelType(rast, bandi);
- newnodata := coalesce(nodata, ST_BandNodataValue(sourceraster, bandi), ST_MinPossibleVal(newpixtype));
+ newnodata := coalesce(nodata, ST_BandNodataValue(sourceraster, bandi), ST_MinPossibleValue(newpixtype));
sourceraster := ST_SetBandNodataValue(sourceraster, bandi, newnodata);
newrast := ST_AddBand(newrast, ST_MapAlgebraExpr(sourceraster, bandi, geomrast, 1, 'rast1', newpixtype, newextent));
END LOOP;
-- functions have changed dramatically
DROP FUNCTION IF EXISTS st_intersection(rast raster, band integer, geom geometry);
DROP FUNCTION IF EXISTS st_intersection(rast raster, geom geometry);
+
+-- function was renamed
+DROP FUNCTION IF EXISTS st_minpossibleval(text);
bandend := band;
END IF;
newpixtype := ST_BandPixelType(rast, bandstart);
- newnodata := coalesce(nodata, ST_BandNodataValue(rast, bandstart), ST_MinPossibleVal(newpixtype));
+ newnodata := coalesce(nodata, ST_BandNodataValue(rast, bandstart), ST_MinPossibleValue(newpixtype));
newextent := CASE WHEN trimraster THEN 'INTERSECTION' ELSE 'FIRST' END;
--RAISE NOTICE 'newextent=%', newextent;
--RAISE NOTICE 'bandi=%', bandi;
-- for each band we must determine the nodata value
newpixtype := ST_BandPixelType(rast, bandi);
- newnodata := coalesce(nodata, ST_BandNodataValue(sourceraster, bandi), ST_MinPossibleVal(newpixtype));
+ newnodata := coalesce(nodata, ST_BandNodataValue(sourceraster, bandi), ST_MinPossibleValue(newpixtype));
sourceraster := ST_SetBandNodataValue(sourceraster, bandi, newnodata);
newrast := ST_AddBand(newrast, ST_MapAlgebraExpr(sourceraster, bandi, geomrast, 1, 'rast1', newpixtype, newextent));
END LOOP;
-- Check for notada value
newnodatavalue := ST_BandNodataValue(rast, band);
- IF newnodatavalue IS NULL OR newnodatavalue < ST_MinPossibleVal(newpixeltype) OR newnodatavalue > (-ST_MinPossibleVal(newpixeltype) - 1) THEN
+ IF newnodatavalue IS NULL OR newnodatavalue < ST_MinPossibleValue(newpixeltype) OR newnodatavalue > (-ST_MinPossibleValue(newpixeltype) - 1) THEN
RAISE NOTICE 'ST_MapAlgebra: Source raster do not have a nodata value or is out of range for the new raster pixeltype, nodata value for new raster set to the min value possible';
- newnodatavalue := ST_MinPossibleVal(newpixeltype);
+ newnodatavalue := ST_MinPossibleValue(newpixeltype);
END IF;
-- We set the initial value of the future band to nodata value.
- -- If nodatavalue is null then the raster will be initialise to ST_MinPossibleVal
+ -- If nodatavalue is null then the raster will be initialise to ST_MinPossibleValue
-- but all the values should be recomputed anyway.
newinitialvalue := newnodatavalue;
newnodatavalue := rast2nodataval;
ELSE
RAISE NOTICE 'ST_MapAlgebra: Both source rasters do not have a nodata value, nodata value for new raster set to the minimum value possible';
- newnodatavalue := ST_MinPossibleVal(newrast);
+ newnodatavalue := ST_MinPossibleValue(newrast);
END IF;
-------------------------------------------------------------------
newnodatavalue := rast1nodataval;\r
ELSE\r
RAISE NOTICE 'ST_MapAlgebra: Both source rasters do not have a nodata value, nodata value for new raster set to the minimum value possible';\r
- newnodatavalue := ST_MinPossibleVal(newrast);\r
+ newnodatavalue := ST_MinPossibleValue(newrast);\r
END IF;\r
\r
upnodatanodataexpr := upper(nodatanodataexpr);\r
--
-- Helper method to get the smallest value in a raster, based on the pixeltype.
--
-CREATE OR REPLACE FUNCTION ST_MinPossibleVal(pixeltype text)
+CREATE OR REPLACE FUNCTION ST_MinPossibleValue(pixeltype text)
RETURNS float8 AS
$$
DECLARE
-- Check for notada value
newnodatavalue := ST_BandNodataValue(rast, band);
- IF newnodatavalue IS NULL OR newnodatavalue < ST_MinPossibleVal(newpixeltype) OR newnodatavalue > (-ST_MinPossibleVal(newpixeltype) - 1) THEN
+ IF newnodatavalue IS NULL OR newnodatavalue < ST_MinPossibleValue(newpixeltype) OR newnodatavalue > (-ST_MinPossibleValue(newpixeltype) - 1) THEN
RAISE NOTICE 'ST_MapAlgebraFctNgb: Source raster does not have a nodata value or is out of range for the new raster pixeltype, nodata value for new raster set to the min value possible';
- newnodatavalue := ST_MinPossibleVal(newpixeltype);
+ newnodatavalue := ST_MinPossibleValue(newpixeltype);
END IF;
-- We set the initial value of the future band to nodata value.
- -- If nodatavalue is null then the raster will be initialise to ST_MinPossibleVal
+ -- If nodatavalue is null then the raster will be initialise to ST_MinPossibleValue
-- but all the values should be recomputed anyway.
newinitialvalue := newnodatavalue;
+++ /dev/null
------------------------------------------------------------------------\r
--- ST_MinPossibleVal\r
--- Return the smallest value for a given pixeltyp. \r
--- Should be called like this:\r
--- SELECT ST_MinPossibleVal(ST_BandPixelType(rast, band))\r
------------------------------------------------------------------------\r
-CREATE OR REPLACE FUNCTION ST_MinPossibleVal(pixeltype text)\r
- RETURNS float8 AS\r
- $$\r
- DECLARE\r
- newval int := 0;\r
- BEGIN\r
- newval := CASE \r
- WHEN pixeltype = '1BB' THEN 0\r
- WHEN pixeltype = '2BUI' THEN 0\r
- WHEN pixeltype = '4BUI' THEN 0\r
- WHEN pixeltype = '8BUI' THEN 0\r
- WHEN pixeltype = '8BSI' THEN -128\r
- WHEN pixeltype = '16BUI' THEN 0\r
- WHEN pixeltype = '16BSI' THEN -32768\r
- WHEN pixeltype = '32BUI' THEN 0\r
- WHEN pixeltype = '32BSI' THEN -2147483648\r
- WHEN pixeltype = '32BF' THEN -2147483648 -- Could not find a function returning the smallest real yet\r
- WHEN pixeltype = '64BF' THEN -2147483648 -- Could not find a function returning the smallest float8 yet\r
- END;\r
- RETURN newval;\r
- END;\r
- $$\r
- LANGUAGE 'plpgsql';\r
FROM rt_utility_test
WHERE st_raster2worldcoordy(rast, 1)::numeric != ipy::numeric;
+-----------------------------------------------------------------------
+-- Test 11 - st_minpossiblevalue(pixtype text)
+-----------------------------------------------------------------------
+
+SELECT 'test 11.1', st_minpossiblevalue('1BB') = 0.;
+SELECT 'test 11.2', st_minpossiblevalue('2BUI') = 0.;
+SELECT 'test 11.3', st_minpossiblevalue('4BUI') = 0.;
+SELECT 'test 11.4', st_minpossiblevalue('8BUI') = 0.;
+SELECT 'test 11.5', st_minpossiblevalue('8BSI') < 0.;
+SELECT 'test 11.6', st_minpossiblevalue('16BUI') = 0.;
+SELECT 'test 11.7', st_minpossiblevalue('16BSI') < 0.;
+SELECT 'test 11.8', st_minpossiblevalue('32BUI') = 0.;
+SELECT 'test 11.9', st_minpossiblevalue('32BSI') < 0.;
+SELECT 'test 11.10', st_minpossiblevalue('32BF') < 0.;
+SELECT 'test 11.11', st_minpossiblevalue('64BF') < 0.;
ERROR: Attempting to compute raster coordinate on a raster with rotation providing y only. A x must also be provided
ERROR: Attempting to compute raster coordinates on a raster with rotation providing X only. A Y coordinate must also be provided
ERROR: Attempting to compute raster coordinates on a raster with rotation providing Y only. An X coordinate must also be provided
+test 11.1|t
+test 11.2|t
+test 11.3|t
+test 11.4|t
+test 11.5|t
+test 11.6|t
+test 11.7|t
+test 11.8|t
+test 11.9|t
+test 11.10|t
+test 11.11|t