PostGIS  3.4.0dev-r@@SVN_REVISION@@
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages

◆ LWGEOM_envelope()

Datum LWGEOM_envelope ( PG_FUNCTION_ARGS  )

Definition at line 1763 of file lwgeom_functions_basic.c.

1764 {
1765  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
1766  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
1767  int32_t srid = lwgeom->srid;
1768  POINT4D pt;
1769  GBOX box;
1770  POINTARRAY *pa;
1772 
1773  if (lwgeom_is_empty(lwgeom))
1774  {
1775  /* must be the EMPTY geometry */
1776  PG_RETURN_POINTER(geom);
1777  }
1778 
1779  if (lwgeom_calculate_gbox(lwgeom, &box) == LW_FAILURE)
1780  {
1781  /* must be the EMPTY geometry */
1782  PG_RETURN_POINTER(geom);
1783  }
1784 
1785  /*
1786  * Alter envelope type so that a valid geometry is always
1787  * returned depending upon the size of the geometry. The
1788  * code makes the following assumptions:
1789  * - If the bounding box is a single point then return a
1790  * POINT geometry
1791  * - If the bounding box represents either a horizontal or
1792  * vertical line, return a LINESTRING geometry
1793  * - Otherwise return a POLYGON
1794  */
1795 
1796  if ((box.xmin == box.xmax) && (box.ymin == box.ymax))
1797  {
1798  /* Construct and serialize point */
1799  LWPOINT *point = lwpoint_make2d(srid, box.xmin, box.ymin);
1800  result = geometry_serialize(lwpoint_as_lwgeom(point));
1801  lwpoint_free(point);
1802  }
1803  else if ((box.xmin == box.xmax) || (box.ymin == box.ymax))
1804  {
1805  LWLINE *line;
1806  /* Construct point array */
1807  pa = ptarray_construct_empty(0, 0, 2);
1808 
1809  /* Assign coordinates to POINT2D array */
1810  pt.x = box.xmin;
1811  pt.y = box.ymin;
1812  ptarray_append_point(pa, &pt, LW_TRUE);
1813  pt.x = box.xmax;
1814  pt.y = box.ymax;
1815  ptarray_append_point(pa, &pt, LW_TRUE);
1816 
1817  /* Construct and serialize linestring */
1818  line = lwline_construct(srid, NULL, pa);
1819  result = geometry_serialize(lwline_as_lwgeom(line));
1820  lwline_free(line);
1821  }
1822  else
1823  {
1824  LWPOLY *poly;
1825  POINTARRAY **ppa = lwalloc(sizeof(POINTARRAY *));
1826  pa = ptarray_construct_empty(0, 0, 5);
1827  ppa[0] = pa;
1828 
1829  /* Assign coordinates to POINT2D array */
1830  pt.x = box.xmin;
1831  pt.y = box.ymin;
1832  ptarray_append_point(pa, &pt, LW_TRUE);
1833  pt.x = box.xmin;
1834  pt.y = box.ymax;
1835  ptarray_append_point(pa, &pt, LW_TRUE);
1836  pt.x = box.xmax;
1837  pt.y = box.ymax;
1838  ptarray_append_point(pa, &pt, LW_TRUE);
1839  pt.x = box.xmax;
1840  pt.y = box.ymin;
1841  ptarray_append_point(pa, &pt, LW_TRUE);
1842  pt.x = box.xmin;
1843  pt.y = box.ymin;
1844  ptarray_append_point(pa, &pt, LW_TRUE);
1845 
1846  /* Construct polygon */
1847  poly = lwpoly_construct(srid, NULL, 1, ppa);
1848  result = geometry_serialize(lwpoly_as_lwgeom(poly));
1849  lwpoly_free(poly);
1850  }
1851 
1852  PG_FREE_IF_COPY(geom, 0);
1853 
1854  PG_RETURN_POINTER(result);
1855 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
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:339
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:96
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:329
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:344
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:755
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:93
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:357
double xmax
Definition: liblwgeom.h:355
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354
int32_t srid
Definition: liblwgeom.h:460
double x
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414

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: