PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ covers()

Datum covers ( PG_FUNCTION_ARGS  )

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

References PrepGeomCache::argnum, coveredby(), 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 containsproperly(), lwt_AddPolygon(), and rt_raster_compute_skewed_raster().

2224 {
2225  GSERIALIZED *geom1;
2226  GSERIALIZED *geom2;
2227  int result;
2228  GBOX box1, box2;
2229  int type1, type2;
2230  LWGEOM *lwgeom;
2231  LWPOINT *point;
2232  RTREE_POLY_CACHE *poly_cache;
2233  PrepGeomCache *prep_cache;
2234 
2235  geom1 = PG_GETARG_GSERIALIZED_P(0);
2236  geom2 = PG_GETARG_GSERIALIZED_P(1);
2237 
2238  /* A.Covers(Empty) == FALSE */
2239  if ( gserialized_is_empty(geom1) || gserialized_is_empty(geom2) )
2240  PG_RETURN_BOOL(false);
2241 
2242  errorIfGeometryCollection(geom1,geom2);
2244 
2245  /*
2246  * short-circuit 1: if geom2 bounding box is not completely inside
2247  * geom1 bounding box we can prematurely return FALSE.
2248  * Do the test IFF BOUNDING BOX AVAILABLE.
2249  */
2250  if ( gserialized_get_gbox_p(geom1, &box1) &&
2251  gserialized_get_gbox_p(geom2, &box2) )
2252  {
2253  if ( ! gbox_contains_2d(&box1, &box2) )
2254  {
2255  PG_RETURN_BOOL(FALSE);
2256  }
2257  }
2258  /*
2259  * short-circuit 2: if geom2 is a point and geom1 is a polygon
2260  * call the point-in-polygon function.
2261  */
2262  type1 = gserialized_get_type(geom1);
2263  type2 = gserialized_get_type(geom2);
2264  if ((type1 == POLYGONTYPE || type1 == MULTIPOLYGONTYPE) && type2 == POINTTYPE)
2265  {
2266  POSTGIS_DEBUG(3, "Point in Polygon test requested...short-circuiting.");
2267 
2268  lwgeom = lwgeom_from_gserialized(geom1);
2270 
2271  POSTGIS_DEBUGF(3, "Precall point_in_multipolygon_rtree %p, %p", lwgeom, point);
2272 
2273  poly_cache = GetRtreeCache(fcinfo, geom1);
2274 
2275  if ( poly_cache && poly_cache->ringIndices )
2276  {
2277  result = point_in_multipolygon_rtree(poly_cache->ringIndices, poly_cache->polyCount, poly_cache->ringCounts, point);
2278  }
2279  else if ( type1 == POLYGONTYPE )
2280  {
2281  result = point_in_polygon((LWPOLY*)lwgeom, point);
2282  }
2283  else if ( type1 == MULTIPOLYGONTYPE )
2284  {
2285  result = point_in_multipolygon((LWMPOLY*)lwgeom, point);
2286  }
2287  else
2288  {
2289  /* Gulp! Should not be here... */
2290  elog(ERROR,"Type isn't poly or multipoly!");
2291  PG_RETURN_NULL();
2292  }
2293 
2294  lwgeom_free(lwgeom);
2295  lwpoint_free(point);
2296  PG_FREE_IF_COPY(geom1, 0);
2297  PG_FREE_IF_COPY(geom2, 1);
2298  if ( result != -1 ) /* not outside */
2299  {
2300  PG_RETURN_BOOL(TRUE);
2301  }
2302  else
2303  {
2304  PG_RETURN_BOOL(FALSE);
2305  }
2306  }
2307  else
2308  {
2309  POSTGIS_DEBUGF(3, "Covers: type1: %d, type2: %d", type1, type2);
2310  }
2311 
2312  initGEOS(lwpgnotice, lwgeom_geos_error);
2313 
2314  prep_cache = GetPrepGeomCache( fcinfo, geom1, 0 );
2315 
2316  if ( prep_cache && prep_cache->prepared_geom && prep_cache->argnum == 1 )
2317  {
2318  GEOSGeometry *g1 = (GEOSGeometry *)POSTGIS2GEOS(geom2);
2319  if ( 0 == g1 ) /* exception thrown at construction */
2320  {
2321  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
2322  PG_RETURN_NULL();
2323  }
2324  result = GEOSPreparedCovers( prep_cache->prepared_geom, g1);
2325  GEOSGeom_destroy(g1);
2326  }
2327  else
2328  {
2329  GEOSGeometry *g1;
2330  GEOSGeometry *g2;
2331 
2332  g1 = (GEOSGeometry *)POSTGIS2GEOS(geom1);
2333  if ( 0 == g1 ) /* exception thrown at construction */
2334  {
2335  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
2336  PG_RETURN_NULL();
2337  }
2338  g2 = (GEOSGeometry *)POSTGIS2GEOS(geom2);
2339  if ( 0 == g2 ) /* exception thrown at construction */
2340  {
2341  HANDLE_GEOS_ERROR("Second argument geometry could not be converted to GEOS");
2342  GEOSGeom_destroy(g1);
2343  PG_RETURN_NULL();
2344  }
2345  result = GEOSRelatePattern( g1, g2, "******FF*" );
2346  GEOSGeom_destroy(g1);
2347  GEOSGeom_destroy(g2);
2348  }
2349 
2350  if (result == 2)
2351  {
2352  HANDLE_GEOS_ERROR("GEOSCovers");
2353  PG_RETURN_NULL(); /* never get here */
2354  }
2355 
2356  PG_FREE_IF_COPY(geom1, 0);
2357  PG_FREE_IF_COPY(geom2, 1);
2358 
2359  PG_RETURN_BOOL(result);
2360 
2361 }
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: