PostGIS  2.5.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 2431 of file lwgeodetic.c.

References LWGEOM::bbox, GBOX::flags, gbox_contains_point3d(), gbox_pt_outside(), gbox_to_string(), geog2cart(), geographic_point_init(), LW_FALSE, LW_TRUE, LWDEBUG, LWDEBUGF, lwfree(), lwgeom_calculate_gbox_geodetic(), lwgeom_is_empty(), lwgeom_to_ewkt(), ptarray_contains_point_sphere(), 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().

2432 {
2433  uint32_t i;
2434  int in_hole_count = 0;
2435  POINT3D p;
2436  GEOGRAPHIC_POINT gpt_to_test;
2437  POINT2D pt_outside;
2438  GBOX gbox;
2439 #if POSTGIS_DEBUG_LEVEL >= 4
2440  char *geom_ewkt;
2441 #endif
2442  gbox.flags = 0;
2443 
2444  /* Nulls and empties don't contain anything! */
2445  if ( ! poly || lwgeom_is_empty((LWGEOM*)poly) )
2446  {
2447  LWDEBUG(4,"returning false, geometry is empty or null");
2448  return LW_FALSE;
2449  }
2450 
2451  /* Make sure we have boxes */
2452  if ( poly->bbox )
2453  gbox = *(poly->bbox);
2454  else
2455  lwgeom_calculate_gbox_geodetic((LWGEOM*)poly, &gbox);
2456 
2457  /* Point not in box? Done! */
2458  geographic_point_init(pt_to_test->x, pt_to_test->y, &gpt_to_test);
2459  geog2cart(&gpt_to_test, &p);
2460  if ( ! gbox_contains_point3d(&gbox, &p) )
2461  {
2462  LWDEBUG(4, "the point is not in the box!");
2463  return LW_FALSE;
2464  }
2465 
2466  /* Calculate our outside point from the gbox */
2467  gbox_pt_outside(&gbox, &pt_outside);
2468 
2469  LWDEBUGF(4, "pt_outside POINT(%.18g %.18g)", pt_outside.x, pt_outside.y);
2470  LWDEBUGF(4, "pt_to_test POINT(%.18g %.18g)", pt_to_test->x, pt_to_test->y);
2471 #if POSTGIS_DEBUG_LEVEL >= 4
2472  geom_ewkt = lwgeom_to_ewkt((LWGEOM*)poly);
2473  LWDEBUGF(4, "polygon %s", geom_ewkt);
2474  lwfree(geom_ewkt);
2475  geom_ewkt = gbox_to_string(&gbox);
2476  LWDEBUGF(4, "gbox %s", geom_ewkt);
2477  lwfree(geom_ewkt);
2478 #endif
2479 
2480  /* Not in outer ring? We're done! */
2481  if ( ! ptarray_contains_point_sphere(poly->rings[0], &pt_outside, pt_to_test) )
2482  {
2483  LWDEBUG(4,"returning false, point is outside ring");
2484  return LW_FALSE;
2485  }
2486 
2487  LWDEBUGF(4, "testing %d rings", poly->nrings);
2488 
2489  /* But maybe point is in a hole... */
2490  for ( i = 1; i < poly->nrings; i++ )
2491  {
2492  LWDEBUGF(4, "ring test loop %d", i);
2493  /* Count up hole containment. Odd => outside boundary. */
2494  if ( ptarray_contains_point_sphere(poly->rings[i], &pt_outside, pt_to_test) )
2495  in_hole_count++;
2496  }
2497 
2498  LWDEBUGF(4, "in_hole_count == %d", in_hole_count);
2499 
2500  if ( in_hole_count % 2 )
2501  {
2502  LWDEBUG(4,"returning false, inner ring containment count is odd");
2503  return LW_FALSE;
2504  }
2505 
2506  LWDEBUG(4,"returning true, inner ring containment count is even");
2507  return LW_TRUE;
2508 }
GBOX * bbox
Definition: liblwgeom.h:400
void lwfree(void *mem)
Definition: lwutil.c:244
char * gbox_to_string(const GBOX *gbox)
Allocate a string representation of the GBOX, based on dimensionality of flags.
Definition: g_box.c:399
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:556
int gbox_contains_point3d(const GBOX *gbox, const POINT3D *pt)
Return true if the point is inside the gbox.
Definition: g_box.c:254
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:47
unsigned int uint32_t
Definition: uthash.h:78
double x
Definition: liblwgeom.h:330
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:3529
#define LW_FALSE
Definition: liblwgeom.h:76
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
int lwgeom_calculate_gbox_geodetic(const LWGEOM *geom, GBOX *gbox)
Calculate the geodetic bounding box for an LWGEOM.
Definition: lwgeodetic.c:2940
double y
Definition: liblwgeom.h:330
uint8_t flags
Definition: liblwgeom.h:293
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesian coordinates on unit sphere.
Definition: lwgeodetic.c:373
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:1465
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition: lwgeodetic.c:171
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:1393
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
Here is the call graph for this function:
Here is the caller graph for this function: