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

Definition at line 1735 of file lwgeom_functions_basic.c.

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

1736 {
1737  GSERIALIZED *geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1738  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
1739  int srid = lwgeom->srid;
1740  POINT4D pt;
1741  GBOX box;
1742  POINTARRAY *pa;
1744 
1745 
1746  if ( lwgeom_is_empty(lwgeom) )
1747  {
1748  /* must be the EMPTY geometry */
1749  PG_RETURN_POINTER(geom);
1750  }
1751 
1752  if ( lwgeom_calculate_gbox(lwgeom, &box) == LW_FAILURE )
1753  {
1754  /* must be the EMPTY geometry */
1755  PG_RETURN_POINTER(geom);
1756  }
1757 
1758  /*
1759  * Alter envelope type so that a valid geometry is always
1760  * returned depending upon the size of the geometry. The
1761  * code makes the following assumptions:
1762  * - If the bounding box is a single point then return a
1763  * POINT geometry
1764  * - If the bounding box represents either a horizontal or
1765  * vertical line, return a LINESTRING geometry
1766  * - Otherwise return a POLYGON
1767  */
1768 
1769  if ( (box.xmin == box.xmax) && (box.ymin == box.ymax) )
1770  {
1771  /* Construct and serialize point */
1772  LWPOINT *point = lwpoint_make2d(srid, box.xmin, box.ymin);
1773  result = geometry_serialize(lwpoint_as_lwgeom(point));
1774  lwpoint_free(point);
1775  }
1776  else if ( (box.xmin == box.xmax) || (box.ymin == box.ymax) )
1777  {
1778  LWLINE *line;
1779  /* Construct point array */
1780  pa = ptarray_construct_empty(0, 0, 2);
1781 
1782  /* Assign coordinates to POINT2D array */
1783  pt.x = box.xmin;
1784  pt.y = box.ymin;
1785  ptarray_append_point(pa, &pt, LW_TRUE);
1786  pt.x = box.xmax;
1787  pt.y = box.ymax;
1788  ptarray_append_point(pa, &pt, LW_TRUE);
1789 
1790  /* Construct and serialize linestring */
1791  line = lwline_construct(srid, NULL, pa);
1792  result = geometry_serialize(lwline_as_lwgeom(line));
1793  lwline_free(line);
1794  }
1795  else
1796  {
1797  LWPOLY *poly;
1798  POINTARRAY **ppa = lwalloc(sizeof(POINTARRAY*));
1799  pa = ptarray_construct_empty(0, 0, 5);
1800  ppa[0] = pa;
1801 
1802  /* Assign coordinates to POINT2D array */
1803  pt.x = box.xmin;
1804  pt.y = box.ymin;
1805  ptarray_append_point(pa, &pt, LW_TRUE);
1806  pt.x = box.xmin;
1807  pt.y = box.ymax;
1808  ptarray_append_point(pa, &pt, LW_TRUE);
1809  pt.x = box.xmax;
1810  pt.y = box.ymax;
1811  ptarray_append_point(pa, &pt, LW_TRUE);
1812  pt.x = box.xmax;
1813  pt.y = box.ymin;
1814  ptarray_append_point(pa, &pt, LW_TRUE);
1815  pt.x = box.xmin;
1816  pt.y = box.ymin;
1817  ptarray_append_point(pa, &pt, LW_TRUE);
1818 
1819  /* Construct polygon */
1820  poly = lwpoly_construct(srid, NULL, 1, ppa);
1821  result = geometry_serialize(lwpoly_as_lwgeom(poly));
1822  lwpoly_free(poly);
1823  }
1824 
1825  PG_FREE_IF_COPY(geom, 0);
1826 
1827  PG_RETURN_POINTER(result);
1828 }
double x
Definition: liblwgeom.h:308
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:130
double xmax
Definition: liblwgeom.h:249
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:57
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:180
void lwline_free(LWLINE *line)
Definition: lwline.c:63
char ** result
Definition: liblwgeom.h:218
int32_t srid
Definition: liblwgeom.h:355
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:239
#define LW_FAILURE
Definition: liblwgeom.h:54
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:608
double ymin
Definition: liblwgeom.h:250
LWGEOM * geom
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:249
double xmin
Definition: liblwgeom.h:248
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:141
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:51
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:29
double ymax
Definition: liblwgeom.h:251
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:254
void * lwalloc(size_t size)
Definition: lwutil.c:175
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:1229
double y
Definition: liblwgeom.h:308

Here is the call graph for this function: