PostGIS  2.5.0dev-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 
38 Datum RASTER_to_bytea(PG_FUNCTION_ARGS);
39 
40 Datum RASTER_noop(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 
73 Datum RASTER_out(PG_FUNCTION_ARGS)
74 {
75  rt_pgraster *pgraster = NULL;
76  rt_raster raster = NULL;
77  uint32_t hexwkbsize = 0;
78  char *hexwkb = NULL;
79 
80  POSTGIS_RT_DEBUG(3, "Starting");
81 
82  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
83  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
84 
85  raster = rt_raster_deserialize(pgraster, FALSE);
86  if (!raster) {
87  PG_FREE_IF_COPY(pgraster, 0);
88  elog(ERROR, "RASTER_out: Cannot deserialize raster");
89  PG_RETURN_NULL();
90  }
91 
92  hexwkb = rt_raster_to_hexwkb(raster, FALSE, &hexwkbsize);
93  if (!hexwkb) {
94  rt_raster_destroy(raster);
95  PG_FREE_IF_COPY(pgraster, 0);
96  elog(ERROR, "RASTER_out: Cannot HEX-WKBize raster");
97  PG_RETURN_NULL();
98  }
99 
100  /* Free the raster objects used */
101  rt_raster_destroy(raster);
102  PG_FREE_IF_COPY(pgraster, 0);
103 
104  PG_RETURN_CSTRING(hexwkb);
105 }
106 
112 Datum RASTER_to_bytea(PG_FUNCTION_ARGS)
113 {
114  rt_pgraster *pgraster = NULL;
115  rt_raster raster = NULL;
116  uint8_t *wkb = NULL;
117  uint32_t wkb_size = 0;
118  bytea *result = NULL;
119  int result_size = 0;
120 
121  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
122  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
123 
124  /* Get raster object */
125  raster = rt_raster_deserialize(pgraster, FALSE);
126  if (!raster) {
127  PG_FREE_IF_COPY(pgraster, 0);
128  elog(ERROR, "RASTER_to_bytea: Cannot deserialize raster");
129  PG_RETURN_NULL();
130  }
131 
132  /* Parse raster to wkb object */
133  wkb = rt_raster_to_wkb(raster, FALSE, &wkb_size);
134  if (!wkb) {
135  rt_raster_destroy(raster);
136  PG_FREE_IF_COPY(pgraster, 0);
137  elog(ERROR, "RASTER_to_bytea: Cannot allocate and generate WKB data");
138  PG_RETURN_NULL();
139  }
140 
141  /* Create varlena object */
142  result_size = wkb_size + VARHDRSZ;
143  result = (bytea *)palloc(result_size);
144  SET_VARSIZE(result, result_size);
145  memcpy(VARDATA(result), wkb, VARSIZE(result) - VARHDRSZ);
146 
147  /* Free raster objects used */
148  rt_raster_destroy(raster);
149  pfree(wkb);
150  PG_FREE_IF_COPY(pgraster, 0);
151 
152  PG_RETURN_POINTER(result);
153 }
154 
159 Datum RASTER_noop(PG_FUNCTION_ARGS)
160 {
162  rt_pgraster *pgraster, *result;
163  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
164  raster = rt_raster_deserialize(pgraster, FALSE);
165  if (!raster) {
166  PG_FREE_IF_COPY(pgraster, 0);
167  elog(ERROR, "RASTER_noop: Cannot deserialize raster");
168  PG_RETURN_NULL();
169  }
170  result = rt_raster_serialize(raster);
171  rt_raster_destroy(raster);
172  if (result == NULL)
173  PG_RETURN_NULL();
174 
175  SET_VARSIZE(result, raster->size);
176  PG_RETURN_POINTER(result);
177 }
178 
Datum RASTER_to_bytea(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:112
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:2265
unsigned int uint32_t
Definition: uthash.h:78
PG_FUNCTION_INFO_V1(RASTER_in)
Input is Hex WKB Used as the input function of the raster type.
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:159
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:2230
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:61
unsigned char uint8_t
Definition: uthash.h:79
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:73