PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ _lwt_AddLineEdge()

static LWT_ELEMID _lwt_AddLineEdge ( LWT_TOPOLOGY topo,
LWLINE edge,
double  tol,
int  handleFaceSplit 
)
static

Definition at line 5317 of file lwgeom_topo.c.

5319 {
5320  LWCOLLECTION *col;
5321  LWPOINT *start_point, *end_point;
5322  LWGEOM *tmp = 0, *tmp2;
5323  LWT_ISO_NODE *node;
5324  LWT_ELEMID nid[2]; /* start_node, end_node */
5325  LWT_ELEMID id; /* edge id */
5326  POINT4D p4d;
5327  uint64_t nn, i;
5328  int moved=0, mm;
5329 
5330  LWDEBUGG(1, lwline_as_lwgeom(edge), "_lwtAddLineEdge");
5331  LWDEBUGF(1, "_lwtAddLineEdge with tolerance %g", tol);
5332 
5333  start_point = lwline_get_lwpoint(edge, 0);
5334  if ( ! start_point )
5335  {
5336  lwnotice("Empty component of noded line");
5337  return 0; /* must be empty */
5338  }
5339  nid[0] = _lwt_AddPoint( topo, start_point,
5340  _lwt_minTolerance(lwpoint_as_lwgeom(start_point)),
5341  handleFaceSplit, &mm );
5342  lwpoint_free(start_point); /* too late if lwt_AddPoint calls lwerror */
5343  if ( nid[0] == -1 ) return -1; /* lwerror should have been called */
5344  moved += mm;
5345 
5346 
5347  end_point = lwline_get_lwpoint(edge, edge->points->npoints-1);
5348  if ( ! end_point )
5349  {
5350  lwerror("could not get last point of line "
5351  "after successfully getting first point !?");
5352  return -1;
5353  }
5354  nid[1] = _lwt_AddPoint( topo, end_point,
5356  handleFaceSplit, &mm );
5357  moved += mm;
5358  lwpoint_free(end_point); /* too late if lwt_AddPoint calls lwerror */
5359  if ( nid[1] == -1 ) return -1; /* lwerror should have been called */
5360 
5361  /*
5362  -- Added endpoints may have drifted due to tolerance, so
5363  -- we need to re-snap the edge to the new nodes before adding it
5364  */
5365  if ( moved )
5366  {
5367 
5368  nn = nid[0] == nid[1] ? 1 : 2;
5369  node = lwt_be_getNodeById( topo, nid, &nn,
5371  if (nn == UINT64_MAX)
5372  {
5373  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5374  return -1;
5375  }
5376  start_point = NULL; end_point = NULL;
5377  for (i=0; i<nn; ++i)
5378  {
5379  if ( node[i].node_id == nid[0] ) start_point = node[i].geom;
5380  if ( node[i].node_id == nid[1] ) end_point = node[i].geom;
5381  }
5382  if ( ! start_point || ! end_point )
5383  {
5384  if ( nn ) _lwt_release_nodes(node, nn);
5385  lwerror("Could not find just-added nodes % " LWTFMT_ELEMID
5386  " and %" LWTFMT_ELEMID, nid[0], nid[1]);
5387  return -1;
5388  }
5389 
5390  /* snap */
5391 
5392  getPoint4d_p( start_point->point, 0, &p4d );
5393  lwline_setPoint4d(edge, 0, &p4d);
5394 
5395  getPoint4d_p( end_point->point, 0, &p4d );
5396  lwline_setPoint4d(edge, edge->points->npoints-1, &p4d);
5397 
5398  if ( nn ) _lwt_release_nodes(node, nn);
5399 
5400  /* make valid, after snap (to handle collapses) */
5401  tmp = lwgeom_make_valid(lwline_as_lwgeom(edge));
5402 
5403  col = lwgeom_as_lwcollection(tmp);
5404  if ( col )
5405  {{
5406 
5407  col = lwcollection_extract(col, LINETYPE);
5408 
5409  /* Check if the so-snapped edge collapsed (see #1650) */
5410  if ( col->ngeoms == 0 )
5411  {
5412  lwcollection_free(col);
5413  lwgeom_free(tmp);
5414  LWDEBUG(1, "Made-valid snapped edge collapsed");
5415  return 0;
5416  }
5417 
5418  tmp2 = lwgeom_clone_deep( col->geoms[0] );
5419  lwgeom_free(tmp);
5420  tmp = tmp2;
5421  edge = lwgeom_as_lwline(tmp);
5422  lwcollection_free(col);
5423  if ( ! edge )
5424  {
5425  /* should never happen */
5426  lwerror("lwcollection_extract(LINETYPE) returned a non-line?");
5427  return -1;
5428  }
5429  }}
5430  else
5431  {
5432  edge = lwgeom_as_lwline(tmp);
5433  if ( ! edge )
5434  {
5435  LWDEBUGF(1, "Made-valid snapped edge collapsed to %s",
5437  lwgeom_free(tmp);
5438  return 0;
5439  }
5440  }
5441  }
5442 
5443  /* check if the so-snapped edge _now_ exists */
5444  id = _lwt_GetEqualEdge ( topo, edge );
5445  LWDEBUGF(1, "_lwt_GetEqualEdge returned %" LWTFMT_ELEMID, id);
5446  if ( id == -1 )
5447  {
5448  if ( tmp ) lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5449  return -1;
5450  }
5451  if ( id )
5452  {
5453  if ( tmp ) lwgeom_free(tmp); /* possibly takes "edge" down with it */
5454  return id;
5455  }
5456 
5457  /* No previously existing edge was found, we'll add one */
5458 
5459  /* Remove consecutive vertices below given tolerance
5460  * on edge addition */
5461  if ( tol )
5462  {{
5463  tmp2 = lwline_remove_repeated_points(edge, tol);
5464  LWDEBUGG(1, tmp2, "Repeated-point removed");
5465  edge = lwgeom_as_lwline(tmp2);
5466  if ( tmp ) lwgeom_free(tmp);
5467  tmp = tmp2;
5468 
5469  /* check if the so-decimated edge collapsed to single-point */
5470  if ( nid[0] == nid[1] && edge->points->npoints == 2 )
5471  {
5472  lwgeom_free(tmp);
5473  LWDEBUG(1, "Repeated-point removed edge collapsed");
5474  return 0;
5475  }
5476 
5477  /* check if the so-decimated edge _now_ exists */
5478  id = _lwt_GetEqualEdge ( topo, edge );
5479  LWDEBUGF(1, "_lwt_GetEqualEdge returned %" LWTFMT_ELEMID, id);
5480  if ( id == -1 )
5481  {
5482  lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5483  return -1;
5484  }
5485  if ( id )
5486  {
5487  lwgeom_free(tmp); /* takes "edge" down with it */
5488  return id;
5489  }
5490  }}
5491 
5492 
5493  /* TODO: skip checks ? */
5494  id = _lwt_AddEdge( topo, nid[0], nid[1], edge, 0, handleFaceSplit ? 1 : -1 );
5495  LWDEBUGF(1, "lwt_AddEdgeModFace returned %" LWTFMT_ELEMID, id);
5496  if ( id == -1 )
5497  {
5498  lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5499  return -1;
5500  }
5501  lwgeom_free(tmp); /* possibly takes "edge" down with it */
5502 
5503  return id;
5504 }
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:161
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:321
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
#define LINETYPE
Definition: liblwgeom.h:117
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:511
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:326
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:357
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:125
void lwline_setPoint4d(LWLINE *line, uint32_t which, POINT4D *newpoint)
Definition: lwline.c:364
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
Definition: lwcollection.c:387
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:215
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
LWPOINT * lwline_get_lwpoint(const LWLINE *line, uint32_t where)
Returns freshly allocated LWPOINT that corresponds to the index where.
Definition: lwline.c:309
LWGEOM * lwline_remove_repeated_points(const LWLINE *in, double tolerance)
Definition: lwline.c:439
LWT_INT64 LWT_ELEMID
Identifier of topology element.
#define LWT_COL_NODE_GEOM
#define LWT_COL_NODE_NODE_ID
Node fields.
#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
static LWT_ELEMID _lwt_AddEdge(LWT_TOPOLOGY *topo, LWT_ELEMID start_node, LWT_ELEMID end_node, LWLINE *geom, int skipChecks, int modFace)
Definition: lwgeom_topo.c:2267
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:461
static double _lwt_minTolerance(LWGEOM *g)
Definition: lwgeom_topo.c:4872
static LWT_ELEMID _lwt_GetEqualEdge(LWT_TOPOLOGY *topo, LWLINE *edge)
Definition: lwgeom_topo.c:5243
LWT_ISO_NODE * lwt_be_getNodeById(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t *numelems, int fields)
Definition: lwgeom_topo.c:155
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:43
static LWT_ELEMID _lwt_AddPoint(LWT_TOPOLOGY *topo, LWPOINT *point, double tol, int findFace, int *moved)
Definition: lwgeom_topo.c:4919
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwinline.h:135
uint32_t ngeoms
Definition: liblwgeom.h:566
LWGEOM ** geoms
Definition: liblwgeom.h:561
POINTARRAY * points
Definition: liblwgeom.h:469
POINTARRAY * point
Definition: liblwgeom.h:457
LWPOINT * geom
const LWT_BE_IFACE * be_iface
uint32_t npoints
Definition: liblwgeom.h:413

References _lwt_AddEdge(), _lwt_AddPoint(), _lwt_GetEqualEdge(), _lwt_minTolerance(), _lwt_release_nodes(), LWT_TOPOLOGY_T::be_iface, LWT_ISO_NODE::geom, LWCOLLECTION::geoms, getPoint4d_p(), LINETYPE, lwcollection_extract(), lwcollection_free(), LWDEBUG, LWDEBUGF, LWDEBUGG, lwerror(), lwgeom_as_lwcollection(), lwgeom_as_lwline(), lwgeom_clone_deep(), lwgeom_free(), lwgeom_get_type(), lwgeom_make_valid(), lwline_as_lwgeom(), lwline_get_lwpoint(), lwline_remove_repeated_points(), lwline_setPoint4d(), lwnotice(), lwpoint_as_lwgeom(), lwpoint_free(), lwt_be_getNodeById(), lwt_be_lastErrorMessage(), LWT_COL_NODE_GEOM, LWT_COL_NODE_NODE_ID, LWTFMT_ELEMID, lwtype_name(), LWCOLLECTION::ngeoms, POINTARRAY::npoints, LWPOINT::point, and LWLINE::points.

Referenced by _lwt_AddLine().

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