~ubuntu-branches/ubuntu/feisty/postgis/feisty

« back to all changes in this revision

Viewing changes to lwgeom/lwgeom_inout.c

  • Committer: Bazaar Package Importer
  • Author(s): Alex Bodnaru
  • Date: 2005-05-05 10:02:45 UTC
  • Revision ID: james.westby@ubuntu.com-20050505100245-3005l6jn1jwvpsrw
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

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