1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* Copyright 2009 Paul Ramsey <pramsey@cleverelephant.ca>
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU General Public Licence. See the COPYING file.
*
**********************************************************************/
#include "postgres.h"
#include "access/gist.h" /* For GiST */
#include "access/itup.h"
#include "access/skey.h"
#include "../postgis_config.h"
/*#define POSTGIS_DEBUG_LEVEL 4*/
#include "liblwgeom.h" /* For standard geometry types. */
#include "lwgeom_pg.h" /* For debugging macros. */
#include "gserialized_gist.h"
#define FLAGS_NDIMS_GIDX(f) ( FLAGS_GET_GEODETIC(f) ? 3 : \
FLAGS_GET_M(f) ? 4 : \
FLAGS_GET_Z(f) ? 3 : 2 )
/* Generate human readable form for GIDX. */
char* gidx_to_string(GIDX *a)
{
char *str, *rv;
int i, ndims;
if ( a == NULL )
return pstrdup("<NULLPTR>");
str = (char*)palloc(128); /* 15*2*4+8==128 */
rv = str;
ndims = GIDX_NDIMS(a);
str += sprintf(str, "GIDX(");
for ( i = 0; i < ndims; i++ )
str += sprintf(str, " %.12g", GIDX_GET_MIN(a,i));
str += sprintf(str, ",");
for ( i = 0; i < ndims; i++ )
str += sprintf(str, " %.12g", GIDX_GET_MAX(a,i));
str += sprintf(str, " )");
return rv;
}
static uint8_t
gserialized_datum_get_flags(Datum gsdatum)
{
GSERIALIZED *gpart;
POSTGIS_DEBUG(4, "entered function");
gpart = (GSERIALIZED*)PG_DETOAST_DATUM_SLICE(gsdatum, 0, 40);
POSTGIS_DEBUGF(4, "got flags %d", gpart->flags);
return gpart->flags;
}
/* Convert a double-based GBOX into a float-based GIDX,
ensuring the float box is larger than the double box */
static int gidx_from_gbox_p(GBOX box, GIDX *a)
{
int ndims;
ndims = FLAGS_NDIMS_GIDX(box.flags);
SET_VARSIZE(a, VARHDRSZ + ndims * 2 * sizeof(float));
GIDX_SET_MIN(a,0,next_float_down(box.xmin));
GIDX_SET_MAX(a,0,next_float_up(box.xmax));
GIDX_SET_MIN(a,1,next_float_down(box.ymin));
GIDX_SET_MAX(a,1,next_float_up(box.ymax));
/* Geodetic indexes are always 3d, geocentric x/y/z */
if ( FLAGS_GET_GEODETIC(box.flags) )
{
GIDX_SET_MIN(a,2,next_float_down(box.zmin));
GIDX_SET_MAX(a,2,next_float_up(box.zmax));
}
else
{
/* Cartesian with Z implies Z is third dimension */
if ( FLAGS_GET_Z(box.flags) )
{
GIDX_SET_MIN(a,2,next_float_down(box.zmin));
GIDX_SET_MAX(a,2,next_float_up(box.zmax));
}
/* M is always fourth dimension, we pad if needed */
if ( FLAGS_GET_M(box.flags) )
{
if ( ! FLAGS_GET_Z(box.flags) )
{
GIDX_SET_MIN(a,2,-1*FLT_MAX);
GIDX_SET_MAX(a,2,FLT_MAX);
}
GIDX_SET_MIN(a,3,next_float_down(box.mmin));
GIDX_SET_MAX(a,3,next_float_up(box.mmax));
}
}
POSTGIS_DEBUGF(5, "converted %s to %s", gbox_to_string(&box), gidx_to_string(a));
return LW_SUCCESS;
}
/* Convert a gidx to a gbox */
static void gbox_from_gidx(GIDX *a, GBOX *gbox, int flags)
{
gbox->xmin = (double)GIDX_GET_MIN(a,0);
gbox->xmax = (double)GIDX_GET_MAX(a,0);
gbox->ymin = (double)GIDX_GET_MIN(a,1);
gbox->ymax = (double)GIDX_GET_MAX(a,1);
if ( FLAGS_GET_Z(flags) ) {
gbox->zmin = (double)GIDX_GET_MIN(a,2);
gbox->zmax = (double)GIDX_GET_MAX(a,2);
}
if ( FLAGS_GET_M(flags) ) {
gbox->mmin = (double)GIDX_GET_MIN(a,3);
gbox->mmax = (double)GIDX_GET_MAX(a,3);
}
}
/**
* Given a #GSERIALIZED datum, as quickly as possible (peaking into the top
* of the memory) return the gbox extents. Does not deserialize the geometry,
* but <em>WARNING</em> returns a slightly larger bounding box than actually
* encompasses the objects. For geography objects returns geocentric bounding
* box, for geometry objects returns cartesian bounding box.
*/
int
gserialized_datum_get_gbox_p(Datum gsdatum, GBOX *gbox)
{
char gboxmem[GIDX_MAX_SIZE];
GIDX *gidx = (GIDX*)gboxmem;
if( LW_FAILURE == gserialized_datum_get_gidx_p(gsdatum, gidx) )
return LW_FAILURE;
gbox->flags = gserialized_datum_get_flags(gsdatum);
gbox_from_gidx(gidx, gbox, gbox->flags);
return LW_SUCCESS;
}
/**
* Update the bounding box of a #GSERIALIZED, allocating a fresh one
* if there is not enough space to just write the new box in.
* <em>WARNING</em> if a new object needs to be created, the
* input pointer will have to be freed by the caller! Check
* to see if input == output. Returns null if there's a problem
* like mismatched dimensions.
*/
GSERIALIZED* gserialized_set_gidx(GSERIALIZED *g, GIDX *gidx)
{
int g_ndims = FLAGS_NDIMS_BOX(g->flags);
int box_ndims = GIDX_NDIMS(gidx);
GSERIALIZED *g_out = NULL;
size_t box_size = 2 * g_ndims * sizeof(float);
/* The dimensionality of the inputs has to match or we are SOL. */
if ( g_ndims != box_ndims )
{
return NULL;
}
/* Serialized already has room for a box. */
if ( FLAGS_GET_BBOX(g->flags) )
{
g_out = g;
}
/* Serialized has no box. We need to allocate enough space for the old
data plus the box, and leave a gap in the memory segment to write
the new values into.
*/
else
{
size_t varsize_new = VARSIZE(g) + box_size;
uint8_t *ptr;
g_out = palloc(varsize_new);
/* Copy the head of g into place */
memcpy(g_out, g, 8);
/* Copy the body of g into place after leaving space for the box */
ptr = g_out->data;
ptr += box_size;
memcpy(ptr, g->data, VARSIZE(g) - 8);
FLAGS_SET_BBOX(g_out->flags, 1);
SET_VARSIZE(g_out, varsize_new);
}
/* Now write the gidx values into the memory segement */
memcpy(g_out->data, gidx->c, box_size);
return g_out;
}
/**
* Remove the bounding box from a #GSERIALIZED. Returns a freshly
* allocated #GSERIALIZED every time.
*/
GSERIALIZED* gserialized_drop_gidx(GSERIALIZED *g)
{
int g_ndims = FLAGS_NDIMS_BOX(g->flags);
size_t box_size = 2 * g_ndims * sizeof(float);
size_t g_out_size = VARSIZE(g) - box_size;
GSERIALIZED *g_out = palloc(g_out_size);
/* Copy the contents while omitting the box */
if ( FLAGS_GET_BBOX(g->flags) )
{
uint8_t *outptr = (uint8_t*)g_out;
uint8_t *inptr = (uint8_t*)g;
/* Copy the header (size+type) of g into place */
memcpy(outptr, inptr, 8);
outptr += 8;
inptr += 8 + box_size;
/* Copy parts after the box into place */
memcpy(outptr, inptr, g_out_size - 8);
FLAGS_SET_BBOX(g_out->flags, 0);
SET_VARSIZE(g_out, g_out_size);
}
/* No box? Nothing to do but copy and return. */
else
{
memcpy(g_out, g, g_out_size);
}
return g_out;
}
/**
* Peak into a #GSERIALIZED datum to find the bounding box. If the
* box is there, copy it out and return it. If not, calculate the box from the
* full object and return the box based on that. If no box is available,
* return #LW_FAILURE, otherwise #LW_SUCCESS.
*/
int
gserialized_datum_get_gidx_p(Datum gsdatum, GIDX *gidx)
{
GSERIALIZED *gpart;
int result = LW_SUCCESS;
POSTGIS_DEBUG(4, "entered function");
/*
** The most info we need is the 8 bytes of serialized header plus the 32 bytes
** of floats necessary to hold the 8 floats of the largest XYZM index
** bounding box, so 40 bytes.
*/
gpart = (GSERIALIZED*)PG_DETOAST_DATUM_SLICE(gsdatum, 0, 40);
POSTGIS_DEBUGF(4, "got flags %d", gpart->flags);
/* Do we even have a serialized bounding box? */
if ( FLAGS_GET_BBOX(gpart->flags) )
{
/* Yes! Copy it out into the GIDX! */
size_t size = gbox_serialized_size(gpart->flags);
POSTGIS_DEBUG(4, "copying box out of serialization");
memcpy(gidx->c, gpart->data, size);
/* if M is present but Z is not, pad Z and shift M */
if ( FLAGS_GET_M(gpart->flags) && ! FLAGS_GET_Z(gpart->flags) )
{
size += 2 * sizeof(float);
GIDX_SET_MIN(gidx,3,GIDX_GET_MIN(gidx,2));
GIDX_SET_MAX(gidx,3,GIDX_GET_MAX(gidx,2));
GIDX_SET_MIN(gidx,2,-1*FLT_MAX);
GIDX_SET_MAX(gidx,2,FLT_MAX);
}
SET_VARSIZE(gidx, VARHDRSZ + size);
result = LW_SUCCESS;
}
else
{
/* No, we need to calculate it from the full object. */
GSERIALIZED *g = (GSERIALIZED*)PG_DETOAST_DATUM(gsdatum);
LWGEOM *lwgeom = lwgeom_from_gserialized(g);
GBOX gbox;
if ( lwgeom_calculate_gbox(lwgeom, &gbox) == LW_FAILURE )
{
POSTGIS_DEBUG(4, "could not calculate bbox, returning failure");
lwgeom_free(lwgeom);
POSTGIS_FREE_IF_COPY_P(gpart, gsdatum);
POSTGIS_FREE_IF_COPY_P(g, gsdatum);
return LW_FAILURE;
}
lwgeom_free(lwgeom);
POSTGIS_FREE_IF_COPY_P(g, gsdatum);
result = gidx_from_gbox_p(gbox, gidx);
}
POSTGIS_FREE_IF_COPY_P(gpart, gsdatum);
if ( result == LW_SUCCESS )
{
POSTGIS_DEBUGF(4, "got gidx %s", gidx_to_string(gidx));
}
return result;
}
/*
** Peak into a geography to find the bounding box. If the
** box is there, copy it out and return it. If not, calculate the box from the
** full geography and return the box based on that. If no box is available,
** return LW_FAILURE, otherwise LW_SUCCESS.
*/
int gserialized_get_gidx_p(GSERIALIZED *g, GIDX *gidx)
{
int result = LW_SUCCESS;
POSTGIS_DEBUG(4, "entered function");
POSTGIS_DEBUGF(4, "got flags %d", g->flags);
if ( FLAGS_GET_BBOX(g->flags) )
{
int ndims = FLAGS_NDIMS_GIDX(g->flags);
const size_t size = 2 * ndims * sizeof(float);
POSTGIS_DEBUG(4, "copying box out of serialization");
memcpy(gidx->c, g->data, size);
SET_VARSIZE(gidx, VARHDRSZ + size);
}
else
{
/* No, we need to calculate it from the full object. */
LWGEOM *lwgeom = lwgeom_from_gserialized(g);
GBOX gbox;
if ( lwgeom_calculate_gbox(lwgeom, &gbox) == LW_FAILURE )
{
POSTGIS_DEBUG(4, "could not calculate bbox, returning failure");
lwgeom_free(lwgeom);
return LW_FAILURE;
}
lwgeom_free(lwgeom);
result = gidx_from_gbox_p(gbox, gidx);
}
if ( result == LW_SUCCESS )
{
POSTGIS_DEBUGF(4, "got gidx %s", gidx_to_string(gidx));
}
return result;
}
/*
** GIDX expansion, make d units bigger in all dimensions.
*/
void gidx_expand(GIDX *a, float d)
{
int i;
POSTGIS_DEBUG(5, "entered function");
if ( a == NULL ) return;
for (i = 0; i < GIDX_NDIMS(a); i++)
{
GIDX_SET_MIN(a, i, GIDX_GET_MIN(a, i) - d);
GIDX_SET_MAX(a, i, GIDX_GET_MAX(a, i) + d);
}
}
/* Allocates a new GIDX on the heap of the requested dimensionality */
GIDX* gidx_new(int ndims)
{
size_t size = GIDX_SIZE(ndims);
GIDX *g = (GIDX*)palloc(size);
Assert( (ndims <= GIDX_MAX_DIM) && (size <= GIDX_MAX_SIZE) );
POSTGIS_DEBUGF(5,"created new gidx of %d dimensions, size %d", ndims, (int)size);
SET_VARSIZE(g, size);
return g;
}
|