PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ coveredby()

Datum coveredby ( PG_FUNCTION_ARGS  )

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

1966 {
1967  GSERIALIZED *geom1;
1968  GSERIALIZED *geom2;
1969  GEOSGeometry *g1, *g2;
1970  int result;
1971  GBOX box1, box2;
1972  char *patt = "**F**F***";
1973 
1974  geom1 = PG_GETARG_GSERIALIZED_P(0);
1975  geom2 = PG_GETARG_GSERIALIZED_P(1);
1976 
1977  errorIfGeometryCollection(geom1,geom2);
1979 
1980  /* A.CoveredBy(Empty) == FALSE */
1981  if ( gserialized_is_empty(geom1) || gserialized_is_empty(geom2) )
1982  PG_RETURN_BOOL(false);
1983 
1984  /*
1985  * short-circuit 1: if geom1 bounding box is not completely inside
1986  * geom2 bounding box we can return FALSE.
1987  */
1988  if ( gserialized_get_gbox_p(geom1, &box1) &&
1989  gserialized_get_gbox_p(geom2, &box2) )
1990  {
1991  if ( ! gbox_contains_2d(&box2, &box1) )
1992  {
1993  PG_RETURN_BOOL(false);
1994  }
1995 
1996  POSTGIS_DEBUG(3, "bounding box short-circuit missed.");
1997  }
1998  /*
1999  * short-circuit 2: if geom1 is a point and geom2 is a polygon
2000  * call the point-in-polygon function.
2001  */
2002  if (is_point(geom1) && is_poly(geom2))
2003  {
2004  GSERIALIZED* gpoly = is_poly(geom1) ? geom1 : geom2;
2005  GSERIALIZED* gpoint = is_point(geom1) ? geom1 : geom2;
2006  RTREE_POLY_CACHE* cache = GetRtreeCache(fcinfo, gpoly);
2007  int retval;
2008 
2009  POSTGIS_DEBUG(3, "Point in Polygon test requested...short-circuiting.");
2010  if (gserialized_get_type(gpoint) == POINTTYPE)
2011  {
2012  LWGEOM* point = lwgeom_from_gserialized(gpoint);
2013  int pip_result = pip_short_circuit(cache, lwgeom_as_lwpoint(point), gpoly);
2014  lwgeom_free(point);
2015 
2016  retval = (pip_result != -1); /* not outside */
2017  }
2018  else if (gserialized_get_type(gpoint) == MULTIPOINTTYPE)
2019  {
2021  uint32_t i;
2022 
2023  retval = LW_TRUE;
2024  for (i = 0; i < mpoint->ngeoms; i++)
2025  {
2026  int pip_result = pip_short_circuit(cache, mpoint->geoms[i], gpoly);
2027  if (pip_result == -1)
2028  {
2029  retval = LW_FALSE;
2030  break;
2031  }
2032  }
2033 
2034  lwmpoint_free(mpoint);
2035  }
2036  else
2037  {
2038  /* Never get here */
2039  elog(ERROR,"Type isn't point or multipoint!");
2040  PG_RETURN_NULL();
2041  }
2042 
2043  PG_FREE_IF_COPY(geom1, 0);
2044  PG_FREE_IF_COPY(geom2, 1);
2045  PG_RETURN_BOOL(retval);
2046  }
2047  else
2048  {
2049  POSTGIS_DEBUGF(3, "CoveredBy: type1: %d, type2: %d", gserialized_get_type(geom1), gserialized_get_type(geom2));
2050  }
2051 
2052  initGEOS(lwpgnotice, lwgeom_geos_error);
2053 
2054  g1 = POSTGIS2GEOS(geom1);
2055 
2056  if (!g1)
2057  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
2058 
2059  g2 = POSTGIS2GEOS(geom2);
2060 
2061  if (!g2)
2062  {
2063  GEOSGeom_destroy(g1);
2064  HANDLE_GEOS_ERROR("Second argument geometry could not be converted to GEOS");
2065  }
2066 
2067  result = GEOSRelatePattern(g1,g2,patt);
2068 
2069  GEOSGeom_destroy(g1);
2070  GEOSGeom_destroy(g2);
2071 
2072  if (result == 2) HANDLE_GEOS_ERROR("GEOSCoveredBy");
2073 
2074  PG_FREE_IF_COPY(geom1, 0);
2075  PG_FREE_IF_COPY(geom2, 1);
2076 
2077  PG_RETURN_BOOL(result);
2078 }
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:346
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:100
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:179
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:640
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:86
void lwgeom_geos_error(const char *fmt,...)
#define LW_FALSE
Definition: liblwgeom.h:77
void lwmpoint_free(LWMPOINT *mpt)
Definition: lwmpoint.c:72
LWMPOINT * lwgeom_as_lwmpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:233
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define MULTIPOINTTYPE
Definition: liblwgeom.h:88
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:161
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:338
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
RTREE_POLY_CACHE * GetRtreeCache(FunctionCallInfo 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
static int pip_short_circuit(RTREE_POLY_CACHE *poly_cache, LWPOINT *point, GSERIALIZED *gpoly)
#define HANDLE_GEOS_ERROR(label)
static char is_point(const GSERIALIZED *g)
void errorIfGeometryCollection(GSERIALIZED *g1, GSERIALIZED *g2)
Throws an ereport ERROR if either geometry is a COLLECTIONTYPE.
GEOSGeometry * POSTGIS2GEOS(GSERIALIZED *pglwgeom)
static char is_poly(const GSERIALIZED *g)
uint32_t ngeoms
Definition: liblwgeom.h:471
LWPOINT ** geoms
Definition: liblwgeom.h:473
The tree structure used for fast P-i-P tests by point_in_multipolygon_rtree()
Definition: lwgeom_rtree.h:59
unsigned int uint32_t
Definition: uthash.h:78

References error_if_srid_mismatch(), errorIfGeometryCollection(), gbox_contains_2d(), LWMPOINT::geoms, 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, pip_short_circuit(), POINTTYPE, and POSTGIS2GEOS().

Referenced by rt_raster_coveredby().

Here is the call graph for this function:
Here is the caller graph for this function: