PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ LWGEOM_envelope()

Datum LWGEOM_envelope ( PG_FUNCTION_ARGS  )

Definition at line 1699 of file lwgeom_functions_basic.c.

1700 {
1701  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
1702  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
1703  int32_t srid = lwgeom->srid;
1704  POINT4D pt;
1705  GBOX box;
1706  POINTARRAY *pa;
1708 
1709  if (lwgeom_is_empty(lwgeom))
1710  {
1711  /* must be the EMPTY geometry */
1712  PG_RETURN_POINTER(geom);
1713  }
1714 
1715  if (lwgeom_calculate_gbox(lwgeom, &box) == LW_FAILURE)
1716  {
1717  /* must be the EMPTY geometry */
1718  PG_RETURN_POINTER(geom);
1719  }
1720 
1721  /*
1722  * Alter envelope type so that a valid geometry is always
1723  * returned depending upon the size of the geometry. The
1724  * code makes the following assumptions:
1725  * - If the bounding box is a single point then return a
1726  * POINT geometry
1727  * - If the bounding box represents either a horizontal or
1728  * vertical line, return a LINESTRING geometry
1729  * - Otherwise return a POLYGON
1730  */
1731 
1732  if ((box.xmin == box.xmax) && (box.ymin == box.ymax))
1733  {
1734  /* Construct and serialize point */
1735  LWPOINT *point = lwpoint_make2d(srid, box.xmin, box.ymin);
1736  result = geometry_serialize(lwpoint_as_lwgeom(point));
1737  lwpoint_free(point);
1738  }
1739  else if ((box.xmin == box.xmax) || (box.ymin == box.ymax))
1740  {
1741  LWLINE *line;
1742  /* Construct point array */
1743  pa = ptarray_construct_empty(0, 0, 2);
1744 
1745  /* Assign coordinates to POINT2D array */
1746  pt.x = box.xmin;
1747  pt.y = box.ymin;
1748  ptarray_append_point(pa, &pt, LW_TRUE);
1749  pt.x = box.xmax;
1750  pt.y = box.ymax;
1751  ptarray_append_point(pa, &pt, LW_TRUE);
1752 
1753  /* Construct and serialize linestring */
1754  line = lwline_construct(srid, NULL, pa);
1755  result = geometry_serialize(lwline_as_lwgeom(line));
1756  lwline_free(line);
1757  }
1758  else
1759  {
1760  LWPOLY *poly;
1761  POINTARRAY **ppa = lwalloc(sizeof(POINTARRAY *));
1762  pa = ptarray_construct_empty(0, 0, 5);
1763  ppa[0] = pa;
1764 
1765  /* Assign coordinates to POINT2D array */
1766  pt.x = box.xmin;
1767  pt.y = box.ymin;
1768  ptarray_append_point(pa, &pt, LW_TRUE);
1769  pt.x = box.xmin;
1770  pt.y = box.ymax;
1771  ptarray_append_point(pa, &pt, LW_TRUE);
1772  pt.x = box.xmax;
1773  pt.y = box.ymax;
1774  ptarray_append_point(pa, &pt, LW_TRUE);
1775  pt.x = box.xmax;
1776  pt.y = box.ymin;
1777  ptarray_append_point(pa, &pt, LW_TRUE);
1778  pt.x = box.xmin;
1779  pt.y = box.ymin;
1780  ptarray_append_point(pa, &pt, LW_TRUE);
1781 
1782  /* Construct polygon */
1783  poly = lwpoly_construct(srid, NULL, 1, ppa);
1784  result = geometry_serialize(lwpoly_as_lwgeom(poly));
1785  lwpoly_free(poly);
1786  }
1787 
1788  PG_FREE_IF_COPY(geom, 0);
1789 
1790  PG_RETURN_POINTER(result);
1791 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:322
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
Definition: lwpoint.c:163
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
#define LW_FAILURE
Definition: liblwgeom.h:110
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:312
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:327
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:59
int lwgeom_calculate_gbox(const LWGEOM *lwgeom, GBOX *gbox)
Calculate bounding box of a geometry, automatically taking into account whether it is cartesian or ge...
Definition: lwgeom.c:738
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 * lwalloc(size_t size)
Definition: lwutil.c:227
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:175
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:107
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:43
void lwline_free(LWLINE *line)
Definition: lwline.c:67
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
double ymax
Definition: liblwgeom.h:371
double xmax
Definition: liblwgeom.h:369
double ymin
Definition: liblwgeom.h:370
double xmin
Definition: liblwgeom.h:368
int32_t srid
Definition: liblwgeom.h:474
double x
Definition: liblwgeom.h:428
double y
Definition: liblwgeom.h:428

References LW_FAILURE, LW_TRUE, lwalloc(), lwgeom_calculate_gbox(), lwgeom_from_gserialized(), lwgeom_is_empty(), lwline_as_lwgeom(), lwline_construct(), lwline_free(), lwpoint_as_lwgeom(), lwpoint_free(), lwpoint_make2d(), lwpoly_as_lwgeom(), lwpoly_construct(), lwpoly_free(), ptarray_append_point(), ptarray_construct_empty(), result, LWGEOM::srid, POINT4D::x, GBOX::xmax, GBOX::xmin, POINT4D::y, GBOX::ymax, and GBOX::ymin.

Here is the call graph for this function: