PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ BOX2D_to_LWGEOM()

Datum BOX2D_to_LWGEOM ( PG_FUNCTION_ARGS  )

Definition at line 462 of file lwgeom_box.c.

References BOX2D_construct(), geometry_serialize(), LW_FALSE, LW_TRUE, lwline_as_lwgeom(), lwline_construct(), lwline_free(), lwpoint_as_lwgeom(), lwpoint_free(), lwpoint_make2d(), lwpoly_as_lwgeom(), lwpoly_construct_rectangle(), lwpoly_free(), PG_FUNCTION_INFO_V1(), ptarray_append_point(), ptarray_construct_empty(), SRID_UNKNOWN, POINT4D::x, GBOX::xmax, GBOX::xmin, POINT4D::y, GBOX::ymax, and GBOX::ymin.

Referenced by BOX2D_combine().

463 {
464  GBOX *box = (GBOX *)PG_GETARG_POINTER(0);
465  POINTARRAY *pa = ptarray_construct_empty(0, 0, 5);
466  POINT4D pt;
467  GSERIALIZED *result;
468 
469 
470  /*
471  * Alter BOX2D cast so that a valid geometry is always
472  * returned depending upon the size of the BOX2D. The
473  * code makes the following assumptions:
474  * - If the BOX2D is a single point then return a
475  * POINT geometry
476  * - If the BOX2D represents either a horizontal or
477  * vertical line, return a LINESTRING geometry
478  * - Otherwise return a POLYGON
479  */
480 
481  if ( (box->xmin == box->xmax) && (box->ymin == box->ymax) )
482  {
483  /* Construct and serialize point */
484  LWPOINT *point = lwpoint_make2d(SRID_UNKNOWN, box->xmin, box->ymin);
485  result = geometry_serialize(lwpoint_as_lwgeom(point));
486  lwpoint_free(point);
487  }
488  else if ( (box->xmin == box->xmax) || (box->ymin == box->ymax) )
489  {
490  LWLINE *line;
491 
492  /* Assign coordinates to point array */
493  pt.x = box->xmin;
494  pt.y = box->ymin;
495  ptarray_append_point(pa, &pt, LW_TRUE);
496  pt.x = box->xmax;
497  pt.y = box->ymax;
498  ptarray_append_point(pa, &pt, LW_TRUE);
499 
500  /* Construct and serialize linestring */
501  line = lwline_construct(SRID_UNKNOWN, NULL, pa);
502  result = geometry_serialize(lwline_as_lwgeom(line));
503  lwline_free(line);
504  }
505  else
506  {
507  POINT4D points[4];
508  LWPOLY *poly;
509 
510  /* Initialize the 4 vertices of the polygon */
511  points[0] = (POINT4D) { box->xmin, box->ymin };
512  points[1] = (POINT4D) { box->xmin, box->ymax };
513  points[2] = (POINT4D) { box->xmax, box->ymax };
514  points[3] = (POINT4D) { box->xmax, box->ymin };
515 
516  /* Construct polygon */
517  poly = lwpoly_construct_rectangle(LW_FALSE, LW_FALSE, &points[0], &points[1],
518  &points[2], &points[3]);
519  result = geometry_serialize(lwpoly_as_lwgeom(poly));
520  lwpoly_free(poly);
521  }
522 
523  PG_RETURN_POINTER(result);
524 }
double x
Definition: liblwgeom.h:351
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:145
double xmax
Definition: liblwgeom.h:292
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
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:252
double ymin
Definition: liblwgeom.h:293
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:262
double xmin
Definition: liblwgeom.h:291
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
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 ymax
Definition: liblwgeom.h:294
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:267
double y
Definition: liblwgeom.h:351
LWPOLY * lwpoly_construct_rectangle(char hasz, char hasm, POINT4D *p1, POINT4D *p2, POINT4D *p3, POINT4D *p4)
Definition: lwpoly.c:80
Here is the call graph for this function:
Here is the caller graph for this function: