PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ coveredby()

Datum coveredby ( PG_FUNCTION_ARGS  )

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

2019{
2020 GSERIALIZED *geom1;
2021 GSERIALIZED *geom2;
2022 GEOSGeometry *g1, *g2;
2023 int result;
2024 GBOX box1, box2;
2025 char *patt = "**F**F***";
2026
2027 geom1 = PG_GETARG_GSERIALIZED_P(0);
2028 geom2 = PG_GETARG_GSERIALIZED_P(1);
2029 gserialized_error_if_srid_mismatch(geom1, geom2, __func__);
2030
2031 /* A.CoveredBy(Empty) == FALSE */
2032 if ( gserialized_is_empty(geom1) || gserialized_is_empty(geom2) )
2033 PG_RETURN_BOOL(false);
2034
2035 /*
2036 * short-circuit 1: if geom1 bounding box is not completely inside
2037 * geom2 bounding box we can return FALSE.
2038 */
2039 if ( gserialized_get_gbox_p(geom1, &box1) &&
2040 gserialized_get_gbox_p(geom2, &box2) )
2041 {
2042 if ( ! gbox_contains_2d(&box2, &box1) )
2043 {
2044 PG_RETURN_BOOL(false);
2045 }
2046
2047 POSTGIS_DEBUG(3, "bounding box short-circuit missed.");
2048 }
2049 /*
2050 * short-circuit 2: if geom1 is a point and geom2 is a polygon
2051 * call the point-in-polygon function.
2052 */
2053 if (is_point(geom1) && is_poly(geom2))
2054 {
2055 GSERIALIZED* gpoly = is_poly(geom1) ? geom1 : geom2;
2056 GSERIALIZED* gpoint = is_point(geom1) ? geom1 : geom2;
2057 RTREE_POLY_CACHE* cache = GetRtreeCache(fcinfo, gpoly);
2058 int retval;
2059
2060 POSTGIS_DEBUG(3, "Point in Polygon test requested...short-circuiting.");
2061 if (gserialized_get_type(gpoint) == POINTTYPE)
2062 {
2063 LWGEOM* point = lwgeom_from_gserialized(gpoint);
2064 int pip_result = pip_short_circuit(cache, lwgeom_as_lwpoint(point), gpoly);
2065 lwgeom_free(point);
2066
2067 retval = (pip_result != -1); /* not outside */
2068 }
2069 else if (gserialized_get_type(gpoint) == MULTIPOINTTYPE)
2070 {
2072 uint32_t i;
2073
2074 retval = LW_TRUE;
2075 for (i = 0; i < mpoint->ngeoms; i++)
2076 {
2077 int pip_result = pip_short_circuit(cache, mpoint->geoms[i], gpoly);
2078 if (pip_result == -1)
2079 {
2080 retval = LW_FALSE;
2081 break;
2082 }
2083 }
2084
2085 lwmpoint_free(mpoint);
2086 }
2087 else
2088 {
2089 /* Never get here */
2090 elog(ERROR,"Type isn't point or multipoint!");
2091 PG_RETURN_NULL();
2092 }
2093
2094 PG_FREE_IF_COPY(geom1, 0);
2095 PG_FREE_IF_COPY(geom2, 1);
2096 PG_RETURN_BOOL(retval);
2097 }
2098 else
2099 {
2100 POSTGIS_DEBUGF(3, "CoveredBy: type1: %d, type2: %d", gserialized_get_type(geom1), gserialized_get_type(geom2));
2101 }
2102
2103 initGEOS(lwpgnotice, lwgeom_geos_error);
2104
2105 g1 = POSTGIS2GEOS(geom1);
2106
2107 if (!g1)
2108 HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
2109
2110 g2 = POSTGIS2GEOS(geom2);
2111
2112 if (!g2)
2113 {
2114 GEOSGeom_destroy(g1);
2115 HANDLE_GEOS_ERROR("Second argument geometry could not be converted to GEOS");
2116 }
2117
2118 result = GEOSRelatePattern(g1,g2,patt);
2119
2120 GEOSGeom_destroy(g1);
2121 GEOSGeom_destroy(g2);
2122
2123 if (result == 2) HANDLE_GEOS_ERROR("GEOSCoveredBy");
2124
2125 PG_FREE_IF_COPY(geom1, 0);
2126 PG_FREE_IF_COPY(geom2, 1);
2127
2128 PG_RETURN_BOOL(result);
2129}
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 gbox.c:339
void gserialized_error_if_srid_mismatch(const GSERIALIZED *g1, const GSERIALIZED *g2, const char *funcname)
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *gbox)
Read the box from the GSERIALIZED or calculate it if necessary.
Definition gserialized.c:65
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition gserialized.c:89
void lwgeom_geos_error(const char *fmt,...)
#define LW_FALSE
Definition liblwgeom.h:108
void lwmpoint_free(LWMPOINT *mpt)
Definition lwmpoint.c:72
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1138
#define MULTIPOINTTYPE
Definition liblwgeom.h:119
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition liblwgeom.h:116
LWMPOINT * lwgeom_as_lwmpoint(const LWGEOM *lwgeom)
Definition lwgeom.c:224
#define LW_TRUE
Return types for functions with status returns.
Definition liblwgeom.h:107
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...
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition lwinline.h:121
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)
GEOSGeometry * POSTGIS2GEOS(GSERIALIZED *pglwgeom)
static char is_poly(const GSERIALIZED *g)
uint32_t ngeoms
Definition liblwgeom.h:524
LWPOINT ** geoms
Definition liblwgeom.h:519
The tree structure used for fast P-i-P tests by point_in_multipolygon_rtree()

References gbox_contains_2d(), LWMPOINT::geoms, GetRtreeCache(), gserialized_error_if_srid_mismatch(), gserialized_get_gbox_p(), 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: