/* Get pixel geographical shape */
Datum RASTER_getPixelPolygons(PG_FUNCTION_ARGS);
+/* Get raster band's surface */
+Datum RASTER_getBandSurface(PG_FUNCTION_ARGS);
+
/* Get pixels of value */
Datum RASTER_pixelOfValue(PG_FUNCTION_ARGS);
}
}
+/**
+ * Get raster band's surface
+ */
+PG_FUNCTION_INFO_V1(RASTER_getBandSurface);
+Datum RASTER_getBandSurface(PG_FUNCTION_ARGS)
+{
+ rt_pgraster *pgraster = NULL;
+ rt_raster raster = NULL;
+ int num_bands = 0;
+ int nband = 1;
+ LWMPOLY *surface = NULL;
+ GSERIALIZED *rtn = NULL;
+
+ /* raster */
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+ pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+
+ raster = rt_raster_deserialize(pgraster, FALSE);
+ if (!raster) {
+ elog(ERROR, "RASTER_getBandSurface: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
+
+ /* num_bands */
+ num_bands = rt_raster_get_num_bands(raster);
+ if (num_bands < 1) {
+ elog(NOTICE, "Raster provided has no bands");
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
+
+ /* band index is 1-based */
+ if (!PG_ARGISNULL(1))
+ nband = PG_GETARG_INT32(1);
+ if (nband < 1 || nband > num_bands) {
+ elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
+
+ /* get band surface */
+ surface = rt_raster_surface(raster, nband - 1);
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+
+ if (surface == NULL) {
+ elog(ERROR, "RASTER_getBandSurface: Could not get raster band's surface");
+ PG_RETURN_NULL();
+ }
+
+ rtn = geometry_serialize(lwmpoly_as_lwgeom(surface));
+ lwmpoly_free(surface);
+
+ PG_RETURN_POINTER(rtn);
+}
+
/**
* Get pixels of value
*/
--- /dev/null
+DROP TABLE IF EXISTS raster_surface;
+CREATE TABLE raster_surface (
+ rast raster
+);
+CREATE OR REPLACE FUNCTION make_test_raster()
+ RETURNS void
+ AS $$
+ DECLARE
+ width int := 5;
+ height int := 5;
+ x int;
+ y int;
+ rast raster;
+ BEGIN
+ rast := ST_MakeEmptyRaster(width, height, 0, 0, 1, -1, 0, 0, 0);
+ rast := ST_AddBand(rast, 1, '32BUI', 1, 0);
+
+ INSERT INTO raster_surface VALUES (rast);
+
+ RETURN;
+ END;
+ $$ LANGUAGE 'plpgsql';
+SELECT make_test_raster();
+DROP FUNCTION make_test_raster();
+
+SELECT
+ ST_AsText(ST_BandSurface(rast))
+FROM raster_surface;
+
+SELECT
+ ST_AsText(ST_BandSurface(rast))
+FROM (
+ SELECT
+ ST_SetValue(
+ rast, 1, 1, 1, 0
+ ) AS rast
+ FROM raster_surface
+) foo;
+
+SELECT
+ ST_AsText(ST_BandSurface(rast))
+FROM (
+ SELECT
+ ST_SetValue(
+ ST_SetValue(
+ rast, 1, 1, 1, 0
+ ),
+ 1, 2, 2, 0
+ ) AS rast
+ FROM raster_surface
+) foo;
+
+SELECT
+ ST_AsText(ST_BandSurface(rast))
+FROM (
+ SELECT
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ rast, 1, 1, 1, 0
+ ),
+ 1, 2, 2, 0
+ ),
+ 1, 3, 3, 0
+ ) AS rast
+ FROM raster_surface
+) foo;
+
+SELECT
+ ST_AsText(ST_BandSurface(rast))
+FROM (
+ SELECT
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ rast, 1, 1, 1, 0
+ ),
+ 1, 2, 2, 0
+ ),
+ 1, 3, 3, 0
+ ),
+ 1, 4, 4, 0
+ ) AS rast
+ FROM raster_surface
+) foo;
+
+SELECT
+ ST_AsText(ST_BandSurface(rast))
+FROM (
+ SELECT
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ rast, 1, 1, 1, 0
+ ),
+ 1, 2, 2, 0
+ ),
+ 1, 3, 3, 0
+ ),
+ 1, 4, 4, 0
+ ),
+ 1, 5, 5, 0
+ ) AS rast
+ FROM raster_surface
+) foo;
+
+SELECT
+ ST_AsText(ST_BandSurface(rast))
+FROM (
+ SELECT
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ ST_SetValue(
+ rast, 1, 1, 1, 0
+ ),
+ 1, 2, 2, 0
+ ),
+ 1, 3, 3, 0
+ ),
+ 1, 4, 4, 0
+ ),
+ 1, 5, 5, 0
+ ),
+ 1, 5, 1, 0
+ ),
+ 1, 4, 2, 0
+ ),
+ 1, 2, 4, 0
+ ),
+ 1, 1, 5, 0
+ ) AS rast
+ FROM raster_surface
+) foo;
+
+DROP TABLE IF EXISTS raster_surface;
--- /dev/null
+NOTICE: table "raster_surface" does not exist, skipping
+MULTIPOLYGON(((0 0,1 0,2 0,3 0,4 0,5 0,5 -1,5 -2,5 -3,5 -4,5 -5,4 -5,3 -5,2 -5,1 -5,0 -5,0 -4,0 -3,0 -2,0 -1,0 0)))
+MULTIPOLYGON(((1 0,2 0,3 0,4 0,5 0,5 -1,5 -2,5 -3,5 -4,5 -5,4 -5,3 -5,2 -5,1 -5,0 -5,0 -4,0 -3,0 -2,0 -1,1 -1,1 0)))
+MULTIPOLYGON(((1 0,2 0,3 0,4 0,5 0,5 -1,5 -2,5 -3,5 -4,5 -5,4 -5,3 -5,2 -5,1 -5,0 -5,0 -4,0 -3,0 -2,0 -1,1 -1,1 0),(1 -1,1 -2,2 -2,2 -1,1 -1)))
+MULTIPOLYGON(((1 0,2 0,3 0,4 0,5 0,5 -1,5 -2,5 -3,5 -4,5 -5,4 -5,3 -5,2 -5,1 -5,0 -5,0 -4,0 -3,0 -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