PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ LWGEOM_envelope()

Datum LWGEOM_envelope ( PG_FUNCTION_ARGS  )

Definition at line 1663 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(), LWGEOM_isempty(), 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(), LWGEOM::srid, POINT4D::x, GBOX::xmax, GBOX::xmin, POINT4D::y, GBOX::ymax, and GBOX::ymin.

Referenced by LWGEOM_to_BOX().

1664 {
1665  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
1666  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
1667  int srid = lwgeom->srid;
1668  POINT4D pt;
1669  GBOX box;
1670  POINTARRAY *pa;
1671  GSERIALIZED *result;
1672 
1673 
1674  if ( lwgeom_is_empty(lwgeom) )
1675  {
1676  /* must be the EMPTY geometry */
1677  PG_RETURN_POINTER(geom);
1678  }
1679 
1680  if ( lwgeom_calculate_gbox(lwgeom, &box) == LW_FAILURE )
1681  {
1682  /* must be the EMPTY geometry */
1683  PG_RETURN_POINTER(geom);
1684  }
1685 
1686  /*
1687  * Alter envelope type so that a valid geometry is always
1688  * returned depending upon the size of the geometry. The
1689  * code makes the following assumptions:
1690  * - If the bounding box is a single point then return a
1691  * POINT geometry
1692  * - If the bounding box represents either a horizontal or
1693  * vertical line, return a LINESTRING geometry
1694  * - Otherwise return a POLYGON
1695  */
1696 
1697  if ( (box.xmin == box.xmax) && (box.ymin == box.ymax) )
1698  {
1699  /* Construct and serialize point */
1700  LWPOINT *point = lwpoint_make2d(srid, box.xmin, box.ymin);
1701  result = geometry_serialize(lwpoint_as_lwgeom(point));
1702  lwpoint_free(point);
1703  }
1704  else if ( (box.xmin == box.xmax) || (box.ymin == box.ymax) )
1705  {
1706  LWLINE *line;
1707  /* Construct point array */
1708  pa = ptarray_construct_empty(0, 0, 2);
1709 
1710  /* Assign coordinates to POINT2D array */
1711  pt.x = box.xmin;
1712  pt.y = box.ymin;
1713  ptarray_append_point(pa, &pt, LW_TRUE);
1714  pt.x = box.xmax;
1715  pt.y = box.ymax;
1716  ptarray_append_point(pa, &pt, LW_TRUE);
1717 
1718  /* Construct and serialize linestring */
1719  line = lwline_construct(srid, NULL, pa);
1720  result = geometry_serialize(lwline_as_lwgeom(line));
1721  lwline_free(line);
1722  }
1723  else
1724  {
1725  LWPOLY *poly;
1726  POINTARRAY **ppa = lwalloc(sizeof(POINTARRAY*));
1727  pa = ptarray_construct_empty(0, 0, 5);
1728  ppa[0] = pa;
1729 
1730  /* Assign coordinates to POINT2D array */
1731  pt.x = box.xmin;
1732  pt.y = box.ymin;
1733  ptarray_append_point(pa, &pt, LW_TRUE);
1734  pt.x = box.xmin;
1735  pt.y = box.ymax;
1736  ptarray_append_point(pa, &pt, LW_TRUE);
1737  pt.x = box.xmax;
1738  pt.y = box.ymax;
1739  ptarray_append_point(pa, &pt, LW_TRUE);
1740  pt.x = box.xmax;
1741  pt.y = box.ymin;
1742  ptarray_append_point(pa, &pt, LW_TRUE);
1743  pt.x = box.xmin;
1744  pt.y = box.ymin;
1745  ptarray_append_point(pa, &pt, LW_TRUE);
1746 
1747  /* Construct polygon */
1748  poly = lwpoly_construct(srid, NULL, 1, ppa);
1749  result = geometry_serialize(lwpoly_as_lwgeom(poly));
1750  lwpoly_free(poly);
1751  }
1752 
1753  PG_FREE_IF_COPY(geom, 0);
1754 
1755  PG_RETURN_POINTER(result);
1756 }
double x
Definition: liblwgeom.h:336
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: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
int32_t srid
Definition: liblwgeom.h:383
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:239
#define LW_FAILURE
Definition: liblwgeom.h:64
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:652
double ymin
Definition: liblwgeom.h:278
LWGEOM * geom
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
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
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:1297
double y
Definition: liblwgeom.h:336
Here is the call graph for this function:
Here is the caller graph for this function: