PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ coveredby()

Datum coveredby ( PG_FUNCTION_ARGS  )

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

References crosses(), error_if_srid_mismatch(), errorIfGeometryCollection(), FALSE, gbox_contains_2d(), 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(), RTREE_POLY_CACHE::ringCounts, RTREE_POLY_CACHE::ringIndices, and TRUE.

Referenced by covers().

2377 {
2378  GSERIALIZED *geom1;
2379  GSERIALIZED *geom2;
2380  GEOSGeometry *g1, *g2;
2381  int result;
2382  GBOX box1, box2;
2383  LWGEOM *lwgeom;
2384  LWPOINT *point;
2385  int type1, type2;
2386  RTREE_POLY_CACHE *poly_cache;
2387  char *patt = "**F**F***";
2388 
2389  geom1 = PG_GETARG_GSERIALIZED_P(0);
2390  geom2 = PG_GETARG_GSERIALIZED_P(1);
2391 
2392  errorIfGeometryCollection(geom1,geom2);
2394 
2395  /* A.CoveredBy(Empty) == FALSE */
2396  if ( gserialized_is_empty(geom1) || gserialized_is_empty(geom2) )
2397  PG_RETURN_BOOL(false);
2398 
2399  /*
2400  * short-circuit 1: if geom1 bounding box is not completely inside
2401  * geom2 bounding box we can prematurely return FALSE.
2402  * Do the test IFF BOUNDING BOX AVAILABLE.
2403  */
2404  if ( gserialized_get_gbox_p(geom1, &box1) &&
2405  gserialized_get_gbox_p(geom2, &box2) )
2406  {
2407  if ( ! gbox_contains_2d(&box2, &box1) )
2408  {
2409  PG_RETURN_BOOL(FALSE);
2410  }
2411 
2412  POSTGIS_DEBUG(3, "bounding box short-circuit missed.");
2413  }
2414  /*
2415  * short-circuit 2: if geom1 is a point and geom2 is a polygon
2416  * call the point-in-polygon function.
2417  */
2418  type1 = gserialized_get_type(geom1);
2419  type2 = gserialized_get_type(geom2);
2420  if ((type2 == POLYGONTYPE || type2 == MULTIPOLYGONTYPE) && type1 == POINTTYPE)
2421  {
2422  POSTGIS_DEBUG(3, "Point in Polygon test requested...short-circuiting.");
2423 
2425  lwgeom = lwgeom_from_gserialized(geom2);
2426 
2427  poly_cache = GetRtreeCache(fcinfo, geom2);
2428 
2429  if ( poly_cache && poly_cache->ringIndices )
2430  {
2431  result = point_in_multipolygon_rtree(poly_cache->ringIndices, poly_cache->polyCount, poly_cache->ringCounts, point);
2432  }
2433  else if ( type2 == POLYGONTYPE )
2434  {
2435  result = point_in_polygon((LWPOLY*)lwgeom, point);
2436  }
2437  else if ( type2 == MULTIPOLYGONTYPE )
2438  {
2439  result = point_in_multipolygon((LWMPOLY*)lwgeom, point);
2440  }
2441  else
2442  {
2443  /* Gulp! Should not be here... */
2444  elog(ERROR,"Type isn't poly or multipoly!");
2445  PG_RETURN_NULL();
2446  }
2447 
2448  lwgeom_free(lwgeom);
2449  lwpoint_free(point);
2450  PG_FREE_IF_COPY(geom1, 0);
2451  PG_FREE_IF_COPY(geom2, 1);
2452  if ( result != -1 ) /* not outside */
2453  {
2454  PG_RETURN_BOOL(TRUE);
2455  }
2456  else
2457  {
2458  PG_RETURN_BOOL(FALSE);
2459  }
2460  }
2461 
2462  initGEOS(lwpgnotice, lwgeom_geos_error);
2463 
2464  g1 = (GEOSGeometry *)POSTGIS2GEOS(geom1);
2465 
2466  if ( 0 == g1 ) /* exception thrown at construction */
2467  {
2468  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
2469  PG_RETURN_NULL();
2470  }
2471 
2472  g2 = (GEOSGeometry *)POSTGIS2GEOS(geom2);
2473 
2474  if ( 0 == g2 ) /* exception thrown at construction */
2475  {
2476  HANDLE_GEOS_ERROR("Second argument geometry could not be converted to GEOS");
2477  GEOSGeom_destroy(g1);
2478  PG_RETURN_NULL();
2479  }
2480 
2481  result = GEOSRelatePattern(g1,g2,patt);
2482 
2483  GEOSGeom_destroy(g1);
2484  GEOSGeom_destroy(g2);
2485 
2486  if (result == 2)
2487  {
2488  HANDLE_GEOS_ERROR("GEOSCoveredBy");
2489  PG_RETURN_NULL(); /* never get here */
2490  }
2491 
2492  PG_FREE_IF_COPY(geom1, 0);
2493  PG_FREE_IF_COPY(geom2, 1);
2494 
2495  PG_RETURN_BOOL(result);
2496 }
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
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
#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: