PostGIS  2.5.7dev-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 4738 of file lwgeom_topo.c.

4739 {
4740  LWT_ELEMID id = 0;
4741  LWT_ISO_EDGE *elem;
4742  int num, i;
4743  int flds = LWT_COL_EDGE_EDGE_ID |
4747  LWGEOM *qp = lwpoint_as_lwgeom(pt);
4748 
4749  id = lwt_be_getFaceContainingPoint(topo, pt);
4750  if ( id == -2 ) {
4751  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
4752  return -1;
4753  }
4754 
4755  if ( id > 0 )
4756  {
4757  return id;
4758  }
4759  id = 0; /* or it'll be -1 for not found */
4760 
4761  LWDEBUG(1, "No face properly contains query point,"
4762  " looking for edges");
4763 
4764  /* Not in a face, may be in universe or on edge, let's check
4765  * for distance */
4766  /* NOTE: we never pass a tolerance of 0 to avoid ever using
4767  * ST_Within, which doesn't include endpoints matches */
4768  elem = lwt_be_getEdgeWithinDistance2D(topo, pt, tol?tol:1e-5, &num, flds, 0);
4769  if ( num == -1 )
4770  {
4771  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
4772  return -1;
4773  }
4774  for (i=0; i<num; ++i)
4775  {
4776  LWT_ISO_EDGE *e = &(elem[i]);
4777  LWT_ELEMID eface = 0;
4778  LWGEOM* geom;
4779  double dist;
4780 
4781  if ( ! e->geom )
4782  {
4783  _lwt_release_edges(elem, num);
4784  lwnotice("Corrupted topology: edge %" LWTFMT_ELEMID
4785  " has null geometry", e->edge_id);
4786  continue;
4787  }
4788 
4789  /* don't consider dangling edges */
4790  if ( e->face_left == e->face_right )
4791  {
4792  LWDEBUGF(1, "Edge %" LWTFMT_ELEMID
4793  " is dangling, won't consider it", e->edge_id);
4794  continue;
4795  }
4796 
4797  geom = lwline_as_lwgeom(e->geom);
4798  dist = lwgeom_mindistance2d_tolerance(geom, qp, tol);
4799 
4800  LWDEBUGF(1, "Distance from edge %" LWTFMT_ELEMID
4801  " is %g (tol=%g)", e->edge_id, dist, tol);
4802 
4803  /* we won't consider edges too far */
4804  if ( dist > tol ) continue;
4805  if ( e->face_left == 0 ) {
4806  eface = e->face_right;
4807  }
4808  else if ( e->face_right == 0 ) {
4809  eface = e->face_left;
4810  }
4811  else {
4812  _lwt_release_edges(elem, num);
4813  lwerror("Two or more faces found");
4814  return -1;
4815  }
4816 
4817  if ( id && id != eface )
4818  {
4819  _lwt_release_edges(elem, num);
4820  lwerror("Two or more faces found"
4821 #if 0 /* debugging */
4822  " (%" LWTFMT_ELEMID
4823  " and %" LWTFMT_ELEMID ")", id, eface
4824 #endif
4825  );
4826  return -1;
4827  }
4828  else id = eface;
4829  }
4830  if ( num ) _lwt_release_edges(elem, num);
4831 
4832  return id;
4833 }
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:330
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:335
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
LWT_INT64 LWT_ELEMID
Identifier of topology element.
#define LWT_COL_EDGE_FACE_LEFT
#define LWT_COL_EDGE_EDGE_ID
Edge fields.
#define LWT_COL_EDGE_GEOM
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
const char * lwt_be_lastErrorMessage(const LWT_BE_IFACE *be)
Definition: lwgeom_topo.c:120
LWT_ELEMID lwt_be_getFaceContainingPoint(LWT_TOPOLOGY *topo, LWPOINT *pt)
Definition: lwgeom_topo.c:330
LWT_ISO_EDGE * lwt_be_getEdgeWithinDistance2D(LWT_TOPOLOGY *topo, LWPOINT *pt, double dist, int *numelems, int fields, int limit)
Definition: lwgeom_topo.c:260
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:44
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:457
LWT_ELEMID face_right
LWT_ELEMID face_left
LWLINE * geom
LWT_ELEMID edge_id
const LWT_BE_IFACE * be_iface

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.

Here is the call graph for this function: