PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum LWGEOM_envelope ( PG_FUNCTION_ARGS  )

Definition at line 1700 of file lwgeom_functions_basic.c.

References dumpnode::geom, geometry_serialize(), 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(), LWGEOM::srid, POINT4D::x, GBOX::xmax, GBOX::xmin, POINT4D::y, GBOX::ymax, and GBOX::ymin.

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

Here is the call graph for this function: