extern uint8_t lwgeom_makeType(char hasZ, char hasM, char has_srid, int type);
extern uint8_t lwgeom_makeType_full(char hasZ, char hasM, char has_srid, int type, char hasBBOX);
-/*
- * This function computes the size in bytes
- * of the serialized geometries.
- */
-extern size_t serialized_lwgeom_size(const uint8_t *serialized_form);
-extern size_t lwgeom_size_subgeom(const uint8_t *serialized_form, int geom_number);
-extern size_t lwgeom_size_line(const uint8_t *serialized_line);
-extern size_t lwgeom_size_circstring(const uint8_t *serialized_curve);
-extern size_t lwgeom_size_point(const uint8_t *serialized_point);
-extern size_t lwgeom_size_poly(const uint8_t *serialized_line);
-extern size_t lwgeom_size_triangle(const uint8_t *serialized_line);
-
-
/*--------------------------------------------------------
* all the base types (point/line/polygon) will have a
* basic constructor, basic de-serializer, basic serializer,
* bounding box finder and (TODO) serialized form size finder.
*--------------------------------------------------------*/
-/*
- * given the LWPOINT serialized form (or a pointer into a muli* one)
- * construct a proper LWPOINT.
- * serialized_form should point to the 8bit type format (with type = 1)
- * Returns NULL if serialized form is not a point.
- * See serialized form doc
- */
-extern LWPOINT *lwpoint_deserialize(uint8_t *serialized_form);
-
/*
* Find size this point would get when serialized (no BBOX)
*/
* LWLINE functions
******************************************************************/
-/*
- * given the LWGEOM serialized form (or a pointer into a muli* one)
- * construct a proper LWLINE.
- * serialized_form should point to the 8bit type format (with type = 2)
- * See SERIALIZED_FORM doc
- */
-extern LWLINE *lwline_deserialize(uint8_t *serialized_form);
-
/* find the size this line would get when serialized */
extern size_t lwline_serialize_size(LWLINE *line);
* LWPOLY functions
******************************************************************/
-/*
- * given the LWPOLY serialized form (or a pointer into a muli* one)
- * construct a proper LWPOLY.
- * serialized_form should point to the 8bit type format (with type = 3)
- * See SERIALIZED_FORM doc
- */
-extern LWPOLY *lwpoly_deserialize(uint8_t *serialized_form);
-
/* find the size this polygon would get when serialized */
extern size_t lwpoly_serialize_size(LWPOLY *poly);
* LWTRIANGLE functions
******************************************************************/
-/*
- * given the LWGEOM serialized form
- * construct a proper LWTRIANGLE.
- * serialized_form should point to the 8bit type format
- * See SERIALIZED_FORM doc
- */
-extern LWTRIANGLE *lwtriangle_deserialize(uint8_t *serialized_form);
-
/* find the size this triangle would get when serialized */
extern size_t lwtriangle_serialize_size(LWTRIANGLE *triangle);
* LWCIRCSTRING functions
******************************************************************/
-/*
- * given the LWGEOM serialized form (or a pointer into a muli* one)
- * construct a proper LWCIRCSTRING.
- * serialized_form should point to the 8bit type format (with type = 2)
- * See SERIALIZED_FORM doc
- */
-extern LWCIRCSTRING *lwcircstring_deserialize(uint8_t *serialized_form);
-
/* find the size this curve would get when serialized */
extern size_t lwcircstring_serialize_size(LWCIRCSTRING *curve);
* The deserialized (recursive) structure will store
* pointers to the serialized form (POINTARRAYs).
*/
-LWGEOM *lwgeom_deserialize(uint8_t *serializedform);
BOX3D *lwgeom_compute_box3d(const LWGEOM *geom);
* LWMULTIx and LWCOLLECTION functions
******************************************************************/
-LWMPOINT *lwmpoint_deserialize(uint8_t *serializedform);
-LWMLINE *lwmline_deserialize(uint8_t *serializedform);
-LWMPOLY *lwmpoly_deserialize(uint8_t *serializedform);
-LWCOLLECTION *lwcollection_deserialize(uint8_t *serializedform);
-LWCOMPOUND *lwcompound_deserialize(uint8_t *serialized_form);
-LWCURVEPOLY *lwcurvepoly_deserialize(uint8_t *serialized_form);
-LWMCURVE *lwmcurve_deserialize(uint8_t *serialized_form);
-LWMSURFACE *lwmsurface_deserialize(uint8_t *serialized_form);
-LWPSURFACE *lwpsurface_deserialize(uint8_t *serialized_form);
-LWTIN *lwtin_deserialize(uint8_t *serialized_form);
-
LWGEOM *lwcollection_getsubgeom(LWCOLLECTION *col, int gnum);
BOX3D *lwcollection_compute_box3d(LWCOLLECTION *col);
LWCOLLECTION* lwcollection_extract(LWCOLLECTION *col, int type);
+
/******************************************************************
* SERIALIZED FORM functions
******************************************************************/
LWGEOM_INSPECTED;
-/*
- * note - for a simple type (ie. point), this will have
- * sub_geom[0] = serialized_form.
- * for multi-geomtries sub_geom[0] will be a few bytes into the
- * serialized form.
- * This function just computes the length of each sub-object and
- * pre-caches this info.
- * For a geometry collection of multi* geometries, you can inspect
- * the sub-components as well.
- */
-extern LWGEOM_INSPECTED *lwgeom_inspect(const uint8_t *serialized_form);
-
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual sub-geometry isnt a POINT, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a point (with geom_num=0), multipoint
- * or geometrycollection
- */
-extern LWPOINT *lwgeom_getpoint(uint8_t *serialized_form, int geom_number);
-extern LWPOINT *lwgeom_getpoint_inspected(LWGEOM_INSPECTED *inspected, int geom_number);
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual geometry isnt a LINE, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a line, multiline or geometrycollection
- */
-extern LWLINE *lwgeom_getline(uint8_t *serialized_form, int geom_number);
-extern LWLINE *lwgeom_getline_inspected(LWGEOM_INSPECTED *inspected, int geom_number);
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual geometry isnt a POLYGON, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a polygon, multipolygon or geometrycollection
- */
-extern LWPOLY *lwgeom_getpoly(uint8_t *serialized_form, int geom_number);
-extern LWPOLY *lwgeom_getpoly_inspected(LWGEOM_INSPECTED *inspected, int geom_number);
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual geometry isnt a TRIANGLE, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a triangle, Tin or geometrycollection
- */
-extern LWTRIANGLE *lwgeom_gettriangle(uint8_t *serialized_form, int geom_number);
-extern LWTRIANGLE *lwgeom_gettriangle_inspected(LWGEOM_INSPECTED *inspected, int geom_number);
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual geometry isnt a POLYGON, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a polygon, multipolygon or geometrycollection
- */
-extern LWCIRCSTRING *lwgeom_getcircstring_inspected(LWGEOM_INSPECTED *inspected, int geom_number);
-
-extern LWGEOM *lwgeom_getgeom_inspected(LWGEOM_INSPECTED *inspected, int geom_number);
-
-
-
-/*
- * this gets the serialized form of a sub-geometry
- * 1st geometry has geom_number = 0
- * if this isnt a multi* geometry, and geom_number ==0 then it returns
- * itself
- * returns null on problems.
- * in the future this is how you would access a muli* portion of a
- * geometry collection.
- * GEOMETRYCOLLECTION(MULTIPOINT(0 0, 1 1), LINESTRING(0 0, 1 1))
- * ie. lwgeom_getpoint( lwgeom_getsubgeometry( serialized, 0), 1)
- * --> POINT(1 1)
- * you can inspect the sub-geometry as well if you wish.
- */
-extern uint8_t *lwgeom_getsubgeometry(const uint8_t *serialized_form, int geom_number);
-extern uint8_t *lwgeom_getsubgeometry_inspected(LWGEOM_INSPECTED *inspected, int geom_number);
-
-
-/*
- * 1st geometry has geom_number = 0
- * use geom_number = -1 to find the actual type of the serialized form.
- * ie lwgeom_gettype( <'MULTIPOINT(0 0, 1 1)'>, -1)
- * --> multipoint
- * ie lwgeom_gettype( <'MULTIPOINT(0 0, 1 1)'>, 0)
- * --> point
- * gets the 8bit type of the geometry at location geom_number
- */
-extern uint8_t lwgeom_getsubtype(uint8_t *serialized_form, int geom_number);
-extern uint8_t lwgeom_getsubtype_inspected(LWGEOM_INSPECTED *inspected, int geom_number);
-
-
-/*
- * how many sub-geometries are there?
- * for point,line,polygon will return 1.
- */
-extern int lwgeom_getnumgeometries(uint8_t *serialized_form);
-extern int lwgeom_getnumgeometries_inspected(LWGEOM_INSPECTED *inspected);
-
-
-
/*
* set finalType to COLLECTIONTYPE or 0 (0 means choose a best type)
* (ie. give it 2 points and ask it to be a multipoint)
extern void printLWPSURFACE(LWPSURFACE *psurf);
extern void printLWTIN(LWTIN *tin);
extern void printBYTES(uint8_t *a, int n);
-extern void printMULTI(uint8_t *serialized);
extern float LWGEOM_Minf(float a, float b);
lwgeom_release(lwcircstring_as_lwgeom(lwcirc));
}
-/*
- * given the LWGEOM serialized form (or a point into a multi* one)
- * construct a proper LWCIRCSTRING.
- * serialized_form should point to the 8bit type format (with type = 8)
- * See serialized form doc
- */
-LWCIRCSTRING *
-lwcircstring_deserialize(uint8_t *serialized_form)
-{
- uint8_t type;
- LWCIRCSTRING *result;
- uint8_t *loc=NULL;
- uint32_t npoints;
- POINTARRAY *pa;
-
- type = (uint8_t)serialized_form[0];
- if (lwgeom_getType(type) != CIRCSTRINGTYPE)
- {
- lwerror("lwcircstring_deserialize: attempt to deserialize a circularstring which is really a %s",
- lwtype_name(TYPE_GETTYPE(type)));
- return NULL;
- }
-
- result = (LWCIRCSTRING*) lwalloc(sizeof(LWCIRCSTRING));
- result->type = CIRCSTRINGTYPE;
- result->flags = gflags(TYPE_HASZ(type),TYPE_HASM(type),0);
-
- loc = serialized_form + 1;
- if (lwgeom_hasBBOX(type))
- {
- BOX2DFLOAT4 *box2df;
- LWDEBUG(3, "lwcircstring_deserialize: input has bbox");
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, loc, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- loc += sizeof(BOX2DFLOAT4);
- }
- else
- {
- LWDEBUG(3, "lwcircstring_deserialize: input lacks bbox");
-
- result->bbox = NULL;
- }
-
- if (lwgeom_hasSRID(type))
- {
- LWDEBUG(3, "lwcircstring_deserialize: input has srid");
-
- result->srid = lw_get_int32_t(loc);
- loc += 4; /* type + SRID */
- }
- else
- {
- LWDEBUG(3, "lwcircstring_deserialize: input lacks srid");
-
- result->srid = SRID_UNKNOWN;
- }
-
- /* we've read the type (1 byte) and SRID (4 bytes, if present) */
-
- npoints = lw_get_uint32_t(loc);
-
- LWDEBUGF(3, "circstring npoints = %d", npoints);
-
- loc += 4;
- pa = ptarray_construct_reference_data(FLAGS_GET_Z(result->flags), FLAGS_GET_M(result->flags), npoints, loc);
-
- result->points = pa;
- return result;
-}
/*
* convert this circularstring into its serialized form
lwfree(curve);
}
-/* find length of this serialized curve */
-size_t
-lwgeom_size_circstring(const uint8_t *serialized_curve)
-{
- int type = (uint8_t)serialized_curve[0];
- uint32_t result = 1; /* type */
- const uint8_t *loc;
- uint32_t npoints;
-
- LWDEBUG(2, "lwgeom_size_circstring called");
- if (lwgeom_getType(type) != CIRCSTRINGTYPE)
- lwerror("lwgeom_size_circstring::attempt to find the length of a non-circularstring");
-
- loc = serialized_curve + 1;
- if (lwgeom_hasBBOX(type))
- {
- loc += sizeof(BOX2DFLOAT4);
- result += sizeof(BOX2DFLOAT4);
- }
-
- if (lwgeom_hasSRID(type))
- {
- loc += 4; /* type + SRID */
- result += 4;
- }
-
- /* we've read the type (1 byte) and SRID (4 bytes, if present) */
- npoints = lw_get_uint32_t(loc);
- result += sizeof(uint32_t); /* npoints */
-
- result += TYPE_NDIMS(type) * sizeof(double) * npoints;
-
- LWDEBUGF(3, "lwgeom_size_circstring returning %d", result);
-
- return result;
-}
void printLWCIRCSTRING(LWCIRCSTRING *curve)
{
}
-LWCOLLECTION *
-lwcollection_deserialize(uint8_t *srl)
-{
- LWCOLLECTION *result;
- LWGEOM_INSPECTED *insp;
- char typefl = srl[0];
- int type = lwgeom_getType(typefl);
- int i;
-
- if ( type != COLLECTIONTYPE )
- {
- lwerror("lwcollection_deserialize called on NON geometrycollection: %d - %s", type, lwtype_name(type));
- return NULL;
- }
- insp = lwgeom_inspect(srl);
-
- result = lwalloc(sizeof(LWCOLLECTION));
- result->type = type;
- result->flags = gflags(TYPE_HASZ(typefl),TYPE_HASM(typefl),0);
- result->srid = insp->srid;
- result->ngeoms = insp->ngeometries;
-
- if (lwgeom_hasBBOX(srl[0]))
- {
- BOX2DFLOAT4 *box2df;
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, srl+1, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- }
- else
- {
- result->bbox = NULL;
- }
-
-
- if ( insp->ngeometries )
- {
- result->geoms = lwalloc(sizeof(LWGEOM *)*insp->ngeometries);
- for (i=0; i<insp->ngeometries; i++)
- {
- result->geoms[i] = lwgeom_deserialize(insp->sub_geoms[i]);
- }
- }
- else
- {
- result->geoms = NULL;
- }
-
- return result;
-}
LWGEOM *
lwcollection_getsubgeom(LWCOLLECTION *col, int gnum)
#include "liblwgeom_internal.h"
#include "lwgeom_log.h"
-LWCOMPOUND *
-lwcompound_deserialize(uint8_t *serialized)
-{
- LWCOMPOUND *result;
- LWGEOM_INSPECTED *insp;
- int type = lwgeom_getType(serialized[0]);
- int i;
-
- if (type != COMPOUNDTYPE)
- {
- lwerror("lwcompound_deserialize called on non compound: %d - %s", type, lwtype_name(type));
- return NULL;
- }
- insp = lwgeom_inspect(serialized);
-
- result = lwalloc(sizeof(LWCOMPOUND));
- result->type = COMPOUNDTYPE;
- result->flags = gflags(TYPE_HASZ(insp->type),TYPE_HASM(insp->type),0);
- result->srid = insp->srid;
- result->ngeoms = insp->ngeometries;
- result->geoms = lwalloc(sizeof(LWGEOM *)*insp->ngeometries);
-
- if (lwgeom_hasBBOX(serialized[0]))
- {
- BOX2DFLOAT4 *box2df;
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, serialized + 1, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- }
- else result->bbox = NULL;
-
- for (i = 0; i < insp->ngeometries; i++)
- {
- if (lwgeom_getType(insp->sub_geoms[i][0]) == LINETYPE)
- result->geoms[i] = (LWGEOM *)lwline_deserialize(insp->sub_geoms[i]);
- else
- result->geoms[i] = (LWGEOM *)lwcircstring_deserialize(insp->sub_geoms[i]);
- if (TYPE_NDIMS(result->geoms[i]->type) != TYPE_NDIMS(result->type))
- {
- lwerror("Mixed dimensions (compound: %d, line/circularstring %d:%d)",
- TYPE_NDIMS(result->type), i,
- TYPE_NDIMS(result->geoms[i]->type)
- );
- lwfree(result);
- return NULL;
- }
- }
- return result;
-}
int
lwcompound_is_closed(const LWCOMPOUND *compound)
#include "lwgeom_log.h"
-LWCURVEPOLY *
-lwcurvepoly_deserialize(uint8_t *srl)
-{
- LWCURVEPOLY *result;
- LWGEOM_INSPECTED *insp;
- uint8_t type = (uint8_t)srl[0];
- int geomtype = lwgeom_getType(type);
- int i;
-
- LWDEBUG(3, "lwcurvepoly_deserialize called.");
-
- if (geomtype != CURVEPOLYTYPE)
- {
- lwerror("lwcurvepoly_deserialize called on NON curvepoly: %d",
- geomtype);
- return NULL;
- }
-
- insp = lwgeom_inspect(srl);
-
- result = lwalloc(sizeof(LWCURVEPOLY));
- result->type = CURVEPOLYTYPE;
- result->flags = gflags(TYPE_HASZ(type),TYPE_HASM(type),0);
- result->srid = insp->srid;
- result->nrings = insp->ngeometries;
- result->rings = lwalloc(sizeof(LWGEOM *)*insp->ngeometries);
-
- if (lwgeom_hasBBOX(srl[0]))
- {
- BOX2DFLOAT4 *box2df;
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, srl + 1, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- }
- else result->bbox = NULL;
-
- for (i = 0; i < insp->ngeometries; i++)
- {
- result->rings[i] = lwgeom_deserialize(insp->sub_geoms[i]);
- if (result->rings[i]->type != CIRCSTRINGTYPE
- && result->rings[i]->type != LINETYPE
- && result->rings[i]->type != COMPOUNDTYPE)
- {
- lwerror("Only Circular curves, Linestrings and Compound curves are supported as rings, not %s (%d)", lwtype_name(result->rings[i]->type), result->rings[i]->type);
- lwfree(result);
- lwfree(insp);
- return NULL;
- }
- if (FLAGS_NDIMS(result->rings[i]->flags) != FLAGS_NDIMS(result->flags))
- {
- lwerror("Mixed dimensions (curvepoly %d, ring %d)",
- FLAGS_NDIMS(result->flags), i,
- FLAGS_NDIMS(result->rings[i]->flags));
- lwfree(result);
- lwfree(insp);
- return NULL;
- }
- }
- return result;
-}
-
LWCURVEPOLY *
lwcurvepoly_construct_empty(int srid, char hasz, char hasm)
{
#include "libtgeom.h"
-LWGEOM *
-lwgeom_deserialize(uint8_t *srl)
-{
- int type = TYPE_GETTYPE(srl[0]);
-
- LWDEBUGF(2, "lwgeom_deserialize got %d - %s", type, lwtype_name(type));
-
- switch (type)
- {
- case POINTTYPE:
- return (LWGEOM *)lwpoint_deserialize(srl);
- case LINETYPE:
- return (LWGEOM *)lwline_deserialize(srl);
- case CIRCSTRINGTYPE:
- return (LWGEOM *)lwcircstring_deserialize(srl);
- case POLYGONTYPE:
- return (LWGEOM *)lwpoly_deserialize(srl);
- case TRIANGLETYPE:
- return (LWGEOM *)lwtriangle_deserialize(srl);
- case MULTIPOINTTYPE:
- return (LWGEOM *)lwmpoint_deserialize(srl);
- case MULTILINETYPE:
- return (LWGEOM *)lwmline_deserialize(srl);
- case MULTIPOLYGONTYPE:
- return (LWGEOM *)lwmpoly_deserialize(srl);
- case COLLECTIONTYPE:
- return (LWGEOM *)lwcollection_deserialize(srl);
- case COMPOUNDTYPE:
- return (LWGEOM *)lwcompound_deserialize(srl);
- case CURVEPOLYTYPE:
- return (LWGEOM *)lwcurvepoly_deserialize(srl);
- case MULTICURVETYPE:
- return (LWGEOM *)lwmcurve_deserialize(srl);
- case MULTISURFACETYPE:
- return (LWGEOM *)lwmsurface_deserialize(srl);
- case POLYHEDRALSURFACETYPE:
- return (LWGEOM *)lwpsurface_deserialize(srl);
- case TINTYPE:
- return (LWGEOM *)lwtin_deserialize(srl);
- default:
- lwerror("lwgeom_deserialize: Unknown geometry type: %s", lwtype_name(type));
- return NULL;
- }
-
-}
-
size_t
lwgeom_serialize_size(LWGEOM *lwgeom)
{
}
-/*************************************************************************
- *
- * Multi-geometry support
- *
- * Note - for a simple type (ie. point), this will have
- * sub_geom[0] = serialized_form.
- *
- * For multi-geomtries sub_geom[0] will be a few bytes
- * into the serialized form.
- *
- * This function just computes the length of each sub-object and
- * pre-caches this info.
- *
- * For a geometry collection of multi* geometries, you can inspect
- * the sub-components
- * as well.
- */
-LWGEOM_INSPECTED *
-lwgeom_inspect(const uint8_t *serialized_form)
-{
- LWGEOM_INSPECTED *result = lwalloc(sizeof(LWGEOM_INSPECTED));
- uint8_t typefl = (uint8_t)serialized_form[0];
- uint8_t type;
- uint8_t **sub_geoms;
- const uint8_t *loc;
- int t;
-
- LWDEBUGF(2, "lwgeom_inspect: serialized@%p", serialized_form);
-
- if (serialized_form == NULL)
- return NULL;
-
- result->serialized_form = serialized_form;
- result->type = (uint8_t) serialized_form[0];
- result->srid = SRID_UNKNOWN; /* assume */
-
- type = lwgeom_getType(typefl);
-
- loc = serialized_form+1;
-
- if ( lwgeom_hasBBOX(typefl) )
- {
- loc += sizeof(BOX2DFLOAT4);
- }
-
- if ( lwgeom_hasSRID(typefl) )
- {
- result->srid = lw_get_int32_t(loc);
- loc += 4;
- }
-
- if ( (type==POINTTYPE) || (type==LINETYPE) || (type==POLYGONTYPE) ||
- (type == CIRCSTRINGTYPE) || (type == TRIANGLETYPE))
- {
- /* simple geometry (point/line/polygon/circstring/triangle)-- not multi! */
- result->ngeometries = 1;
- sub_geoms = (uint8_t**) lwalloc(sizeof(char*));
- sub_geoms[0] = (uint8_t *)serialized_form;
- result->sub_geoms = (uint8_t **)sub_geoms;
- return result;
- }
-
- /* its a GeometryCollection or multi* geometry */
-
- result->ngeometries = lw_get_uint32_t(loc);
- loc +=4;
-
- LWDEBUGF(3, "lwgeom_inspect: geometry is a collection of %d elements",
- result->ngeometries);
-
- if ( ! result->ngeometries ) return result;
-
- sub_geoms = lwalloc(sizeof(uint8_t*) * result->ngeometries );
- result->sub_geoms = sub_geoms;
- sub_geoms[0] = (uint8_t *)loc;
-
- LWDEBUGF(3, "subgeom[0] @ %p (+%d)", sub_geoms[0], sub_geoms[0]-serialized_form);
-
- for (t=1; t<result->ngeometries; t++)
- {
- /* -1 = entire object */
- int sub_length = lwgeom_size_subgeom(sub_geoms[t-1], -1);
- sub_geoms[t] = sub_geoms[t-1] + sub_length;
-
- LWDEBUGF(3, "subgeom[%d] @ %p (+%d)",
- t, sub_geoms[t], sub_geoms[0]-serialized_form);
- }
-
- return result;
-
-}
-
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual sub-geometry isnt a POINT, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a point (with geom_num=0),
- * multipoint or geometrycollection
- */
-LWPOINT *
-lwgeom_getpoint(uint8_t *serialized_form, int geom_number)
-{
- int type = lwgeom_getType((uint8_t)serialized_form[0]);
- uint8_t *sub_geom;
-
- if ((type == POINTTYPE) && (geom_number == 0))
- {
- /* Be nice and do as they want instead of what they say */
- return lwpoint_deserialize(serialized_form);
- }
-
- if ((type != MULTIPOINTTYPE) && (type != COLLECTIONTYPE) )
- return NULL;
-
- sub_geom = lwgeom_getsubgeometry(serialized_form, geom_number);
- if (sub_geom == NULL)
- return NULL;
-
- type = lwgeom_getType(sub_geom[0]);
- if (type != POINTTYPE)
- return NULL;
-
- return lwpoint_deserialize(sub_geom);
-}
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual sub-geometry isnt a POINT, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a point (with geom_num=0), multipoint
- * or geometrycollection
- */
-LWPOINT *lwgeom_getpoint_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
-{
- uint8_t *sub_geom;
- uint8_t type;
-
- sub_geom = lwgeom_getsubgeometry_inspected(inspected, geom_number);
-
- if (sub_geom == NULL) return NULL;
-
- type = lwgeom_getType(sub_geom[0]);
- if (type != POINTTYPE) return NULL;
-
- return lwpoint_deserialize(sub_geom);
-}
-
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual geometry isnt a LINE, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a line, multiline or geometrycollection
- */
-LWLINE *
-lwgeom_getline(uint8_t *serialized_form, int geom_number)
-{
- uint8_t type = lwgeom_getType( (uint8_t) serialized_form[0]);
- uint8_t *sub_geom;
-
- if ((type == LINETYPE) && (geom_number == 0))
- {
- /* be nice and do as they want instead of what they say */
- return lwline_deserialize(serialized_form);
- }
-
- if ((type != MULTILINETYPE) && (type != COLLECTIONTYPE) )
- return NULL;
-
- sub_geom = lwgeom_getsubgeometry(serialized_form, geom_number);
- if (sub_geom == NULL) return NULL;
-
- type = lwgeom_getType((uint8_t) sub_geom[0]);
- if (type != LINETYPE) return NULL;
-
- return lwline_deserialize(sub_geom);
-}
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual geometry isnt a LINE, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a line, multiline or geometrycollection
- */
-LWLINE *
-lwgeom_getline_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
-{
- uint8_t *sub_geom;
- uint8_t type;
-
- sub_geom = lwgeom_getsubgeometry_inspected(inspected, geom_number);
-
- if (sub_geom == NULL) return NULL;
-
- type = lwgeom_getType((uint8_t) sub_geom[0]);
- if (type != LINETYPE) return NULL;
-
- return lwline_deserialize(sub_geom);
-}
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual geometry isnt a POLYGON, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a polygon, multipolygon or geometrycollection
- */
-LWPOLY *
-lwgeom_getpoly(uint8_t *serialized_form, int geom_number)
-{
- uint8_t type = lwgeom_getType((uint8_t)serialized_form[0]);
- uint8_t *sub_geom;
-
- if ((type == POLYGONTYPE) && (geom_number == 0))
- {
- /* Be nice and do as they want instead of what they say */
- return lwpoly_deserialize(serialized_form);
- }
-
- if ((type != MULTIPOLYGONTYPE) && (type != COLLECTIONTYPE) )
- return NULL;
-
- sub_geom = lwgeom_getsubgeometry(serialized_form, geom_number);
- if (sub_geom == NULL) return NULL;
-
- type = lwgeom_getType(sub_geom[0]);
- if (type != POLYGONTYPE) return NULL;
-
- return lwpoly_deserialize(sub_geom);
-}
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual geometry isnt a POLYGON, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a polygon, multipolygon or geometrycollection
- */
-LWPOLY *
-lwgeom_getpoly_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
-{
- uint8_t *sub_geom;
- uint8_t type;
-
- sub_geom = lwgeom_getsubgeometry_inspected(inspected, geom_number);
-
- if (sub_geom == NULL) return NULL;
-
- type = lwgeom_getType(sub_geom[0]);
- if (type != POLYGONTYPE) return NULL;
-
- return lwpoly_deserialize(sub_geom);
-}
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual geometry isnt a TRIANGLE, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a Triangle, Tin or geometrycollection
- */
-LWTRIANGLE *
-lwgeom_gettriangle(uint8_t *serialized_form, int geom_number)
-{
- uint8_t type = lwgeom_getType( (uint8_t) serialized_form[0]);
- uint8_t *sub_geom;
-
- if ((type == TRIANGLETYPE) && (geom_number == 0))
- {
- /* be nice and do as they want instead of what they say */
- return lwtriangle_deserialize(serialized_form);
- }
-
- if ((type != TINTYPE) && (type != COLLECTIONTYPE) )
- return NULL;
-
- sub_geom = lwgeom_getsubgeometry(serialized_form, geom_number);
- if (sub_geom == NULL) return NULL;
-
- type = lwgeom_getType((uint8_t) sub_geom[0]);
- if (type != TRIANGLETYPE) return NULL;
-
- return lwtriangle_deserialize(sub_geom);
-}
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual geometry isnt a TRIANGLE, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a Triangle, Tin or geometrycollection
- */
-LWTRIANGLE *
-lwgeom_gettriangle_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
-{
- uint8_t *sub_geom;
- uint8_t type;
-
- sub_geom = lwgeom_getsubgeometry_inspected(inspected, geom_number);
-
- if (sub_geom == NULL) return NULL;
-
- type = lwgeom_getType(sub_geom[0]);
- if (type != TRIANGLETYPE) return NULL;
-
- return lwtriangle_deserialize(sub_geom);
-}
-
-/*
- * 1st geometry has geom_number = 0
- * if the actual geometry isnt a CIRCULARSTRING, null is returned (see _gettype()).
- * if there arent enough geometries, return null.
- * this is fine to call on a circularstring
- */
-LWCIRCSTRING *
-lwgeom_getcircstring_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
-{
- uint8_t *sub_geom;
- uint8_t type;
-
- sub_geom = lwgeom_getsubgeometry_inspected(inspected, geom_number);
-
- if (sub_geom == NULL) return NULL;
-
- type = lwgeom_getType(sub_geom[0]);
- if (type != CIRCSTRINGTYPE) return NULL;
-
- return lwcircstring_deserialize(sub_geom);
-}
-
-/*
- * 1st geometry has geom_number = 0
- * if there arent enough geometries, return null.
- */
-LWGEOM *lwgeom_getgeom_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
-{
- uint8_t *sub_geom;
- uint8_t type;
-
- sub_geom = lwgeom_getsubgeometry_inspected(inspected, geom_number);
-
- if (sub_geom == NULL) return NULL;
-
- type = lwgeom_getType(sub_geom[0]);
-
- return lwgeom_deserialize(sub_geom);
-}
-
-
-/*
- * This gets the serialized form of a sub-geometry
- *
- * 1st geometry has geom_number = 0
- * if this isnt a multi* geometry, and geom_number ==0 then it returns
- * itself.
- *
- * Returns null on problems.
- *
- * In the future this is how you would access a muli* portion of a
- * geometry collection.
- * GEOMETRYCOLLECTION(MULTIPOINT(0 0, 1 1), LINESTRING(0 0, 1 1))
- * ie. lwgeom_getpoint( lwgeom_getsubgeometry( serialized, 0), 1)
- * --> POINT(1 1)
- *
- * You can inspect the sub-geometry as well if you wish.
- *
- */
-uint8_t *
-lwgeom_getsubgeometry(const uint8_t *serialized_form, int geom_number)
-{
- uint8_t *result;
- /*major cheat!! */
- LWGEOM_INSPECTED *inspected = lwgeom_inspect(serialized_form);
-
- result = lwgeom_getsubgeometry_inspected(inspected, geom_number);
- lwinspected_release(inspected);
- return result;
-}
-
-uint8_t *
-lwgeom_getsubgeometry_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
-{
- if ((geom_number <0) || (geom_number >= inspected->ngeometries) )
- {
- lwerror("lwgeom_getsubgeometry_inspected: geom_number out of range");
- return NULL;
- }
-
- return inspected->sub_geoms[geom_number];
-}
-
-
-/*
- * 1st geometry has geom_number = 0
- * use geom_number = -1 to find the actual type of the serialized form.
- * ie lwgeom_gettype( <'MULTIPOINT(0 0, 1 1)'>, -1)
- * --> multipoint
- * ie lwgeom_gettype( <'MULTIPOINT(0 0, 1 1)'>, 0)
- * --> point
- * gets the 8bit type of the geometry at location geom_number
- */
-uint8_t
-lwgeom_getsubtype(uint8_t *serialized_form, int geom_number)
-{
- char result;
- /*major cheat!! */
- LWGEOM_INSPECTED *inspected = lwgeom_inspect(serialized_form);
-
- result = lwgeom_getsubtype_inspected(inspected, geom_number);
- lwinspected_release(inspected);
- return result;
-}
-
-uint8_t
-lwgeom_getsubtype_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
-{
- if ((geom_number <0) || (geom_number >= inspected->ngeometries) )
- return 99;
-
- return inspected->sub_geoms[geom_number][0]; /* 1st byte is type */
-}
-
-
-/*
- * How many sub-geometries are there?
- * for point,line,polygon will return 1.
- */
-int
-lwgeom_getnumgeometries(uint8_t *serialized_form)
-{
- uint8_t type = lwgeom_getType((uint8_t)serialized_form[0]);
- uint8_t *loc;
-
- if ( (type==POINTTYPE) || (type==LINETYPE) || (type==POLYGONTYPE) || (type==TRIANGLETYPE) ||
- (type==CIRCSTRINGTYPE) || (type==COMPOUNDTYPE) || (type==CURVEPOLYTYPE) )
- {
- return 1;
- }
-
- loc = serialized_form+1;
-
- if (lwgeom_hasBBOX((uint8_t) serialized_form[0]))
- {
- loc += sizeof(BOX2DFLOAT4);
- }
-
- if (lwgeom_hasSRID((uint8_t) serialized_form[0]) )
- {
- loc += 4;
- }
- /* its a GeometryCollection or multi* geometry */
- return lw_get_uint32_t(loc);
-}
-
-/*
- * How many sub-geometries are there?
- * for point,line,polygon will return 1.
- */
-int
-lwgeom_getnumgeometries_inspected(LWGEOM_INSPECTED *inspected)
-{
- return inspected->ngeometries;
-}
-
-
/*
* Set finalType to COLLECTIONTYPE or 0 (0 means choose a best type)
* (ie. give it 2 points and ask it to be a multipoint)
for (t=0; t<nsubgeometries; t++)
{
- lengths[t] = lwgeom_size_subgeom(serialized_subs[t],-1);
+// lengths[t] = lwgeom_size_subgeom(serialized_subs[t],-1);
+ lengths[t] = 0;
total_length += lengths[t];
this_type = lwgeom_getType((uint8_t) (serialized_subs[t][0]));
if (type == (char)-1)
if (retsize) *retsize = lwgeom_empty_length(srid);
}
-/**
- * helper function (not for general use)
- * find the size a geometry (or a sub-geometry)
- * 1st geometry has geom_number = 0
- * use geom_number = -1 to find the actual type of the serialized form.
- * ie lwgeom_gettype( <'MULTIPOINT(0 0, 1 1)'>, -1)
- * --> size of the multipoint
- * ie lwgeom_gettype( <'MULTIPOINT(0 0, 1 1)'>, 0)
- * --> size of the point
- * take a geometry, and find its length
- */
-size_t
-serialized_lwgeom_size(const uint8_t *serialized_form)
-{
- uint8_t type = lwgeom_getType((uint8_t) serialized_form[0]);
- int t;
- const uint8_t *loc;
- uint32_t ngeoms;
- int sub_size;
- int result = 1; /* type */
-
- LWDEBUG(2, "lwgeom_size called");
-
- if (type == POINTTYPE)
- {
- LWDEBUG(3, "lwgeom_size: is a point");
-
- return lwgeom_size_point(serialized_form);
- }
- else if (type == LINETYPE)
- {
- LWDEBUG(3, "lwgeom_size: is a line");
-
- return lwgeom_size_line(serialized_form);
- }
- else if (type == CIRCSTRINGTYPE)
- {
- LWDEBUG(3, "lwgeom_size: is a circularstring");
-
- return lwgeom_size_circstring(serialized_form);
- }
- else if (type == POLYGONTYPE)
- {
- LWDEBUG(3, "lwgeom_size: is a polygon");
-
- return lwgeom_size_poly(serialized_form);
- }
- else if (type == TRIANGLETYPE)
- {
- LWDEBUG(3, "lwgeom_size: is a triangle");
-
- return lwgeom_size_triangle(serialized_form);
- }
- else if (type == COMPOUNDTYPE)
- {
- LWDEBUG(3, "lwgeom_size: is a compound curve");
- }
-
- if ( type == 0 )
- {
- lwerror("lwgeom_size called with unknown-typed serialized geometry");
- return 0;
- }
-
- /*
- * Handle all the multi* and geometrycollections the same
- *
- * NOTE: for a geometry collection of GC of GC of GC we will
- * be recursing...
- */
-
- LWDEBUGF(3, "lwgeom_size called on a geoemtry with type %d", type);
-
- loc = serialized_form+1;
-
- if (lwgeom_hasBBOX((uint8_t) serialized_form[0]))
- {
- LWDEBUG(3, "lwgeom_size: has bbox");
-
- loc += sizeof(BOX2DFLOAT4);
- result +=sizeof(BOX2DFLOAT4);
- }
-
- if (lwgeom_hasSRID( (uint8_t) serialized_form[0]) )
- {
- LWDEBUG(3, "lwgeom_size: has srid");
-
- result +=4;
- loc +=4;
- }
-
-
- ngeoms = lw_get_uint32_t(loc);
- loc +=4;
- result += 4; /* numgeoms */
-
- LWDEBUGF(3, "lwgeom_size called on a geoemtry with %d elems (result so far: %d)", ngeoms, result);
-
- for (t=0; t<ngeoms; t++)
- {
- sub_size = serialized_lwgeom_size(loc);
-
- LWDEBUGF(3, " subsize %d", sub_size);
-
- loc += sub_size;
- result += sub_size;
- }
-
- LWDEBUGF(3, "lwgeom_size returning %d", result);
-
- return result;
-}
-
-size_t
-lwgeom_size_subgeom(const uint8_t *serialized_form, int geom_number)
-{
- if (geom_number == -1)
- {
- return serialized_lwgeom_size(serialized_form);
- }
- return serialized_lwgeom_size( lwgeom_getsubgeometry(serialized_form,geom_number));
-}
-
-
/****************************************************************
* memory management
}
-void
-printMULTI(uint8_t *serialized)
-{
- LWGEOM_INSPECTED *inspected = lwgeom_inspect(serialized);
- LWLINE *line;
- LWPOINT *point;
- LWPOLY *poly;
- int t;
-
- lwnotice("MULTI* geometry (type = %i), with %i sub-geoms",lwgeom_getType((uint8_t)serialized[0]), inspected->ngeometries);
-
- for (t=0; t<inspected->ngeometries; t++)
- {
- lwnotice(" sub-geometry %i:", t);
- line = NULL;
- point = NULL;
- poly = NULL;
-
- line = lwgeom_getline_inspected(inspected,t);
- if (line !=NULL)
- {
- printLWLINE(line);
- }
- poly = lwgeom_getpoly_inspected(inspected,t);
- if (poly !=NULL)
- {
- printLWPOLY(poly);
- }
- point = lwgeom_getpoint_inspected(inspected,t);
- if (point !=NULL)
- {
- printPA(point->point);
- }
- }
-
- lwnotice("end multi*");
-
- lwinspected_release(inspected);
-}
-
/**
* Get the srid from the LWGEOM.
}
-/*
- * given the LWGEOM serialized form (or a pointer into a muli* one)
- * construct a proper LWLINE.
- * serialized_form should point to the 8bit type format (with type = 2)
- * See serialized form doc
- */
-LWLINE *
-lwline_deserialize(uint8_t *serialized_form)
-{
- uint8_t type;
- LWLINE *result;
- uint8_t *loc =NULL;
- uint32_t npoints;
- POINTARRAY *pa;
-
- type = (uint8_t) serialized_form[0];
-
- if ( lwgeom_getType(type) != LINETYPE)
- {
- lwerror("lwline_deserialize: attempt to deserialize a line which is really a %s", lwtype_name(type));
- return NULL;
- }
-
- result = (LWLINE*) lwalloc(sizeof(LWLINE)) ;
- result->type = LINETYPE;
- result->flags = gflags(TYPE_HASZ(type),TYPE_HASM(type),0);
-
- loc = serialized_form+1;
- if (lwgeom_hasBBOX(type))
- {
- BOX2DFLOAT4 *box2df;
-
- LWDEBUG(3, "lwline_deserialize: input has bbox");
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, loc, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- loc += sizeof(BOX2DFLOAT4);
- }
- else
- {
- result->bbox = NULL;
- /*lwnotice("line has NO bbox"); */
- }
-
- if ( lwgeom_hasSRID(type))
- {
- /*lwnotice("line has srid"); */
- result->srid = lw_get_int32_t(loc);
- loc +=4; /* type + SRID */
- }
- else
- {
- /*lwnotice("line has NO srid"); */
- result->srid = SRID_UNKNOWN;
- }
-
- /* we've read the type (1 byte) and SRID (4 bytes, if present) */
-
- npoints = lw_get_uint32_t(loc);
- /*lwnotice("line npoints = %d", npoints); */
- loc +=4;
- pa = ptarray_construct_reference_data(TYPE_HASZ(type)?1:0,
- TYPE_HASM(type)?1:0, npoints, loc);
-
- result->points = pa;
-
- return result;
-}
/*
* convert this line into its serialize form
lwfree(line);
}
-/* find length of this serialized line */
-size_t
-lwgeom_size_line(const uint8_t *serialized_line)
-{
- int type = (uint8_t) serialized_line[0];
- uint32_t result = 1; /*type */
- const uint8_t *loc;
- uint32_t npoints;
-
- LWDEBUG(2, "lwgeom_size_line called");
-
- if ( lwgeom_getType(type) != LINETYPE)
- lwerror("lwgeom_size_line::attempt to find the length of a non-line");
-
-
- loc = serialized_line+1;
-
- if (lwgeom_hasBBOX(type))
- {
- loc += sizeof(BOX2DFLOAT4);
- result +=sizeof(BOX2DFLOAT4);
- }
-
- if ( lwgeom_hasSRID(type))
- {
- loc += 4; /* type + SRID */
- result +=4;
- }
-
- /* we've read the type (1 byte) and SRID (4 bytes, if present) */
- npoints = lw_get_uint32_t(loc);
- result += sizeof(uint32_t); /* npoints */
-
- result += TYPE_NDIMS(type) * sizeof(double) * npoints;
-
- LWDEBUGF(3, "lwgeom_size_line returning %d", result);
-
- return result;
-}
void printLWLINE(LWLINE *line)
{
#include <string.h>
#include "liblwgeom_internal.h"
-LWMCURVE *
-lwmcurve_deserialize(uint8_t *srl)
-{
- LWMCURVE *result;
- LWGEOM_INSPECTED *insp;
- int stype;
- uint8_t type = (uint8_t)srl[0];
- int geomtype = lwgeom_getType(type);
- int i;
-
- if (geomtype != MULTICURVETYPE)
- {
- lwerror("lwmcurve_deserialize called on NON multicurve: %d - %s", geomtype, lwtype_name(geomtype));
- return NULL;
- }
-
- insp = lwgeom_inspect(srl);
-
- result = lwalloc(sizeof(LWMCURVE));
- result->type = MULTICURVETYPE;
- result->flags = gflags(TYPE_HASZ(type), TYPE_HASM(type), 0);
- result->srid = insp->srid;
- result->ngeoms = insp->ngeometries;
-
- if ( insp->ngeometries )
- {
- result->geoms = lwalloc(sizeof(LWGEOM *)*insp->ngeometries);
- }
- else
- {
- result->geoms = NULL;
- }
-
- if (lwgeom_hasBBOX(type))
- {
- BOX2DFLOAT4 *box2df;
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, srl+1, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- }
- else result->bbox = NULL;
-
- for (i = 0; i < insp->ngeometries; i++)
- {
- stype = lwgeom_getType(insp->sub_geoms[i][0]);
- if (stype == CIRCSTRINGTYPE)
- {
- result->geoms[i] = (LWGEOM *)lwcircstring_deserialize(insp->sub_geoms[i]);
- }
- else if (stype == LINETYPE)
- {
- result->geoms[i] = (LWGEOM *)lwline_deserialize(insp->sub_geoms[i]);
- }
- else if (stype == COMPOUNDTYPE)
- {
- result->geoms[i] = (LWGEOM *)lwcompound_deserialize(insp->sub_geoms[i]);
- }
- else
- {
- lwerror("Only Circular strings, Line strings or Compound curves are permitted in a MultiCurve.");
-
- lwfree(result);
- lwfree(insp);
- return NULL;
- }
-
- if (FLAGS_NDIMS(result->geoms[i]->flags) != FLAGS_NDIMS(result->flags))
- {
- lwerror("Mixed dimensions (multicurve: %d, curve %d:%d)",
- FLAGS_NDIMS(result->flags), i,
- FLAGS_NDIMS(result->geoms[i]->flags));
- lwfree(result);
- lwfree(insp);
- return NULL;
- }
- }
- lwinspected_release(insp);
-
- return result;
-}
return ret;
}
-LWMLINE *
-lwmline_deserialize(uint8_t *srl)
-{
- LWMLINE *result;
- LWGEOM_INSPECTED *insp;
- uint8_t type = (uint8_t)srl[0];
- int geomtype = TYPE_GETTYPE(srl[0]);
- int i;
-
- if ( geomtype != MULTILINETYPE )
- {
- lwerror("lwmline_deserialize called on NON multiline: %d - %s",
- geomtype, lwtype_name(geomtype));
- return NULL;
- }
-
- insp = lwgeom_inspect(srl);
-
- result = lwalloc(sizeof(LWMLINE));
- result->type = geomtype;
- result->flags = gflags( TYPE_HASZ(type), TYPE_HASM(type), 0);
- result->srid = insp->srid;
- result->ngeoms = insp->ngeometries;
-
- if ( insp->ngeometries )
- {
- result->geoms = lwalloc(sizeof(LWLINE *)*insp->ngeometries);
- }
- else
- {
- result->geoms = NULL;
- }
-
- if (lwgeom_hasBBOX(type))
- {
- BOX2DFLOAT4 *box2df;
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, srl+1, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- }
- else result->bbox = NULL;
-
- for (i=0; i<insp->ngeometries; i++)
- {
- result->geoms[i] = lwline_deserialize(insp->sub_geoms[i]);
- if ( FLAGS_NDIMS(result->geoms[i]->flags) != FLAGS_NDIMS(result->flags) )
- {
- lwerror("Mixed dimensions (multiline:%d, line%d:%d)",
- FLAGS_NDIMS(result->flags), i,
- FLAGS_NDIMS(result->geoms[i]->flags)
- );
- return NULL;
- }
- }
- lwinspected_release(insp);
-
- return result;
-}
LWMLINE* lwmline_add_lwline(LWMLINE *mobj, const LWLINE *obj)
{
}
-LWMPOINT *
-lwmpoint_deserialize(uint8_t *srl)
-{
- LWMPOINT *result;
- LWGEOM_INSPECTED *insp;
- uint8_t type = (uint8_t)srl[0];
- int geomtype = lwgeom_getType(type);
- int i;
-
- if ( geomtype != MULTIPOINTTYPE )
- {
- lwerror("lwmpoint_deserialize called on NON multipoint: %d - %s",
- geomtype, lwtype_name(geomtype));
- return NULL;
- }
-
- insp = lwgeom_inspect(srl);
-
- result = lwalloc(sizeof(LWMPOINT));
- result->type = geomtype;
- result->flags = gflags(TYPE_HASZ(type), TYPE_HASM(type), 0);
- result->srid = insp->srid;
- result->ngeoms = insp->ngeometries;
-
- if ( insp->ngeometries )
- {
- result->geoms = lwalloc(sizeof(LWPOINT *)*insp->ngeometries);
- }
- else
- {
- result->geoms = NULL;
- }
- if (lwgeom_hasBBOX(type))
- {
- BOX2DFLOAT4 *box2df;
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, srl+1, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- }
- else
- {
- result->bbox = NULL;
- }
-
- for (i=0; i<insp->ngeometries; i++)
- {
- result->geoms[i] = lwpoint_deserialize(insp->sub_geoms[i]);
- if ( FLAGS_NDIMS(result->geoms[i]->flags) != FLAGS_NDIMS(result->flags) )
- {
- lwerror("Mixed dimensions (multipoint:%d, point%d:%d)",
- FLAGS_NDIMS(result->flags), i,
- FLAGS_NDIMS(result->geoms[i]->flags)
- );
- return NULL;
- }
- }
- lwinspected_release(insp);
-
- return result;
-}
LWMPOINT* lwmpoint_add_lwpoint(LWMPOINT *mobj, const LWPOINT *obj)
{
return ret;
}
-LWMPOLY *
-lwmpoly_deserialize(uint8_t *srl)
-{
- LWMPOLY *result;
- LWGEOM_INSPECTED *insp;
- uint8_t type = (uint8_t)srl[0];
- int geomtype = TYPE_GETTYPE(type);
- int i;
-
- LWDEBUG(2, "lwmpoly_deserialize called");
-
- if ( geomtype != MULTIPOLYGONTYPE )
- {
- lwerror("lwmpoly_deserialize called on NON multipoly: %d - %s",
- geomtype, lwtype_name(geomtype));
- return NULL;
- }
-
- insp = lwgeom_inspect(srl);
-
- result = lwalloc(sizeof(LWMPOLY));
- result->type = geomtype;
- result->flags = gflags(TYPE_HASZ(type),TYPE_HASM(type),0);
- result->srid = insp->srid;
- result->ngeoms = insp->ngeometries;
-
- if ( insp->ngeometries )
- {
- result->geoms = lwalloc(sizeof(LWPOLY *)*insp->ngeometries);
- }
- else
- {
- result->geoms = NULL;
- }
-
- if (lwgeom_hasBBOX(type))
- {
- BOX2DFLOAT4 *box2df;
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, srl+1, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- }
- else result->bbox = NULL;
-
- for (i=0; i<insp->ngeometries; i++)
- {
- result->geoms[i] = lwpoly_deserialize(insp->sub_geoms[i]);
- if ( FLAGS_NDIMS(result->geoms[i]->flags) != FLAGS_NDIMS(result->flags) )
- {
- lwerror("Mixed dimensions (multipoly:%d, poly%d:%d)",
- FLAGS_NDIMS(result->flags), i,
- FLAGS_NDIMS(result->geoms[i]->flags)
- );
- return NULL;
- }
- }
- lwinspected_release(insp);
-
- return result;
-}
LWMPOLY* lwmpoly_add_lwpoly(LWMPOLY *mobj, const LWPOLY *obj)
{
#include "lwgeom_log.h"
-LWMSURFACE *
-lwmsurface_deserialize(uint8_t *srl)
-{
- LWMSURFACE *result;
- LWGEOM_INSPECTED *insp;
- int stype;
- uint8_t type = (uint8_t)srl[0];
- int geomtype = lwgeom_getType(type);
- int i;
-
- LWDEBUG(2, "lwmsurface_deserialize called");
-
- if (geomtype != MULTISURFACETYPE)
- {
- lwerror("lwmsurface_deserialize called on a non-multisurface: %d - %s", geomtype, lwtype_name(geomtype));
- return NULL;
- }
-
- insp = lwgeom_inspect(srl);
-
- result = lwalloc(sizeof(LWMSURFACE));
- result->type = geomtype;
- result->flags = gflags(TYPE_HASZ(type), TYPE_HASM(type), 0);
- result->srid = insp->srid;
- result->ngeoms = insp->ngeometries;
-
- if ( insp->ngeometries )
- {
- result->geoms = lwalloc(sizeof(LWPOLY *)*insp->ngeometries);
- }
- else
- {
- result->geoms = NULL;
- }
-
- if (lwgeom_hasBBOX(type))
- {
- BOX2DFLOAT4 *box2df;
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, srl + 1, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- }
- else result->bbox = NULL;
-
- for (i = 0; i < insp->ngeometries; i++)
- {
- stype = lwgeom_getType(insp->sub_geoms[i][0]);
- if (stype == POLYGONTYPE)
- {
- result->geoms[i] = (LWGEOM *)lwpoly_deserialize(insp->sub_geoms[i]);
- }
- else if (stype == CURVEPOLYTYPE)
- {
- result->geoms[i] = (LWGEOM *)lwcurvepoly_deserialize(insp->sub_geoms[i]);
- }
- else
- {
- lwerror("Only Polygons and Curved Polygons are supported in a MultiSurface.");
- lwfree(result);
- lwfree(insp);
- return NULL;
- }
-
- if (FLAGS_NDIMS(result->geoms[i]->flags) != FLAGS_NDIMS(result->flags))
- {
- lwerror("Mixed dimensions (multisurface: %d, surface %d:%d",
- FLAGS_NDIMS(result->flags), i,
- FLAGS_NDIMS(result->geoms[i]->flags));
- lwfree(result);
- lwfree(insp);
- return NULL;
- }
- }
- lwinspected_release(insp);
-
- return result;
-}
-
return lwpoint_construct(srid, NULL, pa);
}
-/*
- * Given the LWPOINT serialized form (or a pointer into a muli* one)
- * construct a proper LWPOINT.
- * serialized_form should point to the 8bit type format (with type = 1)
- * See serialized form doc
- */
-LWPOINT *
-lwpoint_deserialize(uint8_t *serialized_form)
-{
- int geom_type;
- LWPOINT *result;
- uint8_t *loc = NULL;
- POINTARRAY *pa;
- uint8_t type;
-
- LWDEBUG(2, "lwpoint_deserialize called");
-
- result = (LWPOINT*) lwalloc(sizeof(LWPOINT)) ;
-
- type = (uint8_t) serialized_form[0];
- geom_type = TYPE_GETTYPE(type);
-
- if ( geom_type != POINTTYPE)
- {
- lwerror("lwpoint_deserialize: attempt to deserialize a point which is really a %s", lwtype_name(geom_type));
- return NULL;
- }
- result->type = geom_type;
- result->flags = gflags(TYPE_HASZ(type),TYPE_HASM(type),0);
-
- loc = serialized_form+1;
-
- if (TYPE_HASBBOX(type))
- {
- BOX2DFLOAT4 box2df;
-
- LWDEBUG(3, "lwpoint_deserialize: input has bbox");
-
- FLAGS_SET_BBOX(result->flags, 1);
- memcpy(&box2df, loc, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, &box2df);
- loc += sizeof(BOX2DFLOAT4);
- }
- else
- {
- result->bbox = NULL;
- }
-
- if ( TYPE_HASSRID(type))
- {
- LWDEBUG(3, "lwpoint_deserialize: input has SRID");
-
- result->srid = lw_get_int32_t(loc);
- loc += 4; /* type + SRID */
- }
- else
- {
- result->srid = SRID_UNKNOWN;
- }
-
- /* we've read the type (1 byte) and SRID (4 bytes, if present) */
- pa = ptarray_construct_reference_data(FLAGS_GET_Z(result->flags), FLAGS_GET_M(result->flags), 1, loc);
-
- result->point = pa;
-
- return result;
-}
-
void lwpoint_free(LWPOINT *pt)
{
if ( pt->bbox )
}
-/* Find length of this serialized point */
-size_t
-lwgeom_size_point(const uint8_t *serialized_point)
-{
- uint32_t result = 1;
- uint8_t type;
- const uint8_t *loc;
-
- type = serialized_point[0];
-
- if ( lwgeom_getType(type) != POINTTYPE) return 0;
-
- LWDEBUGF(2, "lwgeom_size_point called (%d)", result);
-
- loc = serialized_point+1;
-
- if (lwgeom_hasBBOX(type))
- {
- loc += sizeof(BOX2DFLOAT4);
- result +=sizeof(BOX2DFLOAT4);
-
- LWDEBUGF(3, "lwgeom_size_point: has bbox (%d)", result);
- }
-
- if ( lwgeom_hasSRID(type))
- {
- LWDEBUGF(3, "lwgeom_size_point: has srid (%d)", result);
-
- loc +=4; /* type + SRID */
- result +=4;
- }
-
- result += TYPE_NDIMS(type)*sizeof(double);
-
- return result;
-}
void
lwpoint_release(LWPOINT *lwpoint)
}
-/*
- * given the LWPOLY serialized form (or a pointer into a muli* one)
- * construct a proper LWPOLY.
- * serialized_form should point to the 8bit type format (with type = 3)
- * See serialized form doc
- */
-LWPOLY *
-lwpoly_deserialize(uint8_t *serialized_form)
-{
-
- LWPOLY *result;
- uint32_t nrings;
- int ndims, hasz, hasm;
- uint32_t npoints;
- uint8_t type;
- uint8_t *loc;
- int t;
-
- if (serialized_form == NULL)
- {
- lwerror("lwpoly_deserialize called with NULL arg");
- return NULL;
- }
-
- type = serialized_form[0];
- ndims = TYPE_NDIMS(type);
- hasz = TYPE_HASZ(type);
- hasm = TYPE_HASM(type);
-
- result = (LWPOLY*) lwalloc(sizeof(LWPOLY));
- result->type = TYPE_GETTYPE(type);
- result->flags = gflags(hasz, hasm, 0);
-
- loc = serialized_form;
-
- if ( TYPE_GETTYPE(type) != POLYGONTYPE)
- {
- lwerror("lwpoly_deserialize: attempt to deserialize a poly which is really a %s", lwtype_name(type));
- return NULL;
- }
-
-
- loc = serialized_form+1;
-
- if (lwgeom_hasBBOX(type))
- {
- BOX2DFLOAT4 *box2df;
-
- LWDEBUG(3, "lwpoly_deserialize: input has bbox");
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, loc, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- loc += sizeof(BOX2DFLOAT4);
- }
- else
- {
- result->bbox = NULL;
- }
-
- if ( lwgeom_hasSRID(type))
- {
- result->srid = lw_get_int32_t(loc);
- loc +=4; /* type + SRID */
- }
- else
- {
- result->srid = SRID_UNKNOWN;
- }
-
- nrings = lw_get_uint32_t(loc);
- result->nrings = nrings;
- result->maxrings = nrings;
- loc +=4;
- if ( nrings )
- {
- result->rings = (POINTARRAY**) lwalloc(nrings* sizeof(POINTARRAY*));
- }
- else
- {
- result->rings = NULL;
- }
-
- for (t =0; t<nrings; t++)
- {
- /* read in a single ring and make a PA */
- npoints = lw_get_uint32_t(loc);
- loc +=4;
- result->rings[t] = ptarray_construct_reference_data(hasz, hasm, npoints, loc);
- loc += sizeof(double)*ndims*npoints;
- }
-
- return result;
-}
-
/*
* create the serialized form of the polygon
* result's first char will be the 8bit type. See serialized form doc
}
-/* find length of this serialized polygon */
-size_t
-lwgeom_size_poly(const uint8_t *serialized_poly)
-{
- uint32_t result = 1; /* char type */
- uint32_t nrings;
- int ndims;
- int t;
- uint8_t type;
- uint32_t npoints;
- const uint8_t *loc;
-
- if (serialized_poly == NULL)
- return -9999;
-
-
- type = (uint8_t) serialized_poly[0];
- ndims = TYPE_NDIMS(type);
-
- if ( lwgeom_getType(type) != POLYGONTYPE)
- return -9999;
-
-
- loc = serialized_poly+1;
-
- if (lwgeom_hasBBOX(type))
- {
- LWDEBUG(3, "lwgeom_size_poly: has bbox");
-
- loc += sizeof(BOX2DFLOAT4);
- result +=sizeof(BOX2DFLOAT4);
- }
-
-
- if ( lwgeom_hasSRID(type))
- {
- LWDEBUG(3, "lwgeom_size_poly: has srid");
-
- loc +=4; /* type + SRID */
- result += 4;
- }
-
-
- nrings = lw_get_uint32_t(loc);
- loc +=4;
- result +=4;
-
- LWDEBUGF(3, "lwgeom_size_poly contains %d rings", nrings);
-
- for (t =0; t<nrings; t++)
- {
- /* read in a single ring and make a PA */
- npoints = lw_get_uint32_t(loc);
- loc += 4;
- result += 4;
-
- if (ndims == 3)
- {
- loc += 24*npoints;
- result += 24*npoints;
- }
- else if (ndims == 2)
- {
- loc += 16*npoints;
- result += 16*npoints;
- }
- else if (ndims == 4)
- {
- loc += 32*npoints;
- result += 32*npoints;
- }
- }
-
- LWDEBUGF(3, "lwgeom_size_poly returning %d", result);
-
- return result;
-}
-
/* find length of this deserialized polygon */
size_t
lwpoly_serialize_size(LWPOLY *poly)
#include "lwgeom_log.h"
-LWPSURFACE *
-lwpsurface_deserialize(uint8_t *srl)
-{
- LWPSURFACE *result;
- LWGEOM_INSPECTED *insp;
- int type = lwgeom_getType(srl[0]);
- int i;
-
- LWDEBUG(2, "lwpsurface_deserialize called");
-
- if ( type != POLYHEDRALSURFACETYPE )
- {
- lwerror("lwpsurface_deserialize called on NON polyhedralsurface: %d - %s",
- type, lwtype_name(type));
- return NULL;
- }
-
- insp = lwgeom_inspect(srl);
-
- result = lwalloc(sizeof(LWPSURFACE));
- result->type = type;
- result->flags = gflags(TYPE_HASZ(srl[0]), TYPE_HASM(srl[0]), 0);
- result->srid = insp->srid;
- result->ngeoms = insp->ngeometries;
-
- if ( insp->ngeometries )
- {
- result->geoms = lwalloc(sizeof(LWPOLY *)*insp->ngeometries);
- }
- else
- {
- result->geoms = NULL;
- }
-
- if (lwgeom_hasBBOX(srl[0]))
- {
- BOX2DFLOAT4 *box2df;
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, srl+1, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- }
- else result->bbox = NULL;
-
- for (i=0; i<insp->ngeometries; i++)
- {
- result->geoms[i] = lwpoly_deserialize(insp->sub_geoms[i]);
- if ( TYPE_NDIMS(result->geoms[i]->type) != TYPE_NDIMS(result->type) )
- {
- lwerror("Mixed dimensions (polyhedralsurface:%d, face%d:%d)",
- TYPE_NDIMS(result->type), i,
- TYPE_NDIMS(result->geoms[i]->type)
- );
- return NULL;
- }
- }
-
- return result;
-}
-
-
LWPSURFACE* lwpsurface_add_lwpoly(LWPSURFACE *mobj, const LWPOLY *obj)
{
return (LWPSURFACE*)lwcollection_add_lwgeom((LWCOLLECTION*)mobj, (LWGEOM*)obj);
#include "lwgeom_log.h"
-LWTIN *
-lwtin_deserialize(uint8_t *srl)
-{
- LWTIN *result;
- LWGEOM_INSPECTED *insp;
- int type = lwgeom_getType(srl[0]);
- int i;
-
- LWDEBUG(2, "lwtin_deserialize called");
-
- if ( type != TINTYPE )
- {
- lwerror("lwtin called on NON tin: %d - %s", type, lwtype_name(type));
- return NULL;
- }
-
- insp = lwgeom_inspect(srl);
-
- result = lwalloc(sizeof(LWTIN));
- result->type = TINTYPE;
- result->flags = gflags(TYPE_HASZ(insp->type), TYPE_HASM(insp->type), 0);
- result->srid = insp->srid;
- result->ngeoms = insp->ngeometries;
-
- if ( insp->ngeometries )
- {
- result->geoms = lwalloc(sizeof(LWTRIANGLE *)*insp->ngeometries);
- }
- else
- {
- result->geoms = NULL;
- }
-
- if (lwgeom_hasBBOX(srl[0]))
- {
- BOX2DFLOAT4 *box2df;
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, srl+1, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
- }
- else result->bbox = NULL;
-
- for (i=0; i<insp->ngeometries; i++)
- {
- result->geoms[i] = lwtriangle_deserialize(insp->sub_geoms[i]);
- if ( FLAGS_NDIMS(result->geoms[i]->flags) != FLAGS_NDIMS(result->flags) )
- {
- lwerror("Mixed dimensions (tin:%d, triangle%d:%d)",
- FLAGS_NDIMS(result->flags), i,
- FLAGS_NDIMS(result->geoms[i]->flags)
- );
- return NULL;
- }
- }
-
- return result;
-}
-
LWTIN* lwtin_add_lwtriangle(LWTIN *mobj, const LWTRIANGLE *obj)
{
}
-/*
- * given the LWTRIANGLE serialized form
- * construct a proper LWTRIANGLE.
- * serialized_form should point to the 8bit type format
- * See serialized form doc
- */
-LWTRIANGLE *
-lwtriangle_deserialize(uint8_t *serialized_form)
-{
- LWTRIANGLE *result;
- POINTARRAY *pa;
- int ndims;
- uint32_t npoints;
- uint8_t type;
- uint8_t *loc;
-
- LWDEBUG(3, "lwtriangle_deserialize called");
-
- if (serialized_form == NULL)
- {
- lwerror("lwtriangle_deserialize called with NULL arg");
- return NULL;
- }
-
- result = (LWTRIANGLE*) lwalloc(sizeof(LWTRIANGLE));
-
- type = serialized_form[0];
- result->type = TRIANGLETYPE;
-
- ndims = TYPE_NDIMS(type);
- result->flags = gflags(TYPE_HASZ(type),TYPE_HASM(type),0);
- loc = serialized_form;
-
- if ( TYPE_GETTYPE(type) != TRIANGLETYPE)
- {
- lwerror("lwtriangle_deserialize: attempt to deserialize a triangle which is really a %s",
- lwtype_name(TYPE_GETTYPE(type)));
- return NULL;
- }
-
- loc = serialized_form+1;
-
- if (lwgeom_hasBBOX(type))
- {
- BOX2DFLOAT4 *box2df;
-
- LWDEBUG(3, "lwtriangle_deserialize: input has bbox");
-
- FLAGS_SET_BBOX(result->flags, 1);
- box2df = lwalloc(sizeof(BOX2DFLOAT4));
- memcpy(box2df, loc, sizeof(BOX2DFLOAT4));
- result->bbox = gbox_from_box2df(result->flags, box2df);
- lwfree(box2df);
-
- loc += sizeof(BOX2DFLOAT4);
- }
- else
- {
- result->bbox = NULL;
- }
-
- if ( lwgeom_hasSRID(type))
- {
- result->srid = lw_get_int32_t(loc);
- loc +=4; /* type + SRID */
- }
- else
- {
- result->srid = SRID_UNKNOWN;
- }
-
- npoints = lw_get_uint32_t(loc);
- /*lwnotice("triangle npoints = %d", npoints); */
- loc +=4;
- pa = ptarray_construct_reference_data(FLAGS_GET_Z(result->flags), FLAGS_GET_M(result->flags), npoints, loc);
-
- result->points = pa;
-
- return result;
-}
-
/*
* create the serialized form of the triangle
* result's first char will be the 8bit type. See serialized form doc
return result;
}
-size_t
-lwgeom_size_triangle(const uint8_t *serialized_triangle)
-{
- int type = (uint8_t) serialized_triangle[0];
- uint32_t result = 1; /*type */
- const uint8_t *loc;
- uint32_t npoints;
-
- LWDEBUG(2, "lwgeom_size_triangle called");
-
- if ( lwgeom_getType(type) != TRIANGLETYPE)
- lwerror("lwgeom_size_triangle::attempt to find the length of a non-triangle");
-
-
- loc = serialized_triangle+1;
-
- if (lwgeom_hasBBOX(type))
- {
- loc += sizeof(BOX2DFLOAT4);
- result +=sizeof(BOX2DFLOAT4);
- }
-
- if ( lwgeom_hasSRID(type))
- {
- loc += 4; /* type + SRID */
- result +=4;
- }
-
- /* we've read the type (1 byte) and SRID (4 bytes, if present) */
- npoints = lw_get_uint32_t(loc);
- result += sizeof(uint32_t); /* npoints */
-
- result += TYPE_NDIMS(type) * sizeof(double) * npoints;
-
- LWDEBUGF(3, "lwgeom_size_triangle returning %d", result);
-
- return result;
-}
/* find length of this deserialized triangle */