2004-09-29 10:50:30 +00:00
|
|
|
// basic LWLINE functions
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "liblwgeom.h"
|
|
|
|
|
2004-10-03 15:52:23 +00:00
|
|
|
//#define DEBUG_CALLS 1
|
|
|
|
|
|
|
|
|
2004-09-29 10:50:30 +00:00
|
|
|
// construct a new LWLINE. points will *NOT* be copied
|
|
|
|
// use SRID=-1 for unknown SRID (will have 8bit type's S = 0)
|
2004-10-04 13:53:42 +00:00
|
|
|
LWLINE *
|
2004-10-07 10:03:23 +00:00
|
|
|
lwline_construct(int SRID, char wantbbox, POINTARRAY *points)
|
2004-09-29 10:50:30 +00:00
|
|
|
{
|
|
|
|
LWLINE *result;
|
2004-10-07 10:03:23 +00:00
|
|
|
result = (LWLINE*) lwalloc(sizeof(LWLINE));
|
2004-09-29 10:50:30 +00:00
|
|
|
|
2004-10-07 10:03:23 +00:00
|
|
|
result->type = lwgeom_makeType_full(
|
|
|
|
TYPE_HASZ(points->dims),
|
|
|
|
TYPE_HASM(points->dims),
|
|
|
|
(SRID!=-1), LINETYPE,
|
2004-10-04 13:53:42 +00:00
|
|
|
wantbbox);
|
2004-09-29 10:50:30 +00:00
|
|
|
result->SRID = SRID;
|
|
|
|
result->points = points;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2004-10-05 16:28:34 +00:00
|
|
|
LWLINE *
|
|
|
|
lwline_deserialize(char *serialized_form)
|
2004-09-29 10:50:30 +00:00
|
|
|
{
|
|
|
|
unsigned char type;
|
|
|
|
LWLINE *result;
|
|
|
|
char *loc =NULL;
|
|
|
|
uint32 npoints;
|
|
|
|
POINTARRAY *pa;
|
|
|
|
|
|
|
|
result = (LWLINE*) lwalloc(sizeof(LWLINE)) ;
|
|
|
|
|
|
|
|
type = (unsigned char) serialized_form[0];
|
2004-10-04 13:53:42 +00:00
|
|
|
result->type = type;
|
2004-09-29 15:25:21 +00:00
|
|
|
|
2004-09-29 10:50:30 +00:00
|
|
|
if ( lwgeom_getType(type) != LINETYPE)
|
|
|
|
{
|
|
|
|
lwerror("lwline_deserialize: attempt to deserialize a line when its not really a line");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
loc = serialized_form+1;
|
|
|
|
|
|
|
|
if (lwgeom_hasBBOX(type))
|
|
|
|
{
|
|
|
|
//lwnotice("line has bbox");
|
|
|
|
loc += sizeof(BOX2DFLOAT4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//lwnotice("line has NO bbox");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( lwgeom_hasSRID(type))
|
|
|
|
{
|
|
|
|
//lwnotice("line has srid");
|
|
|
|
result->SRID = get_int32(loc);
|
|
|
|
loc +=4; // type + SRID
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//lwnotice("line has NO srid");
|
|
|
|
result->SRID = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we've read the type (1 byte) and SRID (4 bytes, if present)
|
|
|
|
|
|
|
|
npoints = get_uint32(loc);
|
|
|
|
//lwnotice("line npoints = %d", npoints);
|
|
|
|
loc +=4;
|
2004-10-05 16:28:34 +00:00
|
|
|
pa = pointArray_construct(loc, TYPE_HASZ(type), TYPE_HASM(type), npoints);
|
2004-09-29 10:50:30 +00:00
|
|
|
result->points = pa;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert this line into its serialize form
|
|
|
|
// result's first char will be the 8bit type. See serialized form doc
|
2004-10-05 16:28:34 +00:00
|
|
|
char *
|
|
|
|
lwline_serialize(LWLINE *line)
|
2004-09-29 10:50:30 +00:00
|
|
|
{
|
2004-09-30 15:42:28 +00:00
|
|
|
size_t size, retsize;
|
|
|
|
char * result;
|
2004-09-29 10:50:30 +00:00
|
|
|
|
2004-09-30 15:42:28 +00:00
|
|
|
if (line == NULL) lwerror("lwline_serialize:: given null line");
|
2004-09-29 10:50:30 +00:00
|
|
|
|
2004-09-30 15:42:28 +00:00
|
|
|
size = lwline_serialize_size(line);
|
2004-09-29 10:50:30 +00:00
|
|
|
result = lwalloc(size);
|
2004-09-30 15:42:28 +00:00
|
|
|
lwline_serialize_buf(line, result, &retsize);
|
2004-09-29 10:50:30 +00:00
|
|
|
|
2004-09-30 15:42:28 +00:00
|
|
|
if ( retsize != size )
|
2004-09-29 10:50:30 +00:00
|
|
|
{
|
2004-09-30 15:42:28 +00:00
|
|
|
lwerror("lwline_serialize_size returned %d, ..serialize_buf returned %d", size, retsize);
|
2004-09-29 10:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert this line into its serialize form writing it into
|
|
|
|
// the given buffer, and returning number of bytes written into
|
|
|
|
// the given int pointer.
|
|
|
|
// result's first char will be the 8bit type. See serialized form doc
|
2004-10-05 16:28:34 +00:00
|
|
|
void
|
|
|
|
lwline_serialize_buf(LWLINE *line, char *buf, size_t *retsize)
|
2004-09-29 10:50:30 +00:00
|
|
|
{
|
|
|
|
char hasSRID;
|
|
|
|
char *loc;
|
2004-10-05 16:28:34 +00:00
|
|
|
int ptsize = pointArray_ptsize(line->points);
|
|
|
|
|
|
|
|
if ( ptsize != sizeof(double)*TYPE_NDIMS(line->type) )
|
|
|
|
{
|
|
|
|
lwerror("lwline_serialize_buf: line has %d dims, its pointarray has %d dims");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_CALLS
|
|
|
|
lwnotice("lwline_serialize_buf(%p, %p, %p) called",
|
|
|
|
line, buf, retsize);
|
|
|
|
#endif
|
2004-09-29 10:50:30 +00:00
|
|
|
|
|
|
|
if (line == NULL)
|
|
|
|
lwerror("lwline_serialize:: given null line");
|
|
|
|
|
|
|
|
hasSRID = (line->SRID != -1);
|
|
|
|
|
2004-10-05 16:28:34 +00:00
|
|
|
buf[0] = line->type;
|
2004-09-29 10:50:30 +00:00
|
|
|
loc = buf+1;
|
|
|
|
|
2004-10-05 16:28:34 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
lwnotice("lwline_serialize_buf added type (%d)", line->type);
|
|
|
|
#endif
|
|
|
|
|
2004-10-04 13:53:42 +00:00
|
|
|
if (TYPE_HASBBOX(line->type))
|
2004-09-30 15:42:28 +00:00
|
|
|
{
|
|
|
|
lwgeom_compute_bbox_p((LWGEOM *)line, (BOX2DFLOAT4 *)loc);
|
|
|
|
loc += sizeof(BOX2DFLOAT4);
|
2004-10-05 16:28:34 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
lwnotice("lwline_serialize_buf added BBOX");
|
|
|
|
#endif
|
2004-09-30 15:42:28 +00:00
|
|
|
}
|
|
|
|
|
2004-09-29 10:50:30 +00:00
|
|
|
if (hasSRID)
|
|
|
|
{
|
|
|
|
memcpy(loc, &line->SRID, sizeof(int32));
|
2004-10-05 16:28:34 +00:00
|
|
|
loc += sizeof(int32);
|
|
|
|
#ifdef DEBUG
|
|
|
|
lwnotice("lwline_serialize_buf added SRID");
|
|
|
|
#endif
|
2004-09-29 10:50:30 +00:00
|
|
|
}
|
|
|
|
|
2004-10-05 17:15:42 +00:00
|
|
|
memcpy(loc, &line->points->npoints, sizeof(uint32));
|
|
|
|
loc += sizeof(uint32);
|
2004-10-05 16:28:34 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
lwnotice("lwline_serialize_buf added npoints (%d)",
|
|
|
|
line->points->npoints);
|
|
|
|
#endif
|
2004-10-01 14:49:34 +00:00
|
|
|
|
2004-09-29 10:50:30 +00:00
|
|
|
//copy in points
|
2004-10-05 16:28:34 +00:00
|
|
|
memcpy(loc, line->points->serialized_pointlist,
|
|
|
|
ptsize*line->points->npoints);
|
2004-10-05 17:15:42 +00:00
|
|
|
loc += ptsize*line->points->npoints;
|
2004-09-29 10:50:30 +00:00
|
|
|
|
2004-10-05 16:28:34 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
lwnotice("lwline_serialize_buf copied serialized_pointlist (%d bytes)",
|
|
|
|
ptsize * line->points->npoints);
|
|
|
|
#endif
|
2004-09-29 10:50:30 +00:00
|
|
|
|
2004-10-05 16:28:34 +00:00
|
|
|
if (retsize) *retsize = loc-buf;
|
2004-10-01 14:49:34 +00:00
|
|
|
|
2004-10-05 16:28:34 +00:00
|
|
|
//printBYTES((unsigned char *)result, loc-buf);
|
|
|
|
|
|
|
|
#ifdef DEBUG_CALLS
|
|
|
|
lwnotice("lwline_serialize_buf returning (loc: %p, size: %d)",
|
|
|
|
loc, loc-buf);
|
|
|
|
#endif
|
2004-09-29 10:50:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// find bounding box (standard one) zmin=zmax=0 if 2d (might change to NaN)
|
|
|
|
BOX3D *lwline_findbbox(LWLINE *line)
|
|
|
|
{
|
|
|
|
BOX3D *ret;
|
|
|
|
|
|
|
|
if (line == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = pointArray_bbox(line->points);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find length of this deserialized line
|
2004-09-29 15:25:21 +00:00
|
|
|
size_t
|
|
|
|
lwline_serialize_size(LWLINE *line)
|
2004-09-29 10:50:30 +00:00
|
|
|
{
|
2004-09-29 15:25:21 +00:00
|
|
|
size_t size = 1; //type
|
2004-09-29 10:50:30 +00:00
|
|
|
|
2004-10-03 15:52:23 +00:00
|
|
|
#ifdef DEBUG_CALLS
|
|
|
|
lwnotice("lwline_serialize_size called");
|
|
|
|
#endif
|
|
|
|
|
2004-09-29 10:50:30 +00:00
|
|
|
if ( line->SRID != -1 ) size += 4; // SRID
|
2004-10-04 13:53:42 +00:00
|
|
|
if ( TYPE_HASBBOX(line->type) ) size += sizeof(BOX2DFLOAT4);
|
2004-09-30 15:42:28 +00:00
|
|
|
|
2004-10-01 14:49:34 +00:00
|
|
|
size += 4; // npoints
|
2004-10-05 17:15:42 +00:00
|
|
|
size += pointArray_ptsize(line->points)*line->points->npoints;
|
2004-09-29 10:50:30 +00:00
|
|
|
|
2004-10-03 15:52:23 +00:00
|
|
|
#ifdef DEBUG_CALLS
|
|
|
|
lwnotice("lwline_serialize_size returning %d", size);
|
|
|
|
#endif
|
|
|
|
|
2004-09-29 10:50:30 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2004-10-05 17:15:42 +00:00
|
|
|
void pfree_line (LWLINE *line)
|
2004-09-29 10:50:30 +00:00
|
|
|
{
|
|
|
|
lwfree(line->points);
|
|
|
|
lwfree(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
// find length of this serialized line
|
2004-10-05 17:15:42 +00:00
|
|
|
size_t
|
2004-09-29 10:50:30 +00:00
|
|
|
lwgeom_size_line(const char *serialized_line)
|
|
|
|
{
|
|
|
|
int type = (unsigned char) serialized_line[0];
|
2004-10-05 16:28:34 +00:00
|
|
|
uint32 result = 1; //type
|
2004-09-29 10:50:30 +00:00
|
|
|
const char *loc;
|
|
|
|
uint32 npoints;
|
|
|
|
|
2004-10-05 16:28:34 +00:00
|
|
|
#ifdef DEBUG_CALLS
|
|
|
|
lwnotice("lwgeom_size_line called");
|
|
|
|
#endif
|
|
|
|
|
2004-09-29 10:50:30 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2004-10-05 16:28:34 +00:00
|
|
|
if ( lwgeom_hasSRID(type))
|
|
|
|
{
|
|
|
|
loc += 4; // type + SRID
|
|
|
|
result +=4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we've read the type (1 byte) and SRID (4 bytes, if present)
|
|
|
|
npoints = get_uint32(loc);
|
|
|
|
result += sizeof(uint32); //npoints
|
|
|
|
|
2004-10-05 17:15:42 +00:00
|
|
|
result += TYPE_NDIMS(type) * sizeof(double) * npoints;
|
2004-10-05 16:28:34 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_CALLS
|
|
|
|
lwnotice("lwgeom_size_line returning %d", result);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return result;
|
2004-09-29 10:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void printLWLINE(LWLINE *line)
|
|
|
|
{
|
|
|
|
lwnotice("LWLINE {");
|
2004-10-04 13:53:42 +00:00
|
|
|
lwnotice(" ndims = %i", (int)TYPE_NDIMS(line->type));
|
2004-09-29 10:50:30 +00:00
|
|
|
lwnotice(" SRID = %i", (int)line->SRID);
|
|
|
|
printPA(line->points);
|
|
|
|
lwnotice("}");
|
|
|
|
}
|
|
|
|
|
2004-09-30 15:42:28 +00:00
|
|
|
int
|
|
|
|
lwline_compute_bbox_p(LWLINE *line, BOX2DFLOAT4 *box)
|
|
|
|
{
|
|
|
|
return ptarray_compute_bbox_p(line->points, box);
|
|
|
|
}
|
2004-10-01 14:49:34 +00:00
|
|
|
|
|
|
|
// Clone LWLINE object. POINTARRAY is not copied.
|
|
|
|
LWLINE *
|
|
|
|
lwline_clone(const LWLINE *g)
|
|
|
|
{
|
|
|
|
LWLINE *ret = lwalloc(sizeof(LWLINE));
|
|
|
|
memcpy(ret, g, sizeof(LWLINE));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add 'what' to this line at position 'where'.
|
|
|
|
// where=0 == prepend
|
|
|
|
// where=-1 == append
|
|
|
|
// Returns a MULTILINE or a GEOMETRYCOLLECTION
|
|
|
|
LWGEOM *
|
|
|
|
lwline_add(const LWLINE *to, uint32 where, const LWGEOM *what)
|
|
|
|
{
|
|
|
|
LWCOLLECTION *col;
|
|
|
|
LWGEOM **geoms;
|
|
|
|
int newtype;
|
|
|
|
|
|
|
|
if ( where != -1 && where != 0 )
|
|
|
|
{
|
|
|
|
lwerror("lwline_add only supports 0 or -1 as second argument, got %d", where);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dimensions compatibility are checked by caller
|
|
|
|
|
|
|
|
// Construct geoms array
|
|
|
|
geoms = lwalloc(sizeof(LWGEOM *)*2);
|
|
|
|
if ( where == -1 ) // append
|
|
|
|
{
|
|
|
|
geoms[0] = lwgeom_clone((LWGEOM *)to);
|
|
|
|
geoms[1] = lwgeom_clone(what);
|
|
|
|
}
|
|
|
|
else // prepend
|
|
|
|
{
|
|
|
|
geoms[0] = lwgeom_clone(what);
|
|
|
|
geoms[1] = lwgeom_clone((LWGEOM *)to);
|
|
|
|
}
|
|
|
|
// reset SRID and wantbbox flag from component types
|
|
|
|
geoms[0]->SRID = geoms[1]->SRID = -1;
|
2004-10-04 13:53:42 +00:00
|
|
|
TYPE_SETHASSRID(geoms[0]->type, 0);
|
|
|
|
TYPE_SETHASSRID(geoms[1]->type, 0);
|
|
|
|
TYPE_SETHASBBOX(geoms[0]->type, 0);
|
|
|
|
TYPE_SETHASBBOX(geoms[1]->type, 0);
|
2004-10-01 14:49:34 +00:00
|
|
|
|
|
|
|
// Find appropriate geom type
|
2004-10-04 13:53:42 +00:00
|
|
|
if ( TYPE_GETTYPE(what->type) == LINETYPE ) newtype = MULTILINETYPE;
|
2004-10-01 14:49:34 +00:00
|
|
|
else newtype = COLLECTIONTYPE;
|
|
|
|
|
2004-10-05 16:28:34 +00:00
|
|
|
col = lwcollection_construct(newtype,
|
|
|
|
to->SRID,
|
2004-10-04 13:53:42 +00:00
|
|
|
( TYPE_HASBBOX(what->type) || TYPE_HASBBOX(to->type) ),
|
|
|
|
2, geoms);
|
2004-10-01 14:49:34 +00:00
|
|
|
|
|
|
|
return (LWGEOM *)col;
|
|
|
|
}
|
2004-10-07 10:03:23 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
lwline_reverse(LWLINE *line)
|
|
|
|
{
|
|
|
|
ptarray_reverse(line->points);
|
|
|
|
}
|