PostGIS  3.4.0dev-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 4780 of file lwgeom_topo.c.

4781 {
4782  LWT_ELEMID id = 0;
4783  LWT_ISO_EDGE *elem;
4784  uint64_t num, i;
4785  int flds = LWT_COL_EDGE_EDGE_ID |
4789  LWGEOM *qp = lwpoint_as_lwgeom(pt);
4790 
4791  id = lwt_GetFaceContainingPoint(topo, pt);
4792  if ( id == -1 ) {
4793  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
4794  return -1;
4795  }
4796 
4797  if ( id > 0 )
4798  {
4799  return id;
4800  }
4801 
4802  if ( tol == 0 )
4803  {
4804  return id;
4805  }
4806 
4807  LWDEBUG(1, "No face properly contains query point,"
4808  " looking for edges");
4809 
4810  /* Not in a face, may be in universe or on edge, let's check
4811  * for distance */
4812  /* NOTE: we never pass a tolerance of 0 to avoid ever using
4813  * ST_Within, which doesn't include endpoints matches */
4814  elem = lwt_be_getEdgeWithinDistance2D(topo, pt, tol?tol:1e-5, &num, flds, 0);
4815  if (num == UINT64_MAX)
4816  {
4817  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
4818  return -1;
4819  }
4820  for (i=0; i<num; ++i)
4821  {
4822  LWT_ISO_EDGE *e = &(elem[i]);
4823  LWT_ELEMID eface = 0;
4824  LWGEOM* geom;
4825  double dist;
4826 
4827  if ( ! e->geom )
4828  {
4829  _lwt_release_edges(elem, num);
4830  lwnotice("Corrupted topology: edge %" LWTFMT_ELEMID
4831  " has null geometry", e->edge_id);
4832  continue;
4833  }
4834 
4835  /* don't consider dangling edges */
4836  if ( e->face_left == e->face_right )
4837  {
4838  LWDEBUGF(1, "Edge %" LWTFMT_ELEMID
4839  " is dangling, won't consider it", e->edge_id);
4840  continue;
4841  }
4842 
4843  geom = lwline_as_lwgeom(e->geom);
4844  dist = lwgeom_mindistance2d_tolerance(geom, qp, tol);
4845 
4846  LWDEBUGF(1, "Distance from edge %" LWTFMT_ELEMID
4847  " is %g (tol=%g)", e->edge_id, dist, tol);
4848 
4849  /* we won't consider edges too far */
4850  if ( dist > tol ) continue;
4851  if ( e->face_left == 0 ) {
4852  eface = e->face_right;
4853  }
4854  else if ( e->face_right == 0 ) {
4855  eface = e->face_left;
4856  }
4857  else {
4858  _lwt_release_edges(elem, num);
4859  lwerror("Two or more faces found");
4860  return -1;
4861  }
4862 
4863  if ( id && id != eface )
4864  {
4865  _lwt_release_edges(elem, num);
4866  lwerror("Two or more faces found"
4867 #if 0 /* debugging */
4868  " (%" LWTFMT_ELEMID
4869  " and %" LWTFMT_ELEMID ")", id, eface
4870 #endif
4871  );
4872  return -1;
4873  }
4874  else id = eface;
4875  }
4876  if ( num ) _lwt_release_edges(elem, num);
4877 
4878  return id;
4879 }
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:207
#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:125
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:7034
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:43
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, const LWPOINT *pt, double dist, uint64_t *numelems, int fields, int64_t limit)
Definition: lwgeom_topo.c:256
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: