| | 420 | -- ST_AsTIFF |
| | 421 | ----------------------------------------------------------------------- |
| | 422 | -- Cannot be strict as "options" and "srs" can be NULL |
| | 423 | CREATE OR REPLACE FUNCTION st_astiff(rast raster, options text[], srs text) |
| | 424 | RETURNS bytea |
| | 425 | AS $$ |
| | 426 | DECLARE |
| | 427 | i int; |
| | 428 | num_bands int; |
| | 429 | nodata int; |
| | 430 | last_nodata int; |
| | 431 | BEGIN |
| | 432 | num_bands := st_numbands($1); |
| | 433 | |
| | 434 | -- TIFF only allows one NODATA value for ALL bands |
| | 435 | FOR i IN 1..num_bands LOOP |
| | 436 | nodata := st_bandnodatavalue($1, i); |
| | 437 | IF last_nodata IS NULL THEN |
| | 438 | last_nodata := nodata; |
| | 439 | ELSEIF nodata != last_nodata THEN |
| | 440 | RAISE WARNING 'The TIFF format only permits one NODATA value for all bands. The value used will be the last band with a NODATA value.'; |
| | 441 | END IF; |
| | 442 | END LOOP; |
| | 443 | |
| | 444 | RETURN st_asgdalraster($1, 'GTiff', $2, $3); |
| | 445 | END; |
| | 446 | $$ LANGUAGE 'plpgsql' IMMUTABLE; |
| | 447 | |
| | 448 | CREATE OR REPLACE FUNCTION st_astiff(rast raster, options text[]) |
| | 449 | RETURNS bytea |
| | 450 | AS $$ SELECT st_astiff($1, $2, st_srtext($1)) $$ |
| | 451 | LANGUAGE 'SQL' IMMUTABLE STRICT; |
| | 452 | |
| | 453 | CREATE OR REPLACE FUNCTION st_astiff(rast raster) |
| | 454 | RETURNS bytea |
| | 455 | AS $$ SELECT st_astiff($1, NULL::text[], st_srtext($1)) $$ |
| | 456 | LANGUAGE 'SQL' IMMUTABLE STRICT; |
| | 457 | |
| | 458 | CREATE OR REPLACE FUNCTION st_astiff(rast raster, nbands int[], options text[], srs text) |
| | 459 | RETURNS bytea |
| | 460 | AS $$ SELECT st_astiff(st_band($1, $2), $3, $4) $$ |
| | 461 | LANGUAGE 'SQL' IMMUTABLE STRICT; |
| | 462 | |
| | 463 | CREATE OR REPLACE FUNCTION st_astiff(rast raster, nbands int[], options text[]) |
| | 464 | RETURNS bytea |
| | 465 | AS $$ |
| | 466 | BEGIN |
| | 467 | rast := st_band($1, $2); |
| | 468 | RETURN st_astiff(rast, $3, st_srtext(rast)); |
| | 469 | END; |
| | 470 | $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT; |
| | 471 | |
| | 472 | CREATE OR REPLACE FUNCTION st_astiff(rast raster, nbands int[]) |
| | 473 | RETURNS bytea |
| | 474 | AS $$ |
| | 475 | BEGIN |
| | 476 | rast := st_band($1, $2); |
| | 477 | RETURN st_astiff(rast, NULL::text[], st_srtext(rast)); |
| | 478 | END; |
| | 479 | $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT; |
| | 480 | |
| | 481 | -- Cannot be strict as "srs" can be NULL |
| | 482 | CREATE OR REPLACE FUNCTION st_astiff(rast raster, compression text, srs text) |
| | 483 | RETURNS bytea |
| | 484 | AS $$ |
| | 485 | DECLARE |
| | 486 | c_type text; |
| | 487 | c_level int; |
| | 488 | i int; |
| | 489 | num_bands int; |
| | 490 | options text[]; |
| | 491 | BEGIN |
| | 492 | compression := trim(both from upper(compression)); |
| | 493 | |
| | 494 | IF length(compression) > 0 THEN |
| | 495 | -- JPEG |
| | 496 | IF position('JPEG' in compression) != 0 THEN |
| | 497 | c_type := 'JPEG'; |
| | 498 | c_level := substring(compression from '[0-9]+$'); |
| | 499 | |
| | 500 | IF c_level IS NOT NULL THEN |
| | 501 | IF c_level > 100 THEN |
| | 502 | c_level := 100; |
| | 503 | ELSEIF c_level < 1 THEN |
| | 504 | c_level := 1; |
| | 505 | END IF; |
| | 506 | |
| | 507 | options := array_append(options, 'JPEG_QUALITY=' || c_level); |
| | 508 | END IF; |
| | 509 | |
| | 510 | -- per band pixel type check |
| | 511 | num_bands := st_numbands($1); |
| | 512 | FOR i IN 1..num_bands LOOP |
| | 513 | IF st_bandpixeltype($1, i) != '8BUI' THEN |
| | 514 | 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; |
| | 515 | END IF; |
| | 516 | END LOOP; |
| | 517 | |
| | 518 | -- DEFLATE |
| | 519 | ELSEIF position('DEFLATE' in compression) != 0 THEN |
| | 520 | c_type := 'DEFLATE'; |
| | 521 | c_level := substring(compression from '[0-9]+$'); |
| | 522 | |
| | 523 | IF c_level IS NOT NULL THEN |
| | 524 | IF c_level > 9 THEN |
| | 525 | c_level := 9; |
| | 526 | ELSEIF c_level < 1 THEN |
| | 527 | c_level := 1; |
| | 528 | END IF; |
| | 529 | |
| | 530 | options := array_append(options, 'ZLEVEL=' || c_level); |
| | 531 | END IF; |
| | 532 | |
| | 533 | ELSE |
| | 534 | c_type := compression; |
| | 535 | |
| | 536 | -- CCITT |
| | 537 | IF position('CCITT' in compression) THEN |
| | 538 | -- per band pixel type check |
| | 539 | num_bands := st_numbands($1); |
| | 540 | FOR i IN 1..num_bands LOOP |
| | 541 | IF st_bandpixeltype($1, i) != '1BB' THEN |
| | 542 | 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; |
| | 543 | END IF; |
| | 544 | END LOOP; |
| | 545 | END IF; |
| | 546 | |
| | 547 | END IF; |
| | 548 | |
| | 549 | -- compression type check |
| | 550 | IF ARRAY[c_type] <@ ARRAY['JPEG', 'LZW', 'PACKBITS', 'DEFLATE', 'CCITTRLE', 'CCITTFAX3', 'CCITTFAX4', 'NONE'] THEN |
| | 551 | options := array_append(options, 'COMPRESS=' || c_type); |
| | 552 | ELSE |
| | 553 | RAISE WARNING 'Unknown compression type: %. The outputted TIFF will not be COMPRESSED.', c_type; |
| | 554 | END IF; |
| | 555 | END IF; |
| | 556 | |
| | 557 | RETURN st_astiff($1, options, $4); |
| | 558 | END; |
| | 559 | $$ LANGUAGE 'plpgsql' IMMUTABLE; |
| | 560 | |
| | 561 | CREATE OR REPLACE FUNCTION st_astiff(rast raster, compression text) |
| | 562 | RETURNS bytea |
| | 563 | AS $$ SELECT st_astiff($1, $2, st_srtext($1)) $$ |
| | 564 | LANGUAGE 'SQL' IMMUTABLE STRICT; |
| | 565 | |
| | 566 | CREATE OR REPLACE FUNCTION st_astiff(rast raster, nbands int[], compression text, srs text) |
| | 567 | RETURNS bytea |
| | 568 | AS $$ SELECT st_astiff(st_band($1, $2), $3, $4) $$ |
| | 569 | LANGUAGE 'SQL' IMMUTABLE STRICT; |
| | 570 | |
| | 571 | CREATE OR REPLACE FUNCTION st_astiff(rast raster, nbands int[], compression text) |
| | 572 | RETURNS bytea |
| | 573 | AS $$ |
| | 574 | BEGIN |
| | 575 | rast := st_band($1, $2); |
| | 576 | RETURN st_astiff(rast, $3, NULL::text[], st_srtext(rast)); |
| | 577 | END; |
| | 578 | $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT; |
| | 579 | |
| | 580 | ----------------------------------------------------------------------- |