PostGIS  3.1.6dev-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 5583 of file lwgeom_topo.c.

5585 {
5586  LWGEOM *geomsbuf[1];
5587  LWGEOM **geoms;
5588  uint32_t ngeoms;
5589  LWGEOM *noded, *tmp;
5590  LWCOLLECTION *col;
5591  LWT_ELEMID *ids;
5592  LWT_ISO_EDGE *edges;
5593  LWT_ISO_NODE *nodes;
5594  uint64_t num, numedges = 0, numnodes = 0;
5595  uint64_t i;
5596  GBOX qbox;
5597 
5598  if ( lwline_is_empty(line) )
5599  {
5600  *nedges = 0;
5601  return NULL;
5602  }
5603 
5604  *nedges = -1; /* error condition, by default */
5605 
5606  /* Get tolerance, if 0 was given */
5607  if ( ! tol ) tol = _LWT_MINTOLERANCE( topo, (LWGEOM*)line );
5608  LWDEBUGF(1, "Working tolerance:%.15g", tol);
5609  LWDEBUGF(1, "Input line has srid=%d", line->srid);
5610 
5611  /* Remove consecutive vertices below given tolerance upfront */
5612  if ( tol )
5613  {{
5615  tmp = lwline_as_lwgeom(clean); /* NOTE: might collapse to non-simple */
5616  LWDEBUGG(1, tmp, "Repeated-point removed");
5617  }} else tmp=(LWGEOM*)line;
5618 
5619  /* 1. Self-node */
5620  noded = lwgeom_node((LWGEOM*)tmp);
5621  if ( tmp != (LWGEOM*)line ) lwgeom_free(tmp);
5622  if ( ! noded ) return NULL; /* should have called lwerror already */
5623  LWDEBUGG(1, noded, "Noded");
5624 
5625  qbox = *lwgeom_get_bbox( lwline_as_lwgeom(line) );
5626  LWDEBUGF(1, "Line BOX is %.15g %.15g, %.15g %.15g", qbox.xmin, qbox.ymin,
5627  qbox.xmax, qbox.ymax);
5628  gbox_expand(&qbox, tol);
5629  LWDEBUGF(1, "BOX expanded by %g is %.15g %.15g, %.15g %.15g",
5630  tol, qbox.xmin, qbox.ymin, qbox.xmax, qbox.ymax);
5631 
5632  LWGEOM **nearby = 0;
5633  int nearbyindex = 0;
5634  int nearbycount = 0;
5635 
5636  /* 2.0. Find edges falling within tol distance */
5637  edges = lwt_be_getEdgeWithinBox2D( topo, &qbox, &numedges, LWT_COL_EDGE_ALL, 0 );
5638  if (numedges == UINT64_MAX)
5639  {
5640  lwgeom_free(noded);
5641  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5642  return NULL;
5643  }
5644  LWDEBUGF(1, "Line has %d points, its bbox intersects %d edges bboxes",
5645  line->points->npoints, numedges);
5646  if ( numedges )
5647  {{
5648  /* collect those whose distance from us is < tol */
5649  nearbycount += numedges;
5650  nearby = lwalloc(numedges * sizeof(LWGEOM *));
5651  for (i=0; i<numedges; ++i)
5652  {
5653  LW_ON_INTERRUPT(return NULL);
5654  LWT_ISO_EDGE *e = &(edges[i]);
5655  LWGEOM *g = lwline_as_lwgeom(e->geom);
5656  LWDEBUGF(2, "Computing distance from edge %d having %d points", i, e->geom->points->npoints);
5657  double dist = lwgeom_mindistance2d(g, noded);
5658  /* must be closer than tolerated, unless distance is zero */
5659  if ( dist && dist >= tol ) continue;
5660  nearby[nearbyindex++] = g;
5661  }
5662  LWDEBUGF(1, "Found %d edges closer than tolerance (%g)", nearbyindex, tol);
5663  }}
5664  int nearbyedgecount = nearbyindex;
5665 
5666  /* 2.1. Find isolated nodes falling within tol distance
5667  *
5668  * TODO: add backend-interface support for only getting isolated nodes
5669  */
5670  nodes = lwt_be_getNodeWithinBox2D( topo, &qbox, &numnodes, LWT_COL_NODE_ALL, 0 );
5671  if (numnodes == UINT64_MAX)
5672  {
5673  lwgeom_free(noded);
5674  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5675  return NULL;
5676  }
5677  LWDEBUGF(1, "Line bbox intersects %d nodes bboxes", numnodes);
5678  if ( numnodes )
5679  {{
5680  /* collect those whose distance from us is < tol */
5681  nearbycount = nearbyedgecount + numnodes;
5682  nearby = nearby ?
5683  lwrealloc(nearby, nearbycount * sizeof(LWGEOM *))
5684  :
5685  lwalloc(nearbycount * sizeof(LWGEOM *))
5686  ;
5687  int nn = 0;
5688  (void)nn;
5689  for (i=0; i<numnodes; ++i)
5690  {
5691  LWT_ISO_NODE *n = &(nodes[i]);
5692  if ( n->containing_face == -1 ) continue; /* skip not-isolated nodes */
5693  LWGEOM *g = lwpoint_as_lwgeom(n->geom);
5694  double dist = lwgeom_mindistance2d(g, noded);
5695  /* must be closer than tolerated, unless distance is zero */
5696  if ( dist && dist >= tol )
5697  {
5698  LWDEBUGF(1, "Node %d is %g units away, we tolerate only %g", n->node_id, dist, tol);
5699  continue;
5700  }
5701  nearby[nearbyindex++] = g;
5702  ++nn;
5703  }
5704  LWDEBUGF(1, "Found %d isolated nodes closer than tolerance (%g)", nn, tol);
5705  }}
5706  int nearbynodecount = nearbyindex - nearbyedgecount;
5707  nearbycount = nearbyindex;
5708 
5709  LWDEBUGF(1, "Number of nearby elements is %d", nearbycount);
5710 
5711  /* 2.2. Snap to nearby elements */
5712  if ( nearbycount )
5713  {{
5714  LWCOLLECTION *col;
5715  LWGEOM *elems;
5716 
5718  NULL, nearbycount, nearby);
5719  elems = lwcollection_as_lwgeom(col);
5720 
5721  LWDEBUGG(1, elems, "Collected nearby elements");
5722 
5723  tmp = _lwt_toposnap(noded, elems, tol);
5724  lwgeom_free(noded);
5725  noded = tmp;
5726  LWDEBUGG(1, noded, "Elements-snapped");
5727 
5728  /* will not release the geoms array */
5729  lwcollection_release(col);
5730 
5731  /*
5732  -- re-node to account for ST_Snap introduced self-intersections
5733  -- See http://trac.osgeo.org/postgis/ticket/1714
5734  -- TODO: consider running UnaryUnion once after all noding
5735  */
5736  tmp = lwgeom_unaryunion(noded);
5737  lwgeom_free(noded);
5738  noded = tmp;
5739  LWDEBUGG(1, noded, "Unary-unioned");
5740  }}
5741 
5742  /* 2.3. Node with nearby edges */
5743  if ( nearbyedgecount )
5744  {{
5745  LWCOLLECTION *col;
5746  LWGEOM *iedges; /* just an alias for col */
5747  LWGEOM *diff, *xset;
5748 
5749  LWDEBUGF(1, "Line intersects %d edges", nearbyedgecount);
5750 
5752  NULL, nearbyedgecount, nearby);
5753  iedges = lwcollection_as_lwgeom(col);
5754  LWDEBUGG(1, iedges, "Collected edges");
5755 
5756  LWDEBUGF(1, "Diffing noded, with srid=%d "
5757  "and interesecting edges, with srid=%d",
5758  noded->srid, iedges->srid);
5759  diff = lwgeom_difference(noded, iedges);
5760  LWDEBUGG(1, diff, "Differenced");
5761 
5762  LWDEBUGF(1, "Intersecting noded, with srid=%d "
5763  "and interesecting edges, with srid=%d",
5764  noded->srid, iedges->srid);
5765  xset = lwgeom_intersection(noded, iedges);
5766  LWDEBUGG(1, xset, "Intersected");
5767  lwgeom_free(noded);
5768 
5769  /* We linemerge here because INTERSECTION, as of GEOS 3.8,
5770  * will result in shared segments being output as multiple
5771  * lines rather than a single line. Example:
5772 
5773  INTERSECTION(
5774  'LINESTRING(0 0, 5 0, 8 0, 10 0,12 0)',
5775  'LINESTRING(5 0, 8 0, 10 0)'
5776  )
5777  ==
5778  MULTILINESTRING((5 0,8 0),(8 0,10 0))
5779 
5780  * We will re-split in a subsequent step, by splitting
5781  * the final line with pre-existing nodes
5782  */
5783  LWDEBUG(1, "Linemerging intersection");
5784  tmp = lwgeom_linemerge(xset);
5785  LWDEBUGG(1, tmp, "Linemerged");
5786  lwgeom_free(xset);
5787  xset = tmp;
5788 
5789  /*
5790  * Here we union the (linemerged) intersection with
5791  * the difference (new lines)
5792  */
5793  LWDEBUG(1, "Unioning difference and (linemerged) intersection");
5794  noded = lwgeom_union(diff, xset);
5795  LWDEBUGG(1, noded, "Diff-Xset Unioned");
5796  lwgeom_free(xset);
5797  lwgeom_free(diff);
5798 
5799  /* will not release the geoms array */
5800  lwcollection_release(col);
5801  }}
5802 
5803 
5804  /* 2.4. Split by pre-existing nodes
5805  *
5806  * Pre-existing nodes are isolated nodes AND endpoints
5807  * of intersecting edges
5808  */
5809  if ( nearbyedgecount )
5810  {
5811  nearbycount += nearbyedgecount * 2; /* make space for endpoints */
5812  nearby = lwrealloc(nearby, nearbycount * sizeof(LWGEOM *));
5813  for (int i=0; i<nearbyedgecount; i++)
5814  {
5815  LWLINE *edge = lwgeom_as_lwline(nearby[i]);
5816  LWPOINT *startNode = lwline_get_lwpoint(edge, 0);
5817  LWPOINT *endNode = lwline_get_lwpoint(edge, edge->points->npoints-1);
5818  /* TODO: only add if within distance to noded AND if not duplicated */
5819  nearby[nearbyindex++] = lwpoint_as_lwgeom(startNode);
5820  nearbynodecount++;
5821  nearby[nearbyindex++] = lwpoint_as_lwgeom(endNode);
5822  nearbynodecount++;
5823  }
5824  }
5825  if ( nearbynodecount )
5826  {
5828  NULL, nearbynodecount,
5829  nearby + nearbyedgecount);
5830  LWGEOM *inodes = lwcollection_as_lwgeom(col);
5831  /* TODO: use lwgeom_split of lwgeom_union ... */
5832  tmp = _lwt_split_by_nodes(noded, inodes);
5833  lwgeom_free(noded);
5834  noded = tmp;
5835  LWDEBUGG(1, noded, "Node-split");
5836  /* will not release the geoms array */
5837  lwcollection_release(col);
5838  }
5839 
5840 
5841  LWDEBUG(1, "Freeing up nearby elements");
5842 
5843  /* TODO: free up endpoints of nearbyedges */
5844  if ( nearby ) lwfree(nearby);
5845  if ( nodes ) _lwt_release_nodes(nodes, numnodes);
5846  if ( edges ) _lwt_release_edges(edges, numedges);
5847 
5848  LWDEBUGG(1, noded, "Finally-noded");
5849 
5850  /* 3. For each (now-noded) segment, insert an edge */
5851  col = lwgeom_as_lwcollection(noded);
5852  if ( col )
5853  {
5854  LWDEBUG(1, "Noded line was a collection");
5855  geoms = col->geoms;
5856  ngeoms = col->ngeoms;
5857  }
5858  else
5859  {
5860  LWDEBUG(1, "Noded line was a single geom");
5861  geomsbuf[0] = noded;
5862  geoms = geomsbuf;
5863  ngeoms = 1;
5864  }
5865 
5866  LWDEBUGF(1, "Line was split into %d edges", ngeoms);
5867 
5868  /* TODO: refactor to first add all nodes (re-snapping edges if
5869  * needed) and then check all edges for existing already
5870  * ( so to save a DB scan for each edge to be added )
5871  */
5872  ids = lwalloc(sizeof(LWT_ELEMID)*ngeoms);
5873  num = 0;
5874  for ( i=0; i<ngeoms; ++i )
5875  {
5876  LWT_ELEMID id;
5877  LWGEOM *g = geoms[i];
5878  g->srid = noded->srid;
5879 
5880 #if POSTGIS_DEBUG_LEVEL > 0
5881  {
5882  size_t sz;
5883  char *wkt1 = lwgeom_to_wkt(g, WKT_EXTENDED, 15, &sz);
5884  LWDEBUGF(1, "Component %d of split line is: %s", i, wkt1);
5885  lwfree(wkt1);
5886  }
5887 #endif
5888 
5889  id = _lwt_AddLineEdge( topo, lwgeom_as_lwline(g), tol, handleFaceSplit );
5890  LWDEBUGF(1, "_lwt_AddLineEdge returned %" LWTFMT_ELEMID, id);
5891  if ( id < 0 )
5892  {
5893  lwgeom_free(noded);
5894  lwfree(ids);
5895  return NULL;
5896  }
5897  if ( ! id )
5898  {
5899  LWDEBUGF(1, "Component %d of split line collapsed", i);
5900  continue;
5901  }
5902 
5903  LWDEBUGF(1, "Component %d of split line is edge %" LWTFMT_ELEMID,
5904  i, id);
5905  ids[num++] = id; /* TODO: skip duplicates */
5906  }
5907 
5908  LWDEBUGG(1, noded, "Noded before free");
5909  lwgeom_free(noded);
5910 
5911  /* TODO: XXX remove duplicated ids if not done before */
5912 
5913  *nedges = num;
5914  return ids;
5915 }
void gbox_expand(GBOX *g, double d)
Move the box minimums down and the maximums up by the distance provided.
Definition: gbox.c:97
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:162
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:322
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:292
#define COLLECTIONTYPE
Definition: liblwgeom.h:122
LWGEOM * lwgeom_node(const LWGEOM *lwgeom_in)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
LWGEOM * lwgeom_intersection(const LWGEOM *geom1, const LWGEOM *geom2)
#define WKT_EXTENDED
Definition: liblwgeom.h:2157
#define MULTIPOINTTYPE
Definition: liblwgeom.h:119
LWGEOM * lwgeom_difference(const LWGEOM *geom1, const LWGEOM *geom2)
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:235
void lwfree(void *mem)
Definition: lwutil.c:242
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:327
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:197
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:704
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:726
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:216
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)
LWT_INT64 LWT_ELEMID
Identifier of topology element.
#define LWT_COL_EDGE_ALL
#define LWT_COL_NODE_ALL
#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
#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 LWGEOM * _lwt_split_by_nodes(const LWGEOM *g, const LWGEOM *nodes)
Definition: lwgeom_topo.c:5561
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:461
static LWT_ELEMID _lwt_AddLineEdge(LWT_TOPOLOGY *topo, LWLINE *edge, double tol, int handleFaceSplit)
Definition: lwgeom_topo.c:5369
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:172
#define _LWT_MINTOLERANCE(topo, geom)
Definition: lwgeom_topo.c:4942
static 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:178
static LWGEOM * _lwt_toposnap(LWGEOM *src, LWGEOM *tgt, double tol)
Definition: lwgeom_topo.c:414
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:43
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:451
double ymax
Definition: liblwgeom.h:371
double xmax
Definition: liblwgeom.h:369
double ymin
Definition: liblwgeom.h:370
double xmin
Definition: liblwgeom.h:368
uint32_t ngeoms
Definition: liblwgeom.h:594
LWGEOM ** geoms
Definition: liblwgeom.h:589
int32_t srid
Definition: liblwgeom.h:474
POINTARRAY * points
Definition: liblwgeom.h:497
int32_t srid
Definition: liblwgeom.h:498
LWLINE * geom
LWT_ELEMID node_id
LWT_ELEMID containing_face
LWPOINT * geom
const LWT_BE_IFACE * be_iface
uint32_t npoints
Definition: liblwgeom.h:441

References _lwt_AddLineEdge(), _LWT_MINTOLERANCE, _lwt_release_edges(), _lwt_release_nodes(), _lwt_split_by_nodes(), _lwt_toposnap(), LWT_TOPOLOGY_T::be_iface, COLLECTIONTYPE, LWT_ISO_NODE::containing_face, gbox_expand(), LWT_ISO_NODE::geom, LWT_ISO_EDGE::geom, LWCOLLECTION::geoms, LW_ON_INTERRUPT, lwalloc(), lwcollection_as_lwgeom(), lwcollection_construct(), lwcollection_release(), LWDEBUG, LWDEBUGF, LWDEBUGG, lwerror(), lwfree(), lwgeom_as_lwcollection(), lwgeom_as_lwline(), lwgeom_difference(), lwgeom_free(), 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_empty(), lwline_remove_repeated_points(), lwpoint_as_lwgeom(), lwrealloc(), lwt_be_getEdgeWithinBox2D(), lwt_be_getNodeWithinBox2D(), lwt_be_lastErrorMessage(), LWT_COL_EDGE_ALL, LWT_COL_NODE_ALL, LWTFMT_ELEMID, MULTIPOINTTYPE, LWCOLLECTION::ngeoms, LWT_ISO_NODE::node_id, POINTARRAY::npoints, LWLINE::points, LWGEOM::srid, LWLINE::srid, LWT_TOPOLOGY_T::srid, WKT_EXTENDED, GBOX::xmax, GBOX::xmin, 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: