2008-05-31 09:56:44 +00:00
|
|
|
/**********************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* PostGIS - Spatial Types for PostgreSQL
|
|
|
|
* http://postgis.refractions.net
|
|
|
|
* Copyright 2001-2006 Refractions Research Inc.
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute and/or modify it under
|
|
|
|
* the terms of the GNU General Public Licence. See the COPYING file.
|
2009-05-03 03:50:27 +00:00
|
|
|
*
|
2008-05-31 09:56:44 +00:00
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-11-01 01:28:12 +00:00
|
|
|
#include "liblwgeom_internal.h"
|
2008-05-31 09:56:44 +00:00
|
|
|
|
|
|
|
LWCOMPOUND *
|
|
|
|
lwcompound_deserialize(uchar *serialized)
|
|
|
|
{
|
2009-06-11 16:44:03 +00:00
|
|
|
LWCOMPOUND *result;
|
|
|
|
LWGEOM_INSPECTED *insp;
|
|
|
|
int type = lwgeom_getType(serialized[0]);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (type != COMPOUNDTYPE)
|
|
|
|
{
|
2010-08-06 21:31:54 +00:00
|
|
|
lwerror("lwcompound_deserialize called on non compound: %d - %s", type, lwtype_name(type));
|
2009-06-11 16:44:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
insp = lwgeom_inspect(serialized);
|
|
|
|
|
|
|
|
result = lwalloc(sizeof(LWCOMPOUND));
|
2010-11-21 19:02:23 +00:00
|
|
|
result->type = COMPOUNDTYPE;
|
2010-11-24 00:28:40 +00:00
|
|
|
result->flags = gflags(TYPE_HASZ(insp->type),TYPE_HASM(insp->type),0);
|
2010-11-25 17:34:21 +00:00
|
|
|
result->srid = insp->srid;
|
2009-06-11 16:44:03 +00:00
|
|
|
result->ngeoms = insp->ngeometries;
|
|
|
|
result->geoms = lwalloc(sizeof(LWGEOM *)*insp->ngeometries);
|
|
|
|
|
|
|
|
if (lwgeom_hasBBOX(serialized[0]))
|
|
|
|
{
|
2010-11-21 19:02:23 +00:00
|
|
|
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);
|
2009-06-11 16:44:03 +00:00
|
|
|
}
|
|
|
|
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))
|
2009-05-03 03:50:27 +00:00
|
|
|
{
|
2009-06-11 16:44:03 +00:00
|
|
|
lwerror("Mixed dimensions (compound: %d, line/circularstring %d:%d)",
|
|
|
|
TYPE_NDIMS(result->type), i,
|
|
|
|
TYPE_NDIMS(result->geoms[i]->type)
|
|
|
|
);
|
|
|
|
lwfree(result);
|
|
|
|
return NULL;
|
2009-05-03 03:50:27 +00:00
|
|
|
}
|
2009-06-11 16:44:03 +00:00
|
|
|
}
|
|
|
|
return result;
|
2008-05-31 09:56:44 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 17:29:29 +00:00
|
|
|
int
|
|
|
|
lwcompound_is_closed(LWCOMPOUND *compound)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
int npoints=0;
|
|
|
|
|
2010-11-21 19:02:23 +00:00
|
|
|
if (!FLAGS_GET_Z(compound->flags))
|
2010-08-13 17:29:29 +00:00
|
|
|
size = sizeof(POINT2D);
|
|
|
|
else size = sizeof(POINT3D);
|
2009-06-11 16:44:03 +00:00
|
|
|
|
2010-11-21 19:02:23 +00:00
|
|
|
if (compound->geoms[compound->ngeoms - 1]->type == CIRCSTRINGTYPE)
|
2010-08-13 17:29:29 +00:00
|
|
|
npoints = ((LWCIRCSTRING *)compound->geoms[compound->ngeoms - 1])->points->npoints;
|
2010-11-21 19:02:23 +00:00
|
|
|
else if (compound->geoms[compound->ngeoms - 1]->type == LINETYPE)
|
2010-08-13 17:29:29 +00:00
|
|
|
npoints = ((LWLINE *)compound->geoms[compound->ngeoms - 1])->points->npoints;
|
2008-05-31 09:56:44 +00:00
|
|
|
|
2010-08-13 17:29:29 +00:00
|
|
|
if ( memcmp(getPoint_internal( (POINTARRAY *)compound->geoms[0]->data, 0),
|
2010-08-15 08:30:08 +00:00
|
|
|
getPoint_internal( (POINTARRAY *)compound->geoms[compound->ngeoms - 1]->data,
|
|
|
|
npoints - 1),
|
2010-11-21 19:02:23 +00:00
|
|
|
size) ) return LW_FALSE;
|
2010-08-15 08:30:08 +00:00
|
|
|
|
2010-11-21 19:02:23 +00:00
|
|
|
return LW_TRUE;
|
2010-08-13 17:29:29 +00:00
|
|
|
}
|
2010-11-30 22:55:54 +00:00
|
|
|
|
|
|
|
double lwcompound_length(const LWCOMPOUND *comp)
|
|
|
|
{
|
|
|
|
double length = 0.0;
|
|
|
|
LWLINE *line;
|
|
|
|
if ( lwgeom_is_empty((LWGEOM*)comp) )
|
|
|
|
return 0.0;
|
|
|
|
line = lwcompound_segmentize(comp, 32);
|
|
|
|
length = lwline_length(line);
|
|
|
|
lwline_free(line);
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
double lwcompound_length_2d(const LWCOMPOUND *comp)
|
|
|
|
{
|
|
|
|
double length = 0.0;
|
|
|
|
LWLINE *line;
|
|
|
|
if ( lwgeom_is_empty((LWGEOM*)comp) )
|
|
|
|
return 0.0;
|
|
|
|
line = lwcompound_segmentize(comp, 32);
|
|
|
|
length = lwline_length_2d(line);
|
|
|
|
lwline_free(line);
|
|
|
|
return length;
|
|
|
|
}
|
2010-12-07 21:08:05 +00:00
|
|
|
|
|
|
|
int lwcompound_add_lwgeom(LWCOMPOUND *comp, LWGEOM *geom)
|
|
|
|
{
|
|
|
|
LWCOLLECTION *col = (LWCOLLECTION*)comp;
|
|
|
|
|
|
|
|
/* Empty things can't continuously join up with other things */
|
|
|
|
if ( lwgeom_is_empty(geom) )
|
|
|
|
{
|
|
|
|
LWDEBUG(4, "Got an empty component for a compound curve!");
|
|
|
|
return LW_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( col->ngeoms > 0 )
|
|
|
|
{
|
|
|
|
POINT4D last, first;
|
|
|
|
/* First point of the component we are adding */
|
|
|
|
LWLINE *newline = (LWLINE*)geom;
|
|
|
|
/* Last point of the previous component */
|
|
|
|
LWLINE *prevline = (LWLINE*)(col->geoms[col->ngeoms-1]);
|
|
|
|
|
|
|
|
getPoint4d_p(newline->points, 0, &first);
|
|
|
|
getPoint4d_p(prevline->points, prevline->points->npoints-1, &last);
|
|
|
|
|
|
|
|
if ( !(FP_EQUALS(first.x,last.x) && FP_EQUALS(first.y,last.y)) )
|
|
|
|
{
|
|
|
|
LWDEBUG(4, "Components don't join up end-to-end!");
|
|
|
|
LWDEBUGF(4, "first pt (%g %g %g %g) last pt (%g %g %g %g)", first.x, first.y, first.z, first.m, last.x, last.y, last.z, last.m);
|
|
|
|
return LW_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
col = lwcollection_add_lwgeom(col, geom);
|
|
|
|
return LW_SUCCESS;
|
|
|
|
}
|