PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ BOX2D_to_LWGEOM()

Datum BOX2D_to_LWGEOM ( PG_FUNCTION_ARGS  )

Definition at line 470 of file lwgeom_box.c.

471 {
472  GBOX *box = (GBOX *)PG_GETARG_POINTER(0);
473  POINTARRAY *pa = ptarray_construct_empty(0, 0, 5);
474  POINT4D pt;
475  GSERIALIZED *result;
476 
477 
478  /*
479  * Alter BOX2D cast so that a valid geometry is always
480  * returned depending upon the size of the BOX2D. The
481  * code makes the following assumptions:
482  * - If the BOX2D is a single point then return a
483  * POINT geometry
484  * - If the BOX2D represents either a horizontal or
485  * vertical line, return a LINESTRING geometry
486  * - Otherwise return a POLYGON
487  */
488 
489  if ( (box->xmin == box->xmax) && (box->ymin == box->ymax) )
490  {
491  /* Construct and serialize point */
492  LWPOINT *point = lwpoint_make2d(SRID_UNKNOWN, box->xmin, box->ymin);
493  result = geometry_serialize(lwpoint_as_lwgeom(point));
494  lwpoint_free(point);
495  }
496  else if ( (box->xmin == box->xmax) || (box->ymin == box->ymax) )
497  {
498  LWLINE *line;
499 
500  /* Assign coordinates to point array */
501  pt.x = box->xmin;
502  pt.y = box->ymin;
503  ptarray_append_point(pa, &pt, LW_TRUE);
504  pt.x = box->xmax;
505  pt.y = box->ymax;
506  ptarray_append_point(pa, &pt, LW_TRUE);
507 
508  /* Construct and serialize linestring */
509  line = lwline_construct(SRID_UNKNOWN, NULL, pa);
510  result = geometry_serialize(lwline_as_lwgeom(line));
511  lwline_free(line);
512  }
513  else
514  {
515  POINT4D points[4];
516  LWPOLY *poly;
517 
518  /* Initialize the 4 vertices of the polygon */
519  points[0] = (POINT4D) { box->xmin, box->ymin, 0.0, 0.0 };
520  points[1] = (POINT4D) { box->xmin, box->ymax, 0.0, 0.0 };
521  points[2] = (POINT4D) { box->xmax, box->ymax, 0.0, 0.0 };
522  points[3] = (POINT4D) { box->xmax, box->ymin, 0.0, 0.0 };
523 
524  /* Construct polygon */
525  poly = lwpoly_construct_rectangle(LW_FALSE, LW_FALSE, &points[0], &points[1],
526  &points[2], &points[3]);
527  result = geometry_serialize(lwpoly_as_lwgeom(poly));
528  lwpoly_free(poly);
529  }
530 
531  PG_RETURN_POINTER(result);
532 }
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:163
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:330
#define LW_FALSE
Definition: liblwgeom.h:77
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:320
LWPOLY * lwpoly_construct_rectangle(char hasz, char hasm, POINT4D *p1, POINT4D *p2, POINT4D *p3, POINT4D *p4)
Definition: lwpoly.c:80
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:335
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
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,...
Definition: ptarray.c:156
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:175
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:188
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
void lwline_free(LWLINE *line)
Definition: lwline.c:76
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
double ymax
Definition: liblwgeom.h:298
double xmax
Definition: liblwgeom.h:296
double ymin
Definition: liblwgeom.h:297
double xmin
Definition: liblwgeom.h:295
double x
Definition: liblwgeom.h:355
double y
Definition: liblwgeom.h:355

References 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(), ptarray_append_point(), ptarray_construct_empty(), SRID_UNKNOWN, POINT4D::x, GBOX::xmax, GBOX::xmin, POINT4D::y, GBOX::ymax, and GBOX::ymin.

Here is the call graph for this function: