PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ _lwt_AddLine()

static LWT_ELEMID* _lwt_AddLine ( LWT_TOPOLOGY topo,
LWLINE line,
double  tol,
int *  nedges,
int  handleFaceSplit 
)
static

Definition at line 7115 of file lwgeom_topo.c.

7117 {
7118  LWGEOM *geomsbuf[1];
7119  LWGEOM **geoms;
7120  uint32_t ngeoms;
7121  LWGEOM *noded, *tmp;
7122  LWCOLLECTION *col;
7123  LWT_ELEMID *ids;
7124  LWT_ISO_EDGE *edges;
7125  LWT_ISO_NODE *nodes;
7126  uint64_t num, numedges = 0, numnodes = 0;
7127  uint64_t i;
7128  GBOX qbox;
7129  int forward;
7130  int input_was_closed = 0;
7131  POINT4D originalStartPoint;
7132 
7133  if ( lwline_is_empty(line) )
7134  {
7135  *nedges = 0;
7136  return NULL;
7137  }
7138 
7139  if ( lwline_is_closed(line) )
7140  {
7141  input_was_closed = 1;
7142  getPoint4d_p( line->points, 0, &originalStartPoint);
7143  LWDEBUGF(1, "Input line is closed, original point is %g,%g", originalStartPoint.x, originalStartPoint.y);
7144  }
7145 
7146  *nedges = -1; /* error condition, by default */
7147 
7148  /* Get tolerance, if 0 was given */
7149  if ( ! tol ) tol = _LWT_MINTOLERANCE( topo, (LWGEOM*)line );
7150  LWDEBUGF(1, "Working tolerance:%.15g", tol);
7151  LWDEBUGF(1, "Input line has srid=%d", line->srid);
7152 
7153  /* Remove consecutive vertices below given tolerance upfront */
7154  if ( tol )
7155  {{
7157  tmp = lwline_as_lwgeom(clean); /* NOTE: might collapse to non-simple */
7158  LWDEBUGG(1, tmp, "Repeated-point removed");
7159  }} else tmp=(LWGEOM*)line;
7160 
7161  /* 1. Self-node */
7162  noded = lwgeom_node((LWGEOM*)tmp);
7163  if ( tmp != (LWGEOM*)line ) lwgeom_free(tmp);
7164  if ( ! noded ) return NULL; /* should have called lwerror already */
7165  LWDEBUGG(1, noded, "Noded");
7166 
7167  qbox = *lwgeom_get_bbox( lwline_as_lwgeom(line) );
7168  LWDEBUGF(1, "Line BOX is %.15g %.15g, %.15g %.15g", qbox.xmin, qbox.ymin,
7169  qbox.xmax, qbox.ymax);
7170  gbox_expand(&qbox, tol);
7171  LWDEBUGF(1, "BOX expanded by %g is %.15g %.15g, %.15g %.15g",
7172  tol, qbox.xmin, qbox.ymin, qbox.xmax, qbox.ymax);
7173 
7174  LWGEOM **nearby = 0;
7175  int nearbyindex = 0;
7176  int nearbycount = 0;
7177 
7178  /* 2.0. Find edges falling within tol distance */
7179  edges = lwt_be_getEdgeWithinBox2D( topo, &qbox, &numedges, LWT_COL_EDGE_ALL, 0 );
7180  if (numedges == UINT64_MAX)
7181  {
7182  lwgeom_free(noded);
7183  PGTOPO_BE_ERROR();
7184  return NULL;
7185  }
7186  LWDEBUGF(1, "Line has %u points, its bbox intersects %llu edges bboxes",
7187  line->points->npoints, numedges);
7188  if ( numedges )
7189  {{
7190  /* TODO: compute earlier for reuse later ? */
7191  GEOSGeometry *noded_g = LWGEOM2GEOS(noded, 0);
7192  /* collect those whose distance from us is < tol */
7193  nearbycount += numedges;
7194  nearby = lwalloc(numedges * sizeof(LWGEOM *));
7195  for (i=0; i<numedges; ++i)
7196  {{
7197  LW_ON_INTERRUPT(return NULL);
7198  LWT_ISO_EDGE *e = &(edges[i]);
7199  LWGEOM *g = lwline_as_lwgeom(e->geom);
7200  GEOSGeometry *edge_g = LWGEOM2GEOS(g, 0);
7201  LWDEBUGF(2, "Computing distance from edge %" LWTFMT_ELEMID " with %u points", i, e->geom->points->npoints);
7202  double dist;
7203  if ( 0 == GEOSDistanceIndexed(edge_g, noded_g, &dist) ) {
7204  GEOSGeom_destroy(edge_g);
7205  GEOSGeom_destroy(noded_g);
7206  lwgeom_free(noded);
7207  lwerror("GEOSDistanceIndexed error: %s", lwgeom_geos_errmsg);
7208  return NULL;
7209  }
7210  GEOSGeom_destroy(edge_g);
7211  if ( dist && dist >= tol ) continue;
7212  nearby[nearbyindex++] = g;
7213  }}
7214  LWDEBUGF(1, "Found %d edges closer than tolerance (%g)", nearbyindex, tol);
7215  GEOSGeom_destroy(noded_g);
7216  }}
7217  int nearbyedgecount = nearbyindex;
7218 
7219  /* 2.1. Find isolated nodes falling within tol distance
7220  *
7221  * TODO: add backend-interface support for only getting isolated nodes
7222  */
7223  nodes = lwt_be_getNodeWithinBox2D( topo, &qbox, &numnodes, LWT_COL_NODE_ALL, 0 );
7224  if (numnodes == UINT64_MAX)
7225  {
7226  lwgeom_free(noded);
7227  PGTOPO_BE_ERROR();
7228  return NULL;
7229  }
7230  LWDEBUGF(1, "Line bbox intersects %llu nodes bboxes", numnodes);
7231  if ( numnodes )
7232  {{
7233  /* collect those whose distance from us is < tol */
7234  nearbycount = nearbyedgecount + numnodes;
7235  nearby = nearby ?
7236  lwrealloc(nearby, nearbycount * sizeof(LWGEOM *))
7237  :
7238  lwalloc(nearbycount * sizeof(LWGEOM *))
7239  ;
7240  int nn = 0;
7241  for (i=0; i<numnodes; ++i)
7242  {
7243  LWT_ISO_NODE *n = &(nodes[i]);
7244  if ( n->containing_face == -1 ) continue; /* skip not-isolated nodes */
7245  LWGEOM *g = lwpoint_as_lwgeom(n->geom);
7246  double dist = lwgeom_mindistance2d(g, noded);
7247  /* must be closer than tolerated, unless distance is zero */
7248  if ( dist && dist >= tol )
7249  {
7250  LWDEBUGF(1, "Node %" LWTFMT_ELEMID " is %g units away, we tolerate only %g", n->node_id, dist, tol);
7251  continue;
7252  }
7253  nearby[nearbyindex++] = g;
7254  nn = nn + 1;
7255  }
7256  LWDEBUGF(1, "Found %d isolated nodes closer than tolerance (%g)", nn, tol);
7257  }}
7258  int nearbynodecount = nearbyindex - nearbyedgecount;
7259  nearbycount = nearbyindex;
7260 
7261  LWDEBUGF(1, "Number of nearby elements is %d", nearbycount);
7262 
7263  /* 2.2. Snap to nearby elements */
7264  if ( nearbycount )
7265  {{
7266  LWCOLLECTION *col;
7267  LWGEOM *elems;
7268 
7270  NULL, nearbycount, nearby);
7271  elems = lwcollection_as_lwgeom(col);
7272 
7273  LWDEBUGG(1, elems, "Collected nearby elements");
7274 
7275  tmp = _lwt_toposnap(noded, elems, tol);
7276  lwgeom_free(noded);
7277  noded = tmp;
7278  LWDEBUGG(1, noded, "Elements-snapped");
7279  if ( input_was_closed )
7280  {{
7281  /* Recompute start point in case it moved */
7282  LWLINE *scrolled = lwgeom_as_lwline(noded);
7283  if (scrolled)
7284  {
7285  getPoint4d_p( scrolled->points, 0, &originalStartPoint);
7286  LWDEBUGF(1, "Closed input line start point after snap %g,%g", originalStartPoint.x, originalStartPoint.y);
7287  }
7288  }}
7289 
7290  /* will not release the geoms array */
7291  lwcollection_release(col);
7292 
7293  /*
7294  -- re-node to account for ST_Snap introduced self-intersections
7295  -- See http://trac.osgeo.org/postgis/ticket/1714
7296  -- TODO: consider running UnaryUnion once after all noding
7297  */
7298  tmp = lwgeom_unaryunion(noded);
7299  lwgeom_free(noded);
7300  noded = tmp;
7301  LWDEBUGG(1, noded, "Unary-unioned");
7302  }}
7303 
7304  /* 2.3. Node with nearby edges */
7305  if ( nearbyedgecount )
7306  {{
7307  LWCOLLECTION *col;
7308  LWGEOM *iedges; /* just an alias for col */
7309  LWGEOM *diff, *xset;
7310 
7311  LWDEBUGF(1, "Line intersects %d edges", nearbyedgecount);
7312 
7314  NULL, nearbyedgecount, nearby);
7315  iedges = lwcollection_as_lwgeom(col);
7316  LWDEBUGG(1, iedges, "Collected edges");
7317 
7318  LWDEBUGF(1, "Diffing noded, with srid=%d "
7319  "and intersecting edges, with srid=%d",
7320  noded->srid, iedges->srid);
7321  diff = lwgeom_difference(noded, iedges);
7322  LWDEBUGG(1, diff, "Differenced");
7323 
7324  LWDEBUGF(1, "Intersecting noded, with srid=%d "
7325  "and intersecting edges, with srid=%d",
7326  noded->srid, iedges->srid);
7327  xset = lwgeom_intersection(noded, iedges);
7328  LWDEBUGG(1, xset, "Intersected");
7329  lwgeom_free(noded);
7330 
7331  /* We linemerge here because INTERSECTION, as of GEOS 3.8,
7332  * will result in shared segments being output as multiple
7333  * lines rather than a single line. Example:
7334 
7335  INTERSECTION(
7336  'LINESTRING(0 0, 5 0, 8 0, 10 0,12 0)',
7337  'LINESTRING(5 0, 8 0, 10 0)'
7338  )
7339  ==
7340  MULTILINESTRING((5 0,8 0),(8 0,10 0))
7341 
7342  * We will re-split in a subsequent step, by splitting
7343  * the final line with pre-existing nodes
7344  */
7345  LWDEBUG(1, "Linemerging intersection");
7346  tmp = lwgeom_linemerge(xset);
7347  LWDEBUGG(1, tmp, "Linemerged");
7348  lwgeom_free(xset);
7349  xset = tmp;
7350 
7351  /*
7352  * Here we union the (linemerged) intersection with
7353  * the difference (new lines)
7354  */
7355  LWDEBUG(1, "Unioning difference and (linemerged) intersection");
7356  noded = lwgeom_union(diff, xset);
7357  LWDEBUGG(1, noded, "Diff-Xset Unioned");
7358  lwgeom_free(xset);
7359  lwgeom_free(diff);
7360 
7361  /* will not release the geoms array */
7362  lwcollection_release(col);
7363 
7364  if ( input_was_closed )
7365  {{
7366  LWLINE *scrolled = lwgeom_as_lwline(noded);
7367  if (scrolled) {
7368  if ( lwline_is_closed(scrolled) ) {
7369  ptarray_scroll_in_place(scrolled->points, &originalStartPoint);
7370  }
7371  else {
7372  LWDEBUGG(1, lwline_as_lwgeom(scrolled), "Closed input became non closed");
7373  }
7374  }
7375  else {
7376  LWDEBUGG(1, noded, "Diff-Xset Unioned cannot be scrolled");
7377  }
7378  }}
7379 
7380 
7381  }}
7382 
7383 
7384  /* 2.4. Split by pre-existing nodes
7385  *
7386  * Pre-existing nodes are isolated nodes AND endpoints
7387  * of intersecting edges
7388  */
7389  if ( nearbyedgecount )
7390  {
7391  nearbycount += nearbyedgecount * 2; /* make space for endpoints */
7392  nearby = lwrealloc(nearby, nearbycount * sizeof(LWGEOM *));
7393  for (int i=0; i<nearbyedgecount; i++)
7394  {
7395  LWLINE *edge = lwgeom_as_lwline(nearby[i]);
7396  LWPOINT *startNode = lwline_get_lwpoint(edge, 0);
7397  LWPOINT *endNode = lwline_get_lwpoint(edge, edge->points->npoints-1);
7398  /* TODO: only add if within distance to noded AND if not duplicated */
7399  nearby[nearbyindex++] = lwpoint_as_lwgeom(startNode);
7400  nearbynodecount++;
7401  nearby[nearbyindex++] = lwpoint_as_lwgeom(endNode);
7402  nearbynodecount++;
7403  }
7404  }
7405  if ( nearbynodecount )
7406  {
7408  NULL, nearbynodecount,
7409  nearby + nearbyedgecount);
7410  LWGEOM *inodes = lwcollection_as_lwgeom(col);
7411  /* TODO: use lwgeom_split of lwgeom_union ... */
7412  tmp = _lwt_split_by_nodes(noded, inodes);
7413  lwgeom_free(noded);
7414  noded = tmp;
7415  LWDEBUGG(1, noded, "Node-split");
7416  /* will not release the geoms array */
7417  lwcollection_release(col);
7418  }
7419 
7420 
7421  LWDEBUG(1, "Freeing up nearby elements");
7422 
7423  /* TODO: free up endpoints of nearbyedges */
7424  if ( nearby ) lwfree(nearby);
7425  if ( nodes ) _lwt_release_nodes(nodes, numnodes);
7426  if ( edges ) _lwt_release_edges(edges, numedges);
7427 
7428  LWDEBUGG(2, noded, "Finally-noded");
7429 
7430  /* 3. For each (now-noded) segment, insert an edge */
7431  col = lwgeom_as_lwcollection(noded);
7432  if ( col )
7433  {
7434  LWDEBUG(1, "Noded line was a collection");
7435  geoms = col->geoms;
7436  ngeoms = col->ngeoms;
7437  }
7438  else
7439  {
7440  LWDEBUG(1, "Noded line was a single geom");
7441  geomsbuf[0] = noded;
7442  geoms = geomsbuf;
7443  ngeoms = 1;
7444  }
7445 
7446  LWDEBUGF(1, "Line was split into %d edges", ngeoms);
7447 
7448  /* TODO: refactor to first add all nodes (re-snapping edges if
7449  * needed) and then check all edges for existing already
7450  * ( so to save a DB scan for each edge to be added )
7451  */
7452  ids = lwalloc(sizeof(LWT_ELEMID)*ngeoms);
7453  num = 0;
7454  for ( i=0; i<ngeoms; ++i )
7455  {
7456  LWT_ELEMID id;
7457  LWGEOM *g = geoms[i];
7458  g->srid = noded->srid;
7459 
7460 #if POSTGIS_DEBUG_LEVEL > 0
7461  {
7462  size_t sz;
7463  char *wkt1 = lwgeom_to_wkt(g, WKT_EXTENDED, 15, &sz);
7464  LWDEBUGF(1, "Component %llu of split line is: %s", i, wkt1);
7465  lwfree(wkt1);
7466  }
7467 #endif
7468 
7469  id = _lwt_AddLineEdge( topo, lwgeom_as_lwline(g), tol, handleFaceSplit, &forward );
7470  LWDEBUGF(1, "_lwt_AddLineEdge returned %" LWTFMT_ELEMID, id);
7471  if ( id < 0 )
7472  {
7473  lwgeom_free(noded);
7474  lwfree(ids);
7475  return NULL;
7476  }
7477  if ( ! id )
7478  {
7479  LWDEBUGF(1, "Component %llu of split line collapsed", i);
7480  continue;
7481  }
7482 
7483  LWDEBUGF(1, "Component %llu of split line is %s edge %" LWTFMT_ELEMID,
7484  i, forward ? "forward" : "backward", id);
7485  ids[num++] = forward ? id : -id; /* TODO: skip duplicates */
7486  }
7487 
7488  LWDEBUGG(2, noded, "Noded before free");
7489  lwgeom_free(noded);
7490 
7491  /* TODO: XXX remove duplicated ids if not done before */
7492 
7493  *nedges = num;
7494  return ids;
7495 }
void gbox_expand(GBOX *g, double d)
Move the box minimums down and the maximums up by the distance provided.
Definition: gbox.c:97
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:179
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:339
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:309
#define COLLECTIONTYPE
Definition: liblwgeom.h:108
LWGEOM * lwgeom_node(const LWGEOM *lwgeom_in)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1218
LWGEOM * lwgeom_intersection(const LWGEOM *geom1, const LWGEOM *geom2)
#define WKT_EXTENDED
Definition: liblwgeom.h:2218
#define MULTIPOINTTYPE
Definition: liblwgeom.h:105
LWGEOM * lwgeom_difference(const LWGEOM *geom1, const LWGEOM *geom2)
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:242
void lwfree(void *mem)
Definition: lwutil.c:248
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:344
LWGEOM * lwgeom_unaryunion(const LWGEOM *geom1)
void lwcollection_release(LWCOLLECTION *lwcollection)
Definition: lwcollection.c:36
double lwgeom_mindistance2d(const LWGEOM *lw1, const LWGEOM *lw2)
Function initializing min distance calculation.
Definition: measures.c:200
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:125
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:708
const GBOX * lwgeom_get_bbox(const LWGEOM *lwgeom)
Get a non-empty geometry bounding box, computing and caching it if not already there.
Definition: lwgeom.c:743
LWGEOM * lwgeom_linemerge(const LWGEOM *geom1)
void * lwalloc(size_t size)
Definition: lwutil.c:227
LWCOLLECTION * lwcollection_construct(uint8_t type, int32_t srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:42
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:233
LWGEOM * lwgeom_union(const LWGEOM *geom1, const LWGEOM *geom2)
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
int lwline_is_empty(const LWLINE *line)
#define LW_ON_INTERRUPT(x)
int lwline_is_closed(const LWLINE *line)
Definition: lwline.c:445
int ptarray_scroll_in_place(POINTARRAY *pa, const POINT4D *newbase)
Definition: ptarray.c:2202
LWT_INT64 LWT_ELEMID
Identifier of topology element.
#define LWT_COL_EDGE_ALL
#define LWT_COL_NODE_ALL
#define PGTOPO_BE_ERROR()
#define LWTFMT_ELEMID
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:101
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:106
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
#define LWDEBUGG(level, geom, msg)
Definition: lwgeom_log.h:111
static LWGEOM * _lwt_split_by_nodes(const LWGEOM *g, const LWGEOM *nodes)
Definition: lwgeom_topo.c:7093
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:470
LWT_ISO_EDGE * lwt_be_getEdgeWithinBox2D(const LWT_TOPOLOGY *topo, const GBOX *box, uint64_t *numelems, int fields, uint64_t limit)
Definition: lwgeom_topo.c:173
void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:460
static LWT_ISO_NODE * lwt_be_getNodeWithinBox2D(const LWT_TOPOLOGY *topo, const GBOX *box, uint64_t *numelems, int fields, uint64_t limit)
Definition: lwgeom_topo.c:167
#define _LWT_MINTOLERANCE(topo, geom)
Definition: lwgeom_topo.c:5062
static LWT_ELEMID _lwt_AddLineEdge(LWT_TOPOLOGY *topo, LWLINE *edge, double tol, int handleFaceSplit, int *forward)
Definition: lwgeom_topo.c:6891
static LWGEOM * _lwt_toposnap(LWGEOM *src, LWGEOM *tgt, double tol)
Definition: lwgeom_topo.c:423
double ymax
Definition: liblwgeom.h:357
double xmax
Definition: liblwgeom.h:355
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354
uint32_t ngeoms
Definition: liblwgeom.h:580
LWGEOM ** geoms
Definition: liblwgeom.h:575
int32_t srid
Definition: liblwgeom.h:460
POINTARRAY * points
Definition: liblwgeom.h:483
int32_t srid
Definition: liblwgeom.h:484
LWLINE * geom
LWT_ELEMID node_id
LWT_ELEMID containing_face
LWPOINT * geom
double x
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414
uint32_t npoints
Definition: liblwgeom.h:427

References _lwt_AddLineEdge(), _LWT_MINTOLERANCE, _lwt_release_edges(), _lwt_release_nodes(), _lwt_split_by_nodes(), _lwt_toposnap(), COLLECTIONTYPE, LWT_ISO_NODE::containing_face, gbox_expand(), LWT_ISO_NODE::geom, LWT_ISO_EDGE::geom, LWCOLLECTION::geoms, getPoint4d_p(), LW_ON_INTERRUPT, lwalloc(), lwcollection_as_lwgeom(), lwcollection_construct(), lwcollection_release(), LWDEBUG, LWDEBUGF, LWDEBUGG, lwerror(), lwfree(), LWGEOM2GEOS(), lwgeom_as_lwcollection(), lwgeom_as_lwline(), lwgeom_difference(), lwgeom_free(), lwgeom_geos_errmsg, lwgeom_get_bbox(), lwgeom_intersection(), lwgeom_linemerge(), lwgeom_mindistance2d(), lwgeom_node(), lwgeom_to_wkt(), lwgeom_unaryunion(), lwgeom_union(), lwline_as_lwgeom(), lwline_get_lwpoint(), lwline_is_closed(), lwline_is_empty(), lwline_remove_repeated_points(), lwpoint_as_lwgeom(), lwrealloc(), lwt_be_getEdgeWithinBox2D(), lwt_be_getNodeWithinBox2D(), LWT_COL_EDGE_ALL, LWT_COL_NODE_ALL, LWTFMT_ELEMID, MULTIPOINTTYPE, LWCOLLECTION::ngeoms, LWT_ISO_NODE::node_id, POINTARRAY::npoints, PGTOPO_BE_ERROR, LWLINE::points, ptarray_scroll_in_place(), LWGEOM::srid, LWLINE::srid, LWT_TOPOLOGY_T::srid, WKT_EXTENDED, POINT4D::x, GBOX::xmax, GBOX::xmin, POINT4D::y, GBOX::ymax, and GBOX::ymin.

Referenced by lwt_AddLine(), and lwt_AddLineNoFace().

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