Ticket #341: st_astiff.patch

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

Adds ST_TIFF capability

  • 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  
    417417        LANGUAGE 'SQL' IMMUTABLE STRICT; 
    418418 
    419419----------------------------------------------------------------------- 
     420-- ST_AsTIFF 
     421----------------------------------------------------------------------- 
     422-- Cannot be strict as "options" and "srs" can be NULL 
     423CREATE 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 
     448CREATE 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 
     453CREATE 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 
     458CREATE 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 
     463CREATE 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 
     472CREATE 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 
     482CREATE 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 
     561CREATE 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 
     566CREATE 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 
     571CREATE 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----------------------------------------------------------------------- 
    420581-- MapAlgebra 
    421582----------------------------------------------------------------------- 
    422583-- 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  
    4444        rt_band.sql \ 
    4545        rt_reclass.sql \ 
    4646        rt_asgdalraster.sql \ 
     47        rt_astiff.sql \ 
    4748        $(NULL) 
    4849 
    4950TEST_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 
     5WARNING:  The TIFF format only permits one NODATA value for all bands.  The value used will be the last band with a NODATA value. 
     681de10108dd8eede75ef47c8f6f09bc5 
     7WARNING:  The TIFF format only permits one NODATA value for all bands.  The value used will be the last band with a NODATA value. 
     8WARNING:  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.