PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ _lwt_AddPoint()

static LWT_ELEMID _lwt_AddPoint ( LWT_TOPOLOGY topo,
LWPOINT point,
double  tol,
int  findFace,
int *  moved 
)
static

Definition at line 5093 of file lwgeom_topo.c.

5095 {
5096  uint64_t num, i;
5097  double mindist = FLT_MAX;
5098  LWT_ISO_NODE *nodes, *nodes2;
5099  LWT_ISO_EDGE *edges, *edges2;
5100  LWGEOM *pt = lwpoint_as_lwgeom(point);
5101  int flds;
5102  LWT_ELEMID id = 0;
5103  scored_pointer *sorted;
5104 
5105  /* Get tolerance, if 0 was given */
5106  if (!tol)
5107  tol = _LWT_MINTOLERANCE(topo, pt);
5108 
5109  LWDEBUGG(1, pt, "Adding point");
5110 
5111  /*
5112  -- 1. Check if any existing node is closer than the given precision
5113  -- and if so pick the closest
5114  TODO: use WithinBox2D
5115  */
5117  nodes = lwt_be_getNodeWithinDistance2D(topo, point, tol, &num, flds, 0);
5118  if (num == UINT64_MAX)
5119  {
5120  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5121  return -1;
5122  }
5123  if ( num )
5124  {
5125  LWDEBUGF(1, "New point is within %.15g units of %d nodes", tol, num);
5126  /* Order by distance if there are more than a single return */
5127  if ( num > 1 )
5128  {{
5129  sorted= lwalloc(sizeof(scored_pointer)*num);
5130  for (i=0; i<num; ++i)
5131  {
5132  sorted[i].ptr = nodes+i;
5133  sorted[i].score = lwgeom_mindistance2d(lwpoint_as_lwgeom(nodes[i].geom), pt);
5134  LWDEBUGF(1, "Node %" LWTFMT_ELEMID " distance: %.15g",
5135  ((LWT_ISO_NODE*)(sorted[i].ptr))->node_id, sorted[i].score);
5136  }
5137  qsort(sorted, num, sizeof(scored_pointer), compare_scored_pointer);
5138  nodes2 = lwalloc(sizeof(LWT_ISO_NODE)*num);
5139  for (i=0; i<num; ++i)
5140  {
5141  nodes2[i] = *((LWT_ISO_NODE*)sorted[i].ptr);
5142  }
5143  lwfree(sorted);
5144  lwfree(nodes);
5145  nodes = nodes2;
5146  }}
5147 
5148  for ( i=0; i<num; ++i )
5149  {
5150  LWT_ISO_NODE *n = &(nodes[i]);
5151  LWGEOM *g = lwpoint_as_lwgeom(n->geom);
5152  double dist = lwgeom_mindistance2d(g, pt);
5153  /* TODO: move this check in the previous sort scan ... */
5154  /* must be closer than tolerated, unless distance is zero */
5155  if ( dist && dist >= tol ) continue;
5156  if ( ! id || dist < mindist )
5157  {
5158  id = n->node_id;
5159  mindist = dist;
5160  }
5161  }
5162  if ( id )
5163  {
5164  /* found an existing node */
5165  if ( nodes ) _lwt_release_nodes(nodes, num);
5166  if ( moved ) *moved = mindist == 0 ? 0 : 1;
5167  return id;
5168  }
5169  }
5170 
5171  initGEOS(lwnotice, lwgeom_geos_error);
5172 
5173  /*
5174  -- 2. Check if any existing edge falls within tolerance
5175  -- and if so split it by a point projected on it
5176  TODO: use WithinBox2D
5177  */
5179  edges = lwt_be_getEdgeWithinDistance2D(topo, point, tol, &num, flds, 0);
5180  if (num == UINT64_MAX)
5181  {
5182  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5183  return -1;
5184  }
5185  if ( num )
5186  {
5187  LWDEBUGF(1, "New point is within %.15g units of %d edges", tol, num);
5188 
5189  /* Order by distance if there are more than a single return */
5190  if ( num > 1 )
5191  {{
5192  int j;
5193  sorted = lwalloc(sizeof(scored_pointer)*num);
5194  for (i=0; i<num; ++i)
5195  {
5196  sorted[i].ptr = edges+i;
5197  sorted[i].score = lwgeom_mindistance2d(lwline_as_lwgeom(edges[i].geom), pt);
5198  LWDEBUGF(1, "Edge %" LWTFMT_ELEMID " distance: %.15g",
5199  ((LWT_ISO_EDGE*)(sorted[i].ptr))->edge_id, sorted[i].score);
5200  }
5201  qsort(sorted, num, sizeof(scored_pointer), compare_scored_pointer);
5202  edges2 = lwalloc(sizeof(LWT_ISO_EDGE)*num);
5203  for (j=0, i=0; i<num; ++i)
5204  {
5205  if ( sorted[i].score == sorted[0].score )
5206  {
5207  edges2[j++] = *((LWT_ISO_EDGE*)sorted[i].ptr);
5208  }
5209  else
5210  {
5211  lwline_free(((LWT_ISO_EDGE*)sorted[i].ptr)->geom);
5212  }
5213  }
5214  num = j;
5215  lwfree(sorted);
5216  lwfree(edges);
5217  edges = edges2;
5218  }}
5219 
5220  for (i=0; i<num; ++i)
5221  {
5222  /* The point is on or near an edge, split the edge */
5223  LWT_ISO_EDGE *e = &(edges[i]);
5224  LWGEOM *g = lwline_as_lwgeom(e->geom);
5225  LWGEOM *prj;
5226  int contains;
5227  LWT_ELEMID edge_id = e->edge_id;
5228 
5229  LWDEBUGF(1, "Splitting edge %" LWTFMT_ELEMID, edge_id);
5230 
5231  /* project point to line, split edge by point */
5232  prj = lwgeom_closest_point(g, pt);
5233  if ( moved ) *moved = lwgeom_same(prj,pt) ? 0 : 1;
5234  if ( lwgeom_has_z(pt) )
5235  {{
5236  /*
5237  -- This is a workaround for ClosestPoint lack of Z support:
5238  -- http://trac.osgeo.org/postgis/ticket/2033
5239  */
5240  LWGEOM *tmp;
5241  double z;
5242  POINT4D p4d;
5243  LWPOINT *prjpt;
5244  /* add Z to "prj" */
5245  tmp = lwgeom_force_3dz(prj, 0);
5246  prjpt = lwgeom_as_lwpoint(tmp);
5247  getPoint4d_p(point->point, 0, &p4d);
5248  z = p4d.z;
5249  getPoint4d_p(prjpt->point, 0, &p4d);
5250  p4d.z = z;
5251  ptarray_set_point4d(prjpt->point, 0, &p4d);
5252  lwgeom_free(prj);
5253  prj = tmp;
5254  }}
5255  const POINT2D *pt = getPoint2d_cp(lwgeom_as_lwpoint(prj)->point, 0);
5257  if ( ! contains )
5258  {{
5259  double snaptol;
5260  LWGEOM *snapedge;
5261  LWLINE *snapline;
5262  POINT4D p1, p2;
5263 
5264  LWDEBUGF(1, "Edge %" LWTFMT_ELEMID
5265  " does not contain projected point to it",
5266  edge_id);
5267 
5268  /* In order to reduce the robustness issues, we'll pick
5269  * an edge that contains the projected point, if possible */
5270  if ( i+1 < num )
5271  {
5272  LWDEBUG(1, "But there's another to check");
5273  lwgeom_free(prj);
5274  continue;
5275  }
5276 
5277  /*
5278  -- The tolerance must be big enough for snapping to happen
5279  -- and small enough to snap only to the projected point.
5280  -- Unfortunately ST_Distance returns 0 because it also uses
5281  -- a projected point internally, so we need another way.
5282  */
5283  snaptol = _lwt_minTolerance(prj);
5284  snapedge = _lwt_toposnap(g, prj, snaptol);
5285  snapline = lwgeom_as_lwline(snapedge);
5286 
5287  LWDEBUGF(1, "Edge snapped with tolerance %g", snaptol);
5288 
5289  /* TODO: check if snapping did anything ? */
5290 #if POSTGIS_DEBUG_LEVEL > 0
5291  {
5292  size_t sz;
5293  char *wkt1 = lwgeom_to_wkt(g, WKT_EXTENDED, 15, &sz);
5294  char *wkt2 = lwgeom_to_wkt(snapedge, WKT_EXTENDED, 15, &sz);
5295  LWDEBUGF(1, "Edge %s snapped became %s", wkt1, wkt2);
5296  lwfree(wkt1);
5297  lwfree(wkt2);
5298  }
5299 #endif
5300 
5301 
5302  /*
5303  -- Snapping currently snaps the first point below tolerance
5304  -- so may possibly move first point. See ticket #1631
5305  */
5306  getPoint4d_p(e->geom->points, 0, &p1);
5307  getPoint4d_p(snapline->points, 0, &p2);
5308  LWDEBUGF(1, "Edge first point is %g %g, "
5309  "snapline first point is %g %g",
5310  p1.x, p1.y, p2.x, p2.y);
5311  if ( p1.x != p2.x || p1.y != p2.y )
5312  {
5313  LWDEBUG(1, "Snapping moved first point, re-adding it");
5314  if ( LW_SUCCESS != ptarray_insert_point(snapline->points, &p1, 0) )
5315  {
5316  lwgeom_free(prj);
5317  lwgeom_free(snapedge);
5318  _lwt_release_edges(edges, num);
5319  lwerror("GEOS exception on Contains: %s", lwgeom_geos_errmsg);
5320  return -1;
5321  }
5322 #if POSTGIS_DEBUG_LEVEL > 0
5323  {
5324  size_t sz;
5325  char *wkt1 = lwgeom_to_wkt(g, WKT_EXTENDED, 15, &sz);
5326  LWDEBUGF(1, "Tweaked snapline became %s", wkt1);
5327  lwfree(wkt1);
5328  }
5329 #endif
5330  }
5331 #if POSTGIS_DEBUG_LEVEL > 0
5332  else {
5333  LWDEBUG(1, "Snapping did not move first point");
5334  }
5335 #endif
5336 
5337  if ( -1 == lwt_ChangeEdgeGeom( topo, edge_id, snapline ) )
5338  {
5339  /* TODO: should have invoked lwerror already, leaking memory */
5340  lwgeom_free(prj);
5341  lwgeom_free(snapedge);
5342  _lwt_release_edges(edges, num);
5343  lwerror("lwt_ChangeEdgeGeom failed");
5344  return -1;
5345  }
5346  lwgeom_free(snapedge);
5347  }}
5348 #if POSTGIS_DEBUG_LEVEL > 0
5349  else
5350  {{
5351  size_t sz;
5352  char *wkt1 = lwgeom_to_wkt(g, WKT_EXTENDED, 15, &sz);
5353  char *wkt2 = lwgeom_to_wkt(prj, WKT_EXTENDED, 15, &sz);
5354  LWDEBUGF(1, "Edge %s contains projected point %s", wkt1, wkt2);
5355  lwfree(wkt1);
5356  lwfree(wkt2);
5357  }}
5358 #endif
5359 
5360  /* TODO: pass 1 as last argument (skipChecks) ? */
5361  id = lwt_ModEdgeSplit( topo, edge_id, lwgeom_as_lwpoint(prj), 0 );
5362  if ( -1 == id )
5363  {
5364  /* TODO: should have invoked lwerror already, leaking memory */
5365  lwgeom_free(prj);
5366  _lwt_release_edges(edges, num);
5367  lwerror("lwt_ModEdgeSplit failed");
5368  return -1;
5369  }
5370 
5371  lwgeom_free(prj);
5372 
5373  /*
5374  * TODO: decimate the two new edges with the given tolerance ?
5375  *
5376  * the edge identifiers to decimate would be: edge_id and "id"
5377  * The problem here is that decimation of existing edges
5378  * may introduce intersections or topological inconsistencies,
5379  * for example:
5380  *
5381  * - A node may end up falling on the other side of the edge
5382  * - The decimated edge might intersect another existing edge
5383  *
5384  */
5385 
5386  break; /* we only want to snap a single edge */
5387  }
5388  _lwt_release_edges(edges, num);
5389  }
5390  else
5391  {
5392  /* The point is isolated, add it as such */
5393  /* TODO: pass 1 as last argument (skipChecks) ? */
5394  id = _lwt_AddIsoNode(topo, -1, point, 0, findFace);
5395  if ( moved ) *moved = 0;
5396  if ( -1 == id )
5397  {
5398  /* should have invoked lwerror already, leaking memory */
5399  lwerror("lwt_AddIsoNode failed");
5400  return -1;
5401  }
5402  }
5403 
5404  return id;
5405 }
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
void lwgeom_geos_error(const char *fmt,...)
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:179
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:339
char lwgeom_same(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
geom1 same as geom2 iff
Definition: lwgeom.c:591
LWGEOM * lwgeom_closest_point(const LWGEOM *lw1, const LWGEOM *lw2)
Definition: measures.c:55
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
#define WKT_EXTENDED
Definition: liblwgeom.h:2168
#define LW_SUCCESS
Definition: liblwgeom.h:112
LWGEOM * lwgeom_force_3dz(const LWGEOM *geom, double zval)
Definition: lwgeom.c:799
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:934
int ptarray_insert_point(POINTARRAY *pa, const POINT4D *p, uint32_t where)
Insert a point into an existing POINTARRAY.
Definition: ptarray.c:85
void lwfree(void *mem)
Definition: lwutil.c:242
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:344
double lwgeom_mindistance2d(const LWGEOM *lw1, const LWGEOM *lw2)
Function initializing min distance calculation.
Definition: measures.c:200
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:126
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:708
void * lwalloc(size_t size)
Definition: lwutil.c:227
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
Definition: lwgeom_api.c:370
void lwline_free(LWLINE *line)
Definition: lwline.c:67
int ptarray_contains_point_partial(const POINTARRAY *pa, const POINT2D *pt, int check_closed, int *winding_number)
Definition: ptarray.c:746
#define LW_BOUNDARY
LWT_INT64 LWT_ELEMID
Identifier of topology element.
#define LWT_COL_EDGE_EDGE_ID
Edge fields.
#define LWT_COL_NODE_GEOM
#define LWT_COL_NODE_NODE_ID
Node 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
#define LWDEBUGG(level, geom, msg)
Definition: lwgeom_log.h:93
const char * lwt_be_lastErrorMessage(const LWT_BE_IFACE *be)
Definition: lwgeom_topo.c:119
LWT_ELEMID lwt_ModEdgeSplit(LWT_TOPOLOGY *topo, LWT_ELEMID edge, LWPOINT *pt, int skipISOChecks)
Split an edge by a node, modifying the original edge and adding a new one.
Definition: lwgeom_topo.c:1063
static LWT_ELEMID _lwt_AddIsoNode(LWT_TOPOLOGY *topo, LWT_ELEMID face, LWPOINT *pt, int skipISOChecks, int checkFace)
Definition: lwgeom_topo.c:531
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:475
static double _lwt_minTolerance(LWGEOM *g)
Definition: lwgeom_topo.c:5046
LWT_ISO_NODE * lwt_be_getNodeWithinDistance2D(LWT_TOPOLOGY *topo, LWPOINT *pt, double dist, uint64_t *numelems, int fields, int64_t limit)
Definition: lwgeom_topo.c:161
static int compare_scored_pointer(const void *si1, const void *si2)
Definition: lwgeom_topo.c:5073
#define _LWT_MINTOLERANCE(topo, geom)
Definition: lwgeom_topo.c:5064
static LWGEOM * _lwt_toposnap(LWGEOM *src, LWGEOM *tgt, double tol)
Definition: lwgeom_topo.c:428
#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
int lwt_ChangeEdgeGeom(LWT_TOPOLOGY *topo, LWT_ELEMID edge_id, LWLINE *geom)
Changes the shape of an edge without affecting the topology structure.
Definition: lwgeom_topo.c:3266
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
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:101
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwinline.h:131
Datum contains(PG_FUNCTION_ARGS)
POINTARRAY * points
Definition: liblwgeom.h:498
POINTARRAY * point
Definition: liblwgeom.h:486
LWLINE * geom
LWT_ELEMID edge_id
LWT_ELEMID node_id
LWPOINT * geom
const LWT_BE_IFACE * be_iface
double x
Definition: liblwgeom.h:429
double z
Definition: liblwgeom.h:429
double y
Definition: liblwgeom.h:429

References _lwt_AddIsoNode(), _lwt_minTolerance(), _LWT_MINTOLERANCE, _lwt_release_edges(), _lwt_release_nodes(), _lwt_toposnap(), LWT_TOPOLOGY_T::be_iface, compare_scored_pointer(), contains(), LWT_ISO_EDGE::edge_id, LWT_ISO_NODE::geom, LWT_ISO_EDGE::geom, getPoint2d_cp(), getPoint4d_p(), LW_BOUNDARY, LW_SUCCESS, lwalloc(), LWDEBUG, LWDEBUGF, LWDEBUGG, lwerror(), lwfree(), lwgeom_as_lwline(), lwgeom_as_lwpoint(), lwgeom_closest_point(), lwgeom_force_3dz(), lwgeom_free(), lwgeom_geos_errmsg, lwgeom_geos_error(), lwgeom_has_z(), lwgeom_mindistance2d(), lwgeom_same(), lwgeom_to_wkt(), lwline_as_lwgeom(), lwline_free(), lwnotice(), lwpoint_as_lwgeom(), lwt_be_getEdgeWithinDistance2D(), lwt_be_getNodeWithinDistance2D(), lwt_be_lastErrorMessage(), lwt_ChangeEdgeGeom(), LWT_COL_EDGE_EDGE_ID, LWT_COL_EDGE_GEOM, LWT_COL_NODE_GEOM, LWT_COL_NODE_NODE_ID, lwt_ModEdgeSplit(), LWTFMT_ELEMID, LWT_ISO_NODE::node_id, LWPOINT::point, LWLINE::points, ptarray_contains_point_partial(), ptarray_insert_point(), ptarray_set_point4d(), scored_pointer_t::ptr, scored_pointer_t::score, WKT_EXTENDED, POINT4D::x, POINT4D::y, and POINT4D::z.

Referenced by _lwt_AddLineEdge(), and lwt_AddPoint().

Here is the call graph for this function:
Here is the caller graph for this function: