PostGIS  3.3.9dev-r@@SVN_REVISION@@
rtpg_inout.c
Go to the documentation of this file.
1 /*
2  *
3  * WKTRaster - Raster Types for PostGIS
4  * http://trac.osgeo.org/postgis/wiki/WKTRaster
5  *
6  * Copyright (C) 2011-2013 Regents of the University of California
7  * <bkpark@ucdavis.edu>
8  * Copyright (C) 2010-2011 Jorge Arevalo <jorge.arevalo@deimos-space.com>
9  * Copyright (C) 2010-2011 David Zwarg <dzwarg@azavea.com>
10  * Copyright (C) 2009-2011 Pierre Racine <pierre.racine@sbf.ulaval.ca>
11  * Copyright (C) 2009-2011 Mateusz Loskot <mateusz@loskot.net>
12  * Copyright (C) 2008-2009 Sandro Santilli <strk@kbt.io>
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software Foundation,
26  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27  *
28  */
29 
30 #include <postgres.h>
31 #include <fmgr.h>
32 #include "rtpostgis.h"
33 
34 Datum RASTER_in(PG_FUNCTION_ARGS);
35 Datum RASTER_out(PG_FUNCTION_ARGS);
36 
37 Datum RASTER_to_bytea(PG_FUNCTION_ARGS);
38 
39 Datum RASTER_noop(PG_FUNCTION_ARGS);
40 
46 Datum RASTER_in(PG_FUNCTION_ARGS)
47 {
49  char *hexwkb = PG_GETARG_CSTRING(0);
50  void *result = NULL;
51 
52  POSTGIS_RT_DEBUG(3, "Starting");
53 
54  raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
55  if (raster == NULL)
56  PG_RETURN_NULL();
57 
60  if (result == NULL)
61  PG_RETURN_NULL();
62 
63  SET_VARSIZE(result, ((rt_pgraster*)result)->size);
64  PG_RETURN_POINTER(result);
65 }
66 
72 Datum RASTER_out(PG_FUNCTION_ARGS)
73 {
74  rt_pgraster *pgraster = NULL;
75  rt_raster raster = NULL;
76  uint32_t hexwkbsize = 0;
77  char *hexwkb = NULL;
78 
79  POSTGIS_RT_DEBUG(3, "Starting");
80 
81  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
82  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
83 
84  raster = rt_raster_deserialize(pgraster, FALSE);
85  if (!raster) {
86  PG_FREE_IF_COPY(pgraster, 0);
87  elog(ERROR, "RASTER_out: Cannot deserialize raster");
88  PG_RETURN_NULL();
89  }
90 
91  hexwkb = rt_raster_to_hexwkb(raster, FALSE, &hexwkbsize);
92  if (!hexwkb) {
94  PG_FREE_IF_COPY(pgraster, 0);
95  elog(ERROR, "RASTER_out: Cannot HEX-WKBize raster");
96  PG_RETURN_NULL();
97  }
98 
99  /* Free the raster objects used */
101  PG_FREE_IF_COPY(pgraster, 0);
102 
103  PG_RETURN_CSTRING(hexwkb);
104 }
105 
111 Datum RASTER_to_bytea(PG_FUNCTION_ARGS)
112 {
113  rt_pgraster *pgraster = NULL;
114  rt_raster raster = NULL;
115  uint8_t *wkb = NULL;
116  uint32_t wkb_size = 0;
117  bytea *result = NULL;
118  int result_size = 0;
119 
120  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
121  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
122 
123  /* Get raster object */
124  raster = rt_raster_deserialize(pgraster, FALSE);
125  if (!raster) {
126  PG_FREE_IF_COPY(pgraster, 0);
127  elog(ERROR, "RASTER_to_bytea: Cannot deserialize raster");
128  PG_RETURN_NULL();
129  }
130 
131  /* Parse raster to wkb object */
132  wkb = rt_raster_to_wkb(raster, FALSE, &wkb_size);
133  if (!wkb) {
135  PG_FREE_IF_COPY(pgraster, 0);
136  elog(ERROR, "RASTER_to_bytea: Cannot allocate and generate WKB data");
137  PG_RETURN_NULL();
138  }
139 
140  /* Create varlena object */
141  result_size = wkb_size + VARHDRSZ;
142  result = (bytea *)palloc(result_size);
143  SET_VARSIZE(result, result_size);
144  memcpy(VARDATA(result), wkb, VARSIZE_ANY_EXHDR(result));
145 
146  /* Free raster objects used */
148  pfree(wkb);
149  PG_FREE_IF_COPY(pgraster, 0);
150 
151  PG_RETURN_POINTER(result);
152 }
153 
158 Datum RASTER_noop(PG_FUNCTION_ARGS)
159 {
161  rt_pgraster *pgraster, *result;
162  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
163  raster = rt_raster_deserialize(pgraster, FALSE);
164  if (!raster) {
165  PG_FREE_IF_COPY(pgraster, 0);
166  elog(ERROR, "RASTER_noop: Cannot deserialize raster");
167  PG_RETURN_NULL();
168  }
171  if (result == NULL)
172  PG_RETURN_NULL();
173 
174  SET_VARSIZE(result, raster->size);
175  PG_RETURN_POINTER(result);
176 }
177 
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
#define FALSE
Definition: dbfopen.c:72
char * rt_raster_to_hexwkb(rt_raster raster, int outasin, uint32_t *hexwkbsize)
Return this raster in HEXWKB form (null-terminated hex)
Definition: rt_wkb.c:679
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:86
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
uint8_t * rt_raster_to_wkb(rt_raster raster, int outasin, uint32_t *wkbsize)
Return this raster in WKB form.
Definition: rt_wkb.c:494
rt_raster rt_raster_from_hexwkb(const char *hexwkb, uint32_t hexwkbsize)
Construct an rt_raster from a text HEXWKB representation.
Definition: rt_wkb.c:406
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:725
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
Datum RASTER_to_bytea(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:111
PG_FUNCTION_INFO_V1(RASTER_in)
Input is Hex WKB Used as the input function of the raster type.
Datum RASTER_out(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:72
Datum RASTER_in(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:46
Datum RASTER_noop(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:158
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:69
Struct definitions.
Definition: librtcore.h:2396