From: Mark Cave-Ayland Date: Sun, 28 Sep 2008 19:48:17 +0000 (+0000) Subject: Update LWGEOM unparser to (E)WKT/WKB to resturn a LWGEOM_UNPARSER_RESULT structure... X-Git-Tag: 1.4.0b1~686 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=74949bc75590aa103c000223ce9356b8a36c4799;p=postgis Update LWGEOM unparser to (E)WKT/WKB to resturn a LWGEOM_UNPARSER_RESULT structure instead of just the WKT/WKB character array. This is the same work done for r3023 but applied to the unparser instead. git-svn-id: http://svn.osgeo.org/postgis/trunk@3026 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/liblwgeom/examples/unparser.c b/liblwgeom/examples/unparser.c index 7bd197c97..765e66736 100644 --- a/liblwgeom/examples/unparser.c +++ b/liblwgeom/examples/unparser.c @@ -39,10 +39,11 @@ int main() /* * An example to show how to call the WKT/WKB unparsers in liblwgeom */ + LWGEOM_UNPARSER_RESULT lwg_unparser_result; + int result; LWGEOM *lwgeom; - uchar *serialized_lwgeom, *wkt, *wkb; - size_t wkb_size; + uchar *serialized_lwgeom; POINTARRAY *pa; POINT2D point2d; @@ -69,14 +70,13 @@ int main() serialized_lwgeom = lwgeom_serialize(lwgeom); /* Output the geometry in WKT and WKB */ - wkt = serialized_lwgeom_to_ewkt(serialized_lwgeom, PARSER_CHECK_ALL); - printf("WKT format : %s\n", wkt); - wkb = serialized_lwgeom_to_hexwkb(serialized_lwgeom, PARSER_CHECK_ALL, NDR, &wkb_size); - printf("HEXWKB format : %s\n\n", wkb); + result = serialized_lwgeom_to_ewkt(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_ALL); + printf("WKT format : %s\n", lwg_unparser_result.wkoutput); + result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_ALL, NDR); + printf("HEXWKB format : %s\n\n", lwg_unparser_result.wkoutput); /* Free all of the allocated items */ - lwfree(wkb); - lwfree(wkt); + lwfree(lwg_unparser_result.wkoutput); lwfree(serialized_lwgeom); pfree_point(testpoint); @@ -105,14 +105,13 @@ int main() serialized_lwgeom = lwgeom_serialize(lwgeom); /* Output the geometry in WKT and WKB */ - wkt = serialized_lwgeom_to_ewkt(serialized_lwgeom, PARSER_CHECK_ALL); - printf("WKT format : %s\n", wkt); - wkb = serialized_lwgeom_to_hexwkb(serialized_lwgeom, PARSER_CHECK_ALL, NDR, &wkb_size); - printf("HEXWKB format : %s\n\n", wkb); + result = serialized_lwgeom_to_ewkt(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_ALL); + printf("WKT format : %s\n", lwg_unparser_result.wkoutput); + result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_ALL, NDR); + printf("HEXWKB format : %s\n\n", lwg_unparser_result.wkoutput); /* Free all of the allocated items */ - lwfree(wkb); - lwfree(wkt); + lwfree(lwg_unparser_result.wkoutput); lwfree(serialized_lwgeom); pfree_line(testline); @@ -177,14 +176,13 @@ int main() serialized_lwgeom = lwgeom_serialize(lwgeom); /* Output the geometry in WKT and WKB */ - wkt = serialized_lwgeom_to_ewkt(serialized_lwgeom, PARSER_CHECK_NONE); - printf("WKT format : %s\n", wkt); - wkb = serialized_lwgeom_to_hexwkb(serialized_lwgeom, PARSER_CHECK_NONE, NDR, &wkb_size); - printf("HEXWKB format : %s\n\n", wkb); + result = serialized_lwgeom_to_ewkt(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_NONE); + printf("WKT format : %s\n", lwg_unparser_result.wkoutput); + result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_NONE, NDR); + printf("HEXWKB format : %s\n\n", lwg_unparser_result.wkoutput); /* Free all of the allocated items */ - lwfree(wkb); - lwfree(wkt); + lwfree(lwg_unparser_result.wkoutput); lwfree(serialized_lwgeom); pfree_polygon(testpoly); } diff --git a/liblwgeom/liblwgeom.h b/liblwgeom/liblwgeom.h index dc9a1bdf7..09f0d3e41 100644 --- a/liblwgeom/liblwgeom.h +++ b/liblwgeom/liblwgeom.h @@ -1121,10 +1121,18 @@ extern void deparse_hex(uchar str, char *result); */ typedef struct struct_lwgeom_parser_result { - uchar *serialized_lwgeom; - int size; + uchar *serialized_lwgeom; /* Pointer to serialized LWGEOM */ + int size; /* Size of serialized LWGEOM in bytes */ } LWGEOM_PARSER_RESULT; +/* + * Unparser result structure: returns the result of attempting to convert LWGEOM to (E)WKT/(E)WKB + */ +typedef struct struct_lwgeom_unparser_result +{ + char *wkoutput; /* Pointer to WKT or WKB output */ + int size; /* Size of serialized LWGEOM in bytes */ +} LWGEOM_UNPARSER_RESULT; /* Parser access routines */ extern char *lwgeom_to_ewkt(LWGEOM *lwgeom, int flags); @@ -1132,10 +1140,10 @@ extern char *lwgeom_to_hexwkb(LWGEOM *lwgeom, int flags, unsigned int byteorder) extern LWGEOM *lwgeom_from_ewkb(uchar *ewkb, int flags, size_t ewkblen); extern uchar *lwgeom_to_ewkb(LWGEOM *lwgeom, int flags, char byteorder, size_t *ewkblen); -extern char *serialized_lwgeom_to_ewkt(uchar *serialized, int flags); +extern int serialized_lwgeom_to_ewkt(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar *serialized, int flags); extern int serialized_lwgeom_from_ewkt(LWGEOM_PARSER_RESULT *lwg_parser_result, char *wkt_input, int flags); -extern char *serialized_lwgeom_to_hexwkb(uchar *serialized, int flags, unsigned int byteorder, size_t *size); -extern char *serialized_lwgeom_to_ewkb(uchar *serialized, int flags, unsigned int byteorder, size_t *size); +extern int serialized_lwgeom_to_hexwkb(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar *serialized, int flags, unsigned int byteorder); +extern int serialized_lwgeom_to_ewkb(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar *serialized, int flags, unsigned int byteorder); extern void *lwalloc(size_t size); diff --git a/liblwgeom/lwgeom.c b/liblwgeom/lwgeom.c index 2d23294d4..6af11db6a 100644 --- a/liblwgeom/lwgeom.c +++ b/liblwgeom/lwgeom.c @@ -459,14 +459,18 @@ lwgeom_add(const LWGEOM *to, uint32 where, const LWGEOM *what) char * lwgeom_to_ewkt(LWGEOM *lwgeom, int flags) { + LWGEOM_UNPARSER_RESULT lwg_unparser_result; uchar *serialized = lwgeom_serialize(lwgeom); - char *ret; + int result; + if ( ! serialized ) { lwerror("Error serializing geom %p", lwgeom); } - ret = unparse_WKT(serialized, lwalloc, lwfree, flags); + + result = unparse_WKT(&lwg_unparser_result, serialized, lwalloc, lwfree, flags); lwfree(serialized); - return ret; + + return lwg_unparser_result.wkoutput; } /* @@ -475,10 +479,14 @@ lwgeom_to_ewkt(LWGEOM *lwgeom, int flags) char * lwgeom_to_hexwkb(LWGEOM *lwgeom, int flags, unsigned int byteorder) { + LWGEOM_UNPARSER_RESULT lwg_unparser_result; uchar *serialized = lwgeom_serialize(lwgeom); - char *hexwkb = unparse_WKB(serialized, lwalloc, lwfree, flags, byteorder,NULL,1); + int result; + + result = unparse_WKB(&lwg_unparser_result, serialized, lwalloc, lwfree, flags, byteorder,1); + lwfree(serialized); - return hexwkb; + return lwg_unparser_result.wkoutput; } /* @@ -487,17 +495,19 @@ lwgeom_to_hexwkb(LWGEOM *lwgeom, int flags, unsigned int byteorder) uchar * lwgeom_to_ewkb(LWGEOM *lwgeom, int flags, char byteorder, size_t *outsize) { + LWGEOM_UNPARSER_RESULT lwg_unparser_result; uchar *serialized = lwgeom_serialize(lwgeom); + int result; /* * We cast return to "unsigned" char as we are * requesting a "binary" output, not HEX * (last argument set to 0) */ - uchar *hexwkb = (uchar *)unparse_WKB(serialized, lwalloc, lwfree, - flags, byteorder, outsize, 0); + result = unparse_WKB(&lwg_unparser_result, serialized, lwalloc, lwfree, + flags, byteorder, 0); lwfree(serialized); - return hexwkb; + return (uchar *)lwg_unparser_result.wkoutput; } /* @@ -558,28 +568,40 @@ serialized_lwgeom_from_ewkt(LWGEOM_PARSER_RESULT *lwg_parser_result, char *wkt_i /* * Return an alloced string */ -char * -serialized_lwgeom_to_ewkt(uchar *serialized, int flags) +int +serialized_lwgeom_to_ewkt(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar *serialized, int flags) { - return unparse_WKT(serialized, lwalloc, lwfree, flags); + int result; + + result = unparse_WKT(lwg_unparser_result, serialized, lwalloc, lwfree, flags); + + return result; } /* * Return an alloced string */ -char * -serialized_lwgeom_to_hexwkb(uchar *serialized, int flags, unsigned int byteorder, size_t *size) +int +serialized_lwgeom_to_hexwkb(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar *serialized, int flags, unsigned int byteorder) { - return unparse_WKB(serialized, lwalloc, lwfree, flags, byteorder, size, 1); + int result; + + result = unparse_WKB(lwg_unparser_result, serialized, lwalloc, lwfree, flags, byteorder, 1); + + return result; } /* * Return an alloced string */ -char * -serialized_lwgeom_to_ewkb(uchar *serialized, int flags, unsigned int byteorder, size_t *size) +int +serialized_lwgeom_to_ewkb(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar *serialized, int flags, unsigned int byteorder) { - return unparse_WKB(serialized, lwalloc, lwfree, flags, byteorder, size, 0); + int result; + + result = unparse_WKB(lwg_unparser_result, serialized, lwalloc, lwfree, flags, byteorder, 0); + + return result; } diff --git a/liblwgeom/lwgparse.c b/liblwgeom/lwgparse.c index 59b2fbdd4..df81045e3 100644 --- a/liblwgeom/lwgparse.c +++ b/liblwgeom/lwgparse.c @@ -1135,6 +1135,7 @@ parse_it(LWGEOM_PARSER_RESULT *lwg_parser_result, const char *geometry, int flag /* Setup the inital parser flags and empty the return struct */ parser_check_flags = flags; lwg_parser_result->serialized_lwgeom = NULL; + lwg_parser_result->size = 0; init_parser(geometry); diff --git a/liblwgeom/lwgunparse.c b/liblwgeom/lwgunparse.c index 629e85cd6..9a03d281c 100644 --- a/liblwgeom/lwgunparse.c +++ b/liblwgeom/lwgunparse.c @@ -44,7 +44,6 @@ uchar* output_line_collection(uchar* geom,outfunc func,int supress); uchar* output_polygon_collection(uchar* geom,int suppress); uchar* output_polygon_ring_collection(uchar* geom,outfunc func,int supress); uchar* output_curve_collection(uchar* geom,outfunc func,int supress); -uchar* output_collection_2(uchar* geom,int suppress); uchar* output_multipoint(uchar* geom,int suppress); uchar* output_compound(uchar* geom, int suppress); uchar* output_multisurface(uchar* geom, int suppress); @@ -81,6 +80,11 @@ void (*write_wkb_bytes)(uchar* ptr,unsigned int cnt,size_t size); */ int unparser_check_flags; +/* + * Unparser result structure + */ +LWGEOM_UNPARSER_RESULT *unparser_result; + /*---------------------------------------------------------- */ @@ -565,18 +569,21 @@ output_wkt(uchar* geom, int supress) return geom; } -char * -unparse_WKT(uchar* serialized, allocator alloc, freeor free, int flags) +int +unparse_WKT(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar* serialized, allocator alloc, freeor free, int flags) { LWDEBUGF(2, "unparse_WKT called with parser flags %d.", flags); if (serialized==NULL) - return NULL; + return 0; - /* Setup the inital parser flags */ + /* Setup the inital parser flags and empty the return struct */ unparser_check_flags = flags; + lwg_unparser_result->wkoutput = NULL; + lwg_unparser_result->size = 0; + unparser_result = lwg_unparser_result; local_malloc=alloc; local_free=free; len = 128; @@ -585,7 +592,11 @@ unparse_WKT(uchar* serialized, allocator alloc, freeor free, int flags) output_wkt(serialized, 0); - return out_start; + /* Store the result in the struct */ + lwg_unparser_result->wkoutput = out_start; + lwg_unparser_result->size = strlen(out_start); + + return -1; } static char outchr[]={"0123456789ABCDEF" }; @@ -873,17 +884,20 @@ output_wkb(uchar* geom) return geom; } -char * -unparse_WKB(uchar* serialized, allocator alloc, freeor free, int flags, char endian, size_t *outsize, uchar hex) +int +unparse_WKB(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar* serialized, allocator alloc, freeor free, int flags, char endian, uchar hex) { LWDEBUGF(2, "unparse_WKB(%p,...) called with parser flags %d", serialized, flags); - if (serialized==NULL) - return NULL; + if (serialized==0) + return 0; - /* Setup the inital parser flags */ + /* Setup the inital parser flags and empty the return struct */ unparser_check_flags = flags; + lwg_unparser_result->wkoutput = NULL; + lwg_unparser_result->size = 0; + unparser_result = lwg_unparser_result; local_malloc=alloc; local_free=free; len = 128; @@ -918,9 +932,11 @@ unparse_WKB(uchar* serialized, allocator alloc, freeor free, int flags, char end *out_pos=0; } - if ( outsize ) *outsize = (out_pos-out_start); + /* Store the result in the struct */ + lwg_unparser_result->wkoutput = out_start; + lwg_unparser_result->size = (out_pos-out_start); - return out_start; + return -1; } diff --git a/liblwgeom/wktparse.h b/liblwgeom/wktparse.h index 37fa2db7f..e02775cdf 100644 --- a/liblwgeom/wktparse.h +++ b/liblwgeom/wktparse.h @@ -24,6 +24,12 @@ typedef struct struct_lwgeom_parser_result { SERIALIZED_LWGEOM *serialized_lwgeom; } LWGEOM_PARSER_RESULT; + +typedef struct struct_lwgeom_unparser_result +{ + char *wkoutput; + int size; +} LWGEOM_UNPARSER_RESULT; #endif typedef void* (*allocator)(size_t size); typedef void (*freeor)(void* mem); @@ -103,8 +109,8 @@ void alloc_wkb(const char* parser); int parse_lwg(LWGEOM_PARSER_RESULT *lwg_parser_result, const char* wkt, int flags, allocator allocfunc,report_error errfunc); int parse_lwgi(LWGEOM_PARSER_RESULT *lwg_parser_result, const char* wkt, int flags, allocator allocfunc,report_error errfunc); -char* unparse_WKT(uchar* serialized, allocator alloc, freeor free, int flags); -char* unparse_WKB(uchar* serialized, allocator alloc, freeor free, int flags, char endian, size_t *outsize, uchar hexform); +int unparse_WKT(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar* serialized, allocator alloc, freeor free, int flags); +int unparse_WKB(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar* serialized, allocator alloc, freeor free, int flags, char endian, uchar hexform); int lwg_parse_yyparse(void); int lwg_parse_yyerror(char* s); diff --git a/lwgeom/lwgeom_functions_basic.c b/lwgeom/lwgeom_functions_basic.c index dc4f6bc9e..bcd42ac8c 100644 --- a/lwgeom/lwgeom_functions_basic.c +++ b/lwgeom/lwgeom_functions_basic.c @@ -2903,34 +2903,35 @@ Datum LWGEOM_setpoint_linestring(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_asEWKT); Datum LWGEOM_asEWKT(PG_FUNCTION_ARGS) { + LWGEOM_UNPARSER_RESULT lwg_unparser_result; PG_LWGEOM *lwgeom; - char *result_cstring; - int len; - char *result,*loc_wkt; + int len, result; + char *lwgeom_result,*loc_wkt; /*char *semicolonLoc; */ lwgeom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - result_cstring = serialized_lwgeom_to_ewkt(SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL); + + result = serialized_lwgeom_to_ewkt(&lwg_unparser_result, SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL); #if 0 - semicolonLoc = strchr(result_cstring,';'); + semicolonLoc = strchr(lwg_unparser_result.wkb,';'); /*loc points to start of wkt */ - if (semicolonLoc == NULL) loc_wkt = result_cstring; + if (semicolonLoc == NULL) loc_wkt = lwg_unparser_result.wkoutput; else loc_wkt = semicolonLoc +1; #endif - loc_wkt = result_cstring; + loc_wkt = lwg_unparser_result.wkoutput; len = strlen(loc_wkt); - result = palloc(len + VARHDRSZ); - SET_VARSIZE(result, len + VARHDRSZ); + lwgeom_result = palloc(len + VARHDRSZ); + SET_VARSIZE(lwgeom_result, len + VARHDRSZ); - memcpy(VARDATA(result), loc_wkt, len); + memcpy(VARDATA(lwgeom_result), loc_wkt, len); - pfree(result_cstring); + pfree(lwg_unparser_result.wkoutput); PG_FREE_IF_COPY(lwgeom, 0); - PG_RETURN_POINTER(result); + PG_RETURN_POINTER(lwgeom_result); } /* diff --git a/lwgeom/lwgeom_inout.c b/lwgeom/lwgeom_inout.c index 7d9aad626..bc36002a0 100644 --- a/lwgeom/lwgeom_inout.c +++ b/lwgeom/lwgeom_inout.c @@ -95,13 +95,14 @@ Datum LWGEOM_in(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_out); Datum LWGEOM_out(PG_FUNCTION_ARGS) { + LWGEOM_UNPARSER_RESULT lwg_unparser_result; PG_LWGEOM *lwgeom; - char *result; + int result; lwgeom = (PG_LWGEOM *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - result = serialized_lwgeom_to_hexwkb(SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL, -1, NULL); + result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL, -1); - PG_RETURN_CSTRING(result); + PG_RETURN_CSTRING(lwg_unparser_result.wkoutput); } /* @@ -110,10 +111,10 @@ Datum LWGEOM_out(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_asHEXEWKB); Datum LWGEOM_asHEXEWKB(PG_FUNCTION_ARGS) { + LWGEOM_UNPARSER_RESULT lwg_unparser_result; PG_LWGEOM *lwgeom; - char *result; + int result; text *text_result; - size_t size; text *type; unsigned int byteorder=-1; @@ -139,12 +140,12 @@ Datum LWGEOM_asHEXEWKB(PG_FUNCTION_ARGS) } } - result = serialized_lwgeom_to_hexwkb(SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL, byteorder, &size); + result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL, byteorder); - text_result = palloc(size+VARHDRSZ); - memcpy(VARDATA(text_result),result,size); - SET_VARSIZE(text_result, size+VARHDRSZ); - pfree(result); + text_result = palloc(lwg_unparser_result.size+VARHDRSZ); + memcpy(VARDATA(text_result),lwg_unparser_result.wkoutput,lwg_unparser_result.size); + SET_VARSIZE(text_result, lwg_unparser_result.size+VARHDRSZ); + pfree(lwg_unparser_result.wkoutput); PG_RETURN_POINTER(text_result); @@ -160,18 +161,18 @@ Datum LWGEOM_asHEXEWKB(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_to_text); Datum LWGEOM_to_text(PG_FUNCTION_ARGS) { + LWGEOM_UNPARSER_RESULT lwg_unparser_result; PG_LWGEOM *lwgeom; - char *result; + int result; text *text_result; - size_t size; lwgeom = (PG_LWGEOM *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - result = serialized_lwgeom_to_hexwkb(SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL, -1, &size); + result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL, -1); - text_result = palloc(size+VARHDRSZ); - memcpy(VARDATA(text_result),result,size); - SET_VARSIZE(text_result, size+VARHDRSZ); - pfree(result); + text_result = palloc(lwg_unparser_result.size+VARHDRSZ); + memcpy(VARDATA(text_result),lwg_unparser_result.wkoutput,lwg_unparser_result.size); + SET_VARSIZE(text_result, lwg_unparser_result.size+VARHDRSZ); + pfree(lwg_unparser_result.wkoutput); PG_RETURN_POINTER(text_result); } @@ -227,18 +228,18 @@ Datum WKBFromLWGEOM(PG_FUNCTION_ARGS) { /* #define BINARY_FROM_HEX 1 */ + LWGEOM_UNPARSER_RESULT lwg_unparser_result; PG_LWGEOM *lwgeom_input; /* SRID=#; */ - char *hexized_wkb; /* hexized_wkb_srid w/o srid */ - char *result; /* wkb */ + char *lwgeom_result; + int result; int size_result; #ifdef BINARY_FROM_HEX - char *hexized_wkb_srid; + char *hexized_wkb; /* hexized_wkb_srid w/o srid */ char *semicolonLoc; int t; #endif /* BINARY_FROM_HEX */ text *type; unsigned int byteorder=-1; - size_t size; #ifdef PROFILE profstart(PROF_QRUN); @@ -267,13 +268,13 @@ Datum WKBFromLWGEOM(PG_FUNCTION_ARGS) lwgeom_input = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); #ifdef BINARY_FROM_HEX - hexized_wkb_srid = serialized_lwgeom_to_hexwkb(SERIALIZED_FORM(lwgeom_input), byteorder, &size); + result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, SERIALIZED_FORM(lwgeom_input), byteorder); - LWDEBUGF(3, "in WKBFromLWGEOM with WKB = '%s'", hexized_wkb_srid); + LWDEBUGF(3, "in WKBFromLWGEOM with WKB = '%s'", lwg_unparser_result.wkoutput); - hexized_wkb = hexized_wkb_srid; + hexized_wkb_srid = lwg_unparser_result.wkoutput; - semicolonLoc = strchr(hexized_wkb_srid,';'); + semicolonLoc = strchr(lwg_unparser_result.wkoutput,';'); if (semicolonLoc != NULL) { hexized_wkb = (semicolonLoc+1); @@ -282,27 +283,27 @@ Datum WKBFromLWGEOM(PG_FUNCTION_ARGS) LWDEBUGF(3, "in WKBFromLWGEOM with WKB (with no 'SRID=#;' = '%s'", hexized_wkb); - size_result = size/2 + VARHDRSZ; - result = palloc(size_result); - SET_VARSIZE(result, size_result); + size_result = lwg_unparser_result.size/2 + VARHDRSZ; + lwgeom_result = palloc(size_result); + SET_VARSIZE(lwgeom_result, size_result); /* have a hexized string, want to make it binary */ - for (t=0; t< (size/2); t++) + for (t=0; t< (lwgeom_unparser_result.size/2); t++) { - ((uchar *) VARDATA(result))[t] = parse_hex( hexized_wkb + (t*2) ); + ((uchar *) VARDATA(lwgeom_result))[t] = parse_hex( hexized_wkb + (t*2) ); } - pfree(hexized_wkb_srid); + pfree(hexized_wkb); #else /* ndef BINARY_FROM_HEX */ - hexized_wkb = serialized_lwgeom_to_ewkb(SERIALIZED_FORM(lwgeom_input), PARSER_CHECK_ALL, byteorder, &size); + result = serialized_lwgeom_to_ewkb(&lwg_unparser_result, SERIALIZED_FORM(lwgeom_input), PARSER_CHECK_ALL, byteorder); - size_result = size+VARHDRSZ; - result = palloc(size_result); - SET_VARSIZE(result, size_result); - memcpy(VARDATA(result), hexized_wkb, size); - pfree(hexized_wkb); + size_result = lwg_unparser_result.size+VARHDRSZ; + lwgeom_result = palloc(size_result); + SET_VARSIZE(lwgeom_result, size_result); + memcpy(VARDATA(lwgeom_result), lwg_unparser_result.wkoutput, lwg_unparser_result.size); + pfree(lwg_unparser_result.wkoutput); #endif @@ -312,9 +313,9 @@ Datum WKBFromLWGEOM(PG_FUNCTION_ARGS) #endif LWDEBUGF(3, "Output size is %lu (comp: %lu)", - VARSIZE(result), (unsigned long)size); + VARSIZE(lwgeom_result), (unsigned long)size); - PG_RETURN_POINTER(result); + PG_RETURN_POINTER(lwgeom_result); } /* puts a bbox inside the geometry */ diff --git a/lwgeom/lwgeom_ogc.c b/lwgeom/lwgeom_ogc.c index 799f344d6..690da1284 100644 --- a/lwgeom/lwgeom_ogc.c +++ b/lwgeom/lwgeom_ogc.c @@ -1065,11 +1065,11 @@ Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_asText); Datum LWGEOM_asText(PG_FUNCTION_ARGS) { + LWGEOM_UNPARSER_RESULT lwg_unparser_result; PG_LWGEOM *lwgeom; PG_LWGEOM *ogclwgeom; - char *result_cstring; - int len; - char *result,*loc_wkt; + int len, result; + char *lwgeom_result,*loc_wkt; char *semicolonLoc; POSTGIS_DEBUG(2, "LWGEOM_asText called."); @@ -1080,25 +1080,25 @@ Datum LWGEOM_asText(PG_FUNCTION_ARGS) ogclwgeom = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1( LWGEOM_force_2d, PointerGetDatum(lwgeom))); - result_cstring = serialized_lwgeom_to_ewkt(SERIALIZED_FORM(ogclwgeom), PARSER_CHECK_ALL); + result = serialized_lwgeom_to_ewkt(&lwg_unparser_result, SERIALIZED_FORM(ogclwgeom), PARSER_CHECK_ALL); - semicolonLoc = strchr(result_cstring,';'); + semicolonLoc = strchr(lwg_unparser_result.wkoutput,';'); /* loc points to start of wkt */ - if (semicolonLoc == NULL) loc_wkt = result_cstring; + if (semicolonLoc == NULL) loc_wkt = lwg_unparser_result.wkoutput; else loc_wkt = semicolonLoc +1; len = strlen(loc_wkt)+VARHDRSZ; - result = palloc(len); - SET_VARSIZE(result, len); + lwgeom_result = palloc(len); + SET_VARSIZE(lwgeom_result, len); - memcpy(VARDATA(result), loc_wkt, len-VARHDRSZ); + memcpy(VARDATA(lwgeom_result), loc_wkt, len-VARHDRSZ); - pfree(result_cstring); + pfree(lwg_unparser_result.wkoutput); PG_FREE_IF_COPY(lwgeom, 0); if ( ogclwgeom != lwgeom ) pfree(ogclwgeom); - PG_RETURN_POINTER(result); + PG_RETURN_POINTER(lwgeom_result); } /* convert LWGEOM to wkb (in BINARY format) */ diff --git a/lwgeom/lwgeom_pg.c b/lwgeom/lwgeom_pg.c index 61d55cbc2..ab6aa8f20 100644 --- a/lwgeom/lwgeom_pg.c +++ b/lwgeom/lwgeom_pg.c @@ -274,8 +274,14 @@ pglwgeom_from_ewkb(uchar *ewkb, int flags, size_t ewkblen) char * pglwgeom_to_ewkb(PG_LWGEOM *geom, int flags, char byteorder, size_t *outsize) { + LWGEOM_UNPARSER_RESULT lwg_unparser_result; + int result; uchar *srl = &(geom->type); - return serialized_lwgeom_to_ewkb(srl, flags, byteorder, outsize); + + result = serialized_lwgeom_to_ewkb(&lwg_unparser_result, srl, flags, byteorder); + + *outsize = lwg_unparser_result.size; + return lwg_unparser_result.wkoutput; } /*