PostGIS  2.1.10dev-r@@SVN_REVISION@@
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 2384 of file lwgeodetic.c.

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

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

2385 {
2386  int i;
2387  int in_hole_count = 0;
2388  POINT3D p;
2389  GEOGRAPHIC_POINT gpt_to_test;
2390  POINT2D pt_outside;
2391  GBOX gbox;
2392  gbox.flags = 0;
2393 
2394  /* Nulls and empties don't contain anything! */
2395  if ( ! poly || lwgeom_is_empty((LWGEOM*)poly) )
2396  {
2397  LWDEBUG(4,"returning false, geometry is empty or null");
2398  return LW_FALSE;
2399  }
2400 
2401  /* Make sure we have boxes */
2402  if ( poly->bbox )
2403  gbox = *(poly->bbox);
2404  else
2405  lwgeom_calculate_gbox_geodetic((LWGEOM*)poly, &gbox);
2406 
2407  /* Point not in box? Done! */
2408  geographic_point_init(pt_to_test->x, pt_to_test->y, &gpt_to_test);
2409  geog2cart(&gpt_to_test, &p);
2410  if ( ! gbox_contains_point3d(&gbox, &p) )
2411  {
2412  LWDEBUG(4, "the point is not in the box!");
2413  return LW_FALSE;
2414  }
2415 
2416  /* Calculate our outside point from the gbox */
2417  gbox_pt_outside(&gbox, &pt_outside);
2418 
2419  LWDEBUGF(4, "pt_outside POINT(%.18g %.18g)", pt_outside.x, pt_outside.y);
2420  LWDEBUGF(4, "pt_to_test POINT(%.18g %.18g)", pt_to_test->x, pt_to_test->y);
2421  LWDEBUGF(4, "polygon %s", lwgeom_to_ewkt((LWGEOM*)poly));
2422  LWDEBUGF(4, "gbox %s", gbox_to_string(&gbox));
2423 
2424  /* Not in outer ring? We're done! */
2425  if ( ! ptarray_contains_point_sphere(poly->rings[0], &pt_outside, pt_to_test) )
2426  {
2427  LWDEBUG(4,"returning false, point is outside ring");
2428  return LW_FALSE;
2429  }
2430 
2431  LWDEBUGF(4, "testing %d rings", poly->nrings);
2432 
2433  /* But maybe point is in a hole... */
2434  for ( i = 1; i < poly->nrings; i++ )
2435  {
2436  LWDEBUGF(4, "ring test loop %d", i);
2437  /* Count up hole containment. Odd => outside boundary. */
2438  if ( ptarray_contains_point_sphere(poly->rings[i], &pt_outside, pt_to_test) )
2439  in_hole_count++;
2440  }
2441 
2442  LWDEBUGF(4, "in_hole_count == %d", in_hole_count);
2443 
2444  if ( in_hole_count % 2 )
2445  {
2446  LWDEBUG(4,"returning false, inner ring containment count is odd");
2447  return LW_FALSE;
2448  }
2449 
2450  LWDEBUG(4,"returning true, inner ring containment count is even");
2451  return LW_TRUE;
2452 }
char * gbox_to_string(const GBOX *gbox)
Allocate a string representation of the GBOX, based on dimensionality of flags.
Definition: g_box.c:328
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:425
int gbox_contains_point3d(const GBOX *gbox, const POINT3D *pt)
Return true if the point is inside the gbox.
Definition: g_box.c:205
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
GBOX * bbox
Definition: liblwgeom.h:409
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:33
double x
Definition: liblwgeom.h:284
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:3201
#define LW_FALSE
Definition: liblwgeom.h:52
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:51
int lwgeom_calculate_gbox_geodetic(const LWGEOM *geom, GBOX *gbox)
Calculate the geodetic bounding box for an LWGEOM.
Definition: lwgeodetic.c:2612
POINTARRAY ** rings
Definition: liblwgeom.h:413
int nrings
Definition: liblwgeom.h:411
double y
Definition: liblwgeom.h:284
uint8_t flags
Definition: liblwgeom.h:247
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesion coordinates on unit sphere.
Definition: lwgeodetic.c:355
void gbox_pt_outside(const GBOX *gbox, POINT2D *pt_outside)
Given a unit geocentric gbox, return a lon/lat (degrees) coordinate point point that is guaranteed to...
Definition: lwgeodetic.c:1443
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition: lwgeodetic.c:157
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1229
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55

Here is the call graph for this function:

Here is the caller graph for this function: