| | 1085 | -- ST_AsTIFF |
| | 1086 | ----------------------------------------------------------------------- |
| | 1087 | -- Cannot be strict as "options" and "srs" can be NULL |
| | 1088 | CREATE 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 | |
| | 1113 | CREATE 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 | |
| | 1118 | CREATE 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 | |
| | 1123 | CREATE 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 | |
| | 1128 | CREATE 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 | |
| | 1137 | CREATE 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 |
| | 1147 | CREATE 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 | |
| | 1226 | CREATE 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 | |
| | 1231 | CREATE 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 | |
| | 1236 | CREATE 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 | ----------------------------------------------------------------------- |