[postgis-commits] svn - r3786 - spike/wktraster/rt_pg

postgis-commits at postgis.refractions.net postgis-commits at postgis.refractions.net
Tue Mar 3 08:20:37 PST 2009


Author: mloskot
Date: 2009-03-03 08:20:36 -0800 (Tue, 03 Mar 2009)
New Revision: 3786

Modified:
   spike/wktraster/rt_pg/rt_pg.c
   spike/wktraster/rt_pg/rtpostgis.sql.in.c
Log:
Implemented RT_Value function to return single pixel value of specified band and in given location (coords). Added a stub for RT_SetValue - currently it returns what it gets, no real writing is performed. TODO: What RT_SetValue is supposed to return. Changed severity of number of NOTICE messages to ERROR.

Modified: spike/wktraster/rt_pg/rt_pg.c
===================================================================
--- spike/wktraster/rt_pg/rt_pg.c	2009-02-28 18:55:15 UTC (rev 3785)
+++ spike/wktraster/rt_pg/rt_pg.c	2009-03-03 16:20:36 UTC (rev 3786)
@@ -94,6 +94,8 @@
 Datum RASTER_getBandPixelType(PG_FUNCTION_ARGS);
 Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS);
 Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS);
+Datum RASTER_getPixelValue(PG_FUNCTION_ARGS);
+Datum RASTER_setPixelValue(PG_FUNCTION_ARGS);
 
 Datum RASTER_send(PG_FUNCTION_ARGS);
 Datum RASTER_dump(PG_FUNCTION_ARGS);
@@ -759,7 +761,7 @@
     /* Index is 1-based */
     index = PG_GETARG_INT32(1);
     if ( index < 1 ) {
-        elog(NOTICE, "Invalid band index (must use 1-based)");
+        elog(ERROR, "Invalid band index (must use 1-based)");
         PG_RETURN_NULL();
     }
     assert(0 <= (index - 1));
@@ -778,7 +780,7 @@
     band = rt_raster_get_band(ctx, raster, index - 1);
     if ( ! band ) {
         /* TODO: Throw rrror or notice or nothing? */
-        elog(NOTICE, "Could not find raster band of index %d", index);
+        elog(ERROR, "Could not find raster band of index %d", index);
         PG_RETURN_NULL();
     }
 
@@ -808,7 +810,7 @@
     /* Index is 1-based */
     index = PG_GETARG_INT32(1);
     if ( index < 1 ) {
-        elog(NOTICE, "Invalid band index (must use 1-based)");
+        elog(ERROR, "Invalid band index (must use 1-based)");
         PG_RETURN_NULL();
     }
     assert(0 <= (index - 1));
@@ -827,7 +829,7 @@
     band = rt_raster_get_band(ctx, raster, index - 1);
     if ( ! band ) {
         /* TODO: Throw rrror or notice or nothing? */
-        elog(NOTICE, "Could not find raster band of index %d", index);
+        elog(ERROR, "Could not find raster band of index %d", index);
         PG_RETURN_NULL();
     }
 
@@ -909,6 +911,7 @@
     /* Index is 1-based */
     index = PG_GETARG_INT32(1);
     if ( index < 1 ) {
+        elog(ERROR, "Invalid band index (must use 1-based)");
         PG_RETURN_NULL();
     }
     assert(0 <= (index - 1));
@@ -927,7 +930,7 @@
     band = rt_raster_get_band(ctx, raster, index - 1);
     if ( ! band ) {
         /* TODO: Throw rrror or notice or nothing? */
-        elog(NOTICE, "Could not find raster band of index %d", index);
+        elog(ERROR, "Could not find raster band of index %d", index);
         PG_RETURN_NULL();
     }
 
@@ -935,6 +938,73 @@
     PG_RETURN_FLOAT4(nodata);
 }
 
+/**
+ * Fetch value of raster sample in specified band.
+ *
+ * TODO: Should we returen NUMERIC instead of FLOAT8 ?
+ */
+PG_FUNCTION_INFO_V1(RASTER_getPixelValue);
+Datum RASTER_getPixelValue(PG_FUNCTION_ARGS)
+{
+    rt_pgraster *pgraster = NULL;
+    rt_raster raster = NULL;
+    rt_band band = NULL;
+    rt_context ctx = NULL;
+    double pixvalue = 0;
+    int32_t nband = 0;
+    int32_t x = 0;
+    int32_t y = 0;
+    uint16_t width = 0;
+    uint16_t height = 0;
+
+    /* Index is 1-based */
+    nband = PG_GETARG_INT32(1);
+    if ( nband < 1 ) {
+        elog(ERROR, "Invalid band index (must use 1-based)");
+        PG_RETURN_NULL();
+    }
+    assert(0 <= (nband - 1));
+    
+    /* Validate pixel coordinates are in range */
+    x = PG_GETARG_INT32(2);
+    y = PG_GETARG_INT32(3);
+    elog(NOTICE, "Pixel coordinates (%d, %d)", x, y);
+
+    /* Deserialize raster */
+    pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+    ctx = get_rt_context(fcinfo);
+
+    raster = rt_raster_deserialize(ctx, pgraster);
+    if ( ! raster ) {
+        elog(ERROR, "Could not deserialize raster");
+        PG_RETURN_NULL();
+    }
+
+    /* Fetch requested band */
+    band = rt_raster_get_band(ctx, raster, nband - 1);
+    if ( ! band ) {
+        /* TODO: Throw rrror or notice or nothing? */
+        elog(ERROR, "Could not find raster band of index %d", index);
+        PG_RETURN_NULL();
+    }
+
+    pixvalue = rt_band_get_pixel(ctx, band, x, y);
+    PG_RETURN_FLOAT8(pixvalue);
+}
+
+/**
+ * Write value of raster sample on given position and in specified band.
+ */
+PG_FUNCTION_INFO_V1(RASTER_setPixelValue);
+Datum RASTER_setPixelValue(PG_FUNCTION_ARGS)
+{
+    double pixvalue = 0;
+    
+    /* TODO: TO BE IMPLEMENTED */
+    pixvalue = PG_GETARG_FLOAT8(4);
+    PG_RETURN_FLOAT8(pixvalue);
+}
+
 /* ---------------------------------------------------------------- */
 /*  Memory allocation / error reporting hooks                       */
 /* ---------------------------------------------------------------- */

Modified: spike/wktraster/rt_pg/rtpostgis.sql.in.c
===================================================================
--- spike/wktraster/rt_pg/rtpostgis.sql.in.c	2009-02-28 18:55:15 UTC (rev 3785)
+++ spike/wktraster/rt_pg/rtpostgis.sql.in.c	2009-03-03 16:20:36 UTC (rev 3786)
@@ -140,7 +140,16 @@
 	AS 'MODULE_PATHNAME','RASTER_getBandNoDataValue'
 	LANGUAGE 'C' _IMMUTABLE_STRICT; 
 
+CREATEFUNCTION rt_value(raster, integer, integer, integer)
+	RETURNS float8
+	AS 'MODULE_PATHNAME','RASTER_getPixelValue'
+	LANGUAGE 'C' _IMMUTABLE_STRICT; 
 
+-- TODO: To be implemented
+CREATEFUNCTION rt_setvalue(raster, integer, integer, integer, float8)
+	RETURNS float8
+	AS 'MODULE_PATHNAME','RASTER_setPixelValue'
+	LANGUAGE 'C' _IMMUTABLE_STRICT; 
 -- 
 -- rt_MakeEmptyRaster( <width>, <height>, <ipx>, <ipy>,
 --                                        <scalex>, <scaley>,



More information about the postgis-commits mailing list