PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ _lwt_AddLineEdge()

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

Definition at line 5426 of file lwgeom_topo.c.

References _lwt_GetEqualEdge(), _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_free(), lwt_AddEdgeModFace(), lwt_AddPoint(), 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().

5427 {
5428  LWCOLLECTION *col;
5429  LWPOINT *start_point, *end_point;
5430  LWGEOM *tmp, *tmp2;
5431  LWT_ISO_NODE *node;
5432  LWT_ELEMID nid[2]; /* start_node, end_node */
5433  LWT_ELEMID id; /* edge id */
5434  POINT4D p4d;
5435  int nn, i;
5436 
5437  LWDEBUGG(1, lwline_as_lwgeom(edge), "_lwtAddLineEdge");
5438  LWDEBUGF(1, "_lwtAddLineEdge with tolerance %g", tol);
5439 
5440  start_point = lwline_get_lwpoint(edge, 0);
5441  if ( ! start_point )
5442  {
5443  lwnotice("Empty component of noded line");
5444  return 0; /* must be empty */
5445  }
5446  nid[0] = lwt_AddPoint( topo, start_point, tol );
5447  lwpoint_free(start_point); /* too late if lwt_AddPoint calls lwerror */
5448  if ( nid[0] == -1 ) return -1; /* lwerror should have been called */
5449 
5450  end_point = lwline_get_lwpoint(edge, edge->points->npoints-1);
5451  if ( ! end_point )
5452  {
5453  lwerror("could not get last point of line "
5454  "after successfully getting first point !?");
5455  return -1;
5456  }
5457  nid[1] = lwt_AddPoint( topo, end_point, tol );
5458  lwpoint_free(end_point); /* too late if lwt_AddPoint calls lwerror */
5459  if ( nid[1] == -1 ) return -1; /* lwerror should have been called */
5460 
5461  /*
5462  -- Added endpoints may have drifted due to tolerance, so
5463  -- we need to re-snap the edge to the new nodes before adding it
5464  */
5465 
5466  nn = nid[0] == nid[1] ? 1 : 2;
5467  node = lwt_be_getNodeById( topo, nid, &nn,
5469  if ( nn == -1 )
5470  {
5471  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5472  return -1;
5473  }
5474  start_point = NULL; end_point = NULL;
5475  for (i=0; i<nn; ++i)
5476  {
5477  if ( node[i].node_id == nid[0] ) start_point = node[i].geom;
5478  if ( node[i].node_id == nid[1] ) end_point = node[i].geom;
5479  }
5480  if ( ! start_point || ! end_point )
5481  {
5482  if ( nn ) _lwt_release_nodes(node, nn);
5483  lwerror("Could not find just-added nodes % " LWTFMT_ELEMID
5484  " and %" LWTFMT_ELEMID, nid[0], nid[1]);
5485  return -1;
5486  }
5487 
5488  /* snap */
5489 
5490  getPoint4d_p( start_point->point, 0, &p4d );
5491  lwline_setPoint4d(edge, 0, &p4d);
5492 
5493  getPoint4d_p( end_point->point, 0, &p4d );
5494  lwline_setPoint4d(edge, edge->points->npoints-1, &p4d);
5495 
5496  if ( nn ) _lwt_release_nodes(node, nn);
5497 
5498  /* make valid, after snap (to handle collapses) */
5499  tmp = lwgeom_make_valid(lwline_as_lwgeom(edge));
5500 
5501  col = lwgeom_as_lwcollection(tmp);
5502  if ( col )
5503  {{
5504 
5505  col = lwcollection_extract(col, LINETYPE);
5506 
5507  /* Check if the so-snapped edge collapsed (see #1650) */
5508  if ( col->ngeoms == 0 )
5509  {
5510  lwcollection_free(col);
5511  lwgeom_free(tmp);
5512  LWDEBUG(1, "Made-valid snapped edge collapsed");
5513  return 0;
5514  }
5515 
5516  tmp2 = lwgeom_clone_deep( col->geoms[0] );
5517  lwgeom_free(tmp);
5518  tmp = tmp2;
5519  edge = lwgeom_as_lwline(tmp);
5520  lwcollection_free(col);
5521  if ( ! edge )
5522  {
5523  /* should never happen */
5524  lwerror("lwcollection_extract(LINETYPE) returned a non-line?");
5525  return -1;
5526  }
5527  }}
5528  else
5529  {
5530  edge = lwgeom_as_lwline(tmp);
5531  if ( ! edge )
5532  {
5533  LWDEBUGF(1, "Made-valid snapped edge collapsed to %s",
5535  lwgeom_free(tmp);
5536  return 0;
5537  }
5538  }
5539 
5540  /* check if the so-snapped edge _now_ exists */
5541  id = _lwt_GetEqualEdge ( topo, edge );
5542  LWDEBUGF(1, "_lwt_GetEqualEdge returned %" LWTFMT_ELEMID, id);
5543  if ( id == -1 )
5544  {
5545  lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5546  return -1;
5547  }
5548  if ( id )
5549  {
5550  lwgeom_free(tmp); /* possibly takes "edge" down with it */
5551  return id;
5552  }
5553 
5554  /* No previously existing edge was found, we'll add one */
5555 
5556  /* Remove consecutive vertices below given tolerance
5557  * on edge addition */
5558  if ( tol )
5559  {{
5560  tmp2 = lwline_remove_repeated_points(edge, tol);
5561  LWDEBUGG(1, tmp2, "Repeated-point removed");
5562  edge = lwgeom_as_lwline(tmp2);
5563  lwgeom_free(tmp);
5564  tmp = tmp2;
5565 
5566  /* check if the so-decimated edge _now_ exists */
5567  id = _lwt_GetEqualEdge ( topo, edge );
5568  LWDEBUGF(1, "_lwt_GetEqualEdge returned %" LWTFMT_ELEMID, id);
5569  if ( id == -1 )
5570  {
5571  lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5572  return -1;
5573  }
5574  if ( id )
5575  {
5576  lwgeom_free(tmp); /* takes "edge" down with it */
5577  return id;
5578  }
5579  }}
5580 
5581 
5582  /* TODO: skip checks ? */
5583  id = lwt_AddEdgeModFace( topo, nid[0], nid[1], edge, 0 );
5584  LWDEBUGF(1, "lwt_AddEdgeModFace returned %" LWTFMT_ELEMID, id);
5585  if ( id == -1 )
5586  {
5587  lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5588  return -1;
5589  }
5590  lwgeom_free(tmp); /* possibly takes "edge" down with it */
5591 
5592  return id;
5593 }
static LWT_ELEMID _lwt_GetEqualEdge(LWT_TOPOLOGY *topo, LWLINE *edge)
Definition: lwgeom_topo.c:5357
#define LINETYPE
Definition: liblwgeom.h:86
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:477
LWT_ELEMID lwt_AddPoint(LWT_TOPOLOGY *topo, LWPOINT *point, double tol)
Adds a point to the topology.
Definition: lwgeom_topo.c:5021
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
Definition: lwcollection.c:369
LWPOINT * geom
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
int npoints
Definition: liblwgeom.h:371
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwgeom.c:878
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:482
POINTARRAY * point
Definition: liblwgeom.h:411
void lwline_setPoint4d(LWLINE *line, uint32_t which, POINT4D *newpoint)
Definition: lwline.c:380
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:298
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
const LWT_BE_IFACE * be_iface
LWGEOM ** geoms
Definition: liblwgeom.h:509
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:138
#define LWDEBUGG(level, geom, msg)
Definition: lwgeom_log.h:93
#define LWT_COL_NODE_NODE_ID
Node fields.
LWGEOM * lwline_remove_repeated_points(const LWLINE *in, double tolerance)
Definition: lwline.c:456
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:192
LWT_ELEMID lwt_AddEdgeModFace(LWT_TOPOLOGY *topo, LWT_ELEMID start_node, LWT_ELEMID end_node, LWLINE *geom, int skipChecks)
Add a new edge possibly splitting a face (modifying it)
Definition: lwgeom_topo.c:2795
LWPOINT * lwline_get_lwpoint(const LWLINE *line, int where)
Returns freshly allocated LWPOINT that corresponds to the index where.
Definition: lwline.c:324
#define LWT_COL_NODE_GEOM
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:340
LWT_INT64 LWT_ELEMID
Identifier of topology element.
LWT_ISO_NODE * lwt_be_getNodeById(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, int *numelems, int fields)
Definition: lwgeom_topo.c:156
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
const char * lwt_be_lastErrorMessage(const LWT_BE_IFACE *be)
Definition: lwgeom_topo.c:120
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
int getPoint4d_p(const POINTARRAY *pa, int n, POINT4D *point)
Definition: lwgeom_api.c:122
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:44
POINTARRAY * points
Definition: liblwgeom.h:422
Here is the call graph for this function:
Here is the caller graph for this function: