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

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

References PrepGeomCache::argnum, error_if_srid_mismatch(), errorIfGeometryCollection(), FALSE, GetPrepGeomCache(), GetRtreeCache(), gserialized_get_gbox_p(), gserialized_get_srid(), gserialized_get_type(), gserialized_is_empty(), lwerror(), lwgeom_as_lwpoint(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_geos_errmsg, lwgeom_geos_error(), lwnotice(), lwpoint_free(), MULTIPOLYGONTYPE, point_in_multipolygon(), point_in_multipolygon_rtree(), point_in_polygon(), POINTTYPE, RTREE_POLY_CACHE::polyCount, POLYGONTYPE, POSTGIS2GEOS(), PrepGeomCache::prepared_geom, result, RTREE_POLY_CACHE::ringCounts, RTREE_POLY_CACHE::ringIndices, TRUE, GBOX::xmax, GBOX::xmin, GBOX::ymax, and GBOX::ymin.

Referenced by lwpoly_split_by_line().

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