11
#include "access/gist.h"
12
#include "access/itup.h"
13
#include "access/rtree.h"
16
#include "utils/elog.h"
18
# include "lib/stringinfo.h" // for binary input
22
#include "liblwgeom.h"
23
#include "stringBuffer.h"
26
//#define PGIS_DEBUG 1
28
#include "lwgeom_pg.h"
32
void elog_ERROR(const char* string);
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);
42
Datum LWGEOM_recv(PG_FUNCTION_ARGS);
43
Datum LWGEOM_send(PG_FUNCTION_ARGS);
45
Datum BOOL_to_text(PG_FUNCTION_ARGS);
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
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)
68
char *str = PG_GETARG_CSTRING(0);
69
char *semicolonLoc,start;
72
//determine if its WKB or WKT
74
semicolonLoc = strchr(str,';');
75
if (semicolonLoc == NULL)
81
start=semicolonLoc[1]; // one in
84
// will handle both EWKB and EWKT
85
ret = (PG_LWGEOM *)parse_lwgeom_wkt(str);
87
if ( is_worth_caching_pglwgeom_bbox(ret) )
89
ret = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1(
90
LWGEOM_addBBOX, PointerGetDatum(ret)));
93
PG_RETURN_POINTER(ret);
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)
110
lwgeom = (PG_LWGEOM *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
111
result = unparse_WKB(SERIALIZED_FORM(lwgeom),lwalloc,lwfree,-1,NULL,1);
113
PG_RETURN_CSTRING(result);
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)
131
lwgeom = (PG_LWGEOM *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
132
result = unparse_WKB(SERIALIZED_FORM(lwgeom),lwalloc,lwfree,-1,&size,1);
134
text_result = palloc(size+VARHDRSZ);
135
memcpy(VARDATA(text_result),result,size);
136
VARATT_SIZEP(text_result) = size+VARHDRSZ;
139
PG_RETURN_POINTER(text_result);
142
// LWGEOMFromWKB(wkb, [SRID] )
143
// NOTE: wkb is in *binary* not hex form.
145
// NOTE: this function is unoptimized, as it convert binary
146
// form to hex form and then calls the unparser ...
148
PG_FUNCTION_INFO_V1(LWGEOMFromWKB);
149
Datum LWGEOMFromWKB(PG_FUNCTION_ARGS)
151
WellKnownBinary *wkb_input;
152
char *wkb_srid_hexized;
153
int size_result,size_header;
160
wkb_input = (WellKnownBinary *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
162
if ( ( PG_NARGS()>1) && ( ! PG_ARGISNULL(1) ))
163
SRID = PG_GETARG_INT32(1);
168
elog(NOTICE,"LWGEOMFromWKB: entry with SRID=%i",SRID);
171
// convert WKB to hexized WKB string
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;
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=#;"
182
for (t=0; t< (wkb_input->size -VARHDRSZ); t++)
184
deparse_hex( ((uchar *) wkb_input)[4 + t], &loc[t*2]);
187
wkb_srid_hexized[size_result-1] = 0; // null term
190
elog(NOTICE,"size_header = %i",size_header);
191
elog(NOTICE,"size_result = %i", size_result);
192
elog(NOTICE,"LWGEOMFromWKB :: '%s'", wkb_srid_hexized);
195
lwgeom = (PG_LWGEOM *)parse_lwgeom_wkt(wkb_srid_hexized);
197
pfree(wkb_srid_hexized);
199
if ( is_worth_caching_pglwgeom_bbox(lwgeom) )
201
lwgeom = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1(
202
LWGEOM_addBBOX, PointerGetDatum(lwgeom)));
206
elog(NOTICE, "LWGEOMFromWKB returning %s", unparse_WKB(SERIALIZED_FORM(lwgeom), pg_alloc, pg_free, -1, NULL, 1));
209
PG_RETURN_POINTER(lwgeom);
212
// WKBFromLWGEOM(lwgeom) --> wkb
213
// this will have no 'SRID=#;'
214
PG_FUNCTION_INFO_V1(WKBFromLWGEOM);
215
Datum WKBFromLWGEOM(PG_FUNCTION_ARGS)
217
//#define BINARY_FROM_HEX 1
219
PG_LWGEOM *lwgeom_input; // SRID=#;<hexized wkb>
220
char *hexized_wkb; // hexized_wkb_srid w/o srid
223
#ifdef BINARY_FROM_HEX
224
char *hexized_wkb_srid;
227
#endif // BINARY_FROM_HEX
229
unsigned int byteorder=-1;
233
profstart(PROF_QRUN);
238
if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
240
type = PG_GETARG_TEXT_P(1);
241
if (VARSIZE(type) < 7)
243
elog(ERROR,"asbinary(geometry, <type>) - type should be 'XDR' or 'NDR'. type length is %i",VARSIZE(type) -VARHDRSZ);
247
if ( ! strncmp(VARDATA(type), "xdr", 3) ||
248
! strncmp(VARDATA(type), "XDR", 3) )
258
lwgeom_input = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
260
#ifdef BINARY_FROM_HEX
261
hexized_wkb_srid = unparse_WKB(SERIALIZED_FORM(lwgeom_input),
262
lwalloc, lwfree, byteorder, &size, 1);
264
//elog(NOTICE, "in WKBFromLWGEOM with WKB = '%s'", hexized_wkb_srid);
266
hexized_wkb = hexized_wkb_srid;
268
semicolonLoc = strchr(hexized_wkb_srid,';');
269
if (semicolonLoc != NULL)
271
hexized_wkb = (semicolonLoc+1);
274
//elog(NOTICE, "in WKBFromLWGEOM with WKB (with no 'SRID=#;' = '%s'", hexized_wkb);
276
size_result = size/2 + VARHDRSZ;
277
result = palloc(size_result);
279
memcpy(result, &size_result,VARHDRSZ); // size header
281
// have a hexized string, want to make it binary
282
for (t=0; t< (size/2); t++)
284
((uchar *) result +VARHDRSZ)[t] = parse_hex( hexized_wkb + (t*2) );
287
pfree(hexized_wkb_srid);
289
#else // ndef BINARY_FROM_HEX
291
hexized_wkb = unparse_WKB(SERIALIZED_FORM(lwgeom_input),
292
lwalloc, lwfree, byteorder, &size, 0);
294
size_result = size+VARHDRSZ;
295
result = palloc(size_result);
296
memcpy(result, &size_result, VARHDRSZ);
297
memcpy(VARDATA(result), hexized_wkb, size);
304
lwnotice("unparse_WKB: prof: %lu", proftime[PROF_QRUN]);
308
lwnotice("Output size is %lu (comp: %lu)",
309
VARSIZE(result), (unsigned long)size);
310
#endif // def PGIS_DEBUG
313
PG_RETURN_POINTER(result);
316
// puts a bbox inside the geometry
317
PG_FUNCTION_INFO_V1(LWGEOM_addBBOX);
318
Datum LWGEOM_addBBOX(PG_FUNCTION_ARGS)
320
PG_LWGEOM *lwgeom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
326
//elog(NOTICE,"in LWGEOM_addBBOX");
328
if (lwgeom_hasBBOX( lwgeom->type ) )
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);
337
//elog(NOTICE,"LWGEOM_addBBOX -- giving it a bbox");
340
if ( ! getbox2d_p(SERIALIZED_FORM(lwgeom), &box) )
342
// Empty geom, no bbox to add
343
result = palloc (lwgeom->size);
344
memcpy(result, lwgeom, lwgeom->size);
345
PG_RETURN_POINTER(result);
347
old_type = lwgeom->type;
349
size = lwgeom->size+sizeof(BOX2DFLOAT4);
351
result = palloc(size);// 16 for bbox2d
354
result->type = lwgeom_makeType_full(
357
lwgeom_hasSRID(old_type), lwgeom_getType(old_type), 1);
359
memcpy(result->data, &box, sizeof(BOX2DFLOAT4));
361
//lwnotice("result->type hasbbox: %d", TYPE_HASBBOX(result->type));
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);
367
PG_RETURN_POINTER(result);
371
is_worth_caching_pglwgeom_bbox(const PG_LWGEOM *in)
376
if ( TYPE_GETTYPE(in->type) == POINTTYPE ) return false;
381
is_worth_caching_serialized_bbox(const uchar *in)
386
if ( TYPE_GETTYPE((uchar)in[0]) == POINTTYPE ) return false;
391
is_worth_caching_lwgeom_bbox(const LWGEOM *in)
396
if ( TYPE_GETTYPE(in->type) == POINTTYPE ) return false;
400
// removes a bbox from a geometry
401
PG_FUNCTION_INFO_V1(LWGEOM_dropBBOX);
402
Datum LWGEOM_dropBBOX(PG_FUNCTION_ARGS)
404
PG_LWGEOM *lwgeom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
409
//elog(NOTICE,"in LWGEOM_dropBBOX");
411
if (!lwgeom_hasBBOX( lwgeom->type ) )
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);
419
//elog(NOTICE,"LWGEOM_dropBBOX -- dropping the bbox");
422
old_type = lwgeom->type;
424
size = lwgeom->size-sizeof(BOX2DFLOAT4);
426
result = palloc(size);// 16 for bbox2d
429
result->type = lwgeom_makeType_full(
432
lwgeom_hasSRID(old_type), lwgeom_getType(old_type), 0);
434
// everything but the type and length
435
memcpy(result->data, lwgeom->data+sizeof(BOX2DFLOAT4), lwgeom->size-5-sizeof(BOX2DFLOAT4));
437
PG_RETURN_POINTER(result);
443
void elog_ERROR(const char* string)
449
// parse_WKT_lwgeom(TEXT) -> LWGEOM
450
PG_FUNCTION_INFO_V1(parse_WKT_lwgeom);
451
Datum parse_WKT_lwgeom(PG_FUNCTION_ARGS)
454
text *wkt_input = PG_GETARG_TEXT_P(0);
455
PG_LWGEOM *ret; //with length
461
wkt_size = VARSIZE(wkt_input)-VARHDRSZ; // actual letters
462
//(*(int*) wkt_input) -4;
464
wkt = palloc( wkt_size+1); //+1 for null
465
memcpy(wkt, VARDATA(wkt_input), wkt_size );
466
wkt[wkt_size] = 0; // null term
469
//elog(NOTICE,"in parse_WKT_lwgeom");
470
//elog(NOTICE,"in parse_WKT_lwgeom with input: '%s'",wkt);
472
ret = (PG_LWGEOM *)parse_lwg((const char *)wkt, (allocator)lwalloc, (report_error)elog_ERROR);
473
//elog(NOTICE,"parse_WKT_lwgeom:: finished parse");
476
if (ret == NULL) elog(ERROR,"parse_WKT:: couldnt parse!");
478
if ( is_worth_caching_pglwgeom_bbox(ret) )
480
ret = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1(
481
LWGEOM_addBBOX, PointerGetDatum(ret)));
484
PG_RETURN_POINTER(ret);
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 #
496
PG_FUNCTION_INFO_V1(LWGEOM_recv);
497
Datum LWGEOM_recv(PG_FUNCTION_ARGS)
499
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
504
elog(NOTICE, "LWGEOM_recv start");
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);
513
elog(NOTICE, "LWGEOM_recv calling LWGEOMFromWKB");
516
/* Call LWGEOM_from_bytea function... */
517
result = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1(
518
LWGEOMFromWKB, PointerGetDatum(wkb)));
521
elog(NOTICE, "LWGEOM_recv advancing StringInfo buffer");
525
elog(NOTICE, "LWGEOM_from_bytea returned %s", unparse_WKB(SERIALIZED_FORM(result),pg_alloc,pg_free,-1,NULL,1));
529
/* Set cursor to the end of buffer (so the backend is happy) */
530
buf->cursor = buf->len;
533
elog(NOTICE, "LWGEOM_recv returning");
536
PG_RETURN_POINTER(result);
539
PG_FUNCTION_INFO_V1(LWGEOM_send);
540
Datum LWGEOM_send(PG_FUNCTION_ARGS)
545
elog(NOTICE, "LWGEOM_send called");
548
result = (bytea *)DatumGetPointer(DirectFunctionCall1(
549
WKBFromLWGEOM, PG_GETARG_DATUM(0)));
551
PG_RETURN_POINTER(result);
554
#endif // USE_VERSION > 73
556
PG_FUNCTION_INFO_V1(LWGEOM_to_bytea);
557
Datum LWGEOM_to_bytea(PG_FUNCTION_ARGS)
562
elog(NOTICE, "LWGEOM_to_bytea called");
565
result = (bytea *)DatumGetPointer(DirectFunctionCall1(
566
WKBFromLWGEOM, PG_GETARG_DATUM(0)));
568
PG_RETURN_POINTER(result);
571
PG_FUNCTION_INFO_V1(LWGEOM_from_bytea);
572
Datum LWGEOM_from_bytea(PG_FUNCTION_ARGS)
577
elog(NOTICE, "LWGEOM_from_bytea start");
580
result = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1(
581
LWGEOMFromWKB, PG_GETARG_DATUM(0)));
583
PG_RETURN_POINTER(result);
586
PG_FUNCTION_INFO_V1(BOOL_to_text);
587
Datum BOOL_to_text(PG_FUNCTION_ARGS)
589
bool b = PG_GETARG_BOOL(0);
595
result = palloc(VARHDRSZ+1*sizeof(char));
596
VARATT_SIZEP(result) = VARHDRSZ+1*sizeof(char);
597
memcpy(VARDATA(result), &c, 1*sizeof(char));
599
PG_RETURN_POINTER(result);