PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ lwpoly_covers_point2d()

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 outside point (lon/lat decimal degrees) (calculate with gbox_pt_outside()) return LW_TRUE if point is inside or on edge of polygon.

Definition at line 2535 of file lwgeodetic.c.

2536 {
2537  uint32_t i;
2538  int in_hole_count = 0;
2539  POINT3D p;
2540  GEOGRAPHIC_POINT gpt_to_test;
2541  POINT2D pt_outside;
2542  GBOX gbox;
2543 #if POSTGIS_DEBUG_LEVEL >= 4
2544  char *geom_ewkt;
2545 #endif
2546  gbox.flags = 0;
2547 
2548  /* Nulls and empties don't contain anything! */
2549  if ( ! poly || lwgeom_is_empty((LWGEOM*)poly) )
2550  {
2551  LWDEBUG(4,"returning false, geometry is empty or null");
2552  return LW_FALSE;
2553  }
2554 
2555  /* Make sure we have boxes */
2556  if ( poly->bbox )
2557  gbox = *(poly->bbox);
2558  else
2559  lwgeom_calculate_gbox_geodetic((LWGEOM*)poly, &gbox);
2560 
2561  /* Point not in box? Done! */
2562  geographic_point_init(pt_to_test->x, pt_to_test->y, &gpt_to_test);
2563  geog2cart(&gpt_to_test, &p);
2564  if ( ! gbox_contains_point3d(&gbox, &p) )
2565  {
2566  LWDEBUG(4, "the point is not in the box!");
2567  return LW_FALSE;
2568  }
2569 
2570  /* Calculate our outside point from the gbox */
2571  lwpoly_pt_outside(poly, &pt_outside);
2572 
2573  LWDEBUGF(4, "pt_outside POINT(%.18g %.18g)", pt_outside.x, pt_outside.y);
2574  LWDEBUGF(4, "pt_to_test POINT(%.18g %.18g)", pt_to_test->x, pt_to_test->y);
2575 #if POSTGIS_DEBUG_LEVEL >= 4
2576  geom_ewkt = lwgeom_to_ewkt((LWGEOM*)poly);
2577  LWDEBUGF(4, "polygon %s", geom_ewkt);
2578  lwfree(geom_ewkt);
2579  geom_ewkt = gbox_to_string(&gbox);
2580  LWDEBUGF(4, "gbox %s", geom_ewkt);
2581  lwfree(geom_ewkt);
2582 #endif
2583 
2584  /* Not in outer ring? We're done! */
2585  if ( ! ptarray_contains_point_sphere(poly->rings[0], &pt_outside, pt_to_test) )
2586  {
2587  LWDEBUG(4,"returning false, point is outside ring");
2588  return LW_FALSE;
2589  }
2590 
2591  LWDEBUGF(4, "testing %d rings", poly->nrings);
2592 
2593  /* But maybe point is in a hole... */
2594  for ( i = 1; i < poly->nrings; i++ )
2595  {
2596  LWDEBUGF(4, "ring test loop %d", i);
2597  /* Count up hole containment. Odd => outside boundary. */
2598  if ( ptarray_contains_point_sphere(poly->rings[i], &pt_outside, pt_to_test) )
2599  in_hole_count++;
2600  }
2601 
2602  LWDEBUGF(4, "in_hole_count == %d", in_hole_count);
2603 
2604  if ( in_hole_count % 2 )
2605  {
2606  LWDEBUG(4,"returning false, inner ring containment count is odd");
2607  return LW_FALSE;
2608  }
2609 
2610  LWDEBUG(4,"returning true, inner ring containment count is even");
2611  return LW_TRUE;
2612 }
int gbox_contains_point3d(const GBOX *gbox, const POINT3D *pt)
Return true if the point is inside the gbox.
Definition: gbox.c:247
char * gbox_to_string(const GBOX *gbox)
Allocate a string representation of the GBOX, based on dimensionality of flags.
Definition: gbox.c:392
#define LW_FALSE
Definition: liblwgeom.h:94
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:565
void lwfree(void *mem)
Definition: lwutil.c:242
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
int ptarray_contains_point_sphere(const POINTARRAY *pa, const POINT2D *pt_outside, const POINT2D *pt_to_test)
This routine returns LW_TRUE if the stabline joining the pt_outside and pt_to_test crosses the ring a...
Definition: lwgeodetic.c:3646
int lwgeom_calculate_gbox_geodetic(const LWGEOM *geom, GBOX *gbox)
Calculate the geodetic bounding box for an LWGEOM.
Definition: lwgeodetic.c:3027
int lwpoly_pt_outside(const LWPOLY *poly, POINT2D *pt_outside)
Definition: lwgeodetic.c:1533
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition: lwgeodetic.c:180
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesian coordinates on unit sphere.
Definition: lwgeodetic.c:404
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
lwflags_t flags
Definition: liblwgeom.h:353
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:54
GBOX * bbox
Definition: liblwgeom.h:458
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390

References LWPOLY::bbox, GBOX::flags, gbox_contains_point3d(), gbox_to_string(), geog2cart(), geographic_point_init(), LW_FALSE, LW_TRUE, LWDEBUG, LWDEBUGF, lwfree(), lwgeom_calculate_gbox_geodetic(), lwgeom_is_empty(), lwgeom_to_ewkt(), lwpoly_pt_outside(), LWPOLY::nrings, ptarray_contains_point_sphere(), LWPOLY::rings, POINT2D::x, and POINT2D::y.

Referenced by lwgeom_covers_lwgeom_sphere(), lwgeom_distance_spheroid(), lwpoly_covers_pointarray(), test_lwpoly_covers_point2d(), and test_tree_circ_pip2().

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