PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ lwgeom_covers_lwgeom_sphere()

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 2416 of file lwgeodetic.c.

2417 {
2418  int type1, type2;
2419  GBOX gbox1, gbox2;
2420  gbox1.flags = gbox2.flags = 0;
2421 
2422  assert(lwgeom1);
2423  assert(lwgeom2);
2424 
2425  type1 = lwgeom1->type;
2426  type2 = lwgeom2->type;
2427 
2428  /* dim(geom2) > dim(geom1) always returns false (because geom2 is bigger) */
2429  if ( (type1 == POINTTYPE && type2 == LINETYPE)
2430  || (type1 == POINTTYPE && type2 == POLYGONTYPE)
2431  || (type1 == LINETYPE && type2 == POLYGONTYPE) )
2432  {
2433  LWDEBUG(4, "dimension of geom2 is bigger than geom1");
2434  return LW_FALSE;
2435  }
2436 
2437  /* Make sure we have boxes */
2438  if ( lwgeom1->bbox )
2439  gbox1 = *(lwgeom1->bbox);
2440  else
2441  lwgeom_calculate_gbox_geodetic(lwgeom1, &gbox1);
2442 
2443  /* Make sure we have boxes */
2444  if ( lwgeom2->bbox )
2445  gbox2 = *(lwgeom2->bbox);
2446  else
2447  lwgeom_calculate_gbox_geodetic(lwgeom2, &gbox2);
2448 
2449 
2450  /* Handle the polygon/point case */
2451  if ( type1 == POLYGONTYPE && type2 == POINTTYPE )
2452  {
2453  POINT2D pt_to_test;
2454  getPoint2d_p(((LWPOINT*)lwgeom2)->point, 0, &pt_to_test);
2455  return lwpoly_covers_point2d((LWPOLY*)lwgeom1, &pt_to_test);
2456  }
2457  else if ( type1 == POLYGONTYPE && type2 == LINETYPE)
2458  {
2459  return lwpoly_covers_lwline((LWPOLY*)lwgeom1, (LWLINE*)lwgeom2);
2460  }
2461  else if ( type1 == POLYGONTYPE && type2 == POLYGONTYPE)
2462  {
2463  return lwpoly_covers_lwpoly((LWPOLY*)lwgeom1, (LWPOLY*)lwgeom2);
2464  }
2465  else if ( type1 == LINETYPE && type2 == POINTTYPE)
2466  {
2467  return lwline_covers_lwpoint((LWLINE*)lwgeom1, (LWPOINT*)lwgeom2);
2468  }
2469  else if ( type1 == LINETYPE && type2 == LINETYPE)
2470  {
2471  return lwline_covers_lwline((LWLINE*)lwgeom1, (LWLINE*)lwgeom2);
2472  }
2473  else if ( type1 == POINTTYPE && type2 == POINTTYPE)
2474  {
2475  return lwpoint_same((LWPOINT*)lwgeom1, (LWPOINT*)lwgeom2);
2476  }
2477 
2478  /* If any of the first argument parts covers the second argument, it's true */
2479  if ( lwtype_is_collection( type1 ) )
2480  {
2481  uint32_t i;
2482  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom1;
2483 
2484  for ( i = 0; i < col->ngeoms; i++ )
2485  {
2486  if ( lwgeom_covers_lwgeom_sphere(col->geoms[i], lwgeom2) )
2487  {
2488  return LW_TRUE;
2489  }
2490  }
2491  return LW_FALSE;
2492  }
2493 
2494  /* Only if all of the second arguments are covered by the first argument is the condition true */
2495  if ( lwtype_is_collection( type2 ) )
2496  {
2497  uint32_t i;
2498  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom2;
2499 
2500  for ( i = 0; i < col->ngeoms; i++ )
2501  {
2502  if ( ! lwgeom_covers_lwgeom_sphere(lwgeom1, col->geoms[i]) )
2503  {
2504  return LW_FALSE;
2505  }
2506  }
2507  return LW_TRUE;
2508  }
2509 
2510  /* Don't get here */
2511  lwerror("lwgeom_covers_lwgeom_sphere: reached end of function without resolution");
2512  return LW_FALSE;
2513 
2514 }
#define LW_FALSE
Definition: liblwgeom.h:108
#define LINETYPE
Definition: liblwgeom.h:117
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:343
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition: lwgeom.c:1088
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
#define POLYGONTYPE
Definition: liblwgeom.h:118
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:107
char lwpoint_same(const LWPOINT *p1, const LWPOINT *p2)
Definition: lwpoint.c:264
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:2521
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:2747
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:2605
int lwgeom_calculate_gbox_geodetic(const LWGEOM *geom, GBOX *gbox)
Calculate the geodetic bounding box for an LWGEOM.
Definition: lwgeodetic.c:3018
int lwpoly_covers_lwline(const LWPOLY *poly, const LWLINE *line)
Definition: lwgeodetic.c:2650
int lwgeom_covers_lwgeom_sphere(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
Calculate covers predicate for two lwgeoms on the sphere.
Definition: lwgeodetic.c:2416
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:2776
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
lwflags_t flags
Definition: liblwgeom.h:367
uint32_t ngeoms
Definition: liblwgeom.h:594
LWGEOM ** geoms
Definition: liblwgeom.h:589
uint8_t type
Definition: liblwgeom.h:476
GBOX * bbox
Definition: liblwgeom.h:472

References LWGEOM::bbox, GBOX::flags, LWCOLLECTION::geoms, getPoint2d_p(), LINETYPE, LW_FALSE, LW_TRUE, LWDEBUG, lwerror(), lwgeom_calculate_gbox_geodetic(), lwgeom_covers_lwgeom_sphere(), 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_coveredby(), geography_covers(), and lwgeom_covers_lwgeom_sphere().

Here is the call graph for this function:
Here is the caller graph for this function: