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

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

References error_if_srid_mismatch(), errorIfGeometryCollection(), FALSE, 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(), result, RTREE_POLY_CACHE::ringCounts, RTREE_POLY_CACHE::ringIndices, TRUE, GBOX::xmax, GBOX::xmin, GBOX::ymax, and GBOX::ymin.

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