override CFLAGS += -DUSE_STATS
endif
-OBJS=lwgeom_pg.o lwgeom.o lwpoint.o lwline.o lwpoly.o lwmpoint.o lwmline.o lwmpoly.o lwcollection.o lwgeom_spheroid.o lwgeom_api.o lwgeom_ogc.o lwgeom_functions_analytic.o lwgeom_geos.o lwgeom_inout.o lwgeom_estimate.o lwgeom_functions_basic.o lwgeom_gist.o lwgeom_btree.o lwgeom_transform.o stringBuffer.o lwgeom_box.o lwgeom_box3d.o lwgeom_box2dfloat4.o lwgeom_chip.o lex.yy.o wktparse.tab.o lwgparse.o wktunparse.o lwgeom_svg.o lwgeom_gml.o $(GEOS_WRAPPER)
+OBJS=lwgeom_pg.o liblwgeom.o lwgeom.o lwpoint.o lwline.o lwpoly.o lwmpoint.o lwmline.o lwmpoly.o lwcollection.o lwgeom_spheroid.o lwgeom_api.o lwgeom_ogc.o lwgeom_functions_analytic.o lwgeom_geos.o lwgeom_inout.o lwgeom_estimate.o lwgeom_functions_basic.o lwgeom_gist.o lwgeom_btree.o lwgeom_transform.o stringBuffer.o lwgeom_box.o lwgeom_box3d.o lwgeom_box2dfloat4.o lwgeom_chip.o lex.yy.o wktparse.tab.o lwgparse.o wktunparse.o lwgeom_svg.o lwgeom_gml.o $(GEOS_WRAPPER)
OTHERS=y.output lex.yy.c wktparse.tab.c wktparse.tab.h lwpostgis.sql
double xmax, ymax, zmax;
} BOX3D;
-
typedef struct chiptag
{
int size; //unused (for use by postgresql)
typedef struct
{
+ int type;
char ndims; // 2=2d, 3=3d, 4=4d, 5=undef
int SRID; // spatial ref sys
POINTARRAY *point; // hide 2d/3d (this will be an array of 1 point)
extern LWPOINT *lwpoint_deserialize(char *serialized_form);
// Find size this point would get when serialized (no BBOX)
-extern uint32 lwpoint_size(LWPOINT *point);
+extern size_t lwpoint_serialize_size(LWPOINT *point);
// convert this point into its serialize form
// result's first char will be the 8bit type. See serialized form doc
typedef struct
{
- char ndims; // 2=2d, 3=3d, 4=4d, 5=undef
+ int type;
+ char ndims; // 2=2d, 3=3d, 4=4d, 5=undef
int SRID; // spatial ref sys -1=none
POINTARRAY *points; // array of POINT3D
} LWLINE; //"light-weight line"
extern LWLINE *lwline_deserialize(char *serialized_form);
// find the size this line would get when serialized (no BBOX)
-extern uint32 lwline_size(LWLINE *line);
+extern size_t lwline_serialize_size(LWLINE *line);
// convert this line into its serialize form
// result's first char will be the 8bit type. See serialized form doc
typedef struct
{
+ char type;
int32 SRID;
char ndims;
int nrings;
extern LWPOLY *lwpoly_deserialize(char *serialized_form);
// find the size this polygon would get when serialized (no bbox!)
-extern uint32 lwpoly_size(LWPOLY *poly);
+extern size_t lwpoly_serialize_size(LWPOLY *poly);
// create the serialized form of the polygon
// result's first char will be the 8bit type. See serialized form doc
// MULTIPOINTTYPE
typedef struct
{
+ int type;
int32 SRID;
char ndims;
int npoints;
LWPOINT **points;
} LWMPOINT;
+extern size_t lwmpoint_serialize_size(LWMPOINT *mpoint);
+extern void lwmpoint_serialize_buf(LWMPOINT *mpoint, char *buf, int *size);
+
// MULTILINETYPE
typedef struct
-{
+{
+ int type;
int32 SRID;
char ndims;
int nlines;
LWLINE **lines;
} LWMLINE;
+extern size_t lwmline_serialize_size(LWMLINE *mline);
+extern void lwmline_serialize_buf(LWMLINE *mline, char *buf, int *size);
+
// MULTIPOLYGONTYPE
typedef struct
-{
+{
+ int type;
int32 SRID;
char ndims;
int npolys;
LWPOLY **polys;
} LWMPOLY;
-// COLLECTIONTYPE
+extern size_t lwmpoly_serialize_size(LWMPOLY *mpoly);
+extern void lwmpoly_serialize_buf(LWMPOLY *mpoly, char *buf, int *size);
+
+// LWGEOM (any type)
typedef struct
{
+ int type;
+ void *data;
+} LWGEOM;
+
+extern size_t lwgeom_serialize_size(LWGEOM *geom);
+extern void lwgeom_serialize_buf(LWGEOM *geom, char *buf, int *size);
+extern char *lwgeom_serialize(LWGEOM *geom, char wantbbox);
+
+// COLLECTIONTYPE
+typedef struct
+{
+ int type;
int32 SRID;
char ndims;
int ngeoms;
- struct LWGEOM **geoms;
+ LWGEOM **geoms;
} LWCOLLECTION;
-// LWGEOM (any type)
-typedef struct
-{
- char type;
- union {
- LWPOINT *point;
- LWMPOINT *mpoint;
- LWLINE *line;
- LWMLINE *mline;
- LWPOLY *poly;
- LWMPOLY *mpoly;
- LWCOLLECTION *collection;
- };
-} LWGEOM;
+extern size_t lwcollection_serialize_size(LWCOLLECTION *coll);
+extern void lwcollection_serialize_buf(LWCOLLECTION *mcoll, char *buf, int *size);
LWGEOM *lwgeom_deserialize(char *serializedform);
LWMPOINT *lwmpoint_deserialize(char *serializedform);
LWMLINE *lwmline_deserialize(char *serializedform);
LWMPOLY *lwmpoly_deserialize(char *serializedform);
LWCOLLECTION *lwcollection_deserialize(char *serializedform);
+LWGEOM *lwcollection_getsubgeom(LWCOLLECTION *, int);
+
//------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "liblwgeom.h"
LWCOLLECTION *
insp = lwgeom_inspect(srl);
result = lwalloc(sizeof(LWCOLLECTION));
+ result->type = COLLECTIONTYPE;
result->SRID = insp->SRID;
result->ndims = lwgeom_ndims(insp->type);
result->ngeoms = insp->ngeometries;
for (i=0; i<insp->ngeometries; i++)
{
- result->geoms[i] = (struct LWGEOM *)lwgeom_deserialize(insp->sub_geoms[i]);
+ result->geoms[i] = lwgeom_deserialize(insp->sub_geoms[i]);
}
return result;
}
+LWGEOM *
+lwcollection_getsubgeom(LWCOLLECTION *col, int gnum)
+{
+ return (LWGEOM *)col->geoms[gnum];
+}
+
+// find serialized size of this collection
+size_t
+lwcollection_serialize_size(LWCOLLECTION *col)
+{
+ size_t size = 5; // type + nsubgeoms
+ int i;
+
+ if ( col->SRID != -1 ) size += 4; // SRID
+
+ for (i=0; i<col->ngeoms; i++)
+ size += lwgeom_serialize_size(lwcollection_getsubgeom(col, i));
+
+ return size;
+}
+
+// convert this collectoin into its serialize form writing it into
+// the given buffer, and returning number of bytes written into
+// the given int pointer.
+void
+lwcollection_serialize_buf(LWCOLLECTION *coll, char *buf, int *retsize)
+{
+ int size=1; // type
+ int subsize=0;
+ char hasSRID;
+ char *loc;
+ int i;
+
+ hasSRID = (coll->SRID != -1);
+
+ buf[0] = (unsigned char) lwgeom_makeType(coll->ndims,
+ hasSRID, COLLECTIONTYPE);
+ loc = buf+1;
+
+ // Add SRID if requested
+ if (hasSRID)
+ {
+ memcpy(loc, &coll->SRID, 4);
+ size += 4;
+ loc += 4;
+ }
+
+ // Write number of subgeoms
+ memcpy(loc, &coll->ngeoms, 4);
+ size += 4;
+ loc += 4;
+
+ // Serialize subgeoms
+ for (i=0; i<coll->ngeoms; i++)
+ {
+ lwgeom_serialize_buf(coll->geoms[i], loc, &subsize);
+ size += subsize;
+ }
+
+ if (retsize) *retsize = size;
+}
#include "lwgeom_pg.h"
#include "liblwgeom.h"
-#define CONTEXT_PG 0
-#define CONTEXT_STANDALONE 1
-
-/* Define this to the default context liblwgeom runs with */
-#define DEFAULT_CONTEXT CONTEXT_PG
-
-
-/* globals */
-#if DEFAULT_CONTEXT == CONTEXT_PG
-lwallocator lwalloc = pg_alloc;
-lwreallocator lwrealloc = pg_realloc;
-lwfreeor lwfree = pg_free;
-lwreporter lwerror = pg_error;
-lwreporter lwnotice = pg_notice;
-#else
-lwallocator lwalloc = default_allocator;
-lwreallocator lwrealloc = default_reallocator;
-lwfreeor lwfree = default_freeor;
-lwreporter lwerror = default_errorreporter;
-lwreporter lwnotice = default_noticereporter;
-#endif
-
LWGEOM *
lwgeom_deserialize(char *srl)
{
- LWGEOM *result;
int type = lwgeom_getType(srl[0]);
- result = lwalloc(sizeof(LWGEOM));
- result->type = type;
switch (type)
{
case POINTTYPE:
- result->point = lwpoint_deserialize(srl);
- break;
+ return (LWGEOM *)lwpoint_deserialize(srl);
case LINETYPE:
- result->line = lwline_deserialize(srl);
- break;
+ return (LWGEOM *)lwline_deserialize(srl);
case POLYGONTYPE:
- result->poly = lwpoly_deserialize(srl);
- break;
+ return (LWGEOM *)lwpoly_deserialize(srl);
case MULTIPOINTTYPE:
- result->mpoint = lwmpoint_deserialize(srl);
- break;
+ return (LWGEOM *)lwmpoint_deserialize(srl);
case MULTILINETYPE:
- result->mline = lwmline_deserialize(srl);
- break;
+ return (LWGEOM *)lwmline_deserialize(srl);
case MULTIPOLYGONTYPE:
- result->mpoly = lwmpoly_deserialize(srl);
- break;
+ return (LWGEOM *)lwmpoly_deserialize(srl);
case COLLECTIONTYPE:
- result->collection = lwcollection_deserialize(srl);
- break;
+ return (LWGEOM *)lwcollection_deserialize(srl);
default:
lwerror("Unknown geometry type: %d", type);
return NULL;
}
- return result;
}
-void *
-default_allocator(size_t size)
+size_t
+lwgeom_serialize_size(LWGEOM *lwgeom)
{
- void * result;
- result = malloc(size);
- return result;
-}
+ int type = lwgeom->type;
+ size_t size;
-void *
-default_reallocator(void *mem, size_t size)
-{
- void * result;
- result = realloc(mem, size);
- return result;
+ switch (type)
+ {
+ case POINTTYPE:
+ size = lwpoint_serialize_size((LWPOINT *)lwgeom);
+ case LINETYPE:
+ size = lwline_serialize_size((LWLINE *)lwgeom);
+ case POLYGONTYPE:
+ size = lwpoly_serialize_size((LWPOLY *)lwgeom);
+ case MULTIPOINTTYPE:
+ size = lwmpoint_serialize_size((LWMPOINT *)lwgeom);
+ case MULTILINETYPE:
+ size = lwmline_serialize_size((LWMLINE *)lwgeom);
+ case MULTIPOLYGONTYPE:
+ size = lwmpoly_serialize_size((LWMPOLY *)lwgeom);
+ case COLLECTIONTYPE:
+ size = lwcollection_serialize_size((LWCOLLECTION *)lwgeom);
+ default:
+ lwerror("Unknown geometry type: %d", type);
+ return 0;
+ }
+
+ return size;
}
void
-default_errorreporter(const char *fmt, ...)
+lwgeom_serialize_buf(LWGEOM *lwgeom, char *buf, int *retsize)
{
- char *msg;
- va_list ap;
-
- va_start (ap, fmt);
+ int type = lwgeom->type;
+ int size;
- /*
- * This is a GNU extension.
- * Dunno how to handle errors here.
- */
- if (!vasprintf (&msg, fmt, ap))
+ switch (type)
{
- va_end (ap);
- return;
+ case POINTTYPE:
+ lwpoint_serialize_buf((LWPOINT *)lwgeom, buf, &size);
+ break;
+ case LINETYPE:
+ lwline_serialize_buf((LWLINE *)lwgeom, buf, &size);
+ break;
+ case POLYGONTYPE:
+ lwpoly_serialize_buf((LWPOLY *)lwgeom, buf, &size);
+ break;
+ case MULTIPOINTTYPE:
+ lwmpoint_serialize_buf((LWMPOINT *)lwgeom, buf, &size);
+ break;
+ case MULTILINETYPE:
+ lwmline_serialize_buf((LWMLINE *)lwgeom, buf, &size);
+ break;
+ case MULTIPOLYGONTYPE:
+ lwmpoly_serialize_buf((LWMPOLY *)lwgeom, buf, &size);
+ break;
+ case COLLECTIONTYPE:
+ lwcollection_serialize_buf((LWCOLLECTION *)lwgeom, buf, &size);
+ break;
+ default:
+ lwerror("Unknown geometry type: %d", type);
+ return;
}
- va_end(ap);
- fprintf(stderr, "%s", msg);
- free(msg);
+ *retsize = size;
+ return;
}
-void
-default_noticereporter(const char *fmt, ...)
+char *
+lwgeom_serialize(LWGEOM *lwgeom, char wantbbox)
{
- char *msg;
- va_list ap;
+ size_t size = lwgeom_serialize_size(lwgeom);
+ size_t retsize;
+ char *loc;
+
+ if ( wantbbox ) size += sizeof(BOX2DFLOAT4);
- va_start (ap, fmt);
+ char *serialized = lwalloc(size);
- /*
- * This is a GNU extension.
- * Dunno how to handle errors here.
- */
- if (!vasprintf (&msg, fmt, ap))
+ if ( wantbbox ) loc = serialized + sizeof(BOX2DFLOAT4);
+ else loc = serialized;
+
+ lwgeom_serialize_buf(lwgeom, loc, &retsize);
+
+#ifdef DEBUG
+ if ( wantbbox ) retsize += 4;
+ if ( retsize != size )
{
- va_end (ap);
- return;
+ lwerror("lwgeom_serialize: computed size %d, returned size %d",
+ size, retsize);
}
- va_end(ap);
- fprintf(stderr, "%s", msg);
- free(msg);
-}
+#endif
-void
-default_freeor(void *ptr)
-{
- free(ptr);
+ return serialized;
}
// internal
char * lwgeom_summary_recursive(char *serialized, int offset);
+char * lwgeom_summary(LWGEOM *serialized, int offset);
int32 lwgeom_nrings_recursive(char *serialized);
void dump_lwexploded(LWGEOM_EXPLODED *exploded);
POINTARRAY *ptarray_reverse(const POINTARRAY *pa);
uint32 i, j;
int ptsize;
- opa = (POINTARRAY *)palloc(sizeof(POINTARRAY));
+ opa = (POINTARRAY *)lwalloc(sizeof(POINTARRAY));
opa->ndims = ipa->ndims;
opa->npoints = ipa->npoints;
ptsize = pointArray_ptsize(ipa);
- opa->serialized_pointlist = palloc(ipa->npoints*ptsize);
+ opa->serialized_pointlist = lwalloc(ipa->npoints*ptsize);
for (i=0, j=ipa->npoints-1; i<ipa->npoints; i++, j--)
{
POINTARRAY **rpa;
int i;
- rpa = palloc(sizeof(POINTARRAY *)*ipoly->nrings);
+ rpa = lwalloc(sizeof(POINTARRAY *)*ipoly->nrings);
for (i=0; i<ipoly->nrings; i++)
{
int i;
POINTARRAY *opa;
- rpa = palloc(sizeof(POINTARRAY *)*ipoly->nrings);
+ rpa = lwalloc(sizeof(POINTARRAY *)*ipoly->nrings);
if ( ptarray_isccw(ipoly->rings[0]) )
{
PG_RETURN_INT32(size);
}
+char *lwcollection_summary(LWCOLLECTION *collection, int offset);
+char *lwmpoly_summary(LWMPOLY *mpoly, int offset);
+char *lwmline_summary(LWMLINE *mline, int offset);
+char *lwmpoint_summary(LWMPOINT *mpoint, int offset);
+char *lwpoly_summary(LWPOLY *poly, int offset);
+char *lwline_summary(LWLINE *line, int offset);
+char *lwpoint_summary(LWPOINT *point, int offset);
+
/*
- * Returns a palloced string containing summary for the serialized
+ * Returns an alloced string containing summary for the LWGEOM object
+ */
+char *
+lwpoint_summary(LWPOINT *point, int offset)
+{
+ char *result;
+ result = lwalloc(256);
+ sprintf(result, "Object %d is a POINT()\n", offset);
+ return result;
+}
+
+char *
+lwline_summary(LWLINE *line, int offset)
+{
+ char *result;
+ result = lwalloc(32);
+ sprintf(result, "Object %d is a LINE()\n", offset);
+ return result;
+}
+
+char *
+lwpoly_summary(LWPOLY *poly, int offset)
+{
+ char tmp[256];
+ char *result = lwalloc(64*(poly->nrings+1));
+ int i;
+
+ result[0] = '\0';
+ sprintf(tmp, "Object %d is a POLYGON() with %i rings\n",
+ offset, poly->nrings);
+ strcat(result, tmp);
+ for (i=0; i<poly->nrings;i++)
+ {
+ sprintf(tmp," + ring %i has %i points\n",
+ i, poly->rings[i]->npoints);
+ strcat(result,tmp);
+ }
+ return result;
+}
+
+char *
+lwmpoint_summary(LWMPOINT *mpoint, int offset)
+{
+ char *result = lwalloc(60);
+ sprintf(result, "Object %d is a MULTIPOINT() with %d points\n",
+ offset, mpoint->npoints);
+ return result;
+}
+
+char *
+lwmline_summary(LWMLINE *mline, int offset)
+{
+ char *result = lwalloc(60*(mline->nlines+1));
+ sprintf(result, "Object %d is a MULTILINE() with %d lines\n",
+ offset, mline->nlines);
+ return result;
+}
+
+char *
+lwmpoly_summary(LWMPOLY *mpoly, int offset)
+{
+ size_t size = 128;
+ char *result = lwalloc(size);
+ char *tmp;
+ int i;
+
+ sprintf(result, "Object %d is a MULTIPOLYGON() with %d polys\n",
+ offset, mpoly->npolys);
+
+ for (i=0; i<mpoly->npolys; i++)
+ {
+ tmp = lwpoly_summary(mpoly->polys[i], i);
+ size += strlen(tmp)+1;
+ result = lwrealloc(result, size);
+ strcat(result, tmp);
+ //lwfree(tmp);
+ }
+
+ return result;
+}
+
+
+char *
+lwcollection_summary(LWCOLLECTION *collection, int offset)
+{
+ char *result = lwalloc(60*(collection->ngeoms+1));
+ char *tmp;
+ int i;
+
+ sprintf(result, "Object %d is a COLLECTION() with %d subgeoms\n",
+ offset, collection->ngeoms);
+
+ for (i=0; i<collection->ngeoms; i++)
+ {
+ tmp = lwgeom_summary(lwcollection_getsubgeom(collection, i), i);
+ strcat(result, tmp);
+ lwfree(tmp);
+ }
+
+ return result;
+}
+
+char *
+lwgeom_summary(LWGEOM *lwgeom, int offset)
+{
+ char *result;
+
+ switch (lwgeom->type)
+ {
+ case POINTTYPE:
+ return lwpoint_summary((LWPOINT *)lwgeom, offset);
+
+ case LINETYPE:
+ return lwline_summary((LWLINE *)lwgeom, offset);
+
+ case POLYGONTYPE:
+ return lwpoly_summary((LWPOLY *)lwgeom, offset);
+
+ case MULTIPOINTTYPE:
+ return lwmpoint_summary((LWMPOINT *)lwgeom, offset);
+
+ case MULTILINETYPE:
+ return lwmline_summary((LWMLINE *)lwgeom, offset);
+
+ case MULTIPOLYGONTYPE:
+ return lwmpoly_summary((LWMPOLY *)lwgeom, offset);
+
+ case COLLECTIONTYPE:
+ return lwcollection_summary((LWCOLLECTION *)lwgeom, offset);
+ default:
+ result = palloc(256);
+ sprintf(result, "Object is of unknown type: %d",
+ lwgeom->type);
+ return result;
+ }
+
+ return NULL;
+}
+
+/*
+ * Returns a lwalloced string containing summary for the serialized
* LWGEOM object
*/
char *
int32 j,i;
size = 1;
- result = palloc(1);
+ result = lwalloc(1);
result[0] = '\0';
if ( offset == 0 ) idx = 0;
if ( lwgeom_getType(subgeom[0]) == 0 )
{
size += 32;
- result = repalloc(result,size);
+ result = lwrealloc(result,size);
sprintf(tmp,"Object %i is EMPTY()\n", idx++);
strcat(result,tmp);
continue;
if (point !=NULL)
{
size += 32;
- result = repalloc(result,size);
+ result = lwrealloc(result,size);
sprintf(tmp,"Object %i is a POINT()\n", idx++);
strcat(result,tmp);
continue;
if (poly !=NULL)
{
size += 60*(poly->nrings+1);
- result = repalloc(result,size);
+ result = lwrealloc(result,size);
sprintf(tmp,"Object %i is a POLYGON() with %i rings\n",
idx++, poly->nrings);
strcat(result,tmp);
if (line != NULL)
{
size += 57;
- result = repalloc(result,size);
+ result = lwrealloc(result,size);
sprintf(tmp, "Object %i is a LINESTRING() with %i points\n", idx++, line->points->npoints);
strcat(result,tmp);
continue;
ptr = lwgeom_summary_recursive(subgeom, j);
size += strlen(ptr);
- result = repalloc(result,size);
+ result = lwrealloc(result,size);
strcat(result, ptr);
- pfree(ptr);
+ lwfree(ptr);
}
pfree_inspected(inspected);
PG_LWGEOM *geom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
char *result;
text *mytext;
+ LWGEOM *lwgeom;
+
+ init_pg_func();
+
+ lwgeom = lwgeom_deserialize(SERIALIZED_FORM(geom));
- result = lwgeom_summary_recursive(SERIALIZED_FORM(geom), 0);
+ //result = lwgeom_summary_recursive(SERIALIZED_FORM(geom), 0);
+ result = lwgeom_summary(lwgeom, 0);
// create a text obj to return
- mytext = (text *) palloc(VARHDRSZ + strlen(result) );
+ mytext = (text *) lwalloc(VARHDRSZ + strlen(result) );
VARATT_SIZEP(mytext) = VARHDRSZ + strlen(result) ;
memcpy(VARDATA(mytext) , result, strlen(result) );
- pfree(result);
+ lwfree(result);
PG_RETURN_POINTER(mytext);
}
{
char *ver = POSTGIS_LIB_VERSION;
text *result;
- result = (text *) palloc(VARHDRSZ + strlen(ver));
+ result = (text *) lwalloc(VARHDRSZ + strlen(ver));
VARATT_SIZEP(result) = VARHDRSZ + strlen(ver) ;
memcpy(VARDATA(result), ver, strlen(ver));
PG_RETURN_POINTER(result);
{
char *ver = POSTGIS_SCRIPTS_VERSION;
text *result;
- result = (text *) palloc(VARHDRSZ + strlen(ver));
+ result = (text *) lwalloc(VARHDRSZ + strlen(ver));
VARATT_SIZEP(result) = VARHDRSZ + strlen(ver) ;
memcpy(VARDATA(result), ver, strlen(ver));
PG_RETURN_POINTER(result);
{
newpts.ndims = 3;
newpts.npoints = 1;
- newpts.serialized_pointlist = palloc(sizeof(POINT3D));
+ newpts.serialized_pointlist = lwalloc(sizeof(POINT3D));
loc = newpts.serialized_pointlist;
getPoint3d_p(point->point, 0, loc);
point->point = &newpts;
{
newpts.ndims = 3;
newpts.npoints = line->points->npoints;
- newpts.serialized_pointlist = palloc(24*line->points->npoints);
+ newpts.serialized_pointlist = lwalloc(24*line->points->npoints);
loc = newpts.serialized_pointlist;
for (j=0; j<line->points->npoints; j++)
{
{
newpts.ndims = 3;
newpts.npoints = 0;
- newpts.serialized_pointlist = palloc(1);
- nrings = palloc(sizeof(POINTARRAY *)*poly->nrings);
+ newpts.serialized_pointlist = lwalloc(1);
+ nrings = lwalloc(sizeof(POINTARRAY *)*poly->nrings);
loc = newpts.serialized_pointlist;
for (j=0; j<poly->nrings; j++)
{
POINTARRAY *ring = poly->rings[j];
- POINTARRAY *nring = palloc(sizeof(POINTARRAY));
+ POINTARRAY *nring = lwalloc(sizeof(POINTARRAY));
nring->ndims = 3;
nring->npoints = ring->npoints;
nring->serialized_pointlist =
- palloc(ring->npoints*24);
+ lwalloc(ring->npoints*24);
loc = nring->serialized_pointlist;
for (k=0; k<ring->npoints; k++)
{
{
newpts.ndims = 4;
newpts.npoints = 1;
- newpts.serialized_pointlist = palloc(sizeof(POINT4D));
+ newpts.serialized_pointlist = lwalloc(sizeof(POINT4D));
loc = newpts.serialized_pointlist;
getPoint4d_p(point->point, 0, loc);
point->point = &newpts;
{
newpts.ndims = 4;
newpts.npoints = line->points->npoints;
- newpts.serialized_pointlist = palloc(sizeof(POINT4D)*line->points->npoints);
+ newpts.serialized_pointlist = lwalloc(sizeof(POINT4D)*line->points->npoints);
loc = newpts.serialized_pointlist;
for (j=0; j<line->points->npoints; j++)
{
{
newpts.ndims = 4;
newpts.npoints = 0;
- newpts.serialized_pointlist = palloc(1);
- nrings = palloc(sizeof(POINTARRAY *)*poly->nrings);
+ newpts.serialized_pointlist = lwalloc(1);
+ nrings = lwalloc(sizeof(POINTARRAY *)*poly->nrings);
loc = newpts.serialized_pointlist;
for (j=0; j<poly->nrings; j++)
{
POINTARRAY *ring = poly->rings[j];
- POINTARRAY *nring = palloc(sizeof(POINTARRAY));
+ POINTARRAY *nring = lwalloc(sizeof(POINTARRAY));
nring->ndims = 4;
nring->npoints = ring->npoints;
nring->serialized_pointlist =
- palloc(ring->npoints*sizeof(POINT4D));
+ lwalloc(ring->npoints*sizeof(POINT4D));
loc = nring->serialized_pointlist;
for (k=0; k<ring->npoints; k++)
{
if ( lwgeom_ndims(geom->type) == 2 ) PG_RETURN_POINTER(geom);
// allocate a larger for safety and simplicity
- result = (PG_LWGEOM *) palloc(geom->size);
+ result = (PG_LWGEOM *) lwalloc(geom->size);
lwgeom_force2d_recursive(SERIALIZED_FORM(geom),
SERIALIZED_FORM(result), &size);
// we can safely avoid this... memory will be freed at
// end of query processing anyway.
- //result = repalloc(result, size+4);
+ //result = lwrealloc(result, size+4);
result->size = size+4;
if ( olddims == 3 ) PG_RETURN_POINTER(geom);
if ( olddims > 3 ) {
- result = (PG_LWGEOM *) palloc(geom->size);
+ result = (PG_LWGEOM *) lwalloc(geom->size);
} else {
// allocate double as memory a larger for safety
- result = (PG_LWGEOM *) palloc(geom->size*1.5);
+ result = (PG_LWGEOM *) lwalloc(geom->size*1.5);
}
lwgeom_force3d_recursive(SERIALIZED_FORM(geom),
// we can safely avoid this... memory will be freed at
// end of query processing anyway.
- //result = repalloc(result, size+4);
+ //result = lwrealloc(result, size+4);
result->size = size+4;
if ( olddims == 4 ) PG_RETURN_POINTER(geom);
// allocate double as memory a larger for safety
- result = (PG_LWGEOM *) palloc(geom->size*2);
+ result = (PG_LWGEOM *) lwalloc(geom->size*2);
lwgeom_force4d_recursive(SERIALIZED_FORM(geom),
SERIALIZED_FORM(result), &size);
// we can safely avoid this... memory will be freed at
// end of query processing anyway.
- //result = repalloc(result, size+4);
+ //result = lwrealloc(result, size+4);
result->size = size+4;
// alread a multi*, just make it a collection
if ( oldtype > 3 )
{
- result = (PG_LWGEOM *)palloc(geom->size);
+ result = (PG_LWGEOM *)lwalloc(geom->size);
result->size = geom->size;
result->type = TYPE_SETTYPE(geom->type, COLLECTIONTYPE);
memcpy(result->data, geom->data, geom->size-5);
size = geom->size+5; // 4 for numgeoms, 1 for type
- result = (PG_LWGEOM *)palloc(size); // 4 for numgeoms, 1 for type
+ result = (PG_LWGEOM *)lwalloc(size); // 4 for numgeoms, 1 for type
result->size = size;
result->type = TYPE_SETTYPE(geom->type, COLLECTIONTYPE);
size = geom->size+5; // 4 for numgeoms, 1 for type
- result = (PG_LWGEOM *)palloc(size); // 4 for numgeoms, 1 for type
+ result = (PG_LWGEOM *)lwalloc(size); // 4 for numgeoms, 1 for type
result->size = size;
result->type = TYPE_SETTYPE(geom->type, newtype);
//elog(NOTICE, "geom_accum: adding %p (nelems=%d)", geom, nelems);
/*
- * Might use a more optimized version instead of repalloc'ing
+ * Might use a more optimized version instead of lwrealloc'ing
* at every iteration. This is not the bottleneck anyway.
* --strk(TODO);
*/
++nelems;
nbytes = ARR_OVERHEAD(1) + sizeof(Pointer *) * nelems;
if ( ! array ) {
- result = (ArrayType *) palloc(nbytes);
+ result = (ArrayType *) lwalloc(nbytes);
result->size = nbytes;
result->ndim = 1;
*((int *) ARR_DIMS(result)) = nelems;
} else {
- result = (ArrayType *) repalloc(array, nbytes);
+ result = (ArrayType *) lwrealloc(array, nbytes);
result->size = nbytes;
result->ndim = 1;
*((int *) ARR_DIMS(result)) = nelems;
PG_LWGEOM *geom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
double d = PG_GETARG_FLOAT8(1);
BOX2DFLOAT4 box;
- POINT2D *pts = palloc(sizeof(POINT2D)*5);
+ POINT2D *pts = lwalloc(sizeof(POINT2D)*5);
POINTARRAY *pa[1];
LWPOLY *poly;
int SRID;
pts[4].x = box.xmin; pts[4].y = box.ymin;
// Construct point array
- pa[0] = palloc(sizeof(POINTARRAY));
+ pa[0] = lwalloc(sizeof(POINTARRAY));
pa[0]->serialized_pointlist = (char *)pts;
pa[0]->ndims = 2;
pa[0]->npoints = 5;
{
PG_LWGEOM *lwgeom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
BOX2DFLOAT4 box2d;
- BOX *result = (BOX *)palloc(sizeof(BOX));
+ BOX *result = (BOX *)lwalloc(sizeof(BOX));
if ( ! getbox2d_p(SERIALIZED_FORM(lwgeom), &box2d) )
{
{
PG_LWGEOM *geom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
BOX2DFLOAT4 box;
- POINT2D *pts = palloc(sizeof(POINT2D)*5);
+ POINT2D *pts = lwalloc(sizeof(POINT2D)*5);
POINTARRAY *pa[1];
LWPOLY *poly;
int SRID;
pts[4].x = box.xmin; pts[4].y = box.ymin;
// Construct point array
- pa[0] = palloc(sizeof(POINTARRAY));
+ pa[0] = lwalloc(sizeof(POINTARRAY));
pa[0]->serialized_pointlist = (char *)pts;
pa[0]->ndims = 2;
pa[0]->npoints = 5;
pbuf.x = pbuf.y = pbuf.z = pbuf.m = 0;
// Initial storage
- opa = (POINTARRAY *)palloc(ptsize * maxpoints);
+ opa = (POINTARRAY *)lwalloc(ptsize * maxpoints);
opa->ndims = ipa->ndims;
opa->npoints = 0;
- opa->serialized_pointlist = (char *)palloc(maxpoints*ptsize);
+ opa->serialized_pointlist = (char *)lwalloc(maxpoints*ptsize);
// Add first point
opa->npoints++;
// Add point
if ( ++(opa->npoints) > maxpoints ) {
maxpoints *= 1.5;
- opa->serialized_pointlist = (char *)repalloc(
+ opa->serialized_pointlist = (char *)lwrealloc(
opa->serialized_pointlist,
maxpoints*ptsize
);
}
size = lwexploded_findlength(exp, wantbbox);
- result = palloc(size+4);
+ result = lwalloc(size+4);
result->size = (size+4);
lwexploded_serialize_buf(exp, wantbbox, SERIALIZED_FORM(result), &size);
}
size = lwexploded_findlength(exp, wantbbox);
- result = palloc(size+4);
+ result = lwalloc(size+4);
result->size = (size+4);
lwexploded_serialize_buf(exp, wantbbox, SERIALIZED_FORM(result), &size);
LWLINE *result;
result = (LWLINE*) lwalloc( sizeof(LWLINE));
+ result->type = LINETYPE;
result->ndims =ndims;
result->SRID = SRID;
result->points = points;
result = (LWLINE*) lwalloc(sizeof(LWLINE)) ;
type = (unsigned char) serialized_form[0];
+ result->type = LINETYPE;
+
if ( lwgeom_getType(type) != LINETYPE)
{
lwerror("lwline_deserialize: attempt to deserialize a line when its not really a line");
}
// find length of this deserialized line
-uint32
-lwline_size(LWLINE *line)
+size_t
+lwline_serialize_size(LWLINE *line)
{
- uint32 size = 1; //type
+ size_t size = 1; //type
if ( line->SRID != -1 ) size += 4; // SRID
size += sizeof(double)*line->ndims*line->points->npoints; // points
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "liblwgeom.h"
LWMLINE *
insp = lwgeom_inspect(srl);
result = lwalloc(sizeof(LWMLINE));
+ result->type = MULTILINETYPE;
result->SRID = insp->SRID;
result->ndims = lwgeom_ndims(insp->type);
result->nlines = insp->ngeometries;
return result;
}
+// find serialized size of this mline
+size_t
+lwmline_serialize_size(LWMLINE *mline)
+{
+ size_t size = 5; // type + nsubgeoms
+ int i;
+
+ if ( mline->SRID != -1 ) size += 4; // SRID
+
+ for (i=0; i<mline->nlines; i++)
+ size += lwline_serialize_size(mline->lines[i]);
+
+ return size;
+}
+
+// convert this multiline into its serialize form writing it into
+// the given buffer, and returning number of bytes written into
+// the given int pointer.
+void
+lwmline_serialize_buf(LWMLINE *mline, char *buf, int *retsize)
+{
+ int size=1; // type
+ int subsize=0;
+ char hasSRID;
+ char *loc;
+ int i;
+
+ hasSRID = (mline->SRID != -1);
+
+ buf[0] = (unsigned char) lwgeom_makeType(mline->ndims,
+ hasSRID, MULTILINETYPE);
+ loc = buf+1;
+
+ // Add SRID if requested
+ if (hasSRID)
+ {
+ memcpy(loc, &mline->SRID, 4);
+ size += 4;
+ loc += 4;
+ }
+
+ // Write number of subgeoms
+ memcpy(loc, &mline->nlines, 4);
+ size += 4;
+ loc += 4;
+
+ // Serialize subgeoms
+ for (i=0; i<mline->nlines; i++)
+ {
+ lwline_serialize_buf(mline->lines[i], loc, &subsize);
+ size += subsize;
+ }
+
+ if (retsize) *retsize = size;
+}
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "liblwgeom.h"
LWMPOINT *
insp = lwgeom_inspect(srl);
result = lwalloc(sizeof(LWMPOINT));
+ result->type = MULTIPOINTTYPE;
result->SRID = insp->SRID;
result->ndims = lwgeom_ndims(insp->type);
result->npoints = insp->ngeometries;
return result;
}
+// find serialized size of this mpoint
+size_t
+lwmpoint_serialize_size(LWMPOINT *mpoint)
+{
+ size_t size = 5; // type + nsubgeoms
+ int i;
+
+ if ( mpoint->SRID != -1 ) size += 4; // SRID
+
+ for (i=0; i<mpoint->npoints; i++)
+ size += lwpoint_serialize_size(mpoint->points[i]);
+
+ return size;
+}
+
+// convert this multipoint into its serialize form writing it into
+// the given buffer, and returning number of bytes written into
+// the given int pointer.
+void
+lwmpoint_serialize_buf(LWMPOINT *mpoint, char *buf, int *retsize)
+{
+ int size=1; // type
+ int subsize=0;
+ char hasSRID;
+ char *loc;
+ int i;
+
+ hasSRID = (mpoint->SRID != -1);
+
+ buf[0] = (unsigned char) lwgeom_makeType(mpoint->ndims,
+ hasSRID, MULTIPOINTTYPE);
+ loc = buf+1;
+
+ // Add SRID if requested
+ if (hasSRID)
+ {
+ memcpy(loc, &mpoint->SRID, 4);
+ size += 4;
+ loc += 4;
+ }
+
+ // Write number of subgeoms
+ memcpy(loc, &mpoint->npoints, 4);
+ size += 4;
+ loc += 4;
+
+ // Serialize subgeoms
+ for (i=0; i<mpoint->npoints; i++)
+ {
+ lwpoint_serialize_buf(mpoint->points[i], loc, &subsize);
+ size += subsize;
+ }
+
+ if (retsize) *retsize = size;
+}
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "liblwgeom.h"
LWMPOLY *
insp = lwgeom_inspect(srl);
result = lwalloc(sizeof(LWMPOLY));
+ result->type = srl[0];
+ result->type = MULTIPOLYGONTYPE;
result->SRID = insp->SRID;
result->ndims = lwgeom_ndims(insp->type);
result->npolys = insp->ngeometries;
return result;
}
+// find serialized size of this mpoly
+size_t
+lwmpoly_serialize_size(LWMPOLY *mpoly)
+{
+ size_t size = 5; // type + nsubgeoms
+ int i;
+
+ if ( mpoly->SRID != -1 ) size += 4; // SRID
+
+ for (i=0; i<mpoly->npolys; i++)
+ size += lwpoly_serialize_size(mpoly->polys[i]);
+
+ return size;
+}
+
+// convert this multipolygon into its serialize form writing it into
+// the given buffer, and returning number of bytes written into
+// the given int pointer.
+void
+lwmpoly_serialize_buf(LWMPOLY *mpoly, char *buf, int *retsize)
+{
+ int size=1; // type
+ int subsize=0;
+ char hasSRID;
+ char *loc;
+ int i;
+
+ hasSRID = (mpoly->SRID != -1);
+
+ buf[0] = (unsigned char) lwgeom_makeType(mpoly->ndims,
+ hasSRID, MULTIPOLYGONTYPE);
+ loc = buf+1;
+
+ // Add SRID if requested
+ if (hasSRID)
+ {
+ memcpy(loc, &mpoly->SRID, 4);
+ size += 4;
+ loc += 4;
+ }
+
+ // Write number of subgeoms
+ memcpy(loc, &mpoly->npolys, 4);
+ size += 4;
+ loc += 4;
+
+ // Serialize subgeoms
+ for (i=0; i<mpoly->npolys; i++)
+ {
+ lwpoly_serialize_buf(mpoly->polys[i], loc, &subsize);
+ size += subsize;
+ }
+
+ if (retsize) *retsize = size;
+}
}
// find length of this deserialized point
-uint32
-lwpoint_size(LWPOINT *point)
+size_t
+lwpoint_serialize_size(LWPOINT *point)
{
- uint32 size = 1; // type
+ size_t size = 1; // type
if ( point->SRID != -1 ) size += 4; // SRID
size += point->ndims * sizeof(double); // point
return NULL; // error
result = lwalloc(sizeof(LWPOINT));
+ result->type = POINTTYPE;
result->ndims = ndims;
result->SRID = SRID;
type = (unsigned char) serialized_form[0];
if ( lwgeom_getType(type) != POINTTYPE) return NULL;
+ result->type = POINTTYPE;
loc = serialized_form+1;
LWPOLY *result;
result = (LWPOLY*) lwalloc(sizeof(LWPOLY));
+ result->type = POLYGONTYPE;
result->ndims = ndims;
result->SRID = SRID;
result->nrings = nrings;
result = (LWPOLY*) lwalloc(sizeof(LWPOLY));
-
type = (unsigned char) serialized_form[0];
+ result->type = POLYGONTYPE;
+
ndims = lwgeom_ndims(type);
loc = serialized_form;
}
// find length of this deserialized polygon
-uint32
-lwpoly_size(LWPOLY *poly)
+size_t
+lwpoly_serialize_size(LWPOLY *poly)
{
- uint32 size = 1; // type
+ size_t size = 1; // type
uint32 i;
if ( poly->SRID != -1 ) size += 4; // SRID