42 uint8_t gflags = g->
gflags;
66 return 6 *
sizeof(float);
74 return *((uint32_t*)loc);
77 uint8_t
g1flags(
int has_z,
int has_m,
int is_geodetic)
116 static const intptr_t size_of_gserialized_up_to_data = (intptr_t) & ((
GSERIALIZED *)NULL)->
data;
118 return size_of_gserialized_up_to_data + 8 *
sizeof(float) +
sizeof(uint32_t);
134 ptr = (uint32_t*)(g->
data);
145 srid = srid | (
s->srid[0] << 16);
146 srid = srid | (
s->srid[1] << 8);
147 srid = srid |
s->srid[2];
150 srid = (srid<<11)>>11;
161 LWDEBUGF(3,
"%s called with srid = %d", __func__, srid);
170 s->srid[0] = (srid & 0x001F0000) >> 16;
171 s->srid[1] = (srid & 0x0000FF00) >> 8;
172 s->srid[2] = (srid & 0x000000FF);
182 memcpy(&num, p+4, 4);
187 for ( i = 0; i < num; i++ )
205 uint8_t *p = (uint8_t*)g;
223 void hashlittle2(
const void *key,
size_t length, uint32_t *pc, uint32_t *pb);
229 int32_t pb = 0, pc = 0;
232 uint8_t *b1 = (uint8_t*)g1 + hsz1;
235 size_t bsz1 = sz1 - hsz1;
238 size_t bsz2 = bsz1 +
sizeof(int);
241 memcpy(b2, &srid,
sizeof(
int));
243 memcpy(b2+
sizeof(
int), b1, bsz1);
245 hashlittle2(b2, bsz2, (uint32_t *)&pb, (uint32_t *)&pc);
264 float *fbox = (
float*)(g->
data);
265 gbox->
xmin = fbox[i++];
266 gbox->
xmax = fbox[i++];
267 gbox->
ymin = fbox[i++];
268 gbox->
ymax = fbox[i++];
273 gbox->
zmin = fbox[i++];
274 gbox->
zmax = fbox[i++];
280 gbox->
zmin = fbox[i++];
281 gbox->
zmax = fbox[i++];
285 gbox->
mmin = fbox[i++];
286 gbox->
mmax = fbox[i++];
312 double *dptr = (
double*)(g->
data);
315 int32_t *iptr = (int32_t *)(g->
data);
316 int isempty = (iptr[1] == 0);
321 gbox->
xmin = gbox->
xmax = dptr[i++];
322 gbox->
ymin = gbox->
ymax = dptr[i++];
326 gbox->
zmin = gbox->
zmax = dptr[i++];
330 gbox->
mmin = gbox->
mmax = dptr[i++];
340 double *dptr = (
double*)(g->
data);
341 int32_t *iptr = (int32_t *)(g->
data);
342 int npoints = iptr[1];
381 double *dptr = (
double*)(g->
data);
382 int32_t *iptr = (int32_t *)(g->
data);
383 int ngeoms = iptr[1];
404 gbox->
xmin = gbox->
xmax = dptr[i++];
405 gbox->
ymin = gbox->
ymax = dptr[i++];
409 gbox->
zmin = gbox->
zmax = dptr[i++];
413 gbox->
mmin = gbox->
mmax = dptr[i++];
423 double *dptr = (
double*)(g->
data);
424 int32_t *iptr = (int32_t *)(g->
data);
425 int ngeoms = iptr[1];
477 out_point->
x = dptr[dim++];
478 out_point->
y = dptr[dim++];
482 out_point->
z = dptr[dim++];
486 out_point->
m = dptr[dim];
493 uint8_t *geometry_start = ((uint8_t *)g->
data);
499 uint32_t isEmpty = (((uint32_t *)geometry_start)[1]) == 0;
505 uint32_t
type = (((uint32_t *)geometry_start)[0]);
507 double *double_array_start = NULL;
512 double_array_start = (
double *)(geometry_start + 2 *
sizeof(uint32_t));
516 lwerror(
"%s is currently not implemented for type %d", __func__,
type);
596 LWDEBUGF(3,
"point size = %d", size);
610 LWDEBUGF(3,
"linestring size = %d", size);
624 LWDEBUGF(3,
"triangle size = %d", size);
640 for ( i = 0; i < poly->
nrings; i++ )
646 LWDEBUGF(3,
"polygon size = %d", size);
660 LWDEBUGF(3,
"circstring size = %d", size);
674 for ( i = 0; i < col->
ngeoms; i++ )
678 LWDEBUGF(3,
"lwcollection subgeom(%d) size = %d", i, subsize);
681 LWDEBUGF(3,
"lwcollection size = %d", size);
730 LWDEBUGF(3,
"%s size = %d", __func__, size);
753 lwerror(
"Dimensions mismatch in lwpoint");
755 LWDEBUGF(2,
"%s (%p, %p) called", __func__, point, buf);
760 memcpy(loc, &
type,
sizeof(uint32_t));
761 loc +=
sizeof(uint32_t);
763 memcpy(loc, &(point->
point->
npoints),
sizeof(uint32_t));
764 loc +=
sizeof(uint32_t);
773 return (
size_t)(loc - buf);
786 LWDEBUGF(2,
"%s (%p, %p) called", __func__, line, buf);
789 lwerror(
"Dimensions mismatch in lwline");
796 memcpy(loc, &
type,
sizeof(uint32_t));
797 loc +=
sizeof(uint32_t);
801 loc +=
sizeof(uint32_t);
814 return (
size_t)(loc - buf);
833 memcpy(loc, &
type,
sizeof(uint32_t));
834 loc +=
sizeof(uint32_t);
837 memcpy(loc, &(poly->
nrings),
sizeof(uint32_t));
838 loc +=
sizeof(uint32_t);
841 for ( i = 0; i < poly->
nrings; i++ )
843 memcpy(loc, &(poly->
rings[i]->
npoints),
sizeof(uint32_t));
844 loc +=
sizeof(uint32_t);
850 memset(loc, 0,
sizeof(uint32_t));
851 loc +=
sizeof(uint32_t);
855 for ( i = 0; i < poly->
nrings; i++ )
861 lwerror(
"Dimensions mismatch in lwpoly");
868 return (
size_t)(loc - buf);
881 LWDEBUGF(2,
"%s (%p, %p) called", __func__, triangle, buf);
884 lwerror(
"Dimensions mismatch in lwtriangle");
891 memcpy(loc, &
type,
sizeof(uint32_t));
892 loc +=
sizeof(uint32_t);
896 loc +=
sizeof(uint32_t);
907 LWDEBUGF(3,
"%s copied serialized_pointlist (%d bytes)", __func__, ptsize * triangle->
points->
npoints);
909 return (
size_t)(loc - buf);
923 lwerror(
"Dimensions mismatch in lwcircstring");
930 memcpy(loc, &
type,
sizeof(uint32_t));
931 loc +=
sizeof(uint32_t);
935 loc +=
sizeof(uint32_t);
945 return (
size_t)(loc - buf);
962 memcpy(loc, &
type,
sizeof(uint32_t));
963 loc +=
sizeof(uint32_t);
966 memcpy(loc, &coll->
ngeoms,
sizeof(uint32_t));
967 loc +=
sizeof(uint32_t);
970 for ( i=0; i<coll->
ngeoms; i++ )
973 lwerror(
"Dimensions mismatch in lwcollection");
978 return (
size_t)(loc - buf);
986 LWDEBUGF(2,
"Input type (%d) %s, hasz: %d hasm: %d",
989 LWDEBUGF(2,
"LWGEOM(%p) uint8_t(%p)", geom, buf);
1030 memcpy(loc, &f,
sizeof(
float));
1031 loc +=
sizeof(float);
1034 memcpy(loc, &f,
sizeof(
float));
1035 loc +=
sizeof(float);
1038 memcpy(loc, &f,
sizeof(
float));
1039 loc +=
sizeof(float);
1042 memcpy(loc, &f,
sizeof(
float));
1043 loc +=
sizeof(float);
1048 memcpy(loc, &f,
sizeof(
float));
1049 loc +=
sizeof(float);
1052 memcpy(loc, &f,
sizeof(
float));
1053 loc +=
sizeof(float);
1055 return_size = (size_t)(loc - buf);
1056 LWDEBUGF(4,
"returning size %d", return_size);
1063 memcpy(loc, &f,
sizeof(
float));
1064 loc +=
sizeof(float);
1067 memcpy(loc, &f,
sizeof(
float));
1068 loc +=
sizeof(float);
1075 memcpy(loc, &f,
sizeof(
float));
1076 loc +=
sizeof(float);
1079 memcpy(loc, &f,
sizeof(
float));
1080 loc +=
sizeof(float);
1082 return_size = (size_t)(loc - buf);
1083 LWDEBUGF(4,
"returning size %d", return_size);
1091 size_t expected_size = 0;
1092 size_t return_size = 0;
1093 uint8_t *serialized = NULL;
1094 uint8_t *ptr = NULL;
1116 serialized =
lwalloc(expected_size);
1130 return_size = ptr - serialized;
1132 if ( expected_size != return_size )
1134 lwerror(
"Return size (%d) not equal to expected size (%d)!", return_size, expected_size);
1139 *size = return_size;
1147 g->
size = return_size << 2;
1165 uint8_t *start_ptr = data_ptr;
1167 uint32_t npoints = 0;
1189 *size = data_ptr - start_ptr;
1196 uint8_t *start_ptr = data_ptr;
1198 uint32_t npoints = 0;
1221 *size = data_ptr - start_ptr;
1228 uint8_t *start_ptr = data_ptr;
1230 uint8_t *ordinate_ptr;
1231 uint32_t nrings = 0;
1245 LWDEBUGF(4,
"nrings = %d", nrings);
1248 ordinate_ptr = data_ptr;
1253 ordinate_ptr += nrings * 4;
1263 for ( i = 0; i < nrings; i++ )
1265 uint32_t npoints = 0;
1278 *size = ordinate_ptr - start_ptr;
1285 uint8_t *start_ptr = data_ptr;
1287 uint32_t npoints = 0;
1293 triangle->
bbox = NULL;
1309 *size = data_ptr - start_ptr;
1316 uint8_t *start_ptr = data_ptr;
1318 uint32_t npoints = 0;
1324 circstring->
bbox = NULL;
1340 *size = data_ptr - start_ptr;
1348 uint8_t *start_ptr = data_ptr;
1350 uint32_t ngeoms = 0;
1360 collection->
bbox = NULL;
1365 collection->
ngeoms = ngeoms;
1375 collection->
geoms = NULL;
1382 for ( i = 0; i < ngeoms; i++ )
1394 data_ptr += subsize;
1398 *size = data_ptr - start_ptr;
1447 uint32_t lwtype = 0;
1448 uint8_t *data_ptr = NULL;
1461 data_ptr = (uint8_t*)g->
data;
1468 lwerror(
"%s: unable create geometry", __func__);
1470 lwgeom->
type = lwtype;
1483 lwgeom->
bbox = NULL;
1495 if (!g)
return NULL;
1497 return (
const float *)(g->
data);
1514 size_t box_size = 2 * g_ndims *
sizeof(float);
1519 if ( g_ndims != box_ndims )
1539 memcpy(g_out, g, 8);
1551 fbox = (
float*)(g_out->
data);
1553 fbox[fbox_pos++] = gbox->
xmin;
1554 fbox[fbox_pos++] = gbox->
xmax;
1555 fbox[fbox_pos++] = gbox->
ymin;
1556 fbox[fbox_pos++] = gbox->
ymax;
1560 fbox[fbox_pos++] = gbox->
zmin;
1561 fbox[fbox_pos++] = gbox->
zmax;
1565 fbox[fbox_pos++] = gbox->
mmin;
1566 fbox[fbox_pos++] = gbox->
mmax;
1580 size_t box_size = 2 * g_ndims *
sizeof(float);
1587 uint8_t *outptr = (uint8_t*)g_out;
1588 uint8_t *inptr = (uint8_t*)g;
1590 memcpy(outptr, inptr, 8);
1592 inptr += 8 + box_size;
1594 memcpy(outptr, inptr, g_out_size - 8);
1601 memcpy(g_out, g, g_out_size);
size_t gbox_serialized_size(lwflags_t flags)
Return the number of bytes necessary to hold a GBOX of this dimension in serialized form.
void gbox_float_round(GBOX *gbox)
Round given GBOX to float boundaries.
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
static void gserialized1_copy_point(double *dptr, lwflags_t flags, POINT4D *out_point)
static size_t gserialized1_from_lwgeom_any(const LWGEOM *geom, uint8_t *buf)
int gserialized1_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
uint8_t g1flags(int has_z, int has_m, int is_geodetic)
int gserialized1_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
int32_t gserialized1_hash(const GSERIALIZED *g1)
Returns a hash code for the srid/type/geometry information in the GSERIALIZED.
int gserialized1_ndims(const GSERIALIZED *gser)
Return the number of dimensions (2, 3, 4) in a geometry.
static LWCIRCSTRING * lwcircstring_from_gserialized1_buffer(uint8_t *data_ptr, lwflags_t lwflags, size_t *size)
void hashlittle2(const void *key, size_t length, uint32_t *pc, uint32_t *pb)
static size_t gserialized1_from_lwtriangle(const LWTRIANGLE *triangle, uint8_t *buf)
static size_t gserialized1_from_lwcollection_size(const LWCOLLECTION *col)
lwflags_t gserialized1_get_lwflags(const GSERIALIZED *g)
Read the flags from a GSERIALIZED and return a standard lwflag integer.
int32_t gserialized1_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
static size_t gserialized1_from_lwpoint_size(const LWPOINT *point)
int gserialized1_get_gbox_p(const GSERIALIZED *g, GBOX *box)
Read the bounding box off a serialization and calculate one if it is not already there.
uint32_t gserialized1_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
static LWPOLY * lwpoly_from_gserialized1_buffer(uint8_t *data_ptr, lwflags_t lwflags, size_t *size)
static size_t gserialized1_from_gbox(const GBOX *gbox, uint8_t *buf)
static size_t gserialized1_from_lwtriangle_size(const LWTRIANGLE *triangle)
static LWGEOM * lwgeom_from_gserialized1_buffer(uint8_t *data_ptr, lwflags_t lwflags, size_t *size)
int gserialized1_peek_gbox_p(const GSERIALIZED *g, GBOX *gbox)
int gserialized1_has_bbox(const GSERIALIZED *gser)
Check if a GSERIALIZED has a bounding box without deserializing first.
GSERIALIZED * gserialized1_set_gbox(GSERIALIZED *g, GBOX *gbox)
Update the bounding box of a GSERIALIZED, allocating a fresh one if there is not enough space to just...
uint32_t gserialized1_max_header_size(void)
Returns the size in bytes to read from toast to get the basic information from a geometry: GSERIALIZE...
LWGEOM * lwgeom_from_gserialized1(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int gserialized1_has_m(const GSERIALIZED *gser)
Check if a GSERIALIZED has an M ordinate.
static size_t gserialized1_from_lwpoint(const LWPOINT *point, uint8_t *buf)
GSERIALIZED * gserialized1_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
const float * gserialized1_get_float_box_p(const GSERIALIZED *g, size_t *ndims)
Point into the float box area of the serialization.
static uint32_t gserialized1_get_uint32_t(const uint8_t *loc)
static uint32_t gserialized1_header_size(const GSERIALIZED *gser)
int gserialized1_peek_first_point(const GSERIALIZED *g, POINT4D *out_point)
int gserialized1_is_geodetic(const GSERIALIZED *gser)
Check if a GSERIALIZED is a geography.
static size_t gserialized1_from_lwcircstring(const LWCIRCSTRING *curve, uint8_t *buf)
static size_t gserialized1_from_lwcollection(const LWCOLLECTION *coll, uint8_t *buf)
static size_t gserialized1_from_lwcircstring_size(const LWCIRCSTRING *curve)
static LWLINE * lwline_from_gserialized1_buffer(uint8_t *data_ptr, lwflags_t lwflags, size_t *size)
size_t gserialized1_from_lwgeom_size(const LWGEOM *geom)
Return the memory size a GSERIALIZED will occupy for a given LWGEOM.
uint8_t lwflags_get_g1flags(lwflags_t lwflags)
static size_t gserialized1_from_lwline_size(const LWLINE *line)
static size_t gserialized1_box_size(const GSERIALIZED *g)
static size_t gserialized1_from_lwpoly(const LWPOLY *poly, uint8_t *buf)
static size_t gserialized1_is_empty_recurse(const uint8_t *p, int *isempty)
static LWCOLLECTION * lwcollection_from_gserialized1_buffer(uint8_t *data_ptr, lwflags_t lwflags, size_t *size)
void gserialized1_set_srid(GSERIALIZED *s, int32_t srid)
Write the SRID into the serialized form (it is packed into three bytes so this is a handy function).
static size_t gserialized1_from_lwpoly_size(const LWPOLY *poly)
GSERIALIZED * gserialized1_drop_gbox(GSERIALIZED *g)
Remove the bounding box from a GSERIALIZED.
static size_t gserialized1_from_lwline(const LWLINE *line, uint8_t *buf)
static int gserialized1_read_gbox_p(const GSERIALIZED *g, GBOX *gbox)
static size_t gserialized1_from_any_size(const LWGEOM *geom)
static LWTRIANGLE * lwtriangle_from_gserialized1_buffer(uint8_t *data_ptr, lwflags_t lwflags, size_t *size)
static LWPOINT * lwpoint_from_gserialized1_buffer(uint8_t *data_ptr, lwflags_t lwflags, size_t *size)
int gserialized1_fast_gbox_p(const GSERIALIZED *g, GBOX *box)
Read the bounding box off a serialization and fail if it is not already there.
#define G1FLAGS_SET_SOLID(gflags, value)
#define G1FLAGS_SET_Z(gflags, value)
#define G1FLAGS_GET_M(gflags)
#define G1FLAGS_SET_M(gflags, value)
#define G1FLAGS_GET_SOLID(gflags)
#define G1FLAGS_SET_BBOX(gflags, value)
#define G1FLAGS_NDIMS(gflags)
#define G1FLAGS_GET_BBOX(gflags)
#define G1FLAGS_SET_GEODETIC(gflags, value)
#define G1FLAGS_NDIMS_BOX(gflags)
#define G1FLAGS_GET_GEODETIC(gflags)
#define G1FLAGS_GET_Z(gflags)
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
POINTARRAY * ptarray_construct_reference_data(char hasz, char hasm, uint32_t npoints, uint8_t *ptlist)
Construct a new POINTARRAY, referencing to the data from ptlist.
void lwgeom_set_srid(LWGEOM *geom, int32_t srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
void lwgeom_free(LWGEOM *geom)
#define FLAGS_NDIMS_BOX(flags)
#define FLAGS_GET_BBOX(flags)
#define FLAGS_SET_BBOX(flags, value)
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
int lwgeom_needs_bbox(const LWGEOM *geom)
Check whether or not a lwgeom is big enough to warrant a bounding box.
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
#define FLAGS_GET_Z(flags)
#define FLAGS_NDIMS(flags)
#define POLYHEDRALSURFACETYPE
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
#define FLAGS_GET_M(flags)
#define FLAGS_GET_SOLID(flags)
#define FLAGS_GET_ZM(flags)
int lwgeom_calculate_gbox(const LWGEOM *lwgeom, GBOX *gbox)
Calculate bounding box of a geometry, automatically taking into account whether it is cartesian or ge...
#define FLAGS_SET_GEODETIC(flags, value)
void * lwalloc(size_t size)
float next_float_up(double d)
lwflags_t lwflags(int hasz, int hasm, int geodetic)
Construct a new flags bitmask.
#define LW_TRUE
Return types for functions with status returns.
#define FLAGS_SET_M(flags, value)
#define SRID_UNKNOWN
Unknown SRID value.
#define FLAGS_SET_Z(flags, value)
#define FLAGS_SET_SOLID(flags, value)
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
float next_float_down(double d)
#define FLAGS_GET_GEODETIC(flags)
int32_t clamp_srid(int32_t srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
#define SIZE_GET(varsize)
Macro for reading the size from the GSERIALIZED size attribute.
#define SIZE_SET(varsize, len)
int lwcollection_allows_subtype(int collectiontype, int subtype)
Check if subtype is allowed in collectiontype.
#define LWDEBUGF(level, msg,...)
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
static size_t ptarray_point_size(const POINTARRAY *pa)
static uint8_t * getPoint_internal(const POINTARRAY *pa, uint32_t n)
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)