PostGIS  2.2.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 4850 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.

4851 {
4852  LWT_ELEMID id = 0;
4853  LWT_ISO_EDGE *elem;
4854  int num, i;
4855  int flds = LWT_COL_EDGE_EDGE_ID |
4859  LWGEOM *qp = lwpoint_as_lwgeom(pt);
4860 
4861  id = lwt_be_getFaceContainingPoint(topo, pt);
4862  if ( id == -2 ) {
4863  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
4864  return -1;
4865  }
4866 
4867  if ( id > 0 )
4868  {
4869  return id;
4870  }
4871  id = 0; /* or it'll be -1 for not found */
4872 
4873  LWDEBUG(1, "No face properly contains query point,"
4874  " looking for edges");
4875 
4876  /* Not in a face, may be in universe or on edge, let's check
4877  * for distance */
4878  /* NOTE: we never pass a tolerance of 0 to avoid ever using
4879  * ST_Within, which doesn't include endpoints matches */
4880  elem = lwt_be_getEdgeWithinDistance2D(topo, pt, tol?tol:1e-5, &num, flds, 0);
4881  if ( num == -1 )
4882  {
4883  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
4884  return -1;
4885  }
4886  for (i=0; i<num; ++i)
4887  {
4888  LWT_ISO_EDGE *e = &(elem[i]);
4889  LWT_ELEMID eface = 0;
4890  LWGEOM* geom;
4891  double dist;
4892 
4893  if ( ! e->geom )
4894  {
4895  _lwt_release_edges(elem, num);
4896  lwnotice("Corrupted topology: edge %" LWTFMT_ELEMID
4897  " has null geometry", e->edge_id);
4898  continue;
4899  }
4900 
4901  /* don't consider dangling edges */
4902  if ( e->face_left == e->face_right )
4903  {
4904  LWDEBUGF(1, "Edge %" LWTFMT_ELEMID
4905  " is dangling, won't consider it", e->edge_id);
4906  continue;
4907  }
4908 
4909  geom = lwline_as_lwgeom(e->geom);
4910  dist = lwgeom_mindistance2d_tolerance(geom, qp, tol);
4911 
4912  LWDEBUGF(1, "Distance from edge %" LWTFMT_ELEMID
4913  " is %g (tol=%g)", e->edge_id, dist, tol);
4914 
4915  /* we won't consider edges too far */
4916  if ( dist > tol ) continue;
4917  if ( e->face_left == 0 ) {
4918  eface = e->face_right;
4919  }
4920  else if ( e->face_right == 0 ) {
4921  eface = e->face_left;
4922  }
4923  else {
4924  _lwt_release_edges(elem, num);
4925  lwerror("Two or more faces found");
4926  return -1;
4927  }
4928 
4929  if ( id && id != eface )
4930  {
4931  _lwt_release_edges(elem, num);
4932  lwerror("Two or more faces found"
4933 #if 0 /* debugging */
4934  " (%" LWTFMT_ELEMID
4935  " and %" LWTFMT_ELEMID ")", id, eface
4936 #endif
4937  );
4938  return -1;
4939  }
4940  else id = eface;
4941  }
4942  if ( num ) _lwt_release_edges(elem, num);
4943 
4944  return id;
4945 }
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:61
double lwgeom_mindistance2d_tolerance(const LWGEOM *lw1, const LWGEOM *lw2, double tolerance)
Function handling min distance calculations and dwithin calculations.
Definition: measures.c:199
#define LWT_COL_EDGE_FACE_RIGHT
LWLINE * geom
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:249
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:471
LWT_ISO_EDGE * lwt_be_getEdgeWithinDistance2D(LWT_TOPOLOGY *topo, LWPOINT *pt, double dist, int *numelems, int fields, int limit)
Definition: lwgeom_topo.c:264
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:254
LWT_INT64 LWT_ELEMID
Identifier of topology element.
#define LWT_COL_EDGE_GEOM
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
LWT_ELEMID lwt_be_getFaceContainingPoint(LWT_TOPOLOGY *topo, LWPOINT *pt)
Definition: lwgeom_topo.c:334
const char * lwt_be_lastErrorMessage(const LWT_BE_IFACE *be)
Definition: lwgeom_topo.c:124
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:36
Here is the call graph for this function: