Ticket #342: st_aspng.patch

File st_aspng.patch, 7.1 KB (added by dustymugs, 13 months ago)

Add ST_AsPNG 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  
    664664        LANGUAGE 'SQL' IMMUTABLE STRICT; 
    665665 
    666666----------------------------------------------------------------------- 
     667-- ST_AsPNG 
     668----------------------------------------------------------------------- 
     669-- Cannot be strict as "options" can be NULL 
     670CREATE OR REPLACE FUNCTION st_aspng(rast raster, options text[]) 
     671        RETURNS bytea 
     672        AS $$ 
     673        DECLARE 
     674                num_bands int; 
     675                i int; 
     676                pt text; 
     677        BEGIN 
     678                num_bands := st_numbands($1); 
     679 
     680                -- PNG only allows 1 or 3 bands 
     681                IF num_bands > 3 THEN 
     682                        RAISE WARNING 'The PNG format only permits one or three bands.  The first three bands will be used.'; 
     683                        rast := st_band(rast, ARRAY[1, 2, 3]); 
     684                        num_bands := st_numbands(rast); 
     685                ELSEIF num_bands > 1 THEN 
     686                        RAISE WARNING 'The PNG format only permits one or three bands.  The first band will be used.'; 
     687                        rast := st_band(rast, ARRAY[1]); 
     688                        num_bands := st_numbands(rast); 
     689                END IF; 
     690 
     691                -- PNG only supports 8BUI and 16BUI pixeltype 
     692                FOR i IN 1..num_bands LOOP 
     693                        pt = st_bandpixeltype(rast, i); 
     694                        IF pt != '8BUI' AND pt != '16BUI' THEN 
     695                                RAISE EXCEPTION 'The pixel type of band % in the raster is not 8BUI or 16BUI.  The PNG format can only be used with 8BUI and 16BUI pixel types.', i; 
     696                        END IF; 
     697                END LOOP; 
     698 
     699                RETURN st_asgdalraster($1, 'PNG', $2, NULL); 
     700        END; 
     701        $$ LANGUAGE 'plpgsql' IMMUTABLE; 
     702 
     703CREATE OR REPLACE FUNCTION st_aspng(rast raster) 
     704        RETURNS bytea 
     705        AS $$ SELECT st_aspng($1, NULL::text[]) $$ 
     706        LANGUAGE 'SQL' IMMUTABLE STRICT; 
     707 
     708CREATE OR REPLACE FUNCTION st_aspng(rast raster, nbands int[], options text[]) 
     709        RETURNS bytea 
     710        AS $$ SELECT st_aspng(st_band($1, $2), $3) $$ 
     711        LANGUAGE 'SQL' IMMUTABLE STRICT; 
     712 
     713CREATE OR REPLACE FUNCTION st_aspng(rast raster, nbands int[]) 
     714        RETURNS bytea 
     715        AS $$ SELECT st_aspng(st_band($1, $2), NULL::text[]) $$ 
     716        LANGUAGE 'SQL' IMMUTABLE STRICT; 
     717 
     718CREATE OR REPLACE FUNCTION st_aspng(rast raster, nband int) 
     719        RETURNS bytea 
     720        AS $$ SELECT st_aspng(st_band($1, $2)) $$ 
     721        LANGUAGE 'SQL' IMMUTABLE STRICT; 
     722 
     723CREATE OR REPLACE FUNCTION st_aspng(rast raster, nbands int[], compression int) 
     724        RETURNS bytea 
     725        AS $$ 
     726        DECLARE 
     727                options text[]; 
     728        BEGIN 
     729                IF compression IS NOT NULL THEN 
     730                        IF compression > 9 THEN 
     731                                compression := 9; 
     732                        ELSEIF compression < 1 THEN 
     733                                compression := 1; 
     734                        END IF; 
     735 
     736                        options := array_append(options, 'ZLEVEL=' || compression); 
     737                END IF; 
     738 
     739                RETURN st_aspng(st_band($1, $2), options); 
     740        END; 
     741        $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT; 
     742 
     743CREATE OR REPLACE FUNCTION st_aspng(rast raster, nband int, options text[]) 
     744        RETURNS bytea 
     745        AS $$ SELECT st_aspng(st_band($1, $2), $3) $$ 
     746        LANGUAGE 'SQL' IMMUTABLE STRICT; 
     747 
     748CREATE OR REPLACE FUNCTION st_aspng(rast raster, nband int, compression int) 
     749        RETURNS bytea 
     750        AS $$ SELECT st_aspng($1, ARRAY[$2], $3) $$ 
     751        LANGUAGE 'SQL' IMMUTABLE STRICT; 
     752 
     753----------------------------------------------------------------------- 
    667754-- MapAlgebra 
    668755----------------------------------------------------------------------- 
    669756-- 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  
    4646        rt_asgdalraster.sql \ 
    4747        rt_astiff.sql \ 
    4848        rt_asjpeg.sql \ 
     49        rt_aspng.sql \ 
    4950        $(NULL) 
    5051 
    5152TEST_PROPS = \ 
  • raster/test/regress/rt_aspng.sql

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

    diff -rupN postgis-old/raster/test/regress/rt_aspng_expected postgis-new/raster/test/regress/rt_aspng_expected
    old new  
     1ERROR:  The pixel type of band 1 in the raster is not 8BUI or 16BUI.  The PNG format can only be used with 8BUI and 16BUI pixel types. 
     255279950e29968bcf36b2c11ce8bf88b 
     3ERROR:  The pixel type of band 1 in the raster is not 8BUI or 16BUI.  The PNG format can only be used with 8BUI and 16BUI pixel types. 
     424188762b5745acda4aa7b92776e7280 
     5WARNING:  The PNG format only permits one or three bands.  The first band will be used. 
     6ERROR:  The pixel type of band 1 in the raster is not 8BUI or 16BUI.  The PNG format can only be used with 8BUI and 16BUI pixel types. 
     7WARNING:  The PNG format only permits one or three bands.  The first band will be used. 
     8ERROR:  The pixel type of band 1 in the raster is not 8BUI or 16BUI.  The PNG format can only be used with 8BUI and 16BUI pixel types. 
     9WARNING:  The PNG format only permits one or three bands.  The first band will be used. 
     10ERROR:  The pixel type of band 1 in the raster is not 8BUI or 16BUI.  The PNG format can only be used with 8BUI and 16BUI pixel types. 
     113b859bf98d434fe1cbefb96844badfec 
     12WARNING:  The PNG format only permits one or three bands.  The first band will be used. 
     1358168aa11f7ccdf095e5ed02bde1e676