PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ contains()

Datum contains ( PG_FUNCTION_ARGS  )

Definition at line 1719 of file postgis/lwgeom_geos.c.

References PrepGeomCache::argnum, containsproperly(), error_if_srid_mismatch(), errorIfGeometryCollection(), FALSE, gbox_contains_2d(), LWMPOINT::geoms, GetPrepGeomCache(), GetRtreeCache(), gserialized_get_gbox_p(), gserialized_get_srid(), gserialized_get_type(), gserialized_is_empty(), HANDLE_GEOS_ERROR, is_point(), is_poly(), LW_FALSE, LW_TRUE, lwgeom_as_lwmpoint(), lwgeom_as_lwpoint(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_geos_error(), lwmpoint_free(), MULTIPOINTTYPE, LWMPOINT::ngeoms, PG_FUNCTION_INFO_V1(), pip_short_circuit(), POINTTYPE, POSTGIS2GEOS(), and PrepGeomCache::prepared_geom.

Referenced by _lwt_AddFaceSplit(), _lwt_CheckEdgeCrossing(), lwpoly_split_by_line(), lwpoly_to_points(), lwt_AddPoint(), and overlaps().

1720 {
1721  GSERIALIZED *geom1;
1722  GSERIALIZED *geom2;
1723  GEOSGeometry *g1, *g2;
1724  GBOX box1, box2;
1725  int result;
1726  PrepGeomCache *prep_cache;
1727 
1728  geom1 = PG_GETARG_GSERIALIZED_P(0);
1729  geom2 = PG_GETARG_GSERIALIZED_P(1);
1730 
1731  errorIfGeometryCollection(geom1,geom2);
1733 
1734  /* A.Contains(Empty) == FALSE */
1735  if ( gserialized_is_empty(geom1) || gserialized_is_empty(geom2) )
1736  PG_RETURN_BOOL(false);
1737 
1738  POSTGIS_DEBUG(3, "contains called.");
1739 
1740  /*
1741  ** short-circuit 1: if geom2 bounding box is not completely inside
1742  ** geom1 bounding box we can prematurely return FALSE.
1743  */
1744  if ( gserialized_get_gbox_p(geom1, &box1) &&
1745  gserialized_get_gbox_p(geom2, &box2) )
1746  {
1747  if ( ! gbox_contains_2d(&box1, &box2) )
1748  {
1749  PG_RETURN_BOOL(FALSE);
1750  }
1751  }
1752 
1753  /*
1754  ** short-circuit 2: if geom2 is a point and geom1 is a polygon
1755  ** call the point-in-polygon function.
1756  */
1757  if (is_poly(geom1) && is_point(geom2))
1758  {
1759  GSERIALIZED* gpoly = is_poly(geom1) ? geom1 : geom2;
1760  GSERIALIZED* gpoint = is_point(geom1) ? geom1 : geom2;
1761  RTREE_POLY_CACHE* cache = GetRtreeCache(fcinfo, gpoly);
1762  int retval;
1763 
1764  POSTGIS_DEBUG(3, "Point in Polygon test requested...short-circuiting.");
1765  if (gserialized_get_type(gpoint) == POINTTYPE)
1766  {
1767  LWGEOM* point = lwgeom_from_gserialized(gpoint);
1768  int pip_result = pip_short_circuit(cache, lwgeom_as_lwpoint(point), gpoly);
1769  lwgeom_free(point);
1770 
1771  retval = (pip_result == 1); /* completely inside */
1772  }
1773  else if (gserialized_get_type(gpoint) == MULTIPOINTTYPE)
1774  {
1776  uint32_t i;
1777  int found_completely_inside = LW_FALSE;
1778 
1779  retval = LW_TRUE;
1780  for (i = 0; i < mpoint->ngeoms; i++)
1781  {
1782  /* We need to find at least one point that's completely inside the
1783  * polygons (pip_result == 1). As long as we have one point that's
1784  * completely inside, we can have as many as we want on the boundary
1785  * itself. (pip_result == 0)
1786  */
1787  int pip_result = pip_short_circuit(cache, mpoint->geoms[i], gpoly);
1788  if (pip_result == 1)
1789  found_completely_inside = LW_TRUE;
1790 
1791  if (pip_result == -1) /* completely outside */
1792  {
1793  retval = LW_FALSE;
1794  break;
1795  }
1796  }
1797 
1798  retval = retval && found_completely_inside;
1799  lwmpoint_free(mpoint);
1800  }
1801  else
1802  {
1803  /* Never get here */
1804  elog(ERROR,"Type isn't point or multipoint!");
1805  PG_RETURN_NULL();
1806  }
1807 
1808  PG_FREE_IF_COPY(geom1, 0);
1809  PG_FREE_IF_COPY(geom2, 1);
1810  PG_RETURN_BOOL(retval);
1811  }
1812  else
1813  {
1814  POSTGIS_DEBUGF(3, "Contains: type1: %d, type2: %d", gserialized_get_type(geom1), gserialized_get_type(geom2));
1815  }
1816 
1817  initGEOS(lwpgnotice, lwgeom_geos_error);
1818 
1819  prep_cache = GetPrepGeomCache( fcinfo, geom1, 0 );
1820 
1821  if ( prep_cache && prep_cache->prepared_geom && prep_cache->argnum == 1 )
1822  {
1823  g1 = (GEOSGeometry *)POSTGIS2GEOS(geom2);
1824  if ( 0 == g1 ) /* exception thrown at construction */
1825  {
1826  HANDLE_GEOS_ERROR("Geometry could not be converted to GEOS");
1827  PG_RETURN_NULL();
1828  }
1829  POSTGIS_DEBUG(4, "containsPrepared: cache is live, running preparedcontains");
1830  result = GEOSPreparedContains( prep_cache->prepared_geom, g1);
1831  GEOSGeom_destroy(g1);
1832  }
1833  else
1834  {
1835  g1 = (GEOSGeometry *)POSTGIS2GEOS(geom1);
1836  if ( 0 == g1 ) /* exception thrown at construction */
1837  {
1838  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
1839  PG_RETURN_NULL();
1840  }
1841  g2 = (GEOSGeometry *)POSTGIS2GEOS(geom2);
1842  if ( 0 == g2 ) /* exception thrown at construction */
1843  {
1844  HANDLE_GEOS_ERROR("Second argument geometry could not be converted to GEOS");
1845  GEOSGeom_destroy(g1);
1846  PG_RETURN_NULL();
1847  }
1848  POSTGIS_DEBUG(4, "containsPrepared: cache is not ready, running standard contains");
1849  result = GEOSContains( g1, g2);
1850  GEOSGeom_destroy(g1);
1851  GEOSGeom_destroy(g2);
1852  }
1853 
1854  if (result == 2)
1855  {
1856  HANDLE_GEOS_ERROR("GEOSContains");
1857  PG_RETURN_NULL(); /* never get here */
1858  }
1859 
1860  PG_FREE_IF_COPY(geom1, 0);
1861  PG_FREE_IF_COPY(geom2, 1);
1862 
1863  PG_RETURN_BOOL(result);
1864 
1865 }
int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *box)
Read the bounding box off a serialization and calculate one if it is not already there.
Definition: g_serialized.c:398
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:69
const GEOSPreparedGeometry * prepared_geom
static char is_poly(const GSERIALIZED *g)
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1063
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
The tree structure used for fast P-i-P tests by point_in_multipolygon_rtree()
Definition: lwgeom_rtree.h:57
int gbox_contains_2d(const GBOX *g1, const GBOX *g2)
Return LW_TRUE if the first GBOX contains the second on the 2d plane, LW_FALSE otherwise.
Definition: g_box.c:351
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:369
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:93
static char is_point(const GSERIALIZED *g)
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:153
void lwmpoint_free(LWMPOINT *mpt)
Definition: lwmpoint.c:72
void lwgeom_geos_error(const char *fmt,...)
#define LW_FALSE
Definition: liblwgeom.h:76
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
LWPOINT ** geoms
Definition: liblwgeom.h:469
#define FALSE
Definition: dbfopen.c:168
GEOSGeometry * POSTGIS2GEOS(GSERIALIZED *pglwgeom)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
LWMPOINT * lwgeom_as_lwmpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:165
PrepGeomCache * GetPrepGeomCache(FunctionCallInfoData *fcinfo, GSERIALIZED *g1, GSERIALIZED *g2)
Given a couple potential geometries and a function call context, return a prepared structure for one ...
#define HANDLE_GEOS_ERROR(label)
RTREE_POLY_CACHE * GetRtreeCache(FunctionCallInfoData *fcinfo, GSERIALIZED *g1)
Checks for a cache hit against the provided geometry and returns a pre-built index structure (RTREE_P...
Definition: lwgeom_rtree.c:432
int ngeoms
Definition: liblwgeom.h:467
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:83
static int pip_short_circuit(RTREE_POLY_CACHE *poly_cache, LWPOINT *point, GSERIALIZED *gpoly)
void errorIfGeometryCollection(GSERIALIZED *g1, GSERIALIZED *g2)
Throws an ereport ERROR if either geometry is a COLLECTIONTYPE.
Here is the call graph for this function:
Here is the caller graph for this function: