PostGIS  2.3.8dev-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 
33 #include "rtpostgis.h"
34 
35 Datum RASTER_in(PG_FUNCTION_ARGS);
36 Datum RASTER_out(PG_FUNCTION_ARGS);
37 Datum RASTER_noop(PG_FUNCTION_ARGS);
38 
39 Datum RASTER_to_bytea(PG_FUNCTION_ARGS);
40 Datum RASTER_to_binary(PG_FUNCTION_ARGS);
41 
47 Datum RASTER_in(PG_FUNCTION_ARGS)
48 {
50  char *hexwkb = PG_GETARG_CSTRING(0);
51  void *result = NULL;
52 
53  POSTGIS_RT_DEBUG(3, "Starting");
54 
55  raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
56  if (raster == NULL)
57  PG_RETURN_NULL();
58 
59  result = rt_raster_serialize(raster);
60  rt_raster_destroy(raster);
61  if (result == NULL)
62  PG_RETURN_NULL();
63 
64  SET_VARSIZE(result, ((rt_pgraster*)result)->size);
65  PG_RETURN_POINTER(result);
66 }
67 
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: Could not deserialize raster");
88  PG_RETURN_NULL();
89  }
90 
91  hexwkb = rt_raster_to_hexwkb(raster, FALSE, &hexwkbsize);
92  if (!hexwkb) {
93  rt_raster_destroy(raster);
94  PG_FREE_IF_COPY(pgraster, 0);
95  elog(ERROR, "RASTER_out: Could not HEX-WKBize raster");
96  PG_RETURN_NULL();
97  }
98 
99  /* Free the raster objects used */
100  rt_raster_destroy(raster);
101  PG_FREE_IF_COPY(pgraster, 0);
102 
103  PG_RETURN_CSTRING(hexwkb);
104 }
105 
110 Datum RASTER_to_bytea(PG_FUNCTION_ARGS)
111 {
112  rt_pgraster *pgraster = NULL;
113  rt_raster raster = NULL;
114  uint8_t *wkb = NULL;
115  uint32_t wkb_size = 0;
116  bytea *result = NULL;
117  int result_size = 0;
118 
119  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
120  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
121 
122  /* Get raster object */
123  raster = rt_raster_deserialize(pgraster, FALSE);
124  if (!raster) {
125  PG_FREE_IF_COPY(pgraster, 0);
126  elog(ERROR, "RASTER_to_bytea: Could not deserialize raster");
127  PG_RETURN_NULL();
128  }
129 
130  /* Parse raster to wkb object */
131  wkb = rt_raster_to_wkb(raster, FALSE, &wkb_size);
132  if (!wkb) {
133  rt_raster_destroy(raster);
134  PG_FREE_IF_COPY(pgraster, 0);
135  elog(ERROR, "RASTER_to_bytea: Could not allocate and generate WKB data");
136  PG_RETURN_NULL();
137  }
138 
139  /* Create varlena object */
140  result_size = wkb_size + VARHDRSZ;
141  result = (bytea *)palloc(result_size);
142  SET_VARSIZE(result, result_size);
143  memcpy(VARDATA(result), wkb, VARSIZE(result) - VARHDRSZ);
144 
145  /* Free raster objects used */
146  rt_raster_destroy(raster);
147  pfree(wkb);
148  PG_FREE_IF_COPY(pgraster, 0);
149 
150  PG_RETURN_POINTER(result);
151 }
152 
157 Datum RASTER_to_binary(PG_FUNCTION_ARGS)
158 {
159  rt_pgraster *pgraster = NULL;
160  rt_raster raster = NULL;
161  uint8_t *wkb = NULL;
162  uint32_t wkb_size = 0;
163  char *result = NULL;
164  int result_size = 0;
165  int outasin = FALSE;
166 
167  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
168  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
169 
170  /* Get raster object */
171  raster = rt_raster_deserialize(pgraster, FALSE);
172  if (!raster) {
173  PG_FREE_IF_COPY(pgraster, 0);
174  elog(ERROR, "RASTER_to_binary: Could not deserialize raster");
175  PG_RETURN_NULL();
176  }
177 
178  if (!PG_ARGISNULL(1))
179  outasin = PG_GETARG_BOOL(1);
180 
181  /* Parse raster to wkb object */
182  wkb = rt_raster_to_wkb(raster, outasin, &wkb_size);
183  if (!wkb) {
184  rt_raster_destroy(raster);
185  PG_FREE_IF_COPY(pgraster, 0);
186  elog(ERROR, "RASTER_to_binary: Could not allocate and generate WKB data");
187  PG_RETURN_NULL();
188  }
189 
190  /* Create varlena object */
191  result_size = wkb_size + VARHDRSZ;
192  result = (char *)palloc(result_size);
193  SET_VARSIZE(result, result_size);
194  memcpy(VARDATA(result), wkb, VARSIZE(result) - VARHDRSZ);
195 
196  /* Free raster objects used */
197  rt_raster_destroy(raster);
198  pfree(wkb);
199  PG_FREE_IF_COPY(pgraster, 0);
200 
201  PG_RETURN_POINTER(result);
202 }
203 
205 Datum RASTER_noop(PG_FUNCTION_ARGS)
206 {
208  rt_pgraster *pgraster, *result;
209  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
210  raster = rt_raster_deserialize(pgraster, FALSE);
211  if (!raster) {
212  PG_FREE_IF_COPY(pgraster, 0);
213  elog(ERROR, "RASTER_noop: Could not deserialize raster");
214  PG_RETURN_NULL();
215  }
216  result = rt_raster_serialize(raster);
217  rt_raster_destroy(raster);
218  if (result == NULL)
219  PG_RETURN_NULL();
220 
221  SET_VARSIZE(result, raster->size);
222  PG_RETURN_POINTER(result);
223 }
224 
Datum RASTER_to_bytea(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:110
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
Datum RASTER_in(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:47
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
uint32_t size
Definition: librtcore.h:2248
PG_FUNCTION_INFO_V1(RASTER_in)
Input is a string with hex chars in it.
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:669
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
Datum RASTER_noop(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:205
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
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2213
Datum RASTER_to_binary(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:157
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:53
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:717
Datum RASTER_out(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:72