Ticket #341: st_astiff.3.patch

File st_astiff.3.patch, 8.7 KB (added by dustymugs, 13 months ago)

Updated patch to better follow guidelines for error messages

  • raster/rt_pg/rtpostgis.sql.in.c

    diff -rupN postgis-old/raster/rt_pg/rtpostgis.sql.in.c postgis-new/raster/rt_pg/rtpostgis.sql.in.c
    old new  
    10821082        LANGUAGE 'SQL' IMMUTABLE STRICT; 
    10831083 
    10841084----------------------------------------------------------------------- 
     1085-- ST_AsTIFF 
     1086----------------------------------------------------------------------- 
     1087-- Cannot be strict as "options" and "srs" can be NULL 
     1088CREATE OR REPLACE FUNCTION st_astiff(rast raster, options text[], srs text) 
     1089        RETURNS bytea 
     1090        AS $$ 
     1091        DECLARE 
     1092                i int; 
     1093                num_bands int; 
     1094                nodata int; 
     1095                last_nodata int; 
     1096        BEGIN 
     1097                num_bands := st_numbands($1); 
     1098 
     1099                -- TIFF only allows one NODATA value for ALL bands 
     1100                FOR i IN 1..num_bands LOOP 
     1101                        nodata := st_bandnodatavalue($1, i); 
     1102                        IF last_nodata IS NULL THEN 
     1103                                last_nodata := nodata; 
     1104                        ELSEIF nodata != last_nodata THEN 
     1105                                RAISE NOTICE 'The TIFF format only permits one NODATA value for all bands.  The value used will be the last band with a NODATA value.'; 
     1106                        END IF; 
     1107                END LOOP; 
     1108 
     1109                RETURN st_asgdalraster($1, 'GTiff', $2, $3); 
     1110        END; 
     1111        $$ LANGUAGE 'plpgsql' IMMUTABLE; 
     1112 
     1113CREATE OR REPLACE FUNCTION st_astiff(rast raster, options text[]) 
     1114        RETURNS bytea 
     1115        AS $$ SELECT st_astiff($1, $2, st_srtext($1)) $$ 
     1116        LANGUAGE 'SQL' IMMUTABLE STRICT; 
     1117 
     1118CREATE OR REPLACE FUNCTION st_astiff(rast raster) 
     1119        RETURNS bytea 
     1120        AS $$ SELECT st_astiff($1, NULL::text[], st_srtext($1)) $$ 
     1121        LANGUAGE 'SQL' IMMUTABLE STRICT; 
     1122 
     1123CREATE OR REPLACE FUNCTION st_astiff(rast raster, nbands int[], options text[], srs text) 
     1124        RETURNS bytea 
     1125        AS $$ SELECT st_astiff(st_band($1, $2), $3, $4) $$ 
     1126        LANGUAGE 'SQL' IMMUTABLE STRICT; 
     1127 
     1128CREATE OR REPLACE FUNCTION st_astiff(rast raster, nbands int[], options text[]) 
     1129        RETURNS bytea 
     1130        AS $$ 
     1131        BEGIN 
     1132                rast := st_band($1, $2); 
     1133                RETURN st_astiff(rast, $3, st_srtext(rast)); 
     1134        END; 
     1135        $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT; 
     1136 
     1137CREATE OR REPLACE FUNCTION st_astiff(rast raster, nbands int[]) 
     1138        RETURNS bytea 
     1139        AS $$ 
     1140        BEGIN 
     1141                rast := st_band($1, $2); 
     1142                RETURN st_astiff(rast, NULL::text[], st_srtext(rast)); 
     1143        END; 
     1144        $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT; 
     1145 
     1146-- Cannot be strict as "srs" can be NULL 
     1147CREATE OR REPLACE FUNCTION st_astiff(rast raster, compression text, srs text) 
     1148        RETURNS bytea 
     1149        AS $$ 
     1150        DECLARE 
     1151                c_type text; 
     1152                c_level int; 
     1153                i int; 
     1154                num_bands int; 
     1155                options text[]; 
     1156        BEGIN 
     1157                compression := trim(both from upper(compression)); 
     1158 
     1159                IF length(compression) > 0 THEN 
     1160                        -- JPEG 
     1161                        IF position('JPEG' in compression) != 0 THEN 
     1162                                c_type := 'JPEG'; 
     1163                                c_level := substring(compression from '[0-9]+$'); 
     1164 
     1165                                IF c_level IS NOT NULL THEN 
     1166                                        IF c_level > 100 THEN 
     1167                                                c_level := 100; 
     1168                                        ELSEIF c_level < 1 THEN 
     1169                                                c_level := 1; 
     1170                                        END IF; 
     1171 
     1172                                        options := array_append(options, 'JPEG_QUALITY=' || c_level); 
     1173                                END IF; 
     1174 
     1175                                -- per band pixel type check 
     1176                                num_bands := st_numbands($1); 
     1177                                FOR i IN 1..num_bands LOOP 
     1178                                        IF st_bandpixeltype($1, i) != '8BUI' THEN 
     1179                                                RAISE EXCEPTION 'The pixel type of band % in the raster is not 8BUI.  JPEG compression can only be used with the 8BUI pixel type.', i; 
     1180                                        END IF; 
     1181                                END LOOP; 
     1182 
     1183                        -- DEFLATE 
     1184                        ELSEIF position('DEFLATE' in compression) != 0 THEN 
     1185                                c_type := 'DEFLATE'; 
     1186                                c_level := substring(compression from '[0-9]+$'); 
     1187 
     1188                                IF c_level IS NOT NULL THEN 
     1189                                        IF c_level > 9 THEN 
     1190                                                c_level := 9; 
     1191                                        ELSEIF c_level < 1 THEN 
     1192                                                c_level := 1; 
     1193                                        END IF; 
     1194 
     1195                                        options := array_append(options, 'ZLEVEL=' || c_level); 
     1196                                END IF; 
     1197 
     1198                        ELSE 
     1199                                c_type := compression; 
     1200 
     1201                                -- CCITT 
     1202                                IF position('CCITT' in compression) THEN 
     1203                                        -- per band pixel type check 
     1204                                        num_bands := st_numbands($1); 
     1205                                        FOR i IN 1..num_bands LOOP 
     1206                                                IF st_bandpixeltype($1, i) != '1BB' THEN 
     1207                                                        RAISE EXCEPTION 'The pixel type of band % in the raster is not 1BB.  CCITT compression can only be used with the 1BB pixel type.', i; 
     1208                                                END IF; 
     1209                                        END LOOP; 
     1210                                END IF; 
     1211 
     1212                        END IF; 
     1213 
     1214                        -- compression type check 
     1215                        IF ARRAY[c_type] <@ ARRAY['JPEG', 'LZW', 'PACKBITS', 'DEFLATE', 'CCITTRLE', 'CCITTFAX3', 'CCITTFAX4', 'NONE'] THEN 
     1216                                options := array_append(options, 'COMPRESS=' || c_type); 
     1217                        ELSE 
     1218                                RAISE NOTICE 'Unknown compression type: %.  The outputted TIFF will not be COMPRESSED.', c_type; 
     1219                        END IF; 
     1220                END IF; 
     1221 
     1222                RETURN st_astiff($1, options, $4); 
     1223        END; 
     1224        $$ LANGUAGE 'plpgsql' IMMUTABLE; 
     1225 
     1226CREATE OR REPLACE FUNCTION st_astiff(rast raster, compression text) 
     1227        RETURNS bytea 
     1228        AS $$ SELECT st_astiff($1, $2, st_srtext($1)) $$ 
     1229        LANGUAGE 'SQL' IMMUTABLE STRICT; 
     1230 
     1231CREATE OR REPLACE FUNCTION st_astiff(rast raster, nbands int[], compression text, srs text) 
     1232        RETURNS bytea 
     1233        AS $$ SELECT st_astiff(st_band($1, $2), $3, $4) $$ 
     1234        LANGUAGE 'SQL' IMMUTABLE STRICT; 
     1235 
     1236CREATE OR REPLACE FUNCTION st_astiff(rast raster, nbands int[], compression text) 
     1237        RETURNS bytea 
     1238        AS $$ 
     1239        BEGIN 
     1240                rast := st_band($1, $2); 
     1241                RETURN st_astiff(rast, $3, NULL::text[], st_srtext(rast)); 
     1242        END; 
     1243        $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT; 
     1244 
     1245----------------------------------------------------------------------- 
    10851246-- MapAlgebra 
    10861247----------------------------------------------------------------------- 
    10871248-- This function can not be STRICT, because nodatavalueexpr can be NULL (could be just '' though) 
  • raster/test/regress/Makefile.in

    diff -rupN postgis-old/raster/test/regress/Makefile.in postgis-new/raster/test/regress/Makefile.in
    old new  
    4343        rt_addband.sql \ 
    4444        rt_band.sql \ 
    4545        rt_asgdalraster.sql \ 
     46        rt_astiff.sql \ 
    4647        $(NULL) 
    4748 
    4849TEST_PROPS = \ 
  • raster/test/regress/rt_astiff.sql

    diff -rupN postgis-old/raster/test/regress/rt_astiff.sql postgis-new/raster/test/regress/rt_astiff.sql
    old new  
     1SELECT md5( 
     2        ST_AsTIFF( 
     3                ST_AddBand(ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1), 1, '64BF', 123.4567, NULL) 
     4        ) 
     5); 
     6SELECT md5( 
     7        ST_AsTIFF( 
     8                ST_AddBand( 
     9                        ST_AddBand( 
     10                                ST_AddBand( 
     11                                        ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0,-1) 
     12                                        , 1, '64BF', 1234.5678, NULL 
     13                                ) 
     14                                , '64BF', 987.654321, NULL 
     15                        ) 
     16                        , '64BF', 9876.54321, NULL 
     17                ) 
     18        ) 
     19); 
     20SELECT md5( 
     21        ST_AsTIFF( 
     22                ST_AddBand( 
     23                        ST_AddBand( 
     24                                ST_AddBand( 
     25                                        ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1) 
     26                                        , 1, '64BF', 1234.5678, -9999 
     27                                ) 
     28                                , '64BF', 987.654321, -9999 
     29                        ) 
     30                        , '64BF', 9876.54321, -9999 
     31                ) 
     32        ) 
     33); 
     34SELECT md5( 
     35        ST_AsTIFF( 
     36                ST_AddBand( 
     37                        ST_AddBand( 
     38                                ST_AddBand( 
     39                                        ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1) 
     40                                        , 1, '64BF', 1234.5678, -9999 
     41                                ) 
     42                                , '64BF', 987.654321, -9999 
     43                        ) 
     44                        , '64BF', 9876.54321, -9999 
     45                ) 
     46                , ARRAY[3] 
     47        ) 
     48); 
     49SELECT md5( 
     50        ST_AsTIFF( 
     51                ST_AddBand( 
     52                        ST_AddBand( 
     53                                ST_AddBand( 
     54                                        ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1) 
     55                                        , 1, '64BF', 1234.5678, -9999 
     56                                ) 
     57                                , '64BF', 987.654321, -9999 
     58                        ) 
     59                        , '64BF', 9876.54321, -1 
     60                ) 
     61        ) 
     62); 
     63SELECT md5( 
     64        ST_AsTIFF( 
     65                ST_AddBand( 
     66                        ST_AddBand( 
     67                                ST_AddBand( 
     68                                        ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1) 
     69                                        , 1, '64BF', 1234.5678, -1 
     70                                ) 
     71                                , '64BF', 987.654321, -9999 
     72                        ) 
     73                        , '64BF', 9876.54321, -9999 
     74                ) 
     75        ) 
     76); 
     77SELECT md5( 
     78        ST_AsTIFF( 
     79                ST_AddBand( 
     80                        ST_AddBand( 
     81                                ST_AddBand( 
     82                                        ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1) 
     83                                        , 1, '64BF', 1234.5678, -9999 
     84                                ) 
     85                                , '64BF', 987.654321, -9999 
     86                        ) 
     87                        , '64BF', 9876.54321, -1 
     88                ) 
     89                , 'JPEG90' 
     90        ) 
     91); 
  • raster/test/regress/rt_astiff_expected

    diff -rupN postgis-old/raster/test/regress/rt_astiff_expected postgis-new/raster/test/regress/rt_astiff_expected
    old new  
     12fd5de5f94416f2999fc24fbc3974a4b 
     24af1ca04adf045690fb60764cc93de75 
     381de10108dd8eede75ef47c8f6f09bc5 
     4915b9a5f0b1baea454eef06a2431891b 
     5NOTICE:  The TIFF format only permits one NODATA value for all bands.  The value used will be the last band with a NODATA value. 
     681de10108dd8eede75ef47c8f6f09bc5 
     7NOTICE:  The TIFF format only permits one NODATA value for all bands.  The value used will be the last band with a NODATA value. 
     8NOTICE:  The TIFF format only permits one NODATA value for all bands.  The value used will be the last band with a NODATA value. 
     98f6e8ae14b92e32d4cf70c85ef67252e 
     10ERROR:  The pixel type of band 1 in the raster is not 8BUI.  JPEG compression can only be used with the 8BUI pixel type.