extern unsigned char parse_hex(char *str);
extern void deparse_hex(unsigned char str, unsigned char *result);
extern char *parse_lwgeom_wkt(char *wkt_input);
-extern char * lwgeom_to_wkt(LWGEOM *lwgeom);
+extern char *lwgeom_to_wkt(LWGEOM *lwgeom);
+extern char *lwgeom_to_hexwkb(LWGEOM *lwgeom, unsigned int byteorder);
extern void *lwalloc(size_t size);
extern void *lwrealloc(void *mem, size_t size);
return ret;
}
+/*
+ * Return an alloced string
+ */
+char *
+lwgeom_to_hexwkb(LWGEOM *lwgeom, unsigned int byteorder)
+{
+ char *serialized = lwgeom_serialize(lwgeom);
+ char *hexwkb = unparse_WKB(serialized, lwalloc, lwfree, byteorder);
+ lwfree(serialized);
+ return hexwkb;
+}
+
// geom1 same as geom2
// iff
// + have same type // + have same # objects
// Conversions
extern char *lwgeom_to_wkt(LWGEOM lwgeom);
+extern char *lwgeom_to_hexwkb(LWGEOM lwgeom, unsigned int byteorder);
// Construction
extern LWGEOM lwpoint_construct(int SRID, char wantbbox, POINTARRAY pa);
init_pg_func();
lwgeom = (PG_LWGEOM *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- result = unparse_WKB(SERIALIZED_FORM(lwgeom),lwalloc,lwfree);
+ result = unparse_WKB(SERIALIZED_FORM(lwgeom),lwalloc,lwfree,-1);
PG_RETURN_CSTRING(result);
}
int size_result;
char *semicolonLoc;
int t;
+ text *type;
+ unsigned int byteorder=-1;
init_pg_func();
+ if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
+ {
+ type = PG_GETARG_TEXT_P(1);
+ if (VARSIZE(type) < 7)
+ {
+ elog(ERROR,"asbinary(geometry, <type>) - type should be 'XDR' or 'NDR'. type length is %i",VARSIZE(type) -4);
+ PG_RETURN_NULL();
+ }
+
+ if ( ! strncmp(VARDATA(type), "xdr", 3) ||
+ ! strncmp(VARDATA(type), "XDR", 3) )
+ {
+ byteorder = BIG_ENDIAN;
+ }
+ else
+ {
+ byteorder = LITTLE_ENDIAN;
+ }
+ }
+
lwgeom_input = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- hexized_wkb_srid = unparse_WKB(SERIALIZED_FORM(lwgeom_input), lwalloc, lwfree);
+ hexized_wkb_srid = unparse_WKB(SERIALIZED_FORM(lwgeom_input),
+ lwalloc, lwfree, byteorder);
//elog(NOTICE, "in WKBFromLWGEOM with WKB = '%s'", hexized_wkb_srid);
PG_LWGEOM *result = NULL;
// read user-requested SRID if any
-#if USE_VERSION < 73
- if ( fcinfo->nargs > 1 )
-#else
if ( PG_NARGS() > 1 )
-#endif
{
SRID = PG_GETARG_INT32(1);
if ( SRID != lwgeom_getSRID(geom) )
#include "fmgr.h"
#include "liblwgeom.h"
+#ifndef PG_NARGS
+#define PG_NARGS() (fcinfo->nargs)
+#endif
+
void *pg_alloc(size_t size);
void *pg_realloc(void *ptr, size_t size);
void pg_free(void *ptr);
AS '@MODULE_FILENAME@','WKBFromLWGEOM'
LANGUAGE 'C' WITH (isstrict,iscachable);
+CREATEFUNCTION AsBinary(geometry,text)
+ RETURNS bytea
+ AS '@MODULE_FILENAME@','WKBFromLWGEOM'
+ LANGUAGE 'C' WITH (isstrict,iscachable);
+
CREATEFUNCTION AsText(geometry)
RETURNS TEXT
AS '@MODULE_FILENAME@','LWGEOM_asText'
// Construct a point LWGEOM
point = lwpoint_construct(-1, 0, pa);
- // Print WKT
+ // Print WKT end HEXWKB
printf("WKT: %s\n", lwgeom_to_wkt(point));
+ printf("HEXWKB: %s\n", lwgeom_to_hexwkb(point,-1));
// Construct a 5-points pointarray2d
pa = ptarray_construct2d(5, pts2d);
// Construct a line LWGEOM
line = lwline_construct(-1, 0, pa);
- // Print WKT
+ // Print WKT and HEXWKB
printf("WKT: %s\n", lwgeom_to_wkt(line));
+ printf("HEXWKB: %s\n", lwgeom_to_hexwkb(point,-1));
return 1;
}
byte* parse_lwg(const char* wkt,allocator allocfunc,report_error errfunc);
byte* parse_lwgi(const char* wkt,allocator allocfunc,report_error errfunc);
char* unparse_WKT(byte* serialized, allocator alloc,freeor free);
-char* unparse_WKB(byte* serialized, allocator alloc,freeor free);
+char* unparse_WKB(byte* serialized, allocator alloc,freeor free, unsigned int endian);
* Copyright Telogis 2004
* www.telogis.com
*
+ * $Id$
*/
+
#include "wktparse.h"
#include <string.h>
#include <math.h>
static int endian_check_int = 1; // dont modify this!!!
#define LITTLE_ENDIAN_CHECK 1
-static char getMachineEndian()
+static unsigned int getMachineEndian()
{
- return *((char *) &endian_check_int); // 0 = big endian, 1 = little endian
+ // 0 = big endian, 1 = little endian
+ if ( *((char *) &endian_check_int) )
+ {
+ return LITTLE_ENDIAN;
+ }
+ else
+ {
+ return BIG_ENDIAN;
+ }
}
//-- Typedefs ----------------------------------------------
byte* output_collection(byte* geom,outfunc func,int supress);
byte* output_collection_2(byte* geom,int suppress);
byte* output_multipoint(byte* geom,int suppress);
-void write_wkb_bytes(byte* ptr,int cnt);
+void write_wkb_bytes(byte* ptr,unsigned int cnt,size_t size);
void write_wkb_int(int i);
byte* output_wkb_collection(byte* geom,outwkbfunc func);
byte* output_wkb_collection_2(byte* geom);
static char* out_pos;
static int len;
static int lwgi;
+static int flipbytes;
//----------------------------------------------------------
static char outchr[]={"0123456789ABCDEF" };
void
-write_wkb_bytes(byte* ptr,int cnt){
- ensure(cnt*2);
+write_wkb_bytes(byte* ptr, unsigned int cnt, size_t size)
+{
+ unsigned int bc; // byte count
+
+ ensure(cnt*2*size);
while(cnt--){
- *out_pos++ = outchr[*ptr>>4];
- *out_pos++ = outchr[*ptr&0x0F];
- ptr ++;
+ if (flipbytes)
+ {
+ for(bc=size; bc; bc--)
+ {
+ *out_pos++ = outchr[ptr[bc-1]>>4];
+ *out_pos++ = outchr[ptr[bc-1]&0x0F];
+ }
+ }
+ else
+ {
+ for(bc=0; bc<size; bc++)
+ {
+ *out_pos++ = outchr[ptr[bc]>>4];
+ *out_pos++ = outchr[ptr[bc]&0x0F];
+ }
+ }
+ ptr+=size;
}
}
output_wkb_point(byte* geom)
{
if ( lwgi ){
- write_wkb_bytes(geom,dims*4);
+ write_wkb_bytes(geom,dims,4);
return geom + (4*dims);
}
else{
- write_wkb_bytes(geom,dims*8);
+ write_wkb_bytes(geom,dims,8);
return geom + (8*dims);
}
}
void
write_wkb_int(int i){
- write_wkb_bytes((byte*)&i,4);
+ write_wkb_bytes((byte*)&i,1,4);
}
byte *
{
unsigned char type=*geom++;
int4 wkbtype;
- byte endian;
dims = TYPE_NDIMS(type);
#ifdef DEBUG
if ( TYPE_HASM(type) )
wkbtype |= WKBMOFFSET;
- if ( getMachineEndian() != LITTLE_ENDIAN_CHECK ){
- endian=0;
- write_wkb_bytes(&endian,1);
- }
- else{
- endian=1;
- write_wkb_bytes(&endian,1);
- }
-
write_wkb_int(wkbtype);
switch(TYPE_GETTYPE(type)){
}
char *
-unparse_WKB(byte* serialized, allocator alloc, freeor free)
+unparse_WKB(byte* serialized, allocator alloc, freeor free, unsigned int endian)
{
+ byte endianbyte;
#ifdef DEBUG
lwnotice("unparse_WKB(%p,...) called", serialized);
out_start = out_pos = alloc(len);
lwgi=0;
+ if ( endian == -1 ) endian = getMachineEndian();
+
+ if ( endian == LITTLE_ENDIAN) endianbyte=1;
+ else endianbyte=0;
+
+ write_wkb_bytes(&endianbyte,1,1);
+
+ if ( endian != getMachineEndian() ) flipbytes = 1;
+ else flipbytes = 0;
+
output_wkb(serialized);
ensure(1);
*out_pos=0;
}
+/******************************************************************
+ * $Log$
+ * Revision 1.10 2004/10/11 14:03:33 strk
+ * Added endiannes specification to unparse_WKB, AsBinary, lwgeom_to_wkb.
+ *
+ ******************************************************************/