PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ lwt_GetFaceByPoint()

LWT_ELEMID lwt_GetFaceByPoint ( LWT_TOPOLOGY topo,
LWPOINT pt,
double  tol 
)

Find the face-id of a face containing a given point.

Parameters
topothe topology to operate on
pointthe point to use for query
tolmax distance around the given point to look for a containing face
Returns
a face identifier if one is found (0 if universe), -1 on error (multiple faces within distance or point on node or edge). The liblwgeom error handler will be invoked in case of error.

Definition at line 4880 of file lwgeom_topo.c.

References _lwt_release_edges(), LWT_TOPOLOGY_T::be_iface, LWT_ISO_EDGE::edge_id, LWT_ISO_EDGE::face_left, LWT_ISO_EDGE::face_right, LWT_ISO_EDGE::geom, LWDEBUG, LWDEBUGF, lwerror(), lwgeom_mindistance2d_tolerance(), lwline_as_lwgeom(), lwnotice(), lwpoint_as_lwgeom(), lwt_be_getEdgeWithinDistance2D(), lwt_be_getFaceContainingPoint(), lwt_be_lastErrorMessage(), LWT_COL_EDGE_EDGE_ID, LWT_COL_EDGE_FACE_LEFT, LWT_COL_EDGE_FACE_RIGHT, LWT_COL_EDGE_GEOM, and LWTFMT_ELEMID.

4881 {
4882  LWT_ELEMID id = 0;
4883  LWT_ISO_EDGE *elem;
4884  int num, i;
4885  int flds = LWT_COL_EDGE_EDGE_ID |
4889  LWGEOM *qp = lwpoint_as_lwgeom(pt);
4890 
4891  id = lwt_be_getFaceContainingPoint(topo, pt);
4892  if ( id == -2 ) {
4893  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
4894  return -1;
4895  }
4896 
4897  if ( id > 0 )
4898  {
4899  return id;
4900  }
4901  id = 0; /* or it'll be -1 for not found */
4902 
4903  LWDEBUG(1, "No face properly contains query point,"
4904  " looking for edges");
4905 
4906  /* Not in a face, may be in universe or on edge, let's check
4907  * for distance */
4908  /* NOTE: we never pass a tolerance of 0 to avoid ever using
4909  * ST_Within, which doesn't include endpoints matches */
4910  elem = lwt_be_getEdgeWithinDistance2D(topo, pt, tol?tol:1e-5, &num, flds, 0);
4911  if ( num == -1 )
4912  {
4913  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
4914  return -1;
4915  }
4916  for (i=0; i<num; ++i)
4917  {
4918  LWT_ISO_EDGE *e = &(elem[i]);
4919  LWT_ELEMID eface = 0;
4920  LWGEOM* geom;
4921  double dist;
4922 
4923  if ( ! e->geom )
4924  {
4925  _lwt_release_edges(elem, num);
4926  lwnotice("Corrupted topology: edge %" LWTFMT_ELEMID
4927  " has null geometry", e->edge_id);
4928  continue;
4929  }
4930 
4931  /* don't consider dangling edges */
4932  if ( e->face_left == e->face_right )
4933  {
4934  LWDEBUGF(1, "Edge %" LWTFMT_ELEMID
4935  " is dangling, won't consider it", e->edge_id);
4936  continue;
4937  }
4938 
4939  geom = lwline_as_lwgeom(e->geom);
4940  dist = lwgeom_mindistance2d_tolerance(geom, qp, tol);
4941 
4942  LWDEBUGF(1, "Distance from edge %" LWTFMT_ELEMID
4943  " is %g (tol=%g)", e->edge_id, dist, tol);
4944 
4945  /* we won't consider edges too far */
4946  if ( dist > tol ) continue;
4947  if ( e->face_left == 0 ) {
4948  eface = e->face_right;
4949  }
4950  else if ( e->face_right == 0 ) {
4951  eface = e->face_left;
4952  }
4953  else {
4954  _lwt_release_edges(elem, num);
4955  lwerror("Two or more faces found");
4956  return -1;
4957  }
4958 
4959  if ( id && id != eface )
4960  {
4961  _lwt_release_edges(elem, num);
4962  lwerror("Two or more faces found"
4963 #if 0 /* debugging */
4964  " (%" LWTFMT_ELEMID
4965  " and %" LWTFMT_ELEMID ")", id, eface
4966 #endif
4967  );
4968  return -1;
4969  }
4970  else id = eface;
4971  }
4972  if ( num ) _lwt_release_edges(elem, num);
4973 
4974  return id;
4975 }
LWT_ELEMID face_left
#define LWT_COL_EDGE_FACE_LEFT
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:89
double lwgeom_mindistance2d_tolerance(const LWGEOM *lw1, const LWGEOM *lw2, double tolerance)
Function handling min distance calculations and dwithin calculations.
Definition: measures.c:213
#define LWT_COL_EDGE_FACE_RIGHT
LWLINE * geom
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:262
const LWT_BE_IFACE * be_iface
LWT_ELEMID face_right
LWT_ELEMID edge_id
#define LWT_COL_EDGE_EDGE_ID
Edge fields.
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:467
LWT_ISO_EDGE * lwt_be_getEdgeWithinDistance2D(LWT_TOPOLOGY *topo, LWPOINT *pt, double dist, int *numelems, int fields, int limit)
Definition: lwgeom_topo.c:260
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:267
LWT_INT64 LWT_ELEMID
Identifier of topology element.
#define LWT_COL_EDGE_GEOM
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
LWT_ELEMID lwt_be_getFaceContainingPoint(LWT_TOPOLOGY *topo, LWPOINT *pt)
Definition: lwgeom_topo.c:330
const char * lwt_be_lastErrorMessage(const LWT_BE_IFACE *be)
Definition: lwgeom_topo.c:120
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:102
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:44
Here is the call graph for this function: