PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ BOX2D_to_LWGEOM()

Datum BOX2D_to_LWGEOM ( PG_FUNCTION_ARGS  )

Definition at line 468 of file lwgeom_box.c.

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

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: