[postgis-tickets] r16385 - Merge branch '2221_WKB_HexWKB'

Bborie Park dustymugs at dspiral.net
Mon Feb 19 02:06:21 PST 2018


Author: dustymugs
Date: 2018-02-19 14:06:20 -0800 (Mon, 19 Feb 2018)
New Revision: 16385

Added:
   trunk/raster/rt_pg/rtpg_wkb.c
   trunk/raster/test/regress/rt_wkb.sql
   trunk/raster/test/regress/rt_wkb_expected
Modified:
   trunk/doc/reference_raster.xml
   trunk/raster/rt_pg/Makefile.in
   trunk/raster/rt_pg/rtpg_band_properties.c
   trunk/raster/rt_pg/rtpg_inout.c
   trunk/raster/rt_pg/rtpg_mapalgebra.c
   trunk/raster/rt_pg/rtpostgis.sql.in
   trunk/raster/test/regress/Makefile.in
   trunk/raster/test/regress/rt_bytea.sql
Log:
Merge branch '2221_WKB_HexWKB'

Modified: trunk/doc/reference_raster.xml
===================================================================
--- trunk/doc/reference_raster.xml	2018-02-19 17:39:56 UTC (rev 16384)
+++ trunk/doc/reference_raster.xml	2018-02-19 22:06:20 UTC (rev 16385)
@@ -8300,11 +8300,122 @@
 		</refentry>
 	</sect1>
 
+	<sect1 id="Raster_Inputs">
+		<title>Raster Inputs</title>
+		<refentry id="RT_ST_RastFromWKB">
+			<refnamediv>
+				<refname>ST_RastFromWKB</refname>
+				<refpurpose>Return a raster value from a Well-Known Binary (WKB) raster.</refpurpose>
+			</refnamediv>
+
+			<refsynopsisdiv>
+				<funcsynopsis>
+				  <funcprototype>
+						<funcdef>raster <function>ST_RastFromWKB</function></funcdef>
+						<paramdef><type>bytea </type> <parameter>wkb</parameter></paramdef>
+				  </funcprototype>
+				</funcsynopsis>
+			</refsynopsisdiv>
+
+			<refsection>
+				<title>Description</title>
+
+				<para>
+					Given a Well-Known Binary (WKB) raster, return a raster.
+				</para>
+
+				<para>Availability: 2.5.0</para>
+			</refsection>
+
+			<refsection>
+				<title>Examples</title>
+
+				<programlisting>
+SELECT (ST_Metadata(
+	ST_RastFromWKB(
+		'\001\000\000\000\000\000\000\000\000\000\000\000@\000\000\000\000\000\000\010@\000\000\000\000\000\000\340?\000\000\000\000\000\000\340?\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\012\000\000\000\012\000\024\000'::bytea
+	)
+)).* AS metadata;
+
+ upperleftx | upperlefty | width | height | scalex | scaley | skewx | skewy | srid | numbands 
+------------+------------+-------+--------+--------+--------+-------+-------+------+----------
+        0.5 |        0.5 |    10 |     20 |      2 |      3 |     0 |     0 |   10 |        0
+				</programlisting>
+
+			</refsection>
+
+			<refsection>
+				<title>See Also</title>
+				<para>
+					<xref linkend="RT_ST_MetaData" />, 
+					<xref linkend="RT_ST_RastFromHexWKB" />, 
+					<xref linkend="RT_ST_AsBinary" />, 
+					<xref linkend="RT_ST_AsHexWKB" />
+				</para>
+			</refsection>
+		</refentry>
+
+		<refentry id="RT_ST_RastFromHexWKB">
+			<refnamediv>
+				<refname>ST_RastFromHexWKB</refname>
+				<refpurpose>Return a raster value from a Hex representation of Well-Known Binary (WKB) raster.</refpurpose>
+			</refnamediv>
+
+			<refsynopsisdiv>
+				<funcsynopsis>
+				  <funcprototype>
+						<funcdef>raster <function>ST_RastFromHexWKB</function></funcdef>
+						<paramdef><type>text </type> <parameter>wkb</parameter></paramdef>
+				  </funcprototype>
+				</funcsynopsis>
+			</refsynopsisdiv>
+
+			<refsection>
+				<title>Description</title>
+
+				<para>
+					Given a Well-Known Binary (WKB) raster in Hex representation, return a raster.
+				</para>
+
+				<para>Availability: 2.5.0</para>
+			</refsection>
+
+			<refsection>
+				<title>Examples</title>
+
+				<programlisting>
+SELECT (ST_Metadata(
+	ST_RastFromHexWKB(
+		'010000000000000000000000400000000000000840000000000000E03F000000000000E03F000000000000000000000000000000000A0000000A001400'
+	)
+)).* AS metadata;
+
+ upperleftx | upperlefty | width | height | scalex | scaley | skewx | skewy | srid | numbands 
+------------+------------+-------+--------+--------+--------+-------+-------+------+----------
+        0.5 |        0.5 |    10 |     20 |      2 |      3 |     0 |     0 |   10 |        0
+				</programlisting>
+
+			</refsection>
+
+			<refsection>
+				<title>See Also</title>
+				<para>
+					<xref linkend="RT_ST_MetaData" />, 
+					<xref linkend="RT_ST_RastFromWKB" />, 
+					<xref linkend="RT_ST_AsBinary" />, 
+					<xref linkend="RT_ST_AsHexWKB" />
+				</para>
+			</refsection>
+		</refentry>
+
+	</sect1>
+
 	<sect1 id="Raster_Outputs">
 		<title>Raster Outputs</title>
+
 		<refentry id="RT_ST_AsBinary">
 			<refnamediv>
-				<refname>ST_AsBinary</refname>
+				<refname>ST_AsBinary/ST_AsWKB</refname>
 				<refpurpose>Return the Well-Known Binary (WKB) representation of the raster.</refpurpose>
 			</refnamediv>
 
@@ -8315,6 +8426,11 @@
 						<paramdef><type>raster </type> <parameter>rast</parameter></paramdef>
 						<paramdef choice="opt"><type>boolean </type> <parameter>outasin=FALSE</parameter></paramdef>
 				  </funcprototype>
+				  <funcprototype>
+						<funcdef>bytea <function>ST_AsWKB</function></funcdef>
+						<paramdef><type>raster </type> <parameter>rast</parameter></paramdef>
+						<paramdef choice="opt"><type>boolean </type> <parameter>outasin=FALSE</parameter></paramdef>
+				  </funcprototype>
 				</funcsynopsis>
 			</refsynopsisdiv>
 
@@ -8336,6 +8452,7 @@
 				</note>
 
 				<para>Enhanced: 2.1.0 Addition of <varname>outasin</varname></para>
+				<para>Enhanced: 2.5.0 Addition of <varname>ST_AsWKB</varname></para>
 			</refsection>
 
 				<refsection>
@@ -8346,14 +8463,75 @@
 
 					 rastbin
 ---------------------------------------------------------------------------------
-\001\000\000\000\000\000\000\000\000\000\000\000@\000\000\000\000\000\000\010@\
-000\000\000\000\000\000\340?\000\000\000\000\000\000\340?\000\000\000\000\000\00
-0\000\000\000\000\000\000\000\000\000\000\012\000\000\000\012\000\024\000
+\001\000\000\000\000\000\000\000\000\000\000\000@\000\000\000\000\000\000\010@\000\000\000\000\000\000\340?\000\000\000\000\000\000\340?\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\012\000\000\000\012\000\024\000
 				</programlisting>
 
 			</refsection>
+
+			<refsection>
+				<title>See Also</title>
+				<para>
+					<xref linkend="RT_ST_RastFromWKB" />, 
+					<xref linkend="RT_ST_AsHexWKB" />
+				</para>
+			</refsection>
 		</refentry>
 
+		<refentry id="RT_ST_AsHexWKB">
+			<refnamediv>
+				<refname>ST_AsHexWKB</refname>
+				<refpurpose>Return the Well-Known Binary (WKB) in Hex representation of the raster.</refpurpose>
+			</refnamediv>
+
+			<refsynopsisdiv>
+				<funcsynopsis>
+				  <funcprototype>
+						<funcdef>bytea <function>ST_AsHexWKB</function></funcdef>
+						<paramdef><type>raster </type> <parameter>rast</parameter></paramdef>
+						<paramdef choice="opt"><type>boolean </type> <parameter>outasin=FALSE</parameter></paramdef>
+				  </funcprototype>
+				</funcsynopsis>
+			</refsynopsisdiv>
+
+			<refsection>
+				<title>Description</title>
+
+				<para>
+					Returns the Binary representation in Hex representation of the raster. If <varname>outasin</varname> is TRUE, out-db bands are treated as in-db.
+					Refer to raster/doc/RFC2-WellKnownBinaryFormat located in the PostGIS source folder for details of the representation.
+				</para>
+
+				<note>
+					<para>
+						By default, Hex WKB output contains the external file path for out-db bands. If the client does not have access to the raster file underlying an out-db band, set <varname>outasin</varname> to TRUE.</para>
+				</note>
+
+				<para>Availability: 2.5.0 </para>
+			</refsection>
+
+				<refsection>
+					<title>Examples</title>
+
+					<programlisting>
+SELECT ST_AsHexWKB(rast) As rastbin FROM dummy_rast WHERE rid=1;
+
+                                                        st_ashexwkb                                                         
+----------------------------------------------------------------------------------------------------------------------------
+ 010000000000000000000000400000000000000840000000000000E03F000000000000E03F000000000000000000000000000000000A0000000A001400
+				</programlisting>
+
+			</refsection>
+
+			<refsection>
+				<title>See Also</title>
+				<para>
+					<xref linkend="RT_ST_RastFromHexWKB" />, 
+					<xref linkend="RT_ST_AsBinary" />
+				</para>
+			</refsection>
+
+		</refentry>
+
 		<refentry id="RT_ST_AsGDALRaster">
 			<refnamediv>
 				<refname>ST_AsGDALRaster</refname>

Modified: trunk/raster/rt_pg/Makefile.in
===================================================================
--- trunk/raster/rt_pg/Makefile.in	2018-02-19 17:39:56 UTC (rev 16384)
+++ trunk/raster/rt_pg/Makefile.in	2018-02-19 22:06:20 UTC (rev 16385)
@@ -41,6 +41,7 @@
 	rtpg_mapalgebra.o \
 	rtpg_utility.o \
 	rtpg_inout.o \
+	rtpg_wkb.o \
 	rtpg_geometry.o \
 	rtpg_raster_properties.o \
 	rtpg_band_properties.o \

Modified: trunk/raster/rt_pg/rtpg_band_properties.c
===================================================================
--- trunk/raster/rt_pg/rtpg_band_properties.c	2018-02-19 17:39:56 UTC (rev 16384)
+++ trunk/raster/rt_pg/rtpg_band_properties.c	2018-02-19 22:06:20 UTC (rev 16385)
@@ -331,9 +331,7 @@
 		PG_RETURN_NULL();
 	}
 
-	result = (text *) palloc(VARHDRSZ + strlen(bandpath) + 1);
-	SET_VARSIZE(result, VARHDRSZ + strlen(bandpath) + 1);
-	strcpy((char *) VARDATA(result), bandpath);
+	result = cstring_to_text(bandpath);
 
 	rt_band_destroy(band);
 	rt_raster_destroy(raster);

Modified: trunk/raster/rt_pg/rtpg_inout.c
===================================================================
--- trunk/raster/rt_pg/rtpg_inout.c	2018-02-19 17:39:56 UTC (rev 16384)
+++ trunk/raster/rt_pg/rtpg_inout.c	2018-02-19 22:06:20 UTC (rev 16385)
@@ -34,14 +34,14 @@
 
 Datum RASTER_in(PG_FUNCTION_ARGS);
 Datum RASTER_out(PG_FUNCTION_ARGS);
-Datum RASTER_noop(PG_FUNCTION_ARGS);
 
 Datum RASTER_to_bytea(PG_FUNCTION_ARGS);
-Datum RASTER_to_binary(PG_FUNCTION_ARGS);
 
+Datum RASTER_noop(PG_FUNCTION_ARGS);
+
 /**
- * Input is a string with hex chars in it.
- * Convert to binary and put in the result
+ * Input is Hex WKB
+ * Used as the input function of the raster type
  */
 PG_FUNCTION_INFO_V1(RASTER_in);
 Datum RASTER_in(PG_FUNCTION_ARGS)
@@ -66,7 +66,8 @@
 }
 
 /**
- * Given a RASTER structure, convert it to Hex and put it in a string
+ * Output is Hex WKB
+ * Used as the output function of the raster type
  */
 PG_FUNCTION_INFO_V1(RASTER_out);
 Datum RASTER_out(PG_FUNCTION_ARGS)
@@ -84,7 +85,7 @@
 	raster = rt_raster_deserialize(pgraster, FALSE);
 	if (!raster) {
 		PG_FREE_IF_COPY(pgraster, 0);
-		elog(ERROR, "RASTER_out: Could not deserialize raster");
+		elog(ERROR, "RASTER_out: Cannot deserialize raster");
 		PG_RETURN_NULL();
 	}
 
@@ -92,7 +93,7 @@
 	if (!hexwkb) {
 		rt_raster_destroy(raster);
 		PG_FREE_IF_COPY(pgraster, 0);
-		elog(ERROR, "RASTER_out: Could not HEX-WKBize raster");
+		elog(ERROR, "RASTER_out: Cannot HEX-WKBize raster");
 		PG_RETURN_NULL();
 	}
 
@@ -104,7 +105,8 @@
 }
 
 /**
- * Return bytea object with raster in Well-Known-Binary form.
+ * Output is WKB
+ * Used to cast a raster to a bytea
  */
 PG_FUNCTION_INFO_V1(RASTER_to_bytea);
 Datum RASTER_to_bytea(PG_FUNCTION_ARGS)
@@ -123,7 +125,7 @@
 	raster = rt_raster_deserialize(pgraster, FALSE);
 	if (!raster) {
 		PG_FREE_IF_COPY(pgraster, 0);
-		elog(ERROR, "RASTER_to_bytea: Could not deserialize raster");
+		elog(ERROR, "RASTER_to_bytea: Cannot deserialize raster");
 		PG_RETURN_NULL();
 	}
 
@@ -132,7 +134,7 @@
 	if (!wkb) {
 		rt_raster_destroy(raster);
 		PG_FREE_IF_COPY(pgraster, 0);
-		elog(ERROR, "RASTER_to_bytea: Could not allocate and generate WKB data");
+		elog(ERROR, "RASTER_to_bytea: Cannot allocate and generate WKB data");
 		PG_RETURN_NULL();
 	}
 
@@ -151,56 +153,8 @@
 }
 
 /**
- * Return bytea object with raster in Well-Known-Binary form requested using ST_AsBinary function.
+ * Deserialize and then immediately serialize. Debugging function
  */
-PG_FUNCTION_INFO_V1(RASTER_to_binary);
-Datum RASTER_to_binary(PG_FUNCTION_ARGS)
-{
-	rt_pgraster *pgraster = NULL;
-	rt_raster raster = NULL;
-	uint8_t *wkb = NULL;
-	uint32_t wkb_size = 0;
-	char *result = NULL;
-	int result_size = 0;
-	int outasin = FALSE;
-
-	if (PG_ARGISNULL(0)) PG_RETURN_NULL();
-	pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-
-	/* Get raster object */
-	raster = rt_raster_deserialize(pgraster, FALSE);
-	if (!raster) {
-		PG_FREE_IF_COPY(pgraster, 0);
-		elog(ERROR, "RASTER_to_binary: Could not deserialize raster");
-		PG_RETURN_NULL();
-	}
-
-	if (!PG_ARGISNULL(1))
-		outasin = PG_GETARG_BOOL(1);
-
-	/* Parse raster to wkb object */
-	wkb = rt_raster_to_wkb(raster, outasin, &wkb_size);
-	if (!wkb) {
-		rt_raster_destroy(raster);
-		PG_FREE_IF_COPY(pgraster, 0);
-		elog(ERROR, "RASTER_to_binary: Could not allocate and generate WKB data");
-		PG_RETURN_NULL();
-	}
-
-	/* Create varlena object */
-	result_size = wkb_size + VARHDRSZ;
-	result = (char *)palloc(result_size);
-	SET_VARSIZE(result, result_size);
-	memcpy(VARDATA(result), wkb, VARSIZE(result) - VARHDRSZ);
-
-	/* Free raster objects used */
-	rt_raster_destroy(raster);
-	pfree(wkb);
-	PG_FREE_IF_COPY(pgraster, 0);
-
-	PG_RETURN_POINTER(result);
-}
-
 PG_FUNCTION_INFO_V1(RASTER_noop);
 Datum RASTER_noop(PG_FUNCTION_ARGS)
 {
@@ -210,7 +164,7 @@
 	raster = rt_raster_deserialize(pgraster, FALSE);
 	if (!raster) {
 		PG_FREE_IF_COPY(pgraster, 0);
-		elog(ERROR, "RASTER_noop: Could not deserialize raster");
+		elog(ERROR, "RASTER_noop: Cannot deserialize raster");
 		PG_RETURN_NULL();
 	}
 	result = rt_raster_serialize(raster);

Modified: trunk/raster/rt_pg/rtpg_mapalgebra.c
===================================================================
--- trunk/raster/rt_pg/rtpg_mapalgebra.c	2018-02-19 17:39:56 UTC (rev 16384)
+++ trunk/raster/rt_pg/rtpg_mapalgebra.c	2018-02-19 22:06:20 UTC (rev 16385)
@@ -36,7 +36,7 @@
 #include <executor/spi.h>
 #include <utils/lsyscache.h> /* for get_typlenbyvalalign */
 #include <utils/array.h> /* for ArrayType */
-#include <utils/builtins.h>
+#include <utils/builtins.h> /* for cstring_to_text */
 #include <catalog/pg_type.h> /* for INT2OID, INT4OID, FLOAT4OID, FLOAT8OID and TEXTOID */
 #include <executor/executor.h> /* for GetAttributeByName */
 

Copied: trunk/raster/rt_pg/rtpg_wkb.c (from rev 16384, trunk/raster/rt_pg/rtpg_inout.c)
===================================================================
--- trunk/raster/rt_pg/rtpg_wkb.c	                        (rev 0)
+++ trunk/raster/rt_pg/rtpg_wkb.c	2018-02-19 22:06:20 UTC (rev 16385)
@@ -0,0 +1,200 @@
+/*
+ *
+ * WKTRaster - Raster Types for PostGIS
+ * http://trac.osgeo.org/postgis/wiki/WKTRaster
+ *
+ * Copyright (C) 2018-2018 Bborie Park <dustymugs at gmail.com>
+ * Copyright (C) 2011-2013 Regents of the University of California
+ *   <bkpark at ucdavis.edu>
+ * Copyright (C) 2010-2011 Jorge Arevalo <jorge.arevalo at deimos-space.com>
+ * Copyright (C) 2010-2011 David Zwarg <dzwarg at azavea.com>
+ * Copyright (C) 2009-2011 Pierre Racine <pierre.racine at sbf.ulaval.ca>
+ * Copyright (C) 2009-2011 Mateusz Loskot <mateusz at loskot.net>
+ * Copyright (C) 2008-2009 Sandro Santilli <strk at kbt.io>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include <postgres.h> /* for palloc */
+#include <fmgr.h> /* for PG_*, Datum*  */
+#include <utils/builtins.h> /*  for cstring_to_text */
+
+#include "rtpostgis.h"
+
+Datum RASTER_asWKB(PG_FUNCTION_ARGS);
+Datum RASTER_asHexWKB(PG_FUNCTION_ARGS);
+
+Datum RASTER_fromWKB(PG_FUNCTION_ARGS);
+Datum RASTER_fromHexWKB(PG_FUNCTION_ARGS);
+
+/**
+ * Output is WKB
+ */
+PG_FUNCTION_INFO_V1(RASTER_asWKB);
+Datum RASTER_asWKB(PG_FUNCTION_ARGS)
+{
+	rt_pgraster *pgraster = NULL;
+	rt_raster raster = NULL;
+	uint8_t *wkb = NULL;
+	uint32_t wkb_size = 0;
+	char *result = NULL;
+	int result_size = 0;
+	int outasin = FALSE;
+
+	if (PG_ARGISNULL(0)) PG_RETURN_NULL();
+	pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+
+	/* Get raster object */
+	raster = rt_raster_deserialize(pgraster, FALSE);
+	if (!raster) {
+		PG_FREE_IF_COPY(pgraster, 0);
+		elog(ERROR, "RASTER_asWKB: Cannot deserialize raster");
+		PG_RETURN_NULL();
+	}
+
+	if (!PG_ARGISNULL(1))
+		outasin = PG_GETARG_BOOL(1);
+
+	/* Parse raster to wkb object */
+	wkb = rt_raster_to_wkb(raster, outasin, &wkb_size);
+	if (!wkb) {
+		rt_raster_destroy(raster);
+		PG_FREE_IF_COPY(pgraster, 0);
+		elog(ERROR, "RASTER_asWKB: Cannot allocate and generate WKB data");
+		PG_RETURN_NULL();
+	}
+
+	/* Create varlena object */
+	result_size = wkb_size + VARHDRSZ;
+	result = (char *)palloc(result_size);
+	SET_VARSIZE(result, result_size);
+	memcpy(VARDATA(result), wkb, VARSIZE(result) - VARHDRSZ);
+
+	/* Free raster objects used */
+	rt_raster_destroy(raster);
+	pfree(wkb);
+	PG_FREE_IF_COPY(pgraster, 0);
+
+	PG_RETURN_POINTER(result);
+}
+
+/**
+ * Output is Hex WKB
+ */
+PG_FUNCTION_INFO_V1(RASTER_asHexWKB);
+Datum RASTER_asHexWKB(PG_FUNCTION_ARGS)
+{
+	rt_pgraster *pgraster = NULL;
+	rt_raster raster = NULL;
+	int outasin = FALSE;
+	uint32_t hexwkbsize = 0;
+	char *hexwkb = NULL;
+	text *result = NULL;
+
+	POSTGIS_RT_DEBUG(3, "Starting");
+
+	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) {
+		PG_FREE_IF_COPY(pgraster, 0);
+		elog(ERROR, "RASTER_asHexWKB: Cannot deserialize raster");
+		PG_RETURN_NULL();
+	}
+
+	if (!PG_ARGISNULL(1))
+		outasin = PG_GETARG_BOOL(1);
+
+	hexwkb = rt_raster_to_hexwkb(raster, outasin, &hexwkbsize);
+	if (!hexwkb) {
+		rt_raster_destroy(raster);
+		PG_FREE_IF_COPY(pgraster, 0);
+		elog(ERROR, "RASTER_asHexWKB: Cannot allocate and generate Hex WKB data");
+		PG_RETURN_NULL();
+	}
+
+	/* Free the raster objects used */
+	rt_raster_destroy(raster);
+	PG_FREE_IF_COPY(pgraster, 0);
+
+	result = cstring_to_text(hexwkb);
+
+	PG_RETURN_TEXT_P(result);
+}
+
+/**
+ * Input is WKB
+ */
+PG_FUNCTION_INFO_V1(RASTER_fromWKB);
+Datum RASTER_fromWKB(PG_FUNCTION_ARGS)
+{
+	bytea *bytea_data;
+	uint8_t *data;
+	int data_len = 0;
+
+	rt_raster raster;
+	void *result = NULL;
+
+	POSTGIS_RT_DEBUG(3, "Starting");
+
+	bytea_data = (bytea *) PG_GETARG_BYTEA_P(0);
+	data = (uint8_t *) VARDATA(bytea_data);
+	data_len = VARSIZE(bytea_data) - VARHDRSZ;
+
+	raster = rt_raster_from_wkb(data, data_len);
+	PG_FREE_IF_COPY(bytea_data, 0);
+	if (raster == NULL)
+		PG_RETURN_NULL();
+
+	result = rt_raster_serialize(raster);
+	rt_raster_destroy(raster);
+	if (result == NULL)
+		PG_RETURN_NULL();
+
+	SET_VARSIZE(result, ((rt_pgraster*)result)->size);
+	PG_RETURN_POINTER(result);
+}
+
+/**
+ * Input is Hex WKB
+ */
+PG_FUNCTION_INFO_V1(RASTER_fromHexWKB);
+Datum RASTER_fromHexWKB(PG_FUNCTION_ARGS)
+{
+	text *hexwkb_text = PG_GETARG_TEXT_P(0);
+	char *hexwkb;
+
+	rt_raster raster;
+	void *result = NULL;
+
+	POSTGIS_RT_DEBUG(3, "Starting");
+
+	hexwkb = text2cstring(hexwkb_text);
+
+	raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
+	PG_FREE_IF_COPY(hexwkb_text, 0);
+	if (raster == NULL)
+		PG_RETURN_NULL();
+
+	result = rt_raster_serialize(raster);
+	rt_raster_destroy(raster);
+	if (result == NULL)
+		PG_RETURN_NULL();
+
+	SET_VARSIZE(result, ((rt_pgraster*)result)->size);
+	PG_RETURN_POINTER(result);
+}

Modified: trunk/raster/rt_pg/rtpostgis.sql.in
===================================================================
--- trunk/raster/rt_pg/rtpostgis.sql.in	2018-02-19 17:39:56 UTC (rev 16384)
+++ trunk/raster/rt_pg/rtpostgis.sql.in	2018-02-19 22:06:20 UTC (rev 16385)
@@ -79,11 +79,15 @@
 -- RASTER Type
 ------------------------------------------------------------------------------
 
+-- part of Raster type
+-- expects input to be Hex WKB
 CREATE OR REPLACE FUNCTION raster_in(cstring)
     RETURNS raster
     AS 'MODULE_PATHNAME','RASTER_in'
     LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;
 
+-- part of Raster type
+-- expects output to be Hex WKB
 CREATE OR REPLACE FUNCTION raster_out(raster)
     RETURNS cstring
     AS 'MODULE_PATHNAME','RASTER_out'
@@ -156,6 +160,7 @@
 
 -- Availability: 2.0.0
 -- Changed: 2.1.4 raised cost
+-- used to cast raster -> geometry
 CREATE OR REPLACE FUNCTION st_convexhull(raster)
     RETURNS geometry
     AS 'MODULE_PATHNAME','RASTER_convex_hull'
@@ -170,6 +175,7 @@
 	AS 'MODULE_PATHNAME','RASTER_convex_hull'
 	LANGUAGE 'c' IMMUTABLE _PARALLEL;
 
+-- used to cast raster -> box3d
 CREATE OR REPLACE FUNCTION box3d(raster)
     RETURNS box3d
     AS 'select box3d( @extschema at .ST_convexhull($1))'
@@ -5836,14 +5842,49 @@
 	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;
 
 -----------------------------------------------------------------------
+-- Raster Inputs
+-----------------------------------------------------------------------
+
+-- expects input to be WKB
+CREATE OR REPLACE FUNCTION st_rastfromwkb(bytea)
+    RETURNS raster
+    AS 'MODULE_PATHNAME', 'RASTER_fromWKB'
+    LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;
+
+-- expectes input to be Hex WKB
+CREATE OR REPLACE FUNCTION st_rastfromhexwkb(text)
+    RETURNS raster
+    AS 'MODULE_PATHNAME', 'RASTER_fromHexWKB'
+    LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;
+
+-----------------------------------------------------------------------
 -- Raster Outputs
 -----------------------------------------------------------------------
 
+-- Availability: 2.5.0
+-- expects output to be WKB
+-- ST_AsWKB() is functionally identitical to bytea()
+-- kept separate as bytea(raster) is for casting 
+CREATE OR REPLACE FUNCTION st_aswkb(raster, outasin boolean DEFAULT FALSE)
+    RETURNS bytea
+    AS 'MODULE_PATHNAME', 'RASTER_asWKB'
+    LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;
+
+-- Changed: 2.5.0 change logic to use ST_AsWKB. Deprecate in 2.6.0
+-- expects output to be WKB
 CREATE OR REPLACE FUNCTION st_asbinary(raster, outasin boolean DEFAULT FALSE)
     RETURNS bytea
-    AS 'MODULE_PATHNAME', 'RASTER_to_binary'
+		AS $$ SELECT @extschema at .ST_AsWKB($1, $2) $$
+    LANGUAGE 'sql' IMMUTABLE STRICT _PARALLEL;
+
+-- Availability: 2.5.0
+-- expect output to be Hex WKB
+CREATE OR REPLACE FUNCTION st_ashexwkb(raster, outasin boolean DEFAULT FALSE)
+    RETURNS text
+    AS 'MODULE_PATHNAME', 'RASTER_asHexWKB'
     LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;
 
+-- used to cast raster -> bytea
 CREATE OR REPLACE FUNCTION bytea(raster)
     RETURNS bytea
     AS 'MODULE_PATHNAME', 'RASTER_to_bytea'

Modified: trunk/raster/test/regress/Makefile.in
===================================================================
--- trunk/raster/test/regress/Makefile.in	2018-02-19 17:39:56 UTC (rev 16384)
+++ trunk/raster/test/regress/Makefile.in	2018-02-19 22:06:20 UTC (rev 16385)
@@ -57,6 +57,7 @@
 
 TEST_BASIC_FUNC = \
 	rt_bytea \
+	rt_wkb \
 	box3d \
 	rt_addband \
 	rt_band \

Modified: trunk/raster/test/regress/rt_bytea.sql
===================================================================
--- trunk/raster/test/regress/rt_bytea.sql	2018-02-19 17:39:56 UTC (rev 16384)
+++ trunk/raster/test/regress/rt_bytea.sql	2018-02-19 22:06:20 UTC (rev 16385)
@@ -177,42 +177,3 @@
 
 -- Cleanup
 DROP TABLE rt_bytea_test;
-
------------------------------------------------------------------------
---- Test out-db as in-db
------------------------------------------------------------------------
-SET postgis.gdal_enabled_drivers = 'GTiff';
-SET postgis.enable_outdb_rasters = TRUE;
-WITH foo AS (
-	SELECT
-		rid,
-		ST_AsBinary(rast, FALSE) AS outout,
-		ST_AsBinary(rast, TRUE) AS outin
-	FROM raster_outdb_template
-)
-SELECT
-	rid
-FROM foo
-WHERE encode(outout, 'base64') = encode(outin, 'base64');
-WITH foo AS (
-	SELECT
-		rid,
-		rast::bytea AS outbytea,
-		ST_AsBinary(rast, FALSE) AS outout
-	FROM raster_outdb_template
-)
-SELECT
-	rid
-FROM foo
-WHERE encode(outbytea, 'base64') != encode(outout, 'base64');
-WITH foo AS (
-	SELECT
-		rid,
-		rast::bytea AS outbytea,
-		ST_AsBinary(rast, TRUE) AS outin
-	FROM raster_outdb_template
-)
-SELECT
-	rid
-FROM foo
-WHERE encode(outbytea, 'base64') = encode(outin, 'base64');

Added: trunk/raster/test/regress/rt_wkb.sql
===================================================================
--- trunk/raster/test/regress/rt_wkb.sql	                        (rev 0)
+++ trunk/raster/test/regress/rt_wkb.sql	2018-02-19 22:06:20 UTC (rev 16385)
@@ -0,0 +1,179 @@
+SET postgis.gdal_enabled_drivers = 'GTiff';
+SET postgis.enable_outdb_rasters = TRUE;
+
+-----------------------------------------------------------------------
+--- ST_AsWKB
+-----------------------------------------------------------------------
+
+WITH foo AS (
+	SELECT
+		rid,
+		ST_AsWKB(rast, FALSE) AS outout,
+		ST_AsWKB(rast, TRUE) AS outin
+	FROM raster_outdb_template
+)
+SELECT
+	rid
+FROM foo
+WHERE encode(outout, 'base64') = encode(outin, 'base64');
+
+WITH foo AS (
+	SELECT
+		rid,
+		rast::bytea AS outbytea,
+		ST_AsWKB(rast, FALSE) AS outout
+	FROM raster_outdb_template
+)
+SELECT
+	rid
+FROM foo
+WHERE encode(outbytea, 'base64') != encode(outout, 'base64');
+
+WITH foo AS (
+	SELECT
+		rid,
+		rast::bytea AS outbytea,
+		ST_AsWKB(rast, TRUE) AS outin
+	FROM raster_outdb_template
+)
+SELECT
+	rid
+FROM foo
+WHERE encode(outbytea, 'base64') = encode(outin, 'base64');
+
+-----------------------------------------------------------------------
+--- ST_AsBinary
+-----------------------------------------------------------------------
+
+WITH foo AS (
+	SELECT
+		rid,
+		ST_AsBinary(rast, FALSE) AS outout,
+		ST_AsBinary(rast, TRUE) AS outin
+	FROM raster_outdb_template
+)
+SELECT
+	rid
+FROM foo
+WHERE encode(outout, 'base64') = encode(outin, 'base64');
+
+WITH foo AS (
+	SELECT
+		rid,
+		rast::bytea AS outbytea,
+		ST_AsBinary(rast, FALSE) AS outout
+	FROM raster_outdb_template
+)
+SELECT
+	rid
+FROM foo
+WHERE encode(outbytea, 'base64') != encode(outout, 'base64');
+
+WITH foo AS (
+	SELECT
+		rid,
+		rast::bytea AS outbytea,
+		ST_AsBinary(rast, TRUE) AS outin
+	FROM raster_outdb_template
+)
+SELECT
+	rid
+FROM foo
+WHERE encode(outbytea, 'base64') = encode(outin, 'base64');
+
+-----------------------------------------------------------------------
+--- ST_AsHexWKB
+-----------------------------------------------------------------------
+
+WITH foo AS (
+	SELECT
+		rid,
+		ST_AsHexWKB(rast, FALSE) AS outout,
+		ST_AsHexWKB(rast, TRUE) AS outin
+	FROM raster_outdb_template
+)
+SELECT
+	rid
+FROM foo
+WHERE outout = outin;
+
+WITH foo AS (
+	SELECT
+		rid,
+		ST_AsWKB(rast, FALSE) AS outout,
+		ST_AsHexWKB(rast, FALSE) AS outouthex
+	FROM raster_outdb_template
+)
+SELECT
+	rid,
+	encode(outout, 'hex'),
+	outouthex
+FROM foo
+WHERE upper(encode(outout, 'hex')) != upper(outouthex);
+
+WITH foo AS (
+	SELECT
+		rid,
+		ST_AsWKB(rast, TRUE) AS outin,
+		ST_AsHexWKB(rast, TRUE) AS outinhex
+	FROM raster_outdb_template
+)
+SELECT
+	rid,
+	encode(outin, 'hex'),
+	outinhex
+FROM foo
+WHERE upper(encode(outin, 'hex')) != upper(outinhex);
+
+-----------------------------------------------------------------------
+--- ST_FromWKB
+-----------------------------------------------------------------------
+
+WITH foo AS (
+	SELECT
+		rid,
+		ST_AsWKB(rast, FALSE) AS wkb
+	FROM raster_outdb_template
+)
+SELECT
+	rid
+FROM foo
+WHERE encode(ST_AsWKB(ST_RastFromWKB(wkb)), 'base64') != encode(wkb, 'base64');
+
+WITH foo AS (
+	SELECT
+		rid,
+		ST_AsWKB(rast, FALSE) AS wkbout,
+		ST_AsWKB(rast, TRUE) AS wkbin
+	FROM raster_outdb_template
+)
+SELECT
+	rid
+FROM foo
+WHERE encode(ST_AsWKB(ST_RastFromWKB(wkbin)), 'base64') = encode(wkbout, 'base64');
+
+-----------------------------------------------------------------------
+--- ST_FromHexWKB
+-----------------------------------------------------------------------
+WITH foo AS (
+	SELECT
+		rid,
+		ST_AsHexWKB(rast, FALSE) AS hexwkb
+	FROM raster_outdb_template
+)
+SELECT
+	rid
+FROM foo
+WHERE ST_AsHexWKB(ST_RastFromHexWKB(hexwkb)) != hexwkb;
+
+WITH foo AS (
+	SELECT
+		rid,
+		ST_AsHexWKB(rast, FALSE) AS hexwkbout,
+		ST_AsHexWKB(rast, TRUE) AS hexwkbin
+	FROM raster_outdb_template
+)
+SELECT
+	rid
+FROM foo
+WHERE ST_AsHexWKB(ST_RastFromHexWKB(hexwkbin)) = hexwkbout;

Added: trunk/raster/test/regress/rt_wkb_expected
===================================================================


More information about the postgis-tickets mailing list