2006-05-30 08:38:58 +00:00
|
|
|
/**********************************************************************
|
2008-06-29 19:11:48 +00:00
|
|
|
* $Id: lwmpoint.c 2369 2006-05-30 08:38:58Z strk $
|
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.
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
lwgeom_release(lwmpoint_as_lwgeom(lwmpoint));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if ( type != MULTIPOINTTYPE )
|
|
|
|
{
|
|
|
|
lwerror("lwmpoint_deserialize called on NON multipoint: %d",
|
|
|
|
type);
|
|
|
|
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;
|
|
|
|
result->geoms = lwalloc(sizeof(LWPOINT *)*result->ngeoms);
|
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));
|
|
|
|
}
|
2004-10-08 13:20:55 +00:00
|
|
|
else result->bbox = NULL;
|
|
|
|
|
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)",
|
2004-10-04 13:53:42 +00:00
|
|
|
TYPE_NDIMS(result->type), i,
|
|
|
|
TYPE_NDIMS(result->geoms[i]->type)
|
|
|
|
);
|
2004-09-29 10:50:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-01-09 15:12:02 +00:00
|
|
|
/*
|
|
|
|
* Add 'what' to this multipoint at position 'where'.
|
|
|
|
* where=0 == prepend
|
|
|
|
* where=-1 == append
|
|
|
|
* Returns a MULTIPOINT or a COLLECTION
|
|
|
|
*/
|
2004-10-01 14:49:34 +00:00
|
|
|
LWGEOM *
|
|
|
|
lwmpoint_add(const LWMPOINT *to, uint32 where, const LWGEOM *what)
|
|
|
|
{
|
|
|
|
LWCOLLECTION *col;
|
|
|
|
LWGEOM **geoms;
|
|
|
|
int newtype;
|
|
|
|
uint32 i;
|
|
|
|
|
|
|
|
if ( where == -1 ) where = to->ngeoms;
|
|
|
|
else if ( where < -1 || where > to->ngeoms )
|
|
|
|
{
|
|
|
|
lwerror("lwmpoint_add: add position out of range %d..%d",
|
|
|
|
-1, to->ngeoms);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-01-09 15:12:02 +00:00
|
|
|
/* dimensions compatibility are checked by caller */
|
2004-10-01 14:49:34 +00:00
|
|
|
|
2006-01-09 15:12:02 +00:00
|
|
|
/* Construct geoms array */
|
2004-10-01 14:49:34 +00:00
|
|
|
geoms = lwalloc(sizeof(LWGEOM *)*(to->ngeoms+1));
|
|
|
|
for (i=0; i<where; i++)
|
|
|
|
{
|
|
|
|
geoms[i] = lwgeom_clone((LWGEOM *)to->geoms[i]);
|
|
|
|
}
|
|
|
|
geoms[where] = lwgeom_clone(what);
|
|
|
|
for (i=where; i<to->ngeoms; i++)
|
|
|
|
{
|
|
|
|
geoms[i+1] = lwgeom_clone((LWGEOM *)to->geoms[i]);
|
|
|
|
}
|
|
|
|
|
2004-10-04 13:53:42 +00:00
|
|
|
if ( TYPE_GETTYPE(what->type) == POINTTYPE ) newtype = MULTIPOINTTYPE;
|
2004-10-01 14:49:34 +00:00
|
|
|
else newtype = COLLECTIONTYPE;
|
|
|
|
|
2004-10-05 16:28:34 +00:00
|
|
|
col = lwcollection_construct(newtype,
|
2004-10-08 13:20:55 +00:00
|
|
|
to->SRID, NULL,
|
2004-10-04 13:53:42 +00:00
|
|
|
to->ngeoms+1, geoms);
|
2004-10-01 14:49:34 +00:00
|
|
|
|
|
|
|
return (LWGEOM *)col;
|
|
|
|
|
|
|
|
}
|
2008-12-19 19:20:34 +00:00
|
|
|
|
2009-01-09 17:41:23 +00:00
|
|
|
void lwmpoint_free(LWMPOINT *mpt)
|
2008-12-19 19:20:34 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if( mpt->bbox )
|
|
|
|
{
|
|
|
|
lwfree(mpt->bbox);
|
|
|
|
}
|
|
|
|
for ( i = 0; i < mpt->ngeoms; i++ )
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
if( mpt->geoms )
|
|
|
|
{
|
|
|
|
lwfree(mpt->geoms);
|
|
|
|
}
|
|
|
|
lwfree(mpt);
|
|
|
|
|
2009-01-12 19:29:57 +00:00
|
|
|
};
|
|
|
|
|