PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ contains()

Datum contains ( PG_FUNCTION_ARGS  )

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

References PrepGeomCache::argnum, containsproperly(), error_if_srid_mismatch(), errorIfGeometryCollection(), FALSE, gbox_contains_2d(), GetPrepGeomCache(), GetRtreeCache(), gserialized_get_gbox_p(), gserialized_get_srid(), gserialized_get_type(), gserialized_is_empty(), HANDLE_GEOS_ERROR, lwgeom_as_lwpoint(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_geos_error(), lwpoint_free(), MULTIPOLYGONTYPE, PG_FUNCTION_INFO_V1(), point_in_multipolygon(), point_in_multipolygon_rtree(), point_in_polygon(), POINTTYPE, RTREE_POLY_CACHE::polyCount, POLYGONTYPE, POSTGIS2GEOS(), PrepGeomCache::prepared_geom, RTREE_POLY_CACHE::ringCounts, RTREE_POLY_CACHE::ringIndices, and TRUE.

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

1997 {
1998  GSERIALIZED *geom1;
1999  GSERIALIZED *geom2;
2000  GEOSGeometry *g1, *g2;
2001  GBOX box1, box2;
2002  int type1, type2;
2003  LWGEOM *lwgeom;
2004  LWPOINT *point;
2005  RTREE_POLY_CACHE *poly_cache;
2006  int result;
2007  PrepGeomCache *prep_cache;
2008 
2009  geom1 = PG_GETARG_GSERIALIZED_P(0);
2010  geom2 = PG_GETARG_GSERIALIZED_P(1);
2011 
2012  errorIfGeometryCollection(geom1,geom2);
2014 
2015  /* A.Contains(Empty) == FALSE */
2016  if ( gserialized_is_empty(geom1) || gserialized_is_empty(geom2) )
2017  PG_RETURN_BOOL(false);
2018 
2019  POSTGIS_DEBUG(3, "contains called.");
2020 
2021  /*
2022  ** short-circuit 1: if geom2 bounding box is not completely inside
2023  ** geom1 bounding box we can prematurely return FALSE.
2024  ** Do the test IFF BOUNDING BOX AVAILABLE.
2025  */
2026  if ( gserialized_get_gbox_p(geom1, &box1) &&
2027  gserialized_get_gbox_p(geom2, &box2) )
2028  {
2029  if ( ! gbox_contains_2d(&box1, &box2) )
2030  {
2031  PG_RETURN_BOOL(FALSE);
2032  }
2033  }
2034 
2035  /*
2036  ** short-circuit 2: if geom2 is a point and geom1 is a polygon
2037  ** call the point-in-polygon function.
2038  */
2039  type1 = gserialized_get_type(geom1);
2040  type2 = gserialized_get_type(geom2);
2041  if ((type1 == POLYGONTYPE || type1 == MULTIPOLYGONTYPE) && type2 == POINTTYPE)
2042  {
2043  POSTGIS_DEBUG(3, "Point in Polygon test requested...short-circuiting.");
2044  lwgeom = lwgeom_from_gserialized(geom1);
2046 
2047  POSTGIS_DEBUGF(3, "Precall point_in_multipolygon_rtree %p, %p", lwgeom, point);
2048 
2049  poly_cache = GetRtreeCache(fcinfo, geom1);
2050 
2051  if ( poly_cache && poly_cache->ringIndices )
2052  {
2053  result = point_in_multipolygon_rtree(poly_cache->ringIndices, poly_cache->polyCount, poly_cache->ringCounts, point);
2054  }
2055  else if ( type1 == POLYGONTYPE )
2056  {
2057  result = point_in_polygon((LWPOLY*)lwgeom, point);
2058  }
2059  else if ( type1 == MULTIPOLYGONTYPE )
2060  {
2061  result = point_in_multipolygon((LWMPOLY*)lwgeom, point);
2062  }
2063  else
2064  {
2065  /* Gulp! Should not be here... */
2066  elog(ERROR,"Type isn't poly or multipoly!");
2067  PG_RETURN_NULL();
2068  }
2069  lwgeom_free(lwgeom);
2070  lwpoint_free(point);
2071  PG_FREE_IF_COPY(geom1, 0);
2072  PG_FREE_IF_COPY(geom2, 1);
2073  if ( result == 1 ) /* completely inside */
2074  {
2075  PG_RETURN_BOOL(TRUE);
2076  }
2077  else
2078  {
2079  PG_RETURN_BOOL(FALSE);
2080  }
2081  }
2082  else
2083  {
2084  POSTGIS_DEBUGF(3, "Contains: type1: %d, type2: %d", type1, type2);
2085  }
2086 
2087  initGEOS(lwpgnotice, lwgeom_geos_error);
2088 
2089  prep_cache = GetPrepGeomCache( fcinfo, geom1, 0 );
2090 
2091  if ( prep_cache && prep_cache->prepared_geom && prep_cache->argnum == 1 )
2092  {
2093  g1 = (GEOSGeometry *)POSTGIS2GEOS(geom2);
2094  if ( 0 == g1 ) /* exception thrown at construction */
2095  {
2096  HANDLE_GEOS_ERROR("Geometry could not be converted to GEOS");
2097  PG_RETURN_NULL();
2098  }
2099  POSTGIS_DEBUG(4, "containsPrepared: cache is live, running preparedcontains");
2100  result = GEOSPreparedContains( prep_cache->prepared_geom, g1);
2101  GEOSGeom_destroy(g1);
2102  }
2103  else
2104  {
2105  g1 = (GEOSGeometry *)POSTGIS2GEOS(geom1);
2106  if ( 0 == g1 ) /* exception thrown at construction */
2107  {
2108  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
2109  PG_RETURN_NULL();
2110  }
2111  g2 = (GEOSGeometry *)POSTGIS2GEOS(geom2);
2112  if ( 0 == g2 ) /* exception thrown at construction */
2113  {
2114  HANDLE_GEOS_ERROR("Second argument geometry could not be converted to GEOS");
2115  GEOSGeom_destroy(g1);
2116  PG_RETURN_NULL();
2117  }
2118  POSTGIS_DEBUG(4, "containsPrepared: cache is not ready, running standard contains");
2119  result = GEOSContains( g1, g2);
2120  GEOSGeom_destroy(g1);
2121  GEOSGeom_destroy(g2);
2122  }
2123 
2124  if (result == 2)
2125  {
2126  HANDLE_GEOS_ERROR("GEOSContains");
2127  PG_RETURN_NULL(); /* never get here */
2128  }
2129 
2130  PG_FREE_IF_COPY(geom1, 0);
2131  PG_FREE_IF_COPY(geom2, 1);
2132 
2133  PG_RETURN_BOOL(result);
2134 
2135 }
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:371
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:55
const GEOSPreparedGeometry * prepared_geom
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:72
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:182
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
The tree structure used for fast P-i-P tests by point_in_multipolygon_rtree()
Definition: lwgeom_rtree.h:33
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:316
int point_in_multipolygon_rtree(RTREE_NODE **root, int polyCount, int *ringCounts, LWPOINT *point)
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:341
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:80
int point_in_multipolygon(LWMPOLY *mpolygon, LWPOINT *point)
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:139
void lwgeom_geos_error(const char *fmt,...)
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:75
int point_in_polygon(LWPOLY *polygon, LWPOINT *point)
#define FALSE
Definition: dbfopen.c:168
GEOSGeometry * POSTGIS2GEOS(GSERIALIZED *pglwgeom)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:70
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_NODE ** ringIndices
Definition: lwgeom_rtree.h:35
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:418
#define TRUE
Definition: dbfopen.c:169
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:69
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: