~ubuntu-branches/ubuntu/trusty/postgis/trusty-security

« back to all changes in this revision

Viewing changes to lwgeom/lwgeom_inout.c

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2009-12-11 13:10:34 UTC
  • mfrom: (1.1.9 upstream) (5.2.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20091211131034-wmsz69wxvt95pe5r
Tags: 1.4.0-2
* Upload to unstable.
* Better parameterized debian/rules against postgis $(VERSION).
* Added dblatex and libcunit1-dev among build-deps.
* Added postgis_comments.sql to contrib/ SQL templates.
* Dropping 8.3 support, no more supported for squeeze.
  (closes: #559587)
* Do not stop on error in postrm if the target dir does not exist.
  (closes: #560409)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "postgres.h"
2
 
 
3
 
#include <math.h>
4
 
#include <float.h>
5
 
#include <string.h>
6
 
#include <stdio.h>
7
 
#include <errno.h>
8
 
#include <sys/param.h>
9
 
#include <sys/types.h>
10
 
 
11
 
#include "access/gist.h"
12
 
#include "access/itup.h"
13
 
 
14
 
#include "fmgr.h"
15
 
#include "utils/elog.h"
16
 
#if USE_VERSION > 73
17
 
# include "lib/stringinfo.h" /* for binary input */
18
 
#endif
19
 
 
20
 
 
21
 
#include "liblwgeom.h"
22
 
#include "stringBuffer.h"
23
 
 
24
 
 
25
 
/* #define PGIS_DEBUG 1 */
26
 
 
27
 
#include "lwgeom_pg.h"
28
 
#include "wktparse.h"
29
 
#include "profile.h"
30
 
 
31
 
void elog_ERROR(const char* string);
32
 
 
33
 
 
34
 
Datum LWGEOM_in(PG_FUNCTION_ARGS);
35
 
Datum LWGEOM_out(PG_FUNCTION_ARGS);
36
 
Datum LWGEOM_to_text(PG_FUNCTION_ARGS);
37
 
Datum LWGEOM_to_bytea(PG_FUNCTION_ARGS);
38
 
Datum LWGEOM_from_bytea(PG_FUNCTION_ARGS);
39
 
Datum parse_WKT_lwgeom(PG_FUNCTION_ARGS);
40
 
#if USE_VERSION > 73
41
 
Datum LWGEOM_recv(PG_FUNCTION_ARGS);
42
 
Datum LWGEOM_send(PG_FUNCTION_ARGS);
43
 
#endif
44
 
Datum BOOL_to_text(PG_FUNCTION_ARGS);
45
 
 
46
 
 
47
 
/*
48
 
 *  included here so we can be independent from postgis
49
 
 * WKB structure  -- exactly the same as TEXT
50
 
 */
51
 
typedef struct Well_known_bin {
52
 
    int32 size;         /* total size of this structure */
53
 
    uchar  data[1];     /* THIS HOLD VARIABLE LENGTH DATA */
54
 
} WellKnownBinary;
55
 
 
56
 
 
57
 
/*
58
 
 * LWGEOM_in(cstring)
59
 
 * format is '[SRID=#;]wkt|wkb'
60
 
 *  LWGEOM_in( 'SRID=99;POINT(0 0)')
61
 
 *  LWGEOM_in( 'POINT(0 0)')            --> assumes SRID=-1
62
 
 *  LWGEOM_in( 'SRID=99;0101000000000000000000F03F000000000000004')
63
 
 *  returns a PG_LWGEOM object
64
 
 */
65
 
PG_FUNCTION_INFO_V1(LWGEOM_in);
66
 
Datum LWGEOM_in(PG_FUNCTION_ARGS)
67
 
{
68
 
        char *str = PG_GETARG_CSTRING(0);
69
 
    SERIALIZED_LWGEOM *serialized_lwgeom;
70
 
    LWGEOM *lwgeom;     
71
 
    PG_LWGEOM *ret;
72
 
 
73
 
        /* will handle both HEXEWKB and EWKT */
74
 
    serialized_lwgeom = parse_lwgeom_wkt(str);
75
 
        lwgeom = lwgeom_deserialize(serialized_lwgeom->lwgeom);
76
 
    
77
 
    ret = pglwgeom_serialize(lwgeom);
78
 
        lwgeom_release(lwgeom);
79
 
 
80
 
        if ( is_worth_caching_pglwgeom_bbox(ret) )
81
 
        {
82
 
                ret = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1(
83
 
                        LWGEOM_addBBOX, PointerGetDatum(ret)));
84
 
        }
85
 
 
86
 
        PG_RETURN_POINTER(ret);
87
 
}
88
 
 
89
 
/*
90
 
 * LWGEOM_out(lwgeom) --> cstring
91
 
 * output is 'SRID=#;<wkb in hex form>'
92
 
 * ie. 'SRID=-99;0101000000000000000000F03F0000000000000040'
93
 
 * WKB is machine endian
94
 
 * if SRID=-1, the 'SRID=-1;' will probably not be present.
95
 
 */
96
 
PG_FUNCTION_INFO_V1(LWGEOM_out);
97
 
Datum LWGEOM_out(PG_FUNCTION_ARGS)
98
 
{
99
 
        PG_LWGEOM *lwgeom;
100
 
        char *result;
101
 
 
102
 
        init_pg_func();
103
 
 
104
 
        lwgeom = (PG_LWGEOM *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
105
 
        result = unparse_WKB(SERIALIZED_FORM(lwgeom),lwalloc,lwfree,-1,NULL,1);
106
 
 
107
 
        PG_RETURN_CSTRING(result);
108
 
}
109
 
 
110
 
/*
111
 
 * AsHEXEWKB(geom, string)
112
 
 */
113
 
PG_FUNCTION_INFO_V1(LWGEOM_asHEXEWKB);
114
 
Datum LWGEOM_asHEXEWKB(PG_FUNCTION_ARGS)
115
 
{
116
 
        PG_LWGEOM *lwgeom;
117
 
        char *result;
118
 
        text *text_result;
119
 
        size_t size;
120
 
        text *type;
121
 
        unsigned int byteorder=-1;
122
 
 
123
 
        init_pg_func();
124
 
 
125
 
        lwgeom = (PG_LWGEOM *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
126
 
 
127
 
        if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
128
 
        {
129
 
                type = PG_GETARG_TEXT_P(1);
130
 
                if (VARSIZE(type) < 7)  
131
 
                {
132
 
                        elog(ERROR,"AsHEXEWKB(geometry, <type>) - type should be 'XDR' or 'NDR'.  type length is %i",VARSIZE(type) -VARHDRSZ);
133
 
                        PG_RETURN_NULL();
134
 
                }
135
 
 
136
 
                if  ( ! strncmp(VARDATA(type), "xdr", 3) ||
137
 
                        ! strncmp(VARDATA(type), "XDR", 3) )
138
 
                {
139
 
                        byteorder = XDR;
140
 
                }
141
 
                else
142
 
                {
143
 
                        byteorder = NDR;
144
 
                }
145
 
        }
146
 
 
147
 
        result = unparse_WKB(SERIALIZED_FORM(lwgeom),lwalloc,lwfree,
148
 
                        byteorder, &size, 1);
149
 
 
150
 
        text_result = palloc(size+VARHDRSZ);
151
 
        memcpy(VARDATA(text_result),result,size);
152
 
        SET_VARSIZE(text_result, size+VARHDRSZ);
153
 
        pfree(result);
154
 
 
155
 
        PG_RETURN_POINTER(text_result);
156
 
 
157
 
}
158
 
 
159
 
/*
160
 
 * LWGEOM_to_text(lwgeom) --> text
161
 
 * output is 'SRID=#;<wkb in hex form>'
162
 
 * ie. 'SRID=-99;0101000000000000000000F03F0000000000000040'
163
 
 * WKB is machine endian
164
 
 * if SRID=-1, the 'SRID=-1;' will probably not be present.
165
 
 */
166
 
PG_FUNCTION_INFO_V1(LWGEOM_to_text);
167
 
Datum LWGEOM_to_text(PG_FUNCTION_ARGS)
168
 
{
169
 
        PG_LWGEOM *lwgeom;
170
 
        char *result;
171
 
        text *text_result;
172
 
        size_t size;
173
 
 
174
 
        init_pg_func();
175
 
 
176
 
        lwgeom = (PG_LWGEOM *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
177
 
        result = unparse_WKB(SERIALIZED_FORM(lwgeom),lwalloc,lwfree,-1,&size,1);
178
 
 
179
 
        text_result = palloc(size+VARHDRSZ);
180
 
        memcpy(VARDATA(text_result),result,size);
181
 
        SET_VARSIZE(text_result, size+VARHDRSZ);
182
 
        pfree(result);
183
 
 
184
 
        PG_RETURN_POINTER(text_result);
185
 
}
186
 
 
187
 
/*
188
 
 * LWGEOMFromWKB(wkb,  [SRID] )
189
 
 * NOTE: wkb is in *binary* not hex form.
190
 
 *
191
 
 * NOTE: this function parses EWKB (extended form)
192
 
 *       which also contains SRID info. When the SRID
193
 
 *       argument is provided it will override any SRID
194
 
 *       info found in EWKB format.
195
 
 *
196
 
 * NOTE: this function is *unoptimized* as will copy
197
 
 *       the object when adding SRID and when adding BBOX.
198
 
 *       additionally, it suffers from the non-optimal
199
 
 *       pglwgeom_from_ewkb function.
200
 
 *
201
 
 */
202
 
PG_FUNCTION_INFO_V1(LWGEOMFromWKB);
203
 
Datum LWGEOMFromWKB(PG_FUNCTION_ARGS)
204
 
{
205
 
        WellKnownBinary *wkb_input;
206
 
        PG_LWGEOM *lwgeom, *lwgeom2;
207
 
 
208
 
        wkb_input = (WellKnownBinary *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
209
 
 
210
 
        lwgeom2 = pglwgeom_from_ewkb((uchar *)VARDATA(wkb_input),
211
 
                VARSIZE(wkb_input)-VARHDRSZ);
212
 
 
213
 
        if (  ( PG_NARGS()>1) && ( ! PG_ARGISNULL(1) ))
214
 
        {
215
 
                lwgeom = pglwgeom_setSRID(lwgeom2, PG_GETARG_INT32(1));
216
 
                lwfree(lwgeom2);
217
 
        }
218
 
        else lwgeom = lwgeom2;
219
 
 
220
 
        if ( is_worth_caching_pglwgeom_bbox(lwgeom) )
221
 
        {
222
 
                lwgeom = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1(
223
 
                        LWGEOM_addBBOX, PointerGetDatum(lwgeom)));
224
 
        }
225
 
 
226
 
        PG_RETURN_POINTER(lwgeom);
227
 
}
228
 
 
229
 
/*
230
 
 * WKBFromLWGEOM(lwgeom) --> wkb
231
 
 * this will have no 'SRID=#;'
232
 
 */
233
 
PG_FUNCTION_INFO_V1(WKBFromLWGEOM);
234
 
Datum WKBFromLWGEOM(PG_FUNCTION_ARGS)
235
 
{
236
 
/* #define BINARY_FROM_HEX 1 */
237
 
 
238
 
        PG_LWGEOM *lwgeom_input; /* SRID=#;<hexized wkb> */
239
 
        char *hexized_wkb; /* hexized_wkb_srid w/o srid */
240
 
        char *result; /* wkb */
241
 
        int size_result;
242
 
#ifdef BINARY_FROM_HEX
243
 
        char *hexized_wkb_srid;
244
 
        char *semicolonLoc;
245
 
        int t;
246
 
#endif /* BINARY_FROM_HEX */
247
 
        text *type;
248
 
        unsigned int byteorder=-1;
249
 
        size_t size;
250
 
 
251
 
#ifdef PROFILE
252
 
        profstart(PROF_QRUN);
253
 
#endif
254
 
 
255
 
        init_pg_func();
256
 
 
257
 
        if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
258
 
        {
259
 
                type = PG_GETARG_TEXT_P(1);
260
 
                if (VARSIZE(type) < 7)  
261
 
                {
262
 
                        elog(ERROR,"asbinary(geometry, <type>) - type should be 'XDR' or 'NDR'.  type length is %i",VARSIZE(type) -VARHDRSZ);
263
 
                        PG_RETURN_NULL();
264
 
                }
265
 
 
266
 
                if  ( ! strncmp(VARDATA(type), "xdr", 3) ||
267
 
                        ! strncmp(VARDATA(type), "XDR", 3) )
268
 
                {
269
 
                        byteorder = XDR;
270
 
                }
271
 
                else
272
 
                {
273
 
                        byteorder = NDR;
274
 
                }
275
 
        }
276
 
 
277
 
        lwgeom_input = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
278
 
 
279
 
#ifdef BINARY_FROM_HEX
280
 
        hexized_wkb_srid = unparse_WKB(SERIALIZED_FORM(lwgeom_input),
281
 
                lwalloc, lwfree, byteorder, &size, 1);
282
 
 
283
 
#ifdef PGIS_DEBUG
284
 
  elog(NOTICE, "in WKBFromLWGEOM with WKB = '%s'", hexized_wkb_srid);
285
 
#endif
286
 
 
287
 
        hexized_wkb = hexized_wkb_srid;
288
 
 
289
 
        semicolonLoc = strchr(hexized_wkb_srid,';');
290
 
        if (semicolonLoc != NULL)
291
 
        {
292
 
                hexized_wkb = (semicolonLoc+1);
293
 
        }
294
 
 
295
 
#ifdef PGIS_DEBUG
296
 
        elog(NOTICE, "in WKBFromLWGEOM with WKB (with no 'SRID=#;' = '%s'",
297
 
                hexized_wkb);
298
 
#endif
299
 
 
300
 
        size_result = size/2 + VARHDRSZ;
301
 
        result = palloc(size_result);
302
 
    SET_VARSIZE(result, size_result);
303
 
 
304
 
        /* have a hexized string, want to make it binary */
305
 
        for (t=0; t< (size/2); t++)
306
 
        {
307
 
                ((uchar *) VARDATA(result))[t] = parse_hex(  hexized_wkb + (t*2) );
308
 
        }
309
 
 
310
 
        pfree(hexized_wkb_srid);
311
 
 
312
 
#else /* ndef BINARY_FROM_HEX */
313
 
 
314
 
        hexized_wkb = unparse_WKB(SERIALIZED_FORM(lwgeom_input),
315
 
                lwalloc, lwfree, byteorder, &size, 0);
316
 
 
317
 
        size_result = size+VARHDRSZ;
318
 
        result = palloc(size_result);
319
 
        SET_VARSIZE(result, size_result);
320
 
        memcpy(VARDATA(result), hexized_wkb, size);
321
 
        pfree(hexized_wkb);
322
 
 
323
 
#endif
324
 
 
325
 
#ifdef PROFILE
326
 
        profstop(PROF_QRUN);
327
 
        lwnotice("unparse_WKB: prof: %lu", proftime[PROF_QRUN]);
328
 
#endif
329
 
 
330
 
#ifdef PGIS_DEBUG
331
 
        lwnotice("Output size is %lu (comp: %lu)",
332
 
                VARSIZE(result), (unsigned long)size);
333
 
#endif /* def PGIS_DEBUG */
334
 
 
335
 
 
336
 
        PG_RETURN_POINTER(result);
337
 
}
338
 
 
339
 
/* puts a bbox inside the geometry */
340
 
PG_FUNCTION_INFO_V1(LWGEOM_addBBOX);
341
 
Datum LWGEOM_addBBOX(PG_FUNCTION_ARGS)
342
 
{
343
 
        PG_LWGEOM *lwgeom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
344
 
        PG_LWGEOM *result;
345
 
        BOX2DFLOAT4     box;
346
 
        uchar   old_type;
347
 
        int             size;
348
 
 
349
 
#ifdef PGIS_DEBUG
350
 
        elog(NOTICE,"in LWGEOM_addBBOX");
351
 
#endif
352
 
 
353
 
        if (lwgeom_hasBBOX( lwgeom->type ) )
354
 
        {
355
 
#ifdef PGIS_DEBUG
356
 
                elog(NOTICE,"LWGEOM_addBBOX  -- already has bbox");
357
 
#endif
358
 
                /* easy - already has one.  Just copy! */
359
 
                result = palloc (VARSIZE(lwgeom));
360
 
        SET_VARSIZE(result, VARSIZE(lwgeom));
361
 
                memcpy(VARDATA(result), VARDATA(lwgeom), VARSIZE(lwgeom)-VARHDRSZ);
362
 
                PG_RETURN_POINTER(result);
363
 
        }
364
 
 
365
 
#ifdef PGIS_DEBUG
366
 
        elog(NOTICE,"LWGEOM_addBBOX  -- giving it a bbox");
367
 
#endif
368
 
 
369
 
        /* construct new one */
370
 
        if ( ! getbox2d_p(SERIALIZED_FORM(lwgeom), &box) )
371
 
        {
372
 
                /* Empty geom, no bbox to add */
373
 
                result = palloc (VARSIZE(lwgeom));
374
 
        SET_VARSIZE(result, VARSIZE(lwgeom));
375
 
                memcpy(VARDATA(result), VARDATA(lwgeom), VARSIZE(lwgeom)-VARHDRSZ);
376
 
                PG_RETURN_POINTER(result);
377
 
        }
378
 
        old_type = lwgeom->type;
379
 
 
380
 
        size = VARSIZE(lwgeom)+sizeof(BOX2DFLOAT4);
381
 
 
382
 
        result = palloc(size); /* 16 for bbox2d */
383
 
        SET_VARSIZE(result, size);
384
 
 
385
 
        result->type = lwgeom_makeType_full(
386
 
                TYPE_HASZ(old_type),
387
 
                TYPE_HASM(old_type),
388
 
                lwgeom_hasSRID(old_type), lwgeom_getType(old_type), 1);
389
 
 
390
 
        /* copy in bbox */
391
 
        memcpy(result->data, &box, sizeof(BOX2DFLOAT4));
392
 
 
393
 
#ifdef PGIS_DEBUG
394
 
        lwnotice("result->type hasbbox: %d", TYPE_HASBBOX(result->type));
395
 
        lwnotice("LWGEOM_addBBOX  -- about to copy serialized form");
396
 
#endif
397
 
 
398
 
        /* everything but the type and length */
399
 
        memcpy((char *)VARDATA(result)+sizeof(BOX2DFLOAT4)+1, (char *)VARDATA(lwgeom)+1, VARSIZE(lwgeom)-VARHDRSZ-1);
400
 
 
401
 
        PG_RETURN_POINTER(result);
402
 
}
403
 
 
404
 
char
405
 
is_worth_caching_pglwgeom_bbox(const PG_LWGEOM *in)
406
 
{
407
 
#if ! AUTOCACHE_BBOX
408
 
        return false;
409
 
#endif
410
 
        if ( TYPE_GETTYPE(in->type) == POINTTYPE ) return false;
411
 
        return true;
412
 
}
413
 
 
414
 
char
415
 
is_worth_caching_serialized_bbox(const uchar *in)
416
 
{
417
 
#if ! AUTOCACHE_BBOX
418
 
        return false;
419
 
#endif
420
 
        if ( TYPE_GETTYPE((uchar)in[0]) == POINTTYPE ) return false;
421
 
        return true;
422
 
}
423
 
 
424
 
char
425
 
is_worth_caching_lwgeom_bbox(const LWGEOM *in)
426
 
{
427
 
#if ! AUTOCACHE_BBOX
428
 
        return false;
429
 
#endif
430
 
        if ( TYPE_GETTYPE(in->type) == POINTTYPE ) return false;
431
 
        return true;
432
 
}
433
 
 
434
 
/* removes a bbox from a geometry */
435
 
PG_FUNCTION_INFO_V1(LWGEOM_dropBBOX);
436
 
Datum LWGEOM_dropBBOX(PG_FUNCTION_ARGS)
437
 
{
438
 
        PG_LWGEOM *lwgeom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
439
 
        PG_LWGEOM *result;
440
 
        uchar old_type;
441
 
        int size;
442
 
 
443
 
#ifdef PGIS_DEBUG
444
 
        elog(NOTICE,"in LWGEOM_dropBBOX");
445
 
#endif
446
 
 
447
 
        if (!lwgeom_hasBBOX( lwgeom->type ) )
448
 
        {
449
 
#ifdef PGIS_DEBUG
450
 
        elog(NOTICE,"LWGEOM_dropBBOX  -- doesnt have a bbox already");
451
 
#endif
452
 
                result = palloc (VARSIZE(lwgeom));
453
 
        SET_VARSIZE(result, VARSIZE(lwgeom));
454
 
                memcpy(VARDATA(result), VARDATA(lwgeom), VARSIZE(lwgeom)-VARHDRSZ);
455
 
                PG_RETURN_POINTER(result);
456
 
        }
457
 
 
458
 
#ifdef PGIS_DEBUG
459
 
        elog(NOTICE,"LWGEOM_dropBBOX  -- dropping the bbox");
460
 
#endif
461
 
 
462
 
        /* construct new one */
463
 
        old_type = lwgeom->type;
464
 
 
465
 
        size = VARSIZE(lwgeom)-sizeof(BOX2DFLOAT4);
466
 
 
467
 
        result = palloc(size); /* 16 for bbox2d */
468
 
    SET_VARSIZE(result, size);
469
 
 
470
 
        result->type = lwgeom_makeType_full(
471
 
                TYPE_HASZ(old_type),
472
 
                TYPE_HASM(old_type),
473
 
                lwgeom_hasSRID(old_type), lwgeom_getType(old_type), 0);
474
 
 
475
 
        /* everything but the type and length */
476
 
        memcpy((char *)VARDATA(result)+1, ((char *)(lwgeom->data))+sizeof(BOX2DFLOAT4), size-VARHDRSZ-1);
477
 
 
478
 
        PG_RETURN_POINTER(result);
479
 
}
480
 
 
481
 
 
482
 
/* for the wkt parser */
483
 
void elog_ERROR(const char* string)
484
 
{
485
 
        elog(ERROR,string);
486
 
}
487
 
 
488
 
/*
489
 
 * parse WKT input
490
 
 * parse_WKT_lwgeom(TEXT) -> LWGEOM
491
 
 */
492
 
PG_FUNCTION_INFO_V1(parse_WKT_lwgeom);
493
 
Datum parse_WKT_lwgeom(PG_FUNCTION_ARGS)
494
 
{
495
 
        /* text */
496
 
        text *wkt_input = PG_GETARG_TEXT_P(0);
497
 
        PG_LWGEOM *ret;  /*with length */
498
 
    SERIALIZED_LWGEOM *serialized_lwgeom;
499
 
    LWGEOM *lwgeom;     
500
 
    char *wkt;
501
 
        int wkt_size ;
502
 
 
503
 
        init_pg_func();
504
 
 
505
 
        wkt_size = VARSIZE(wkt_input)-VARHDRSZ; /* actual letters */
506
 
 
507
 
        wkt = palloc( wkt_size+1); /* +1 for null */
508
 
        memcpy(wkt, VARDATA(wkt_input), wkt_size );
509
 
        wkt[wkt_size] = 0; /* null term */
510
 
 
511
 
 
512
 
#ifdef PGIS_DEBUG
513
 
        elog(NOTICE,"in parse_WKT_lwgeom with input: '%s'",wkt);
514
 
#endif
515
 
 
516
 
    serialized_lwgeom = parse_lwg((const char *)wkt, (allocator)lwalloc, (report_error)elog_ERROR);
517
 
    lwgeom = lwgeom_deserialize(serialized_lwgeom->lwgeom);
518
 
    
519
 
    ret = pglwgeom_serialize(lwgeom);
520
 
        lwgeom_release(lwgeom);
521
 
 
522
 
#ifdef PGIS_DEBUG
523
 
        elog(NOTICE,"parse_WKT_lwgeom:: finished parse");
524
 
#endif
525
 
 
526
 
        pfree (wkt);
527
 
 
528
 
        if (ret == NULL) elog(ERROR,"parse_WKT:: couldnt parse!");
529
 
 
530
 
        if ( is_worth_caching_pglwgeom_bbox(ret) )
531
 
        {
532
 
                ret = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1(
533
 
                        LWGEOM_addBBOX, PointerGetDatum(ret)));
534
 
        }
535
 
 
536
 
        PG_RETURN_POINTER(ret);
537
 
}
538
 
 
539
 
 
540
 
#if USE_VERSION > 73
541
 
/*
542
 
 * This function must advance the StringInfo.cursor pointer
543
 
 * and leave it at the end of StringInfo.buf. If it fails
544
 
 * to do so the backend will raise an exception with message:
545
 
 * ERROR:  incorrect binary data format in bind parameter #
546
 
 *
547
 
 */
548
 
PG_FUNCTION_INFO_V1(LWGEOM_recv);
549
 
Datum LWGEOM_recv(PG_FUNCTION_ARGS)
550
 
{
551
 
        StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
552
 
        bytea *wkb;
553
 
        PG_LWGEOM *result;
554
 
 
555
 
#ifdef PGIS_DEBUG
556
 
        elog(NOTICE, "LWGEOM_recv start");
557
 
#endif
558
 
 
559
 
        /* Add VARLENA size info to make it a valid varlena object */
560
 
        wkb = (bytea *)palloc(buf->len+VARHDRSZ);
561
 
        SET_VARSIZE(wkb, buf->len+VARHDRSZ);
562
 
        memcpy(VARDATA(wkb), buf->data, buf->len);
563
 
 
564
 
#ifdef PGIS_DEBUG
565
 
        elog(NOTICE, "LWGEOM_recv calling LWGEOMFromWKB");
566
 
#endif
567
 
 
568
 
        /* Call LWGEOM_from_bytea function... */
569
 
        result = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1(
570
 
                LWGEOMFromWKB, PointerGetDatum(wkb)));
571
 
 
572
 
#ifdef PGIS_DEBUG
573
 
        elog(NOTICE, "LWGEOM_recv advancing StringInfo buffer");
574
 
#endif
575
 
 
576
 
#ifdef PGIS_DEBUG
577
 
        elog(NOTICE, "LWGEOM_from_bytea returned %s", unparse_WKB(SERIALIZED_FORM(result),pg_alloc,pg_free,-1,NULL,1));
578
 
#endif
579
 
 
580
 
 
581
 
        /* Set cursor to the end of buffer (so the backend is happy) */
582
 
        buf->cursor = buf->len;
583
 
 
584
 
#ifdef PGIS_DEBUG
585
 
        elog(NOTICE, "LWGEOM_recv returning");
586
 
#endif
587
 
 
588
 
        PG_RETURN_POINTER(result);
589
 
}
590
 
 
591
 
PG_FUNCTION_INFO_V1(LWGEOM_send);
592
 
Datum LWGEOM_send(PG_FUNCTION_ARGS)
593
 
{
594
 
        bytea *result;
595
 
 
596
 
#ifdef PGIS_DEBUG
597
 
        elog(NOTICE, "LWGEOM_send called");
598
 
#endif
599
 
 
600
 
        result = (bytea *)DatumGetPointer(DirectFunctionCall1(
601
 
                WKBFromLWGEOM, PG_GETARG_DATUM(0)));
602
 
 
603
 
        PG_RETURN_POINTER(result);
604
 
}
605
 
 
606
 
 
607
 
#endif /* USE_VERSION > 73 */
608
 
 
609
 
PG_FUNCTION_INFO_V1(LWGEOM_to_bytea);
610
 
Datum LWGEOM_to_bytea(PG_FUNCTION_ARGS)
611
 
{
612
 
        bytea *result;
613
 
 
614
 
#ifdef PGIS_DEBUG
615
 
        elog(NOTICE, "LWGEOM_to_bytea called");
616
 
#endif
617
 
 
618
 
        result = (bytea *)DatumGetPointer(DirectFunctionCall1(
619
 
                WKBFromLWGEOM, PG_GETARG_DATUM(0)));
620
 
 
621
 
        PG_RETURN_POINTER(result);
622
 
}
623
 
 
624
 
PG_FUNCTION_INFO_V1(LWGEOM_from_bytea);
625
 
Datum LWGEOM_from_bytea(PG_FUNCTION_ARGS)
626
 
{
627
 
        PG_LWGEOM *result;
628
 
 
629
 
#ifdef PGIS_DEBUG
630
 
        elog(NOTICE, "LWGEOM_from_bytea start");
631
 
#endif
632
 
 
633
 
        result = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1(
634
 
                LWGEOMFromWKB, PG_GETARG_DATUM(0)));
635
 
 
636
 
        PG_RETURN_POINTER(result);
637
 
}
638
 
 
639
 
PG_FUNCTION_INFO_V1(BOOL_to_text);
640
 
Datum BOOL_to_text(PG_FUNCTION_ARGS)
641
 
{
642
 
        bool b = PG_GETARG_BOOL(0);
643
 
        char c;
644
 
        text *result;
645
 
 
646
 
        c = b ? 't' : 'f';
647
 
 
648
 
        result = palloc(VARHDRSZ+1*sizeof(char));
649
 
        SET_VARSIZE(result, VARHDRSZ+1*sizeof(char));
650
 
        memcpy(VARDATA(result), &c, 1*sizeof(char));
651
 
 
652
 
        PG_RETURN_POINTER(result);
653
 
 
654
 
}