PostGIS  2.5.0dev-r@@SVN_REVISION@@
int lwgeom_covers_lwgeom_sphere ( const LWGEOM lwgeom1,
const LWGEOM lwgeom2 
)

Calculate covers predicate for two lwgeoms on the sphere.

Currently only handles point-in-polygon.

Definition at line 2322 of file lwgeodetic.c.

References LWGEOM::bbox, GBOX::flags, LWCOLLECTION::geoms, getPoint2d_p(), LINETYPE, LW_FALSE, LW_TRUE, LWDEBUG, lwerror(), lwgeom_calculate_gbox_geodetic(), lwline_covers_lwline(), lwline_covers_lwpoint(), lwpoint_same(), lwpoly_covers_lwline(), lwpoly_covers_lwpoly(), lwpoly_covers_point2d(), lwtype_is_collection(), LWCOLLECTION::ngeoms, POINTTYPE, POLYGONTYPE, and LWGEOM::type.

Referenced by geography_covers().

2323 {
2324  int type1, type2;
2325  GBOX gbox1, gbox2;
2326  gbox1.flags = gbox2.flags = 0;
2327 
2328  assert(lwgeom1);
2329  assert(lwgeom2);
2330 
2331  type1 = lwgeom1->type;
2332  type2 = lwgeom2->type;
2333 
2334  /* dim(geom2) > dim(geom1) always returns false (because geom2 is bigger) */
2335  if ( (type1 == POINTTYPE && type2 == LINETYPE)
2336  || (type1 == POINTTYPE && type2 == POLYGONTYPE)
2337  || (type1 == LINETYPE && type2 == POLYGONTYPE) )
2338  {
2339  LWDEBUG(4, "dimension of geom2 is bigger than geom1");
2340  return LW_FALSE;
2341  }
2342 
2343  /* Make sure we have boxes */
2344  if ( lwgeom1->bbox )
2345  gbox1 = *(lwgeom1->bbox);
2346  else
2347  lwgeom_calculate_gbox_geodetic(lwgeom1, &gbox1);
2348 
2349  /* Make sure we have boxes */
2350  if ( lwgeom2->bbox )
2351  gbox2 = *(lwgeom2->bbox);
2352  else
2353  lwgeom_calculate_gbox_geodetic(lwgeom2, &gbox2);
2354 
2355 
2356  /* Handle the polygon/point case */
2357  if ( type1 == POLYGONTYPE && type2 == POINTTYPE )
2358  {
2359  POINT2D pt_to_test;
2360  getPoint2d_p(((LWPOINT*)lwgeom2)->point, 0, &pt_to_test);
2361  return lwpoly_covers_point2d((LWPOLY*)lwgeom1, &pt_to_test);
2362  }
2363  else if ( type1 == POLYGONTYPE && type2 == LINETYPE)
2364  {
2365  return lwpoly_covers_lwline((LWPOLY*)lwgeom1, (LWLINE*)lwgeom2);
2366  }
2367  else if ( type1 == POLYGONTYPE && type2 == POLYGONTYPE)
2368  {
2369  return lwpoly_covers_lwpoly((LWPOLY*)lwgeom1, (LWPOLY*)lwgeom2);
2370  }
2371  else if ( type1 == LINETYPE && type2 == POINTTYPE)
2372  {
2373  return lwline_covers_lwpoint((LWLINE*)lwgeom1, (LWPOINT*)lwgeom2);
2374  }
2375  else if ( type1 == LINETYPE && type2 == LINETYPE)
2376  {
2377  return lwline_covers_lwline((LWLINE*)lwgeom1, (LWLINE*)lwgeom2);
2378  }
2379  else if ( type1 == POINTTYPE && type2 == POINTTYPE)
2380  {
2381  return lwpoint_same((LWPOINT*)lwgeom1, (LWPOINT*)lwgeom2);
2382  }
2383 
2384  /* If any of the first argument parts covers the second argument, it's true */
2385  if ( lwtype_is_collection( type1 ) )
2386  {
2387  uint32_t i;
2388  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom1;
2389 
2390  for ( i = 0; i < col->ngeoms; i++ )
2391  {
2392  if ( lwgeom_covers_lwgeom_sphere(col->geoms[i], lwgeom2) )
2393  {
2394  return LW_TRUE;
2395  }
2396  }
2397  return LW_FALSE;
2398  }
2399 
2400  /* Only if all of the second arguments are covered by the first argument is the condition true */
2401  if ( lwtype_is_collection( type2 ) )
2402  {
2403  uint32_t i;
2404  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom2;
2405 
2406  for ( i = 0; i < col->ngeoms; i++ )
2407  {
2408  if ( ! lwgeom_covers_lwgeom_sphere(lwgeom1, col->geoms[i]) )
2409  {
2410  return LW_FALSE;
2411  }
2412  }
2413  return LW_TRUE;
2414  }
2415 
2416  /* Don't get here */
2417  lwerror("lwgeom_covers_lwgeom_sphere: reached end of function without resolution");
2418  return LW_FALSE;
2419 
2420 }
#define LINETYPE
Definition: liblwgeom.h:85
GBOX * bbox
Definition: liblwgeom.h:397
int lwpoly_covers_lwpoly(const LWPOLY *poly1, const LWPOLY *poly2)
Given a polygon1 check if all points of polygon2 are inside polygon1 and no intersections of the poly...
Definition: lwgeodetic.c:2502
char lwpoint_same(const LWPOINT *p1, const LWPOINT *p2)
Definition: lwpoint.c:264
int lwgeom_covers_lwgeom_sphere(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
Calculate covers predicate for two lwgeoms on the sphere.
Definition: lwgeodetic.c:2322
#define POLYGONTYPE
Definition: liblwgeom.h:86
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
uint32_t ngeoms
Definition: liblwgeom.h:506
unsigned int uint32_t
Definition: uthash.h:78
#define LW_FALSE
Definition: liblwgeom.h:76
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
LWGEOM ** geoms
Definition: liblwgeom.h:508
int lwgeom_calculate_gbox_geodetic(const LWGEOM *geom, GBOX *gbox)
Calculate the geodetic bounding box for an LWGEOM.
Definition: lwgeodetic.c:2927
int lwpoly_covers_lwline(const LWPOLY *poly, const LWLINE *line)
Definition: lwgeodetic.c:2560
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition: lwgeom.c:1086
int lwline_covers_lwline(const LWLINE *lwline1, const LWLINE *lwline2)
Check if first and last point of line2 are covered by line1 and then each point in between has to be ...
Definition: lwgeodetic.c:2686
uint8_t flags
Definition: liblwgeom.h:290
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:338
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
uint8_t type
Definition: liblwgeom.h:395
int lwpoly_covers_point2d(const LWPOLY *poly, const POINT2D *pt_to_test)
Given a polygon (lon/lat decimal degrees) and point (lon/lat decimal degrees) and a guaranteed outsid...
Definition: lwgeodetic.c:2427
int lwline_covers_lwpoint(const LWLINE *lwline, const LWPOINT *lwpoint)
return LW_TRUE if any of the line segments covers the point
Definition: lwgeodetic.c:2657
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190

Here is the call graph for this function:

Here is the caller graph for this function: