From 4f0da9b33ccbe8a827cdfe16814adcdd07cb0eb7 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Tue, 24 Aug 2004 14:47:25 +0000 Subject: [PATCH] Added LWGEOM_construct() function to easy the work of dealing with SRID/BBOX optional embedding. git-svn-id: http://svn.osgeo.org/postgis/trunk@735 b70326c6-7e19-0410-871a-916f4a2858ee --- lwgeom/lwgeom.h | 11 +++++++++ lwgeom/lwgeom_api.c | 58 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/lwgeom/lwgeom.h b/lwgeom/lwgeom.h index b56d0dafc..1013b2735 100644 --- a/lwgeom/lwgeom.h +++ b/lwgeom/lwgeom.h @@ -179,6 +179,17 @@ extern int lwgeom_getType(unsigned char type); // returns the tttt value extern unsigned char lwgeom_makeType(int ndims, char hasSRID, int type); extern unsigned char lwgeom_makeType_full(int ndims, char hasSRID, int type, bool hasBBOX); +/* + * Construct a full LWGEOM type (including size header) + * from a serialized form. + * The constructed LWGEOM object will be allocated using palloc + * and the serialized form will be copied. + * If you specify a SRID other then -1 it will be set. + * If you request bbox (wantbbox=1) it will be extracted or computed + * from the serialized form. + */ +extern LWGEOM *LWGEOM_construct(char *serialized, int SRID, int wantbbox); + // all the base types (point/line/polygon) will have a // basic constructor, basic de-serializer, basic serializer, and // bounding box finder. diff --git a/lwgeom/lwgeom_api.c b/lwgeom/lwgeom_api.c index f6a8184b0..a61db323c 100644 --- a/lwgeom/lwgeom_api.c +++ b/lwgeom/lwgeom_api.c @@ -2382,3 +2382,61 @@ LWGEOM *lwgeom_setSRID(LWGEOM *lwgeom, int32 newSRID) } return result; } + +LWGEOM * +LWGEOM_construct(char *ser, int SRID, int wantbbox) +{ + int size; + char *iptr, *optr, *eptr; + int wantsrid = 0; + BOX2DFLOAT4 box; + LWGEOM *result; + + size = lwgeom_seralizedformlength_simple(ser); + eptr = ser+size; // end of subgeom + + iptr = ser+1; // skip type + if ( lwgeom_hasSRID(ser[0]) ) + { + iptr += 4; // skip SRID + size -= 4; + } + if ( lwgeom_hasBBOX(ser[0]) ) + { + iptr += sizeof(BOX2DFLOAT4); // skip BBOX + size -= sizeof(BOX2DFLOAT4); + } + + if ( SRID != -1 ) + { + wantsrid = 1; + size += 4; + } + if ( wantbbox ) + { + size += sizeof(BOX2DFLOAT4); + getbox2d_p(ser, &box); + } + + size+=4; // size header + + result = palloc(size); + result->size = size; + + result->type = lwgeom_makeType_full(lwgeom_ndims(ser[0]), + wantsrid, lwgeom_getType(ser[0]), wantbbox); + optr = result->data; + if ( wantbbox ) + { + memcpy(optr, &box, sizeof(BOX2DFLOAT4)); + optr += sizeof(BOX2DFLOAT4); + } + if ( wantsrid ) + { + memcpy(optr, &SRID, 4); + optr += 4; + } + memcpy(optr, iptr, eptr-iptr); + + return result; +} -- 2.49.0