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 5315 of file lwgeom_topo.c.

5317 {
5318  LWCOLLECTION *col;
5319  LWPOINT *start_point, *end_point;
5320  LWGEOM *tmp = 0, *tmp2;
5321  LWT_ISO_NODE *node;
5322  LWT_ELEMID nid[2]; /* start_node, end_node */
5323  LWT_ELEMID id; /* edge id */
5324  POINT4D p4d;
5325  uint64_t nn, i;
5326  int moved=0, mm;
5327 
5328  LWDEBUGG(1, lwline_as_lwgeom(edge), "_lwtAddLineEdge");
5329  LWDEBUGF(1, "_lwtAddLineEdge with tolerance %g", tol);
5330 
5331  start_point = lwline_get_lwpoint(edge, 0);
5332  if ( ! start_point )
5333  {
5334  lwnotice("Empty component of noded line");
5335  return 0; /* must be empty */
5336  }
5337  nid[0] = _lwt_AddPoint( topo, start_point,
5338  _lwt_minTolerance(lwpoint_as_lwgeom(start_point)),
5339  handleFaceSplit, &mm );
5340  lwpoint_free(start_point); /* too late if lwt_AddPoint calls lwerror */
5341  if ( nid[0] == -1 ) return -1; /* lwerror should have been called */
5342  moved += mm;
5343 
5344 
5345  end_point = lwline_get_lwpoint(edge, edge->points->npoints-1);
5346  if ( ! end_point )
5347  {
5348  lwerror("could not get last point of line "
5349  "after successfully getting first point !?");
5350  return -1;
5351  }
5352  nid[1] = _lwt_AddPoint( topo, end_point,
5354  handleFaceSplit, &mm );
5355  moved += mm;
5356  lwpoint_free(end_point); /* too late if lwt_AddPoint calls lwerror */
5357  if ( nid[1] == -1 ) return -1; /* lwerror should have been called */
5358 
5359  /*
5360  -- Added endpoints may have drifted due to tolerance, so
5361  -- we need to re-snap the edge to the new nodes before adding it
5362  */
5363  if ( moved )
5364  {
5365 
5366  nn = nid[0] == nid[1] ? 1 : 2;
5367  node = lwt_be_getNodeById( topo, nid, &nn,
5369  if (nn == UINT64_MAX)
5370  {
5371  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5372  return -1;
5373  }
5374  start_point = NULL; end_point = NULL;
5375  for (i=0; i<nn; ++i)
5376  {
5377  if ( node[i].node_id == nid[0] ) start_point = node[i].geom;
5378  if ( node[i].node_id == nid[1] ) end_point = node[i].geom;
5379  }
5380  if ( ! start_point || ! end_point )
5381  {
5382  if ( nn ) _lwt_release_nodes(node, nn);
5383  lwerror("Could not find just-added nodes % " LWTFMT_ELEMID
5384  " and %" LWTFMT_ELEMID, nid[0], nid[1]);
5385  return -1;
5386  }
5387 
5388  /* snap */
5389 
5390  getPoint4d_p( start_point->point, 0, &p4d );
5391  lwline_setPoint4d(edge, 0, &p4d);
5392 
5393  getPoint4d_p( end_point->point, 0, &p4d );
5394  lwline_setPoint4d(edge, edge->points->npoints-1, &p4d);
5395 
5396  if ( nn ) _lwt_release_nodes(node, nn);
5397 
5398  /* make valid, after snap (to handle collapses) */
5399  tmp = lwgeom_make_valid(lwline_as_lwgeom(edge));
5400 
5401  col = lwgeom_as_lwcollection(tmp);
5402  if ( col )
5403  {{
5404 
5405  col = lwcollection_extract(col, LINETYPE);
5406 
5407  /* Check if the so-snapped edge collapsed (see #1650) */
5408  if ( col->ngeoms == 0 )
5409  {
5410  lwcollection_free(col);
5411  lwgeom_free(tmp);
5412  LWDEBUG(1, "Made-valid snapped edge collapsed");
5413  return 0;
5414  }
5415 
5416  tmp2 = lwgeom_clone_deep( col->geoms[0] );
5417  lwgeom_free(tmp);
5418  tmp = tmp2;
5419  edge = lwgeom_as_lwline(tmp);
5420  lwcollection_free(col);
5421  if ( ! edge )
5422  {
5423  /* should never happen */
5424  lwerror("lwcollection_extract(LINETYPE) returned a non-line?");
5425  return -1;
5426  }
5427  }}
5428  else
5429  {
5430  edge = lwgeom_as_lwline(tmp);
5431  if ( ! edge )
5432  {
5433  LWDEBUGF(1, "Made-valid snapped edge collapsed to %s",
5435  lwgeom_free(tmp);
5436  return 0;
5437  }
5438  }
5439  }
5440 
5441  /* check if the so-snapped edge _now_ exists */
5442  id = _lwt_GetEqualEdge ( topo, edge );
5443  LWDEBUGF(1, "_lwt_GetEqualEdge returned %" LWTFMT_ELEMID, id);
5444  if ( id == -1 )
5445  {
5446  if ( tmp ) lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5447  return -1;
5448  }
5449  if ( id )
5450  {
5451  if ( tmp ) lwgeom_free(tmp); /* possibly takes "edge" down with it */
5452  return id;
5453  }
5454 
5455  /* No previously existing edge was found, we'll add one */
5456 
5457  /* Remove consecutive vertices below given tolerance
5458  * on edge addition */
5459  if ( tol )
5460  {{
5461  tmp2 = lwline_remove_repeated_points(edge, tol);
5462  LWDEBUGG(1, tmp2, "Repeated-point removed");
5463  edge = lwgeom_as_lwline(tmp2);
5464  if ( tmp ) lwgeom_free(tmp);
5465  tmp = tmp2;
5466 
5467  /* check if the so-decimated edge collapsed to single-point */
5468  if ( nid[0] == nid[1] && edge->points->npoints == 2 )
5469  {
5470  lwgeom_free(tmp);
5471  LWDEBUG(1, "Repeated-point removed edge collapsed");
5472  return 0;
5473  }
5474 
5475  /* check if the so-decimated edge _now_ exists */
5476  id = _lwt_GetEqualEdge ( topo, edge );
5477  LWDEBUGF(1, "_lwt_GetEqualEdge returned %" LWTFMT_ELEMID, id);
5478  if ( id == -1 )
5479  {
5480  lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5481  return -1;
5482  }
5483  if ( id )
5484  {
5485  lwgeom_free(tmp); /* takes "edge" down with it */
5486  return id;
5487  }
5488  }}
5489 
5490 
5491  /* TODO: skip checks ? */
5492  id = _lwt_AddEdge( topo, nid[0], nid[1], edge, 0, handleFaceSplit ? 1 : -1 );
5493  LWDEBUGF(1, "lwt_AddEdgeModFace returned %" LWTFMT_ELEMID, id);
5494  if ( id == -1 )
5495  {
5496  lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5497  return -1;
5498  }
5499  lwgeom_free(tmp); /* possibly takes "edge" down with it */
5500 
5501  return id;
5502 }
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:4870
static LWT_ELEMID _lwt_GetEqualEdge(LWT_TOPOLOGY *topo, LWLINE *edge)
Definition: lwgeom_topo.c:5241
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:4917
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: