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

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

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

Referenced by 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 = (GSERIALIZED *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
2236  geom2 = (GSERIALIZED *) PG_DETOAST_DATUM(PG_GETARG_DATUM(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 (( box2.xmin < box1.xmin ) || ( box2.xmax > box1.xmax ) ||
2254  ( box2.ymin < box1.ymin ) || ( box2.ymax > box1.ymax ))
2255  {
2256  PG_RETURN_BOOL(FALSE);
2257  }
2258  }
2259  /*
2260  * short-circuit 2: if geom2 is a point and geom1 is a polygon
2261  * call the point-in-polygon function.
2262  */
2263  type1 = gserialized_get_type(geom1);
2264  type2 = gserialized_get_type(geom2);
2265  if ((type1 == POLYGONTYPE || type1 == MULTIPOLYGONTYPE) && type2 == POINTTYPE)
2266  {
2267  POSTGIS_DEBUG(3, "Point in Polygon test requested...short-circuiting.");
2268 
2269  lwgeom = lwgeom_from_gserialized(geom1);
2271 
2272  POSTGIS_DEBUGF(3, "Precall point_in_multipolygon_rtree %p, %p", lwgeom, point);
2273 
2274  poly_cache = GetRtreeCache(fcinfo, geom1);
2275 
2276  if ( poly_cache && poly_cache->ringIndices )
2277  {
2278  result = point_in_multipolygon_rtree(poly_cache->ringIndices, poly_cache->polyCount, poly_cache->ringCounts, point);
2279  }
2280  else if ( type1 == POLYGONTYPE )
2281  {
2282  result = point_in_polygon((LWPOLY*)lwgeom, point);
2283  }
2284  else if ( type1 == MULTIPOLYGONTYPE )
2285  {
2286  result = point_in_multipolygon((LWMPOLY*)lwgeom, point);
2287  }
2288  else
2289  {
2290  /* Gulp! Should not be here... */
2291  elog(ERROR,"Type isn't poly or multipoly!");
2292  PG_RETURN_NULL();
2293  }
2294 
2295  lwgeom_free(lwgeom);
2296  lwpoint_free(point);
2297  PG_FREE_IF_COPY(geom1, 0);
2298  PG_FREE_IF_COPY(geom2, 1);
2299  if ( result != -1 ) /* not outside */
2300  {
2301  PG_RETURN_BOOL(TRUE);
2302  }
2303  else
2304  {
2305  PG_RETURN_BOOL(FALSE);
2306  }
2307  }
2308  else
2309  {
2310  POSTGIS_DEBUGF(3, "Covers: type1: %d, type2: %d", type1, type2);
2311  }
2312 
2313  initGEOS(lwnotice, lwgeom_geos_error);
2314 
2315  prep_cache = GetPrepGeomCache( fcinfo, geom1, 0 );
2316 
2317  if ( prep_cache && prep_cache->prepared_geom && prep_cache->argnum == 1 )
2318  {
2319  GEOSGeometry *g1 = (GEOSGeometry *)POSTGIS2GEOS(geom2);
2320  if ( 0 == g1 ) /* exception thrown at construction */
2321  {
2322  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
2323  PG_RETURN_NULL();
2324  }
2325  result = GEOSPreparedCovers( prep_cache->prepared_geom, g1);
2326  GEOSGeom_destroy(g1);
2327  }
2328  else
2329  {
2330  GEOSGeometry *g1;
2331  GEOSGeometry *g2;
2332 
2333  g1 = (GEOSGeometry *)POSTGIS2GEOS(geom1);
2334  if ( 0 == g1 ) /* exception thrown at construction */
2335  {
2336  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
2337  PG_RETURN_NULL();
2338  }
2339  g2 = (GEOSGeometry *)POSTGIS2GEOS(geom2);
2340  if ( 0 == g2 ) /* exception thrown at construction */
2341  {
2342  lwerror("Second argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
2343  GEOSGeom_destroy(g1);
2344  PG_RETURN_NULL();
2345  }
2346  result = GEOSRelatePattern( g1, g2, "******FF*" );
2347  GEOSGeom_destroy(g1);
2348  GEOSGeom_destroy(g2);
2349  }
2350 
2351  if (result == 2)
2352  {
2353  lwerror("GEOSCovers: %s", lwgeom_geos_errmsg);
2354  PG_RETURN_NULL(); /* never get here */
2355  }
2356 
2357  PG_FREE_IF_COPY(geom1, 0);
2358  PG_FREE_IF_COPY(geom2, 1);
2359 
2360  PG_RETURN_BOOL(result);
2361 
2362 }
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
const GEOSPreparedGeometry * prepared_geom
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
PrepGeomCache * GetPrepGeomCache(FunctionCallInfoData *fcinfo, GSERIALIZED *g1, GSERIALIZED *g2)
Given a couple potential geometries and a function call context, return a prepared structure for one ...
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:

Here is the caller graph for this function: