PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ _lwt_AddLineEdge()

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

Definition at line 5303 of file lwgeom_topo.c.

5305 {
5306  LWCOLLECTION *col;
5307  LWPOINT *start_point, *end_point;
5308  LWGEOM *tmp = 0, *tmp2;
5309  LWT_ISO_NODE *node;
5310  LWT_ELEMID nid[2]; /* start_node, end_node */
5311  LWT_ELEMID id; /* edge id */
5312  POINT4D p4d;
5313  int nn, i;
5314  int moved=0, mm;
5315 
5316  LWDEBUGG(1, lwline_as_lwgeom(edge), "_lwtAddLineEdge");
5317  LWDEBUGF(1, "_lwtAddLineEdge with tolerance %g", tol);
5318 
5319  start_point = lwline_get_lwpoint(edge, 0);
5320  if ( ! start_point )
5321  {
5322  lwnotice("Empty component of noded line");
5323  return 0; /* must be empty */
5324  }
5325  nid[0] = _lwt_AddPoint( topo, start_point,
5326  _lwt_minTolerance(lwpoint_as_lwgeom(start_point)),
5327  handleFaceSplit, &mm );
5328  lwpoint_free(start_point); /* too late if lwt_AddPoint calls lwerror */
5329  if ( nid[0] == -1 ) return -1; /* lwerror should have been called */
5330  moved += mm;
5331 
5332 
5333  end_point = lwline_get_lwpoint(edge, edge->points->npoints-1);
5334  if ( ! end_point )
5335  {
5336  lwerror("could not get last point of line "
5337  "after successfully getting first point !?");
5338  return -1;
5339  }
5340  nid[1] = _lwt_AddPoint( topo, end_point,
5342  handleFaceSplit, &mm );
5343  moved += mm;
5344  lwpoint_free(end_point); /* too late if lwt_AddPoint calls lwerror */
5345  if ( nid[1] == -1 ) return -1; /* lwerror should have been called */
5346 
5347  /*
5348  -- Added endpoints may have drifted due to tolerance, so
5349  -- we need to re-snap the edge to the new nodes before adding it
5350  */
5351  if ( moved )
5352  {
5353 
5354  nn = nid[0] == nid[1] ? 1 : 2;
5355  node = lwt_be_getNodeById( topo, nid, &nn,
5357  if ( nn == -1 )
5358  {
5359  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5360  return -1;
5361  }
5362  start_point = NULL; end_point = NULL;
5363  for (i=0; i<nn; ++i)
5364  {
5365  if ( node[i].node_id == nid[0] ) start_point = node[i].geom;
5366  if ( node[i].node_id == nid[1] ) end_point = node[i].geom;
5367  }
5368  if ( ! start_point || ! end_point )
5369  {
5370  if ( nn ) _lwt_release_nodes(node, nn);
5371  lwerror("Could not find just-added nodes % " LWTFMT_ELEMID
5372  " and %" LWTFMT_ELEMID, nid[0], nid[1]);
5373  return -1;
5374  }
5375 
5376  /* snap */
5377 
5378  getPoint4d_p( start_point->point, 0, &p4d );
5379  lwline_setPoint4d(edge, 0, &p4d);
5380 
5381  getPoint4d_p( end_point->point, 0, &p4d );
5382  lwline_setPoint4d(edge, edge->points->npoints-1, &p4d);
5383 
5384  if ( nn ) _lwt_release_nodes(node, nn);
5385 
5386  /* make valid, after snap (to handle collapses) */
5387  tmp = lwgeom_make_valid(lwline_as_lwgeom(edge));
5388 
5389  col = lwgeom_as_lwcollection(tmp);
5390  if ( col )
5391  {{
5392 
5393  col = lwcollection_extract(col, LINETYPE);
5394 
5395  /* Check if the so-snapped edge collapsed (see #1650) */
5396  if ( col->ngeoms == 0 )
5397  {
5398  lwcollection_free(col);
5399  lwgeom_free(tmp);
5400  LWDEBUG(1, "Made-valid snapped edge collapsed");
5401  return 0;
5402  }
5403 
5404  tmp2 = lwgeom_clone_deep( col->geoms[0] );
5405  lwgeom_free(tmp);
5406  tmp = tmp2;
5407  edge = lwgeom_as_lwline(tmp);
5408  lwcollection_free(col);
5409  if ( ! edge )
5410  {
5411  /* should never happen */
5412  lwerror("lwcollection_extract(LINETYPE) returned a non-line?");
5413  return -1;
5414  }
5415  }}
5416  else
5417  {
5418  edge = lwgeom_as_lwline(tmp);
5419  if ( ! edge )
5420  {
5421  LWDEBUGF(1, "Made-valid snapped edge collapsed to %s",
5423  lwgeom_free(tmp);
5424  return 0;
5425  }
5426  }
5427  }
5428 
5429  /* check if the so-snapped edge _now_ exists */
5430  id = _lwt_GetEqualEdge ( topo, edge );
5431  LWDEBUGF(1, "_lwt_GetEqualEdge returned %" LWTFMT_ELEMID, id);
5432  if ( id == -1 )
5433  {
5434  if ( tmp ) lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5435  return -1;
5436  }
5437  if ( id )
5438  {
5439  if ( tmp ) lwgeom_free(tmp); /* possibly takes "edge" down with it */
5440  return id;
5441  }
5442 
5443  /* No previously existing edge was found, we'll add one */
5444 
5445  /* Remove consecutive vertices below given tolerance
5446  * on edge addition */
5447  if ( tol )
5448  {{
5449  tmp2 = lwline_remove_repeated_points(edge, tol);
5450  LWDEBUGG(1, tmp2, "Repeated-point removed");
5451  edge = lwgeom_as_lwline(tmp2);
5452  if ( tmp ) lwgeom_free(tmp);
5453  tmp = tmp2;
5454 
5455  /* check if the so-decimated edge collapsed to single-point */
5456  if ( nid[0] == nid[1] && edge->points->npoints == 2 )
5457  {
5458  lwgeom_free(tmp);
5459  LWDEBUG(1, "Repeated-point removed edge collapsed");
5460  return 0;
5461  }
5462 
5463  /* check if the so-decimated edge _now_ exists */
5464  id = _lwt_GetEqualEdge ( topo, edge );
5465  LWDEBUGF(1, "_lwt_GetEqualEdge returned %" LWTFMT_ELEMID, id);
5466  if ( id == -1 )
5467  {
5468  lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5469  return -1;
5470  }
5471  if ( id )
5472  {
5473  lwgeom_free(tmp); /* takes "edge" down with it */
5474  return id;
5475  }
5476  }}
5477 
5478 
5479  /* TODO: skip checks ? */
5480  id = _lwt_AddEdge( topo, nid[0], nid[1], edge, 0, handleFaceSplit ? 1 : -1 );
5481  LWDEBUGF(1, "lwt_AddEdgeModFace returned %" LWTFMT_ELEMID, id);
5482  if ( id == -1 )
5483  {
5484  lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5485  return -1;
5486  }
5487  lwgeom_free(tmp); /* possibly takes "edge" down with it */
5488 
5489  return id;
5490 }
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:170
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:330
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define LINETYPE
Definition: liblwgeom.h:86
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwgeom.c:923
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:520
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:335
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:356
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:123
void lwline_setPoint4d(LWLINE *line, uint32_t which, POINT4D *newpoint)
Definition: lwline.c:373
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
Definition: lwcollection.c:386
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:224
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:318
LWGEOM * lwline_remove_repeated_points(const LWLINE *in, double tolerance)
Definition: lwline.c:448
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:120
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:2263
LWT_ISO_NODE * lwt_be_getNodeById(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, int *numelems, int fields)
Definition: lwgeom_topo.c:156
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:467
static double _lwt_minTolerance(LWGEOM *g)
Definition: lwgeom_topo.c:4859
static LWT_ELEMID _lwt_GetEqualEdge(LWT_TOPOLOGY *topo, LWLINE *edge)
Definition: lwgeom_topo.c:5229
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:44
static LWT_ELEMID _lwt_AddPoint(LWT_TOPOLOGY *topo, LWPOINT *point, double tol, int findFace, int *moved)
Definition: lwgeom_topo.c:4906
uint32_t ngeoms
Definition: liblwgeom.h:510
LWGEOM ** geoms
Definition: liblwgeom.h:512
POINTARRAY * points
Definition: liblwgeom.h:425
POINTARRAY * point
Definition: liblwgeom.h:414
LWPOINT * geom
const LWT_BE_IFACE * be_iface
uint32_t npoints
Definition: liblwgeom.h:374

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: