File: st_asraster.sql

package info (click to toggle)
postgis 3.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 87,748 kB
  • sloc: ansic: 158,671; sql: 91,546; xml: 54,004; cpp: 12,339; sh: 5,187; perl: 5,100; makefile: 3,085; python: 1,205; yacc: 447; lex: 151; javascript: 6
file content (49 lines) | stat: -rw-r--r-- 2,063 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
----------------------------------------------------------------------
--
--
-- Copyright (c) 2009-2010 Pierre Racine <pierre.racine@sbf.ulaval.ca>
--
----------------------------------------------------------------------

-- NOTE: The ST_AsRaster() function is already implemented in C. This plpgsql script is provided only as an example.
-- Defining the plpgsql function below might overwrite the current C implementation and brake other functions dependent on it.
-- Use with caution.

CREATE OR REPLACE FUNCTION ST_AsRaster(geom geometry, rast raster, pixeltype text, nodatavalue float8, val float8)
    RETURNS raster AS
    $$
    DECLARE
    numband int := ST_NumBands(rast);
        x1w float8 := ST_XMin(geom);
        y1w float8 := ST_YMax(geom);
        x2w float8 := ST_XMax(geom);
        y2w float8 := ST_YMin(geom);
        x1r int := ST_World2RasterCoordX(rast, x1w, y1w);
        y1r int := ST_World2RasterCoordY(rast, x1w, y1w);
        x2r int := ST_World2RasterCoordX(rast, x2w, y2w);
        y2r int := ST_World2RasterCoordY(rast, x2w, y2w);
        newx1w float8 := ST_Raster2WorldCoordX(rast, x1r, y1r);
        newy1w float8 := ST_Raster2WorldCoordY(rast, x1r, y1r);
        newwidth int := abs(x2r - x1r) + 1;
        newheight int := abs(y2r - y1r) + 1;
        newrast raster := ST_AddBand(ST_MakeEmptyRaster(newwidth, newheight, newx1w, newy1w, ST_ScaleX(rast), ST_ScaleY(rast), ST_SkewX(rast), ST_SkewY(rast), ST_SRID(rast)), pixeltype, nodatavalue, nodatavalue);
    BEGIN
    FOR x IN 1..newwidth LOOP
        FOR y IN 1..newheight LOOP
            IF ST_Intersects(geom, ST_Centroid(ST_PixelAsPolygon(newrast, x, y))) THEN
                newrast := ST_SetValue(newrast, 1, x, y, val);
            END IF;
        END LOOP;
    END LOOP;
    RETURN newrast;
    END;
    $$
    LANGUAGE 'plpgsql';

-- Test

SELECT (gv).geom, (gv).val
FROM (SELECT (ST_PixelAsPolygons(rast, 1)) gv
FROM (SELECT ST_AsRaster(the_geom, (SELECT rast FROM srtm_tiled_100x100 LIMIT 1), '8BSI', -1, 1) rast
FROM realcaribou_buffers_wgs
LIMIT 10) foo) foi