PostGIS  2.5.7dev-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 
41 Datum RASTER_to_binary(PG_FUNCTION_ARGS);
42 
43 Datum RASTER_noop(PG_FUNCTION_ARGS);
44 
50 Datum RASTER_to_binary(PG_FUNCTION_ARGS)
51 {
52 
53  elog(ERROR, "RASTER_to_binary: This function is out of date. Run ALTER EXTENSION postgis UPDATE; to fix");
54 
55 }
56 
62 Datum RASTER_in(PG_FUNCTION_ARGS)
63 {
65  char *hexwkb = PG_GETARG_CSTRING(0);
66  void *result = NULL;
67 
68  POSTGIS_RT_DEBUG(3, "Starting");
69 
70  raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
71  if (raster == NULL)
72  PG_RETURN_NULL();
73 
74  result = rt_raster_serialize(raster);
76  if (result == NULL)
77  PG_RETURN_NULL();
78 
79  SET_VARSIZE(result, ((rt_pgraster*)result)->size);
80  PG_RETURN_POINTER(result);
81 }
82 
88 Datum RASTER_out(PG_FUNCTION_ARGS)
89 {
90  rt_pgraster *pgraster = NULL;
91  rt_raster raster = NULL;
92  uint32_t hexwkbsize = 0;
93  char *hexwkb = NULL;
94 
95  POSTGIS_RT_DEBUG(3, "Starting");
96 
97  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
98  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
99 
100  raster = rt_raster_deserialize(pgraster, FALSE);
101  if (!raster) {
102  PG_FREE_IF_COPY(pgraster, 0);
103  elog(ERROR, "RASTER_out: Cannot deserialize raster");
104  PG_RETURN_NULL();
105  }
106 
107  hexwkb = rt_raster_to_hexwkb(raster, FALSE, &hexwkbsize);
108  if (!hexwkb) {
110  PG_FREE_IF_COPY(pgraster, 0);
111  elog(ERROR, "RASTER_out: Cannot HEX-WKBize raster");
112  PG_RETURN_NULL();
113  }
114 
115  /* Free the raster objects used */
117  PG_FREE_IF_COPY(pgraster, 0);
118 
119  PG_RETURN_CSTRING(hexwkb);
120 }
121 
127 Datum RASTER_to_bytea(PG_FUNCTION_ARGS)
128 {
129  rt_pgraster *pgraster = NULL;
130  rt_raster raster = NULL;
131  uint8_t *wkb = NULL;
132  uint32_t wkb_size = 0;
133  bytea *result = NULL;
134  int result_size = 0;
135 
136  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
137  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
138 
139  /* Get raster object */
140  raster = rt_raster_deserialize(pgraster, FALSE);
141  if (!raster) {
142  PG_FREE_IF_COPY(pgraster, 0);
143  elog(ERROR, "RASTER_to_bytea: Cannot deserialize raster");
144  PG_RETURN_NULL();
145  }
146 
147  /* Parse raster to wkb object */
148  wkb = rt_raster_to_wkb(raster, FALSE, &wkb_size);
149  if (!wkb) {
151  PG_FREE_IF_COPY(pgraster, 0);
152  elog(ERROR, "RASTER_to_bytea: Cannot allocate and generate WKB data");
153  PG_RETURN_NULL();
154  }
155 
156  /* Create varlena object */
157  result_size = wkb_size + VARHDRSZ;
158  result = (bytea *)palloc(result_size);
159  SET_VARSIZE(result, result_size);
160  memcpy(VARDATA(result), wkb, VARSIZE(result) - VARHDRSZ);
161 
162  /* Free raster objects used */
164  pfree(wkb);
165  PG_FREE_IF_COPY(pgraster, 0);
166 
167  PG_RETURN_POINTER(result);
168 }
169 
174 Datum RASTER_noop(PG_FUNCTION_ARGS)
175 {
177  rt_pgraster *pgraster, *result;
178  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
179  raster = rt_raster_deserialize(pgraster, FALSE);
180  if (!raster) {
181  PG_FREE_IF_COPY(pgraster, 0);
182  elog(ERROR, "RASTER_noop: Cannot deserialize raster");
183  PG_RETURN_NULL();
184  }
185  result = rt_raster_serialize(raster);
187  if (result == NULL)
188  PG_RETURN_NULL();
189 
190  SET_VARSIZE(result, raster->size);
191  PG_RETURN_POINTER(result);
192 }
193 
#define FALSE
Definition: dbfopen.c:168
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:82
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:127
Datum RASTER_to_binary(PG_FUNCTION_ARGS)
obsolete as of 2.5.0 stubbing for smoother upgrade from 2.4
Definition: rtpg_inout.c:50
PG_FUNCTION_INFO_V1(RASTER_to_binary)
Legacy return error if called Removed in PostGIS 2.5.0.
Datum RASTER_out(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:88
Datum RASTER_in(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:62
Datum RASTER_noop(PG_FUNCTION_ARGS)
Definition: rtpg_inout.c:174
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:61
Struct definitions.
Definition: librtcore.h:2250
unsigned int uint32_t
Definition: uthash.h:78
unsigned char uint8_t
Definition: uthash.h:79