From c02b57d2878e60d0a55442838e7dc377e9876584 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Tue, 21 Dec 2004 15:19:01 +0000 Subject: [PATCH] Canonical binary reverted back to EWKB, now supporting SRID inclusion. git-svn-id: http://svn.osgeo.org/postgis/trunk@1174 b70326c6-7e19-0410-871a-916f4a2858ee --- lwgeom/lwgeom_inout.c | 100 ++++++++++++++---------------------------- lwgeom/lwgparse.c | 10 ++++- lwgeom/wktunparse.c | 17 ++++--- 3 files changed, 52 insertions(+), 75 deletions(-) diff --git a/lwgeom/lwgeom_inout.c b/lwgeom/lwgeom_inout.c index ad0278a37..0e7699aa7 100644 --- a/lwgeom/lwgeom_inout.c +++ b/lwgeom/lwgeom_inout.c @@ -155,70 +155,6 @@ Datum LWGEOM_to_text(PG_FUNCTION_ARGS) PG_RETURN_POINTER(text_result); } -// -// LWGEOM_to_bytea(LWGEOM) -// bytea contains canonical binary form, being -// internal representation with additional byte containing -// byte order -// -PG_FUNCTION_INFO_V1(LWGEOM_to_bytea); -Datum LWGEOM_to_bytea(PG_FUNCTION_ARGS) -{ - bytea *ret; - PG_LWGEOM *pglwg; - - pglwg = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - - ret = (bytea *)palloc(pglwg->size+5); - VARATT_SIZEP(ret) = pglwg->size+1; - *(VARDATA(ret)) = getMachineEndian(); - memcpy((void *)(VARDATA(ret)+1), SERIALIZED_FORM(pglwg), - pglwg->size-4); - -#if DEBUG - elog(NOTICE, "LWGEOM_to_bytea returning %d bytes (machine endian: %d)", - VARSIZE(ret), getMachineEndian()); -#endif - - PG_RETURN_POINTER(ret); -} - -// LWGEOM_from_bytea(bytea) -// bytea contains canonical binary form, being -// internal representation with additional byte containing -// byte order -// -PG_FUNCTION_INFO_V1(LWGEOM_from_bytea); -Datum LWGEOM_from_bytea(PG_FUNCTION_ARGS) -{ - bytea *in; - PG_LWGEOM *ret; - char flipbytes=0; - - in = (bytea *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - - ret = (PG_LWGEOM *)palloc(VARSIZE(in)-1); - ret->size = VARSIZE(in)-1; - - if ( *(VARDATA(in)) != getMachineEndian() ) flipbytes = 1; - if ( flipbytes ) - { - elog(ERROR, "Unable to handle bytes flipping"); - PG_RETURN_NULL(); - } - else - { - memcpy(SERIALIZED_FORM(ret), VARDATA(in)+1, VARSIZE(in)-5); - } - -#if DEBUG - elog(NOTICE, "LWGEOM_from_bytea returning %d bytes", ret->size); -#endif - - PG_RETURN_POINTER(ret); -} - - // LWGEOMFromWKB(wkb, [SRID] ) // NOTE: wkb is in *binary* not hex form. // @@ -528,7 +464,7 @@ Datum LWGEOM_recv(PG_FUNCTION_ARGS) /* Call LWGEOM_from_bytea function... */ result = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1( - LWGEOM_from_bytea, PointerGetDatum(wkb))); + LWGEOMFromWKB, PointerGetDatum(wkb))); #ifdef DEBUG elog(NOTICE, "LWGEOM_recv advancing StringInfo buffer"); @@ -552,14 +488,44 @@ Datum LWGEOM_recv(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_send); Datum LWGEOM_send(PG_FUNCTION_ARGS) { - PG_LWGEOM *result; + bytea *result; #ifdef DEBUG elog(NOTICE, "LWGEOM_send called"); #endif + result = (bytea *)DatumGetPointer(DirectFunctionCall1( + WKBFromLWGEOM, PG_GETARG_DATUM(0))); + + PG_RETURN_POINTER(result); +} + +PG_FUNCTION_INFO_V1(LWGEOM_to_bytea); +Datum LWGEOM_to_bytea(PG_FUNCTION_ARGS) +{ + bytea *result; + +#ifdef DEBUG + elog(NOTICE, "LWGEOM_to_bytea called"); +#endif + + result = (bytea *)DatumGetPointer(DirectFunctionCall1( + WKBFromLWGEOM, PG_GETARG_DATUM(0))); + + PG_RETURN_POINTER(result); +} + +PG_FUNCTION_INFO_V1(LWGEOM_from_bytea); +Datum LWGEOM_from_bytea(PG_FUNCTION_ARGS) +{ + PG_LWGEOM *result; + +#ifdef DEBUG + elog(NOTICE, "LWGEOM_from_bytea start"); +#endif + result = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1( - LWGEOM_to_bytea, PG_GETARG_DATUM(0))); + LWGEOMFromWKB, PG_GETARG_DATUM(0))); PG_RETURN_POINTER(result); } diff --git a/lwgeom/lwgparse.c b/lwgeom/lwgparse.c index 6947ef5fa..61d9a4380 100644 --- a/lwgeom/lwgparse.c +++ b/lwgeom/lwgparse.c @@ -738,6 +738,7 @@ parse_wkb(const char** b) int4 type; byte xdr = read_wkb_byte(b); swap_order=0; + int4 localsrid; if ( xdr != getMachineEndian() ) { @@ -765,8 +766,13 @@ parse_wkb(const char** b) if (type & WKBSRIDFLAG ) { - the_geom.hasZ = 1; - the_geom.ndims++; + // local (in-EWKB) srid spec overrides SRID=#; + localsrid = read_wkb_int(b); + if ( localsrid != -1 ) + { + if ( the_geom.srid == -1 ) the_geom.alloc_size += 4; + the_geom.srid = localsrid; + } } type &=0x0f; diff --git a/lwgeom/wktunparse.c b/lwgeom/wktunparse.c index 6e6a3384a..a2ff9b431 100644 --- a/lwgeom/wktunparse.c +++ b/lwgeom/wktunparse.c @@ -444,12 +444,6 @@ output_wkb(byte* geom) geom+=16; } - if ( TYPE_HASSRID(type) ) { - write_str("SRID="); - write_int(read_int(&geom)); - write_str(";"); - } - //type&=0x0f; wkbtype = TYPE_GETTYPE(type); @@ -457,12 +451,20 @@ output_wkb(byte* geom) wkbtype |= WKBZOFFSET; if ( TYPE_HASM(type) ) wkbtype |= WKBMOFFSET; + if ( TYPE_HASSRID(type) ) { + wkbtype |= WKBSRIDFLAG; + } + // Write byteorder (as from WKB specs...) write_wkb_bytes(&endianbyte,1,1); write_wkb_int(wkbtype); + if ( TYPE_HASSRID(type) ) { + write_wkb_int(read_int(&geom)); + } + switch(TYPE_GETTYPE(type)){ case POINTTYPE: geom=output_wkb_point(geom); @@ -543,6 +545,9 @@ unparse_WKB(byte* serialized, allocator alloc, freeor free, char endian) /****************************************************************** * $Log$ + * Revision 1.15 2004/12/21 15:19:01 strk + * Canonical binary reverted back to EWKB, now supporting SRID inclusion. + * * Revision 1.14 2004/12/17 11:08:53 strk * Moved getMachineEndian from parser to liblwgeom.{h,c}. * Added XDR and NDR defines. -- 2.50.1