2006-05-30 08:38:58 +00:00
|
|
|
/**********************************************************************
|
2009-02-04 00:28:37 +00:00
|
|
|
* $Id$
|
2006-05-30 08:38:58 +00:00
|
|
|
*
|
|
|
|
* 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-06-11 16:44:03 +00:00
|
|
|
*
|
2006-05-30 08:38:58 +00:00
|
|
|
**********************************************************************/
|
|
|
|
|
2004-09-29 10:50:30 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2004-09-29 15:25:21 +00:00
|
|
|
#include <string.h>
|
2004-09-29 10:50:30 +00:00
|
|
|
#include "liblwgeom.h"
|
|
|
|
|
2009-01-08 17:04:00 +00:00
|
|
|
void
|
|
|
|
lwmpoint_release(LWMPOINT *lwmpoint)
|
|
|
|
{
|
2009-06-11 16:44:03 +00:00
|
|
|
lwgeom_release(lwmpoint_as_lwgeom(lwmpoint));
|
2009-01-08 17:04:00 +00:00
|
|
|
}
|
|
|
|
|
2010-03-16 03:13:33 +00:00
|
|
|
LWMPOINT *
|
|
|
|
lwmpoint_construct_empty(int srid, char hasz, char hasm)
|
|
|
|
{
|
|
|
|
LWMPOINT *ret = (LWMPOINT*)lwcollection_construct_empty(srid, hasz, hasm);
|
|
|
|
TYPE_SETTYPE(ret->type, MULTIPOINTTYPE);
|
|
|
|
return ret;
|
|
|
|
}
|
2009-01-08 17:04:00 +00:00
|
|
|
|
2004-09-29 10:50:30 +00:00
|
|
|
LWMPOINT *
|
2005-02-10 10:52:53 +00:00
|
|
|
lwmpoint_deserialize(uchar *srl)
|
2004-09-29 10:50:30 +00:00
|
|
|
{
|
|
|
|
LWMPOINT *result;
|
|
|
|
LWGEOM_INSPECTED *insp;
|
|
|
|
int type = lwgeom_getType(srl[0]);
|
|
|
|
int i;
|
|
|
|
|
2009-06-11 16:44:03 +00:00
|
|
|
if ( type != MULTIPOINTTYPE )
|
2004-09-29 10:50:30 +00:00
|
|
|
{
|
2010-08-13 17:30:10 +00:00
|
|
|
lwerror("lwmpoint_deserialize called on NON multipoint: %d - %s",
|
|
|
|
type, lwtype_name(type));
|
2004-09-29 10:50:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
insp = lwgeom_inspect(srl);
|
|
|
|
|
|
|
|
result = lwalloc(sizeof(LWMPOINT));
|
2004-10-04 13:53:42 +00:00
|
|
|
result->type = insp->type;
|
2004-09-29 10:50:30 +00:00
|
|
|
result->SRID = insp->SRID;
|
2004-09-30 08:18:06 +00:00
|
|
|
result->ngeoms = insp->ngeometries;
|
2010-02-01 17:35:55 +00:00
|
|
|
|
2010-01-04 17:55:14 +00:00
|
|
|
if ( insp->ngeometries )
|
|
|
|
{
|
|
|
|
result->geoms = lwalloc(sizeof(LWPOINT *)*insp->ngeometries);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->geoms = NULL;
|
|
|
|
}
|
2004-09-29 10:50:30 +00:00
|
|
|
|
2004-10-08 13:20:55 +00:00
|
|
|
if (lwgeom_hasBBOX(srl[0]))
|
2005-11-14 09:01:15 +00:00
|
|
|
{
|
|
|
|
result->bbox = lwalloc(sizeof(BOX2DFLOAT4));
|
|
|
|
memcpy(result->bbox, srl+1, sizeof(BOX2DFLOAT4));
|
|
|
|
}
|
2010-02-01 17:35:55 +00:00
|
|
|
else
|
2010-01-04 17:55:14 +00:00
|
|
|
{
|
|
|
|
result->bbox = NULL;
|
|
|
|
}
|
2004-10-08 13:20:55 +00:00
|
|
|
|
2004-09-29 10:50:30 +00:00
|
|
|
for (i=0; i<insp->ngeometries; i++)
|
|
|
|
{
|
2004-09-30 08:18:06 +00:00
|
|
|
result->geoms[i] = lwpoint_deserialize(insp->sub_geoms[i]);
|
2004-10-04 13:53:42 +00:00
|
|
|
if ( TYPE_NDIMS(result->geoms[i]->type) != TYPE_NDIMS(result->type) )
|
2004-09-29 10:50:30 +00:00
|
|
|
{
|
|
|
|
lwerror("Mixed dimensions (multipoint:%d, point%d:%d)",
|
2009-06-11 16:44:03 +00:00
|
|
|
TYPE_NDIMS(result->type), i,
|
|
|
|
TYPE_NDIMS(result->geoms[i]->type)
|
|
|
|
);
|
2004-09-29 10:50:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-08-14 10:57:41 +00:00
|
|
|
lwinspected_release(insp);
|
2004-09-29 10:50:30 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-03-16 03:13:33 +00:00
|
|
|
LWMPOINT* lwmpoint_add_lwpoint(LWMPOINT *mobj, const LWPOINT *obj)
|
2004-10-01 14:49:34 +00:00
|
|
|
{
|
2010-03-16 03:13:33 +00:00
|
|
|
LWDEBUG(4, "Called");
|
|
|
|
return (LWMPOINT*)lwcollection_add_lwgeom((LWCOLLECTION*)mobj, (LWGEOM*)obj);
|
2004-10-01 14:49:34 +00:00
|
|
|
}
|
2008-12-19 19:20:34 +00:00
|
|
|
|
2009-06-11 16:44:03 +00:00
|
|
|
void lwmpoint_free(LWMPOINT *mpt)
|
2008-12-19 19:20:34 +00:00
|
|
|
{
|
|
|
|
int i;
|
2009-06-11 16:44:03 +00:00
|
|
|
if ( mpt->bbox )
|
2008-12-19 19:20:34 +00:00
|
|
|
{
|
|
|
|
lwfree(mpt->bbox);
|
|
|
|
}
|
2009-06-11 16:44:03 +00:00
|
|
|
for ( i = 0; i < mpt->ngeoms; i++ )
|
2008-12-19 19:20:34 +00:00
|
|
|
{
|
2009-06-11 16:44:03 +00:00
|
|
|
if ( mpt->geoms[i] )
|
|
|
|
{
|
2009-01-09 17:41:23 +00:00
|
|
|
lwpoint_free(mpt->geoms[i]);
|
2008-12-19 19:20:34 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-11 16:44:03 +00:00
|
|
|
if ( mpt->geoms )
|
2008-12-19 19:20:34 +00:00
|
|
|
{
|
|
|
|
lwfree(mpt->geoms);
|
|
|
|
}
|
|
|
|
lwfree(mpt);
|
2009-06-11 16:44:03 +00:00
|
|
|
|
2009-12-20 17:42:44 +00:00
|
|
|
}
|
2009-01-12 19:29:57 +00:00
|
|
|
|
2010-02-21 12:22:08 +00:00
|
|
|
LWGEOM*
|
|
|
|
lwmpoint_remove_repeated_points(LWMPOINT *mpoint)
|
|
|
|
{
|
2010-03-16 03:13:33 +00:00
|
|
|
uint32 nnewgeoms;
|
|
|
|
uint32 i, j;
|
2010-02-21 12:22:08 +00:00
|
|
|
LWGEOM **newgeoms;
|
|
|
|
|
|
|
|
newgeoms = lwalloc(sizeof(LWGEOM *)*mpoint->ngeoms);
|
|
|
|
nnewgeoms = 0;
|
|
|
|
for (i=0; i<mpoint->ngeoms; ++i)
|
|
|
|
{
|
|
|
|
/* Brute force, may be optimized by building an index */
|
|
|
|
int seen=0;
|
|
|
|
for (j=0; j<nnewgeoms; ++j)
|
|
|
|
{
|
|
|
|
if ( lwpoint_same((LWPOINT*)newgeoms[j],
|
|
|
|
(LWPOINT*)mpoint->geoms[i]) )
|
|
|
|
{
|
|
|
|
seen=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( seen ) continue;
|
|
|
|
newgeoms[nnewgeoms++] = (LWGEOM*)lwpoint_clone(mpoint->geoms[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (LWGEOM*)lwcollection_construct(mpoint->type,
|
|
|
|
mpoint->SRID, mpoint->bbox ? box2d_clone(mpoint->bbox) : NULL,
|
|
|
|
nnewgeoms, newgeoms);
|
|
|
|
|
|
|
|
}
|
|
|
|
|