PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ BOX2D_to_LWGEOM()

Datum BOX2D_to_LWGEOM ( PG_FUNCTION_ARGS  )

Definition at line 438 of file lwgeom_box.c.

References BOX2D_construct(), geometry_serialize(), LW_TRUE, lwalloc(), lwline_as_lwgeom(), lwline_construct(), lwline_free(), lwpoint_as_lwgeom(), lwpoint_free(), lwpoint_make2d(), lwpoly_as_lwgeom(), lwpoly_construct(), 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().

439 {
440  GBOX *box = (GBOX *)PG_GETARG_POINTER(0);
441  POINTARRAY *pa = ptarray_construct_empty(0, 0, 5);
442  POINT4D pt;
443  GSERIALIZED *result;
444 
445 
446  /*
447  * Alter BOX2D cast so that a valid geometry is always
448  * returned depending upon the size of the BOX2D. The
449  * code makes the following assumptions:
450  * - If the BOX2D is a single point then return a
451  * POINT geometry
452  * - If the BOX2D represents either a horizontal or
453  * vertical line, return a LINESTRING geometry
454  * - Otherwise return a POLYGON
455  */
456 
457  if ( (box->xmin == box->xmax) && (box->ymin == box->ymax) )
458  {
459  /* Construct and serialize point */
460  LWPOINT *point = lwpoint_make2d(SRID_UNKNOWN, box->xmin, box->ymin);
461  result = geometry_serialize(lwpoint_as_lwgeom(point));
462  lwpoint_free(point);
463  }
464  else if ( (box->xmin == box->xmax) || (box->ymin == box->ymax) )
465  {
466  LWLINE *line;
467 
468  /* Assign coordinates to point array */
469  pt.x = box->xmin;
470  pt.y = box->ymin;
471  ptarray_append_point(pa, &pt, LW_TRUE);
472  pt.x = box->xmax;
473  pt.y = box->ymax;
474  ptarray_append_point(pa, &pt, LW_TRUE);
475 
476  /* Construct and serialize linestring */
477  line = lwline_construct(SRID_UNKNOWN, NULL, pa);
478  result = geometry_serialize(lwline_as_lwgeom(line));
479  lwline_free(line);
480  }
481  else
482  {
483  LWPOLY *poly;
484  POINTARRAY **ppa = lwalloc(sizeof(POINTARRAY*));
485 
486  /* Assign coordinates to point array */
487  pt.x = box->xmin;
488  pt.y = box->ymin;
489  ptarray_append_point(pa, &pt, LW_TRUE);
490  pt.x = box->xmin;
491  pt.y = box->ymax;
492  ptarray_append_point(pa, &pt, LW_TRUE);
493  pt.x = box->xmax;
494  pt.y = box->ymax;
495  ptarray_append_point(pa, &pt, LW_TRUE);
496  pt.x = box->xmax;
497  pt.y = box->ymin;
498  ptarray_append_point(pa, &pt, LW_TRUE);
499  pt.x = box->xmin;
500  pt.y = box->ymin;
501  ptarray_append_point(pa, &pt, LW_TRUE);
502 
503  /* Construct polygon */
504  ppa[0] = pa;
505  poly = lwpoly_construct(SRID_UNKNOWN, NULL, 1, ppa);
506  result = geometry_serialize(lwpoly_as_lwgeom(poly));
507  lwpoly_free(poly);
508  }
509 
510  PG_RETURN_POINTER(result);
511 }
double x
Definition: liblwgeom.h:336
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:132
double xmax
Definition: liblwgeom.h:277
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:182
void lwline_free(LWLINE *line)
Definition: lwline.c:63
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:239
double ymin
Definition: liblwgeom.h:278
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:249
double xmin
Definition: liblwgeom.h:276
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_TRUE, then a duplicate point will not be added.
Definition: ptarray.c:156
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:29
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:79
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:61
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:29
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:172
double ymax
Definition: liblwgeom.h:279
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:254
void * lwalloc(size_t size)
Definition: lwutil.c:199
double y
Definition: liblwgeom.h:336
Here is the call graph for this function:
Here is the caller graph for this function: