PostGIS  2.5.0dev-r@@SVN_REVISION@@
static LWT_ELEMID _lwt_AddLineEdge ( LWT_TOPOLOGY topo,
LWLINE edge,
double  tol,
int  handleFaceSplit 
)
static

Definition at line 5513 of file lwgeom_topo.c.

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().

5515 {
5516  LWCOLLECTION *col;
5517  LWPOINT *start_point, *end_point;
5518  LWGEOM *tmp = 0, *tmp2;
5519  LWT_ISO_NODE *node;
5520  LWT_ELEMID nid[2]; /* start_node, end_node */
5521  LWT_ELEMID id; /* edge id */
5522  POINT4D p4d;
5523  int nn, i;
5524  int moved=0, mm;
5525 
5526  LWDEBUGG(1, lwline_as_lwgeom(edge), "_lwtAddLineEdge");
5527  LWDEBUGF(1, "_lwtAddLineEdge with tolerance %g", tol);
5528 
5529  start_point = lwline_get_lwpoint(edge, 0);
5530  if ( ! start_point )
5531  {
5532  lwnotice("Empty component of noded line");
5533  return 0; /* must be empty */
5534  }
5535  nid[0] = _lwt_AddPoint( topo, start_point,
5536  _lwt_minTolerance(lwpoint_as_lwgeom(start_point)),
5537  handleFaceSplit, &mm );
5538  lwpoint_free(start_point); /* too late if lwt_AddPoint calls lwerror */
5539  if ( nid[0] == -1 ) return -1; /* lwerror should have been called */
5540  moved += mm;
5541 
5542 
5543  end_point = lwline_get_lwpoint(edge, edge->points->npoints-1);
5544  if ( ! end_point )
5545  {
5546  lwerror("could not get last point of line "
5547  "after successfully getting first point !?");
5548  return -1;
5549  }
5550  nid[1] = _lwt_AddPoint( topo, end_point,
5552  handleFaceSplit, &mm );
5553  moved += mm;
5554  lwpoint_free(end_point); /* too late if lwt_AddPoint calls lwerror */
5555  if ( nid[1] == -1 ) return -1; /* lwerror should have been called */
5556 
5557  /*
5558  -- Added endpoints may have drifted due to tolerance, so
5559  -- we need to re-snap the edge to the new nodes before adding it
5560  */
5561  if ( moved )
5562  {
5563 
5564  nn = nid[0] == nid[1] ? 1 : 2;
5565  node = lwt_be_getNodeById( topo, nid, &nn,
5567  if ( nn == -1 )
5568  {
5569  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5570  return -1;
5571  }
5572  start_point = NULL; end_point = NULL;
5573  for (i=0; i<nn; ++i)
5574  {
5575  if ( node[i].node_id == nid[0] ) start_point = node[i].geom;
5576  if ( node[i].node_id == nid[1] ) end_point = node[i].geom;
5577  }
5578  if ( ! start_point || ! end_point )
5579  {
5580  if ( nn ) _lwt_release_nodes(node, nn);
5581  lwerror("Could not find just-added nodes % " LWTFMT_ELEMID
5582  " and %" LWTFMT_ELEMID, nid[0], nid[1]);
5583  return -1;
5584  }
5585 
5586  /* snap */
5587 
5588  getPoint4d_p( start_point->point, 0, &p4d );
5589  lwline_setPoint4d(edge, 0, &p4d);
5590 
5591  getPoint4d_p( end_point->point, 0, &p4d );
5592  lwline_setPoint4d(edge, edge->points->npoints-1, &p4d);
5593 
5594  if ( nn ) _lwt_release_nodes(node, nn);
5595 
5596  /* make valid, after snap (to handle collapses) */
5597  tmp = lwgeom_make_valid(lwline_as_lwgeom(edge));
5598 
5599  col = lwgeom_as_lwcollection(tmp);
5600  if ( col )
5601  {{
5602 
5603  col = lwcollection_extract(col, LINETYPE);
5604 
5605  /* Check if the so-snapped edge collapsed (see #1650) */
5606  if ( col->ngeoms == 0 )
5607  {
5608  lwcollection_free(col);
5609  lwgeom_free(tmp);
5610  LWDEBUG(1, "Made-valid snapped edge collapsed");
5611  return 0;
5612  }
5613 
5614  tmp2 = lwgeom_clone_deep( col->geoms[0] );
5615  lwgeom_free(tmp);
5616  tmp = tmp2;
5617  edge = lwgeom_as_lwline(tmp);
5618  lwcollection_free(col);
5619  if ( ! edge )
5620  {
5621  /* should never happen */
5622  lwerror("lwcollection_extract(LINETYPE) returned a non-line?");
5623  return -1;
5624  }
5625  }}
5626  else
5627  {
5628  edge = lwgeom_as_lwline(tmp);
5629  if ( ! edge )
5630  {
5631  LWDEBUGF(1, "Made-valid snapped edge collapsed to %s",
5633  lwgeom_free(tmp);
5634  return 0;
5635  }
5636  }
5637  }
5638 
5639  /* check if the so-snapped edge _now_ exists */
5640  id = _lwt_GetEqualEdge ( topo, edge );
5641  LWDEBUGF(1, "_lwt_GetEqualEdge returned %" LWTFMT_ELEMID, id);
5642  if ( id == -1 )
5643  {
5644  if ( tmp ) lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5645  return -1;
5646  }
5647  if ( id )
5648  {
5649  if ( tmp ) lwgeom_free(tmp); /* possibly takes "edge" down with it */
5650  return id;
5651  }
5652 
5653  /* No previously existing edge was found, we'll add one */
5654 
5655  /* Remove consecutive vertices below given tolerance
5656  * on edge addition */
5657  if ( tol )
5658  {{
5659  tmp2 = lwline_remove_repeated_points(edge, tol);
5660  LWDEBUGG(1, tmp2, "Repeated-point removed");
5661  edge = lwgeom_as_lwline(tmp2);
5662  if ( tmp ) lwgeom_free(tmp);
5663  tmp = tmp2;
5664 
5665  /* check if the so-decimated edge _now_ exists */
5666  id = _lwt_GetEqualEdge ( topo, edge );
5667  LWDEBUGF(1, "_lwt_GetEqualEdge returned %" LWTFMT_ELEMID, id);
5668  if ( id == -1 )
5669  {
5670  lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5671  return -1;
5672  }
5673  if ( id )
5674  {
5675  lwgeom_free(tmp); /* takes "edge" down with it */
5676  return id;
5677  }
5678  }}
5679 
5680 
5681  /* TODO: skip checks ? */
5682  id = _lwt_AddEdge( topo, nid[0], nid[1], edge, 0, handleFaceSplit ? 1 : -1 );
5683  LWDEBUGF(1, "lwt_AddEdgeModFace returned %" LWTFMT_ELEMID, id);
5684  if ( id == -1 )
5685  {
5686  lwgeom_free(tmp); /* probably too late, due to internal lwerror */
5687  return -1;
5688  }
5689  lwgeom_free(tmp); /* possibly takes "edge" down with it */
5690 
5691  return id;
5692 }
static LWT_ELEMID _lwt_GetEqualEdge(LWT_TOPOLOGY *topo, LWLINE *edge)
Definition: lwgeom_topo.c:5439
#define LINETYPE
Definition: liblwgeom.h:85
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:467
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
Definition: lwcollection.c:372
LWPOINT * geom
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwgeom.c:916
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
static LWT_ELEMID _lwt_AddPoint(LWT_TOPOLOGY *topo, LWPOINT *point, double tol, int findFace, int *moved)
Definition: lwgeom_topo.c:5092
#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:513
uint32_t ngeoms
Definition: liblwgeom.h:506
POINTARRAY * point
Definition: liblwgeom.h:410
void lwline_setPoint4d(LWLINE *line, uint32_t which, POINT4D *newpoint)
Definition: lwline.c:373
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:329
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
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:2359
const LWT_BE_IFACE * be_iface
LWGEOM ** geoms
Definition: liblwgeom.h:508
static double _lwt_minTolerance(LWGEOM *g)
Definition: lwgeom_topo.c:5045
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:169
#define LWDEBUGG(level, geom, msg)
Definition: lwgeom_log.h:93
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:113
#define LWT_COL_NODE_NODE_ID
Node fields.
LWGEOM * lwline_remove_repeated_points(const LWLINE *in, double tolerance)
Definition: lwline.c:448
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:223
#define LWT_COL_NODE_GEOM
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:342
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:334
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
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:44
LWPOINT * lwline_get_lwpoint(const LWLINE *line, uint32_t where)
Returns freshly allocated LWPOINT that corresponds to the index where.
Definition: lwline.c:318
POINTARRAY * points
Definition: liblwgeom.h:421
uint32_t npoints
Definition: liblwgeom.h:370

Here is the call graph for this function:

Here is the caller graph for this function: