PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ lwt_GetFaceByPoint()

LWT_ELEMID lwt_GetFaceByPoint ( LWT_TOPOLOGY topo,
const 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 4921 of file lwgeom_topo.c.

4922 {
4923  LWT_ELEMID id = 0;
4924  LWT_ISO_EDGE *elem;
4925  uint64_t num, i;
4926  int flds = LWT_COL_EDGE_EDGE_ID |
4930  LWGEOM *qp = lwpoint_as_lwgeom(pt);
4931 
4932  id = lwt_GetFaceContainingPoint(topo, pt);
4933  if ( id == -1 ) {
4934  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
4935  return -1;
4936  }
4937 
4938  if ( id > 0 )
4939  {
4940  return id;
4941  }
4942 
4943  if ( tol == 0 )
4944  {
4945  return id;
4946  }
4947 
4948  LWDEBUG(1, "No face properly contains query point,"
4949  " looking for edges");
4950 
4951  /* Not in a face, may be in universe or on edge, let's check
4952  * for distance */
4953  /* NOTE: we never pass a tolerance of 0 to avoid ever using
4954  * ST_Within, which doesn't include endpoints matches */
4955  elem = lwt_be_getEdgeWithinDistance2D(topo, pt, tol?tol:1e-5, &num, flds, 0);
4956  if (num == UINT64_MAX)
4957  {
4958  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
4959  return -1;
4960  }
4961  for (i=0; i<num; ++i)
4962  {
4963  LWT_ISO_EDGE *e = &(elem[i]);
4964  LWT_ELEMID eface = 0;
4965  LWGEOM* geom;
4966  double dist;
4967 
4968  if ( ! e->geom )
4969  {
4970  _lwt_release_edges(elem, num);
4971  lwnotice("Corrupted topology: edge %" LWTFMT_ELEMID
4972  " has null geometry", e->edge_id);
4973  continue;
4974  }
4975 
4976  /* don't consider dangling edges */
4977  if ( e->face_left == e->face_right )
4978  {
4979  LWDEBUGF(1, "Edge %" LWTFMT_ELEMID
4980  " is dangling, won't consider it", e->edge_id);
4981  continue;
4982  }
4983 
4984  geom = lwline_as_lwgeom(e->geom);
4985  dist = lwgeom_mindistance2d_tolerance(geom, qp, tol);
4986 
4987  LWDEBUGF(1, "Distance from edge %" LWTFMT_ELEMID
4988  " is %g (tol=%g)", e->edge_id, dist, tol);
4989 
4990  /* we won't consider edges too far */
4991  if ( dist > tol ) continue;
4992  if ( e->face_left == 0 ) {
4993  eface = e->face_right;
4994  }
4995  else if ( e->face_right == 0 ) {
4996  eface = e->face_left;
4997  }
4998  else {
4999  _lwt_release_edges(elem, num);
5000  lwerror("Two or more faces found");
5001  return -1;
5002  }
5003 
5004  if ( id && id != eface )
5005  {
5006  _lwt_release_edges(elem, num);
5007  lwerror("Two or more faces found"
5008 #if 0 /* debugging */
5009  " (%" LWTFMT_ELEMID
5010  " and %" LWTFMT_ELEMID ")", id, eface
5011 #endif
5012  );
5013  return -1;
5014  }
5015  else id = eface;
5016  }
5017  if ( num ) _lwt_release_edges(elem, num);
5018 
5019  return id;
5020 }
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:339
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:344
double lwgeom_mindistance2d_tolerance(const LWGEOM *lw1, const LWGEOM *lw2, double tolerance)
Function handling min distance calculations and dwithin calculations.
Definition: measures.c:210
#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:119
LWT_ELEMID lwt_GetFaceContainingPoint(LWT_TOPOLOGY *topo, const LWPOINT *pt)
Find the face-id of the face properly containing a given point.
Definition: lwgeom_topo.c:7188
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:43
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:465
LWT_ISO_EDGE * lwt_be_getEdgeWithinDistance2D(LWT_TOPOLOGY *topo, const LWPOINT *pt, double dist, uint64_t *numelems, int fields, int64_t limit)
Definition: lwgeom_topo.c:250
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_lastErrorMessage(), LWT_COL_EDGE_EDGE_ID, LWT_COL_EDGE_FACE_LEFT, LWT_COL_EDGE_FACE_RIGHT, LWT_COL_EDGE_GEOM, lwt_GetFaceContainingPoint(), and LWTFMT_ELEMID.

Here is the call graph for this function: