PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
rtpg_wkb.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) 2018-2018 Bborie Park <dustymugs@gmail.com>
7 * Copyright (C) 2011-2013 Regents of the University of California
8 * <bkpark@ucdavis.edu>
9 * Copyright (C) 2010-2011 Jorge Arevalo <jorge.arevalo@deimos-space.com>
10 * Copyright (C) 2010-2011 David Zwarg <dzwarg@azavea.com>
11 * Copyright (C) 2009-2011 Pierre Racine <pierre.racine@sbf.ulaval.ca>
12 * Copyright (C) 2009-2011 Mateusz Loskot <mateusz@loskot.net>
13 * Copyright (C) 2008-2009 Sandro Santilli <strk@kbt.io>
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software Foundation,
27 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 *
29 */
30
31#include <postgres.h> /* for palloc */
32#include <fmgr.h> /* for PG_*, Datum* */
33#include <utils/builtins.h> /* for cstring_to_text */
34
35#include "rtpostgis.h"
36
37Datum RASTER_asWKB(PG_FUNCTION_ARGS);
38Datum RASTER_asHexWKB(PG_FUNCTION_ARGS);
39
40Datum RASTER_fromWKB(PG_FUNCTION_ARGS);
41Datum RASTER_fromHexWKB(PG_FUNCTION_ARGS);
42
47Datum RASTER_asWKB(PG_FUNCTION_ARGS)
48{
49 rt_pgraster *pgraster = NULL;
50 rt_raster raster = NULL;
51 uint8_t *wkb = NULL;
52 uint32_t wkb_size = 0;
53 char *result = NULL;
54 int result_size = 0;
55 int outasin = FALSE;
56
57 if (PG_ARGISNULL(0)) PG_RETURN_NULL();
58 pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
59
60 /* Get raster object */
61 raster = rt_raster_deserialize(pgraster, FALSE);
62 if (!raster) {
63 PG_FREE_IF_COPY(pgraster, 0);
64 elog(ERROR, "RASTER_asWKB: Cannot deserialize raster");
65 PG_RETURN_NULL();
66 }
67
68 if (!PG_ARGISNULL(1))
69 outasin = PG_GETARG_BOOL(1);
70
71 /* Parse raster to wkb object */
72 wkb = rt_raster_to_wkb(raster, outasin, &wkb_size);
73 if (!wkb) {
74 rt_raster_destroy(raster);
75 PG_FREE_IF_COPY(pgraster, 0);
76 elog(ERROR, "RASTER_asWKB: Cannot allocate and generate WKB data");
77 PG_RETURN_NULL();
78 }
79
80 /* Create varlena object */
81 result_size = wkb_size + VARHDRSZ;
82 result = (char *)palloc(result_size);
83 SET_VARSIZE(result, result_size);
84 memcpy(VARDATA(result), wkb, VARSIZE_ANY_EXHDR(result));
85
86 /* Free raster objects used */
87 rt_raster_destroy(raster);
88 pfree(wkb);
89 PG_FREE_IF_COPY(pgraster, 0);
90
91 PG_RETURN_POINTER(result);
92}
93
98Datum RASTER_asHexWKB(PG_FUNCTION_ARGS)
99{
100 rt_pgraster *pgraster = NULL;
101 rt_raster raster = NULL;
102 int outasin = FALSE;
103 uint32_t hexwkbsize = 0;
104 char *hexwkb = NULL;
105 text *result = NULL;
106
107 POSTGIS_RT_DEBUG(3, "Starting");
108
109 if (PG_ARGISNULL(0)) PG_RETURN_NULL();
110 pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
111
112 raster = rt_raster_deserialize(pgraster, FALSE);
113 if (!raster) {
114 PG_FREE_IF_COPY(pgraster, 0);
115 elog(ERROR, "RASTER_asHexWKB: Cannot deserialize raster");
116 PG_RETURN_NULL();
117 }
118
119 if (!PG_ARGISNULL(1))
120 outasin = PG_GETARG_BOOL(1);
121
122 hexwkb = rt_raster_to_hexwkb(raster, outasin, &hexwkbsize);
123 if (!hexwkb) {
124 rt_raster_destroy(raster);
125 PG_FREE_IF_COPY(pgraster, 0);
126 elog(ERROR, "RASTER_asHexWKB: Cannot allocate and generate Hex WKB data");
127 PG_RETURN_NULL();
128 }
129
130 /* Free the raster objects used */
131 rt_raster_destroy(raster);
132 PG_FREE_IF_COPY(pgraster, 0);
133
134 result = cstring_to_text(hexwkb);
135
136 PG_RETURN_TEXT_P(result);
137}
138
143Datum RASTER_fromWKB(PG_FUNCTION_ARGS)
144{
145 bytea *bytea_data;
146 uint8_t *data;
147 int data_len = 0;
148
149 rt_raster raster;
150 void *result = NULL;
151
152 POSTGIS_RT_DEBUG(3, "Starting");
153
154 bytea_data = (bytea *) PG_GETARG_BYTEA_P(0);
155 data = (uint8_t *) VARDATA(bytea_data);
156 data_len = VARSIZE_ANY_EXHDR(bytea_data);
157
158 raster = rt_raster_from_wkb(data, data_len);
159 PG_FREE_IF_COPY(bytea_data, 0);
160 if (raster == NULL)
161 PG_RETURN_NULL();
162
163 result = rt_raster_serialize(raster);
164 rt_raster_destroy(raster);
165 if (result == NULL)
166 PG_RETURN_NULL();
167
168 SET_VARSIZE(result, ((rt_pgraster*)result)->size);
169 PG_RETURN_POINTER(result);
170}
171
176Datum RASTER_fromHexWKB(PG_FUNCTION_ARGS)
177{
178 text *hexwkb_text = PG_GETARG_TEXT_P(0);
179 char *hexwkb;
180
181 rt_raster raster;
182 void *result = NULL;
183
184 POSTGIS_RT_DEBUG(3, "Starting");
185
186 hexwkb = text_to_cstring(hexwkb_text);
187
188 raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
189 PG_FREE_IF_COPY(hexwkb_text, 0);
190 if (raster == NULL)
191 PG_RETURN_NULL();
192
193 result = rt_raster_serialize(raster);
194 rt_raster_destroy(raster);
195 if (result == NULL)
196 PG_RETURN_NULL();
197
198 SET_VARSIZE(result, ((rt_pgraster*)result)->size);
199 PG_RETURN_POINTER(result);
200}
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition cu_print.c:267
#define FALSE
Definition dbfopen.c:72
rt_raster rt_raster_from_wkb(const uint8_t *wkb, uint32_t wkbsize)
Construct an rt_raster from a binary WKB representation.
Definition rt_wkb.c:283
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition rt_raster.c:86
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:693
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:414
uint8_t * rt_raster_to_wkb(rt_raster raster, int outasin, uint32_t *wkbsize)
Return this raster in WKB form.
Definition rt_wkb.c:502
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Datum RASTER_fromWKB(PG_FUNCTION_ARGS)
Definition rtpg_wkb.c:143
PG_FUNCTION_INFO_V1(RASTER_asWKB)
Output is WKB.
Datum RASTER_fromHexWKB(PG_FUNCTION_ARGS)
Definition rtpg_wkb.c:176
Datum RASTER_asHexWKB(PG_FUNCTION_ARGS)
Definition rtpg_wkb.c:98
Datum RASTER_asWKB(PG_FUNCTION_ARGS)
Definition rtpg_wkb.c:47
#define POSTGIS_RT_DEBUG(level, msg)
Definition rtpostgis.h:65
Struct definitions.
Definition librtcore.h:2452