PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ BOX3D_to_LWGEOM()

Datum BOX3D_to_LWGEOM ( PG_FUNCTION_ARGS  )

Alter BOX3D cast so that a valid geometry is always returned depending upon the size of the BOX3D. The code makes the following assumptions:

  • If the BOX3D is a single point then return a POINT geometry
  • If the BOX3D represents a line in any of X, Y or Z dimension, return a LINESTRING geometry
  • If the BOX3D represents a plane in the X, Y, or Z dimension, return a POLYGON geometry
  • Otherwise return a POLYHEDRALSURFACE geometry

Definition at line 196 of file lwgeom_box3d.c.

References LWGEOM::flags, FLAGS_SET_SOLID, geometry_serialize(), gserialized_set_srid(), LW_FALSE, LW_TRUE, lwalloc(), lwcollection_construct(), lwcollection_free(), lwline_as_lwgeom(), lwline_construct(), lwline_free(), lwpoint_as_lwgeom(), lwpoint_construct(), lwpoint_free(), lwpoly_as_lwgeom(), lwpoly_construct_rectangle(), lwpoly_free(), POLYHEDRALSURFACETYPE, ptarray_append_point(), ptarray_construct_empty(), BOX3D::srid, SRID_UNKNOWN, POINT4D::x, BOX3D::xmax, BOX3D::xmin, POINT4D::y, BOX3D::ymax, BOX3D::ymin, POINT4D::z, BOX3D::zmax, and BOX3D::zmin.

Referenced by BOX3D_to_BOX().

197 {
198  BOX3D *box = (BOX3D *)PG_GETARG_POINTER(0);
199  POINTARRAY *pa;
200  GSERIALIZED *result;
201  POINT4D pt;
202 
217 
218  /* BOX3D is a point */
219  if ( (box->xmin == box->xmax) && (box->ymin == box->ymax) &&
220  (box->zmin == box->zmax) )
221  {
222  LWPOINT *lwpt = lwpoint_construct(SRID_UNKNOWN, NULL, pa);
223 
224  pt.x = box->xmin;
225  pt.y = box->ymin;
226  pt.z = box->zmin;
227  ptarray_append_point(pa, &pt, LW_TRUE);
228 
229  result = geometry_serialize(lwpoint_as_lwgeom(lwpt));
230  lwpoint_free(lwpt);
231  }
232  /* BOX3D is a line */
233  else if (((box->xmin == box->xmax ||
234  box->ymin == box->ymax) &&
235  box->zmin == box->zmax) ||
236  ((box->xmin == box->xmax ||
237  box->zmin == box->zmax) &&
238  box->ymin == box->ymax) ||
239  ((box->ymin == box->ymax ||
240  box->zmin == box->zmax) &&
241  box->xmin == box->xmax))
242  {
243  LWLINE *lwline = lwline_construct(SRID_UNKNOWN, NULL, pa);
244 
245  pt.x = box->xmin;
246  pt.y = box->ymin;
247  pt.z = box->zmin;
248  ptarray_append_point(pa, &pt, LW_TRUE);
249  pt.x = box->xmax;
250  pt.y = box->ymax;
251  pt.z = box->zmax;
252  ptarray_append_point(pa, &pt, LW_TRUE);
253 
254  result = geometry_serialize(lwline_as_lwgeom(lwline));
255  lwline_free(lwline);
256  }
257  /* BOX3D is a polygon in the X plane */
258  else if (box->xmin == box->xmax)
259  {
260  POINT4D points[4];
261  LWPOLY *lwpoly;
262 
263  /* Initialize the 4 vertices of the polygon */
264  points[0] = (POINT4D) { box->xmin, box->ymin, box->zmin };
265  points[1] = (POINT4D) { box->xmin, box->ymax, box->zmin };
266  points[2] = (POINT4D) { box->xmin, box->ymax, box->zmax };
267  points[3] = (POINT4D) { box->xmin, box->ymin, box->zmax };
268 
270  &points[0], &points[1], &points[2], &points[3]);
271  result = geometry_serialize(lwpoly_as_lwgeom(lwpoly));
272  lwpoly_free(lwpoly);
273  }
274  /* BOX3D is a polygon in the Y plane */
275  else if (box->ymin == box->ymax)
276  {
277  POINT4D points[4];
278  LWPOLY *lwpoly;
279 
280  /* Initialize the 4 vertices of the polygon */
281  points[0] = (POINT4D) { box->xmin, box->ymin, box->zmin };
282  points[1] = (POINT4D) { box->xmax, box->ymin, box->zmin };
283  points[2] = (POINT4D) { box->xmax, box->ymin, box->zmax };
284  points[3] = (POINT4D) { box->xmin, box->ymin, box->zmax };
285 
287  &points[0], &points[1], &points[2], &points[3]);
288  result = geometry_serialize(lwpoly_as_lwgeom(lwpoly));
289  lwpoly_free(lwpoly);
290  }
291  /* BOX3D is a polygon in the Z plane */
292  else if (box->zmin == box->zmax)
293  {
294  POINT4D points[4];
295  LWPOLY *lwpoly;
296 
297  /* Initialize the 4 vertices of the polygon */
298  points[0] = (POINT4D) { box->xmin, box->ymin, box->zmin };
299  points[1] = (POINT4D) { box->xmin, box->ymax, box->zmin };
300  points[2] = (POINT4D) { box->xmax, box->ymax, box->zmin };
301  points[3] = (POINT4D) { box->xmax, box->ymin, box->zmin };
302 
304  &points[0], &points[1], &points[2], &points[3]);
305  result = geometry_serialize(lwpoly_as_lwgeom(lwpoly));
306  lwpoly_free(lwpoly);
307  }
308  /* BOX3D is a polyhedron */
309  else
310  {
311  POINT4D points[8];
312  static const int ngeoms = 6;
313  LWGEOM **geoms = (LWGEOM **) lwalloc(sizeof(LWGEOM *) * ngeoms);
314  LWGEOM *geom = NULL;
315 
316  /* Initialize the 8 vertices of the box */
317  points[0] = (POINT4D) { box->xmin, box->ymin, box->zmin };
318  points[1] = (POINT4D) { box->xmin, box->ymax, box->zmin };
319  points[2] = (POINT4D) { box->xmax, box->ymax, box->zmin };
320  points[3] = (POINT4D) { box->xmax, box->ymin, box->zmin };
321  points[4] = (POINT4D) { box->xmin, box->ymin, box->zmax };
322  points[5] = (POINT4D) { box->xmin, box->ymax, box->zmax };
323  points[6] = (POINT4D) { box->xmax, box->ymax, box->zmax };
324  points[7] = (POINT4D) { box->xmax, box->ymin, box->zmax };
325 
326  /* add bottom polygon */
328  &points[0], &points[1], &points[2], &points[3]));
329  /* add top polygon */
331  &points[4], &points[7], &points[6], &points[5]));
332  /* add left polygon */
334  &points[0], &points[4], &points[5], &points[1]));
335  /* add right polygon */
337  &points[3], &points[2], &points[6], &points[7]));
338  /* add back polygon */
340  &points[0], &points[3], &points[7], &points[4]));
341  /* add front polygon */
343  &points[1], &points[5], &points[6], &points[2]));
344 
346  SRID_UNKNOWN, NULL, ngeoms, geoms);
347 
348  FLAGS_SET_SOLID(geom->flags, 1);
349 
350  result = geometry_serialize(geom);
352  }
353 
354  gserialized_set_srid(result, box->srid);
355 
356  PG_RETURN_POINTER(result);
357 }
double x
Definition: liblwgeom.h:351
int32_t srid
Definition: liblwgeom.h:278
#define FLAGS_SET_SOLID(flags, value)
Definition: liblwgeom.h:150
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:43
uint8_t flags
Definition: liblwgeom.h:396
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:195
void lwline_free(LWLINE *line)
Definition: lwline.c:76
void gserialized_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)...
Definition: g_serialized.c:100
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:96
double ymin
Definition: liblwgeom.h:276
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:252
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:262
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE, then a duplicate point will not be added.
Definition: ptarray.c:156
#define LW_FALSE
Definition: liblwgeom.h:76
double xmin
Definition: liblwgeom.h:276
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:152
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
double z
Definition: liblwgeom.h:351
double xmax
Definition: liblwgeom.h:277
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:339
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:267
LWPOINT * lwpoint_construct(int srid, GBOX *bbox, POINTARRAY *point)
Definition: lwpoint.c:111
void * lwalloc(size_t size)
Definition: lwutil.c:227
double y
Definition: liblwgeom.h:351
double ymax
Definition: liblwgeom.h:277
LWPOLY * lwpoly_construct_rectangle(char hasz, char hasm, POINT4D *p1, POINT4D *p2, POINT4D *p3, POINT4D *p4)
Definition: lwpoly.c:80
double zmax
Definition: liblwgeom.h:277
double zmin
Definition: liblwgeom.h:276
Here is the call graph for this function:
Here is the caller graph for this function: