PostGIS  3.4.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 5609 of file lwgeom_topo.c.

5611 {
5612  LWGEOM *geomsbuf[1];
5613  LWGEOM **geoms;
5614  uint32_t ngeoms;
5615  LWGEOM *noded, *tmp;
5616  LWCOLLECTION *col;
5617  LWT_ELEMID *ids;
5618  LWT_ISO_EDGE *edges;
5619  LWT_ISO_NODE *nodes;
5620  uint64_t num, numedges = 0, numnodes = 0;
5621  uint64_t i;
5622  GBOX qbox;
5623  int forward;
5624  int input_was_closed = 0;
5625  POINT4D originalStartPoint;
5626 
5627  if ( lwline_is_closed(line) )
5628  {
5629  input_was_closed = 1;
5630  getPoint4d_p( line->points, 0, &originalStartPoint);
5631  LWDEBUGF(1, "Input line is closed, original point is %g,%g", originalStartPoint.x, originalStartPoint.y);
5632  }
5633 
5634  *nedges = -1; /* error condition, by default */
5635 
5636  /* Get tolerance, if 0 was given */
5637  if ( ! tol ) tol = _LWT_MINTOLERANCE( topo, (LWGEOM*)line );
5638  LWDEBUGF(1, "Working tolerance:%.15g", tol);
5639  LWDEBUGF(1, "Input line has srid=%d", line->srid);
5640 
5641  /* Remove consecutive vertices below given tolerance upfront */
5642  if ( tol )
5643  {{
5645  tmp = lwline_as_lwgeom(clean); /* NOTE: might collapse to non-simple */
5646  LWDEBUGG(1, tmp, "Repeated-point removed");
5647  }} else tmp=(LWGEOM*)line;
5648 
5649  /* 1. Self-node */
5650  noded = lwgeom_node((LWGEOM*)tmp);
5651  if ( tmp != (LWGEOM*)line ) lwgeom_free(tmp);
5652  if ( ! noded ) return NULL; /* should have called lwerror already */
5653  LWDEBUGG(1, noded, "Noded");
5654 
5655  qbox = *lwgeom_get_bbox( lwline_as_lwgeom(line) );
5656  LWDEBUGF(1, "Line BOX is %.15g %.15g, %.15g %.15g", qbox.xmin, qbox.ymin,
5657  qbox.xmax, qbox.ymax);
5658  gbox_expand(&qbox, tol);
5659  LWDEBUGF(1, "BOX expanded by %g is %.15g %.15g, %.15g %.15g",
5660  tol, qbox.xmin, qbox.ymin, qbox.xmax, qbox.ymax);
5661 
5662  LWGEOM **nearby = 0;
5663  int nearbyindex = 0;
5664  int nearbycount = 0;
5665 
5666  /* 2.0. Find edges falling within tol distance */
5667  edges = lwt_be_getEdgeWithinBox2D( topo, &qbox, &numedges, LWT_COL_EDGE_ALL, 0 );
5668  if (numedges == UINT64_MAX)
5669  {
5670  lwgeom_free(noded);
5671  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5672  return NULL;
5673  }
5674  LWDEBUGF(1, "Line has %d points, its bbox intersects %d edges bboxes",
5675  line->points->npoints, numedges);
5676  if ( numedges )
5677  {{
5678  /* collect those whose distance from us is < tol */
5679  nearbycount += numedges;
5680  nearby = lwalloc(numedges * sizeof(LWGEOM *));
5681  for (i=0; i<numedges; ++i)
5682  {
5683  LW_ON_INTERRUPT(return NULL);
5684  LWT_ISO_EDGE *e = &(edges[i]);
5685  LWGEOM *g = lwline_as_lwgeom(e->geom);
5686  LWDEBUGF(2, "Computing distance from edge %d having %d points", i, e->geom->points->npoints);
5687  double dist = lwgeom_mindistance2d(g, noded);
5688  /* must be closer than tolerated, unless distance is zero */
5689  if ( dist && dist >= tol ) continue;
5690  nearby[nearbyindex++] = g;
5691  }
5692  LWDEBUGF(1, "Found %d edges closer than tolerance (%g)", nearbyindex, tol);
5693  }}
5694  int nearbyedgecount = nearbyindex;
5695 
5696  /* 2.1. Find isolated nodes falling within tol distance
5697  *
5698  * TODO: add backend-interface support for only getting isolated nodes
5699  */
5700  nodes = lwt_be_getNodeWithinBox2D( topo, &qbox, &numnodes, LWT_COL_NODE_ALL, 0 );
5701  if (numnodes == UINT64_MAX)
5702  {
5703  lwgeom_free(noded);
5704  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5705  return NULL;
5706  }
5707  LWDEBUGF(1, "Line bbox intersects %d nodes bboxes", numnodes);
5708  if ( numnodes )
5709  {{
5710  /* collect those whose distance from us is < tol */
5711  nearbycount = nearbyedgecount + numnodes;
5712  nearby = nearby ?
5713  lwrealloc(nearby, nearbycount * sizeof(LWGEOM *))
5714  :
5715  lwalloc(nearbycount * sizeof(LWGEOM *))
5716  ;
5717  int nn = 0;
5718  for (i=0; i<numnodes; ++i)
5719  {
5720  LWT_ISO_NODE *n = &(nodes[i]);
5721  if ( n->containing_face == -1 ) continue; /* skip not-isolated nodes */
5722  LWGEOM *g = lwpoint_as_lwgeom(n->geom);
5723  double dist = lwgeom_mindistance2d(g, noded);
5724  /* must be closer than tolerated, unless distance is zero */
5725  if ( dist && dist >= tol )
5726  {
5727  LWDEBUGF(1, "Node %d is %g units away, we tolerate only %g", n->node_id, dist, tol);
5728  continue;
5729  }
5730  nearby[nearbyindex++] = g;
5731  nn = nn + 1;
5732  }
5733  LWDEBUGF(1, "Found %d isolated nodes closer than tolerance (%g)", nn, tol);
5734  }}
5735  int nearbynodecount = nearbyindex - nearbyedgecount;
5736  nearbycount = nearbyindex;
5737 
5738  LWDEBUGF(1, "Number of nearby elements is %d", nearbycount);
5739 
5740  /* 2.2. Snap to nearby elements */
5741  if ( nearbycount )
5742  {{
5743  LWCOLLECTION *col;
5744  LWGEOM *elems;
5745 
5747  NULL, nearbycount, nearby);
5748  elems = lwcollection_as_lwgeom(col);
5749 
5750  LWDEBUGG(1, elems, "Collected nearby elements");
5751 
5752  tmp = _lwt_toposnap(noded, elems, tol);
5753  lwgeom_free(noded);
5754  noded = tmp;
5755  LWDEBUGG(1, noded, "Elements-snapped");
5756  if ( input_was_closed )
5757  {{
5758  /* Recompute start point in case it moved */
5759  LWLINE *scrolled = lwgeom_as_lwline(noded);
5760  if (scrolled)
5761  {
5762  getPoint4d_p( scrolled->points, 0, &originalStartPoint);
5763  LWDEBUGF(1, "Closed input line start point after snap %g,%g", originalStartPoint.x, originalStartPoint.y);
5764  }
5765  }}
5766 
5767  /* will not release the geoms array */
5768  lwcollection_release(col);
5769 
5770  /*
5771  -- re-node to account for ST_Snap introduced self-intersections
5772  -- See http://trac.osgeo.org/postgis/ticket/1714
5773  -- TODO: consider running UnaryUnion once after all noding
5774  */
5775  tmp = lwgeom_unaryunion(noded);
5776  lwgeom_free(noded);
5777  noded = tmp;
5778  LWDEBUGG(1, noded, "Unary-unioned");
5779  }}
5780 
5781  /* 2.3. Node with nearby edges */
5782  if ( nearbyedgecount )
5783  {{
5784  LWCOLLECTION *col;
5785  LWGEOM *iedges; /* just an alias for col */
5786  LWGEOM *diff, *xset;
5787 
5788  LWDEBUGF(1, "Line intersects %d edges", nearbyedgecount);
5789 
5791  NULL, nearbyedgecount, nearby);
5792  iedges = lwcollection_as_lwgeom(col);
5793  LWDEBUGG(1, iedges, "Collected edges");
5794 
5795  LWDEBUGF(1, "Diffing noded, with srid=%d "
5796  "and interesecting edges, with srid=%d",
5797  noded->srid, iedges->srid);
5798  diff = lwgeom_difference(noded, iedges);
5799  LWDEBUGG(1, diff, "Differenced");
5800 
5801  LWDEBUGF(1, "Intersecting noded, with srid=%d "
5802  "and interesecting edges, with srid=%d",
5803  noded->srid, iedges->srid);
5804  xset = lwgeom_intersection(noded, iedges);
5805  LWDEBUGG(1, xset, "Intersected");
5806  lwgeom_free(noded);
5807 
5808  /* We linemerge here because INTERSECTION, as of GEOS 3.8,
5809  * will result in shared segments being output as multiple
5810  * lines rather than a single line. Example:
5811 
5812  INTERSECTION(
5813  'LINESTRING(0 0, 5 0, 8 0, 10 0,12 0)',
5814  'LINESTRING(5 0, 8 0, 10 0)'
5815  )
5816  ==
5817  MULTILINESTRING((5 0,8 0),(8 0,10 0))
5818 
5819  * We will re-split in a subsequent step, by splitting
5820  * the final line with pre-existing nodes
5821  */
5822  LWDEBUG(1, "Linemerging intersection");
5823  tmp = lwgeom_linemerge(xset);
5824  LWDEBUGG(1, tmp, "Linemerged");
5825  lwgeom_free(xset);
5826  xset = tmp;
5827 
5828  /*
5829  * Here we union the (linemerged) intersection with
5830  * the difference (new lines)
5831  */
5832  LWDEBUG(1, "Unioning difference and (linemerged) intersection");
5833  noded = lwgeom_union(diff, xset);
5834  LWDEBUGG(1, noded, "Diff-Xset Unioned");
5835  lwgeom_free(xset);
5836  lwgeom_free(diff);
5837 
5838  /* will not release the geoms array */
5839  lwcollection_release(col);
5840 
5841  if ( input_was_closed )
5842  {{
5843  LWLINE *scrolled = lwgeom_as_lwline(noded);
5844  if (scrolled) {
5845  if ( lwline_is_closed(scrolled) ) {
5846  ptarray_scroll_in_place(scrolled->points, &originalStartPoint);
5847  }
5848  else {
5849  LWDEBUGG(1, lwline_as_lwgeom(scrolled), "Linemerged intersected input is not closed anymore");
5850  }
5851  }
5852  else {
5853  LWDEBUGG(1, xset, "Linemerged intersected input is not a line anymore");
5854  }
5855  }}
5856 
5857 
5858  }}
5859 
5860 
5861  /* 2.4. Split by pre-existing nodes
5862  *
5863  * Pre-existing nodes are isolated nodes AND endpoints
5864  * of intersecting edges
5865  */
5866  if ( nearbyedgecount )
5867  {
5868  nearbycount += nearbyedgecount * 2; /* make space for endpoints */
5869  nearby = lwrealloc(nearby, nearbycount * sizeof(LWGEOM *));
5870  for (int i=0; i<nearbyedgecount; i++)
5871  {
5872  LWLINE *edge = lwgeom_as_lwline(nearby[i]);
5873  LWPOINT *startNode = lwline_get_lwpoint(edge, 0);
5874  LWPOINT *endNode = lwline_get_lwpoint(edge, edge->points->npoints-1);
5875  /* TODO: only add if within distance to noded AND if not duplicated */
5876  nearby[nearbyindex++] = lwpoint_as_lwgeom(startNode);
5877  nearbynodecount++;
5878  nearby[nearbyindex++] = lwpoint_as_lwgeom(endNode);
5879  nearbynodecount++;
5880  }
5881  }
5882  if ( nearbynodecount )
5883  {
5885  NULL, nearbynodecount,
5886  nearby + nearbyedgecount);
5887  LWGEOM *inodes = lwcollection_as_lwgeom(col);
5888  /* TODO: use lwgeom_split of lwgeom_union ... */
5889  tmp = _lwt_split_by_nodes(noded, inodes);
5890  lwgeom_free(noded);
5891  noded = tmp;
5892  LWDEBUGG(1, noded, "Node-split");
5893  /* will not release the geoms array */
5894  lwcollection_release(col);
5895  }
5896 
5897 
5898  LWDEBUG(1, "Freeing up nearby elements");
5899 
5900  /* TODO: free up endpoints of nearbyedges */
5901  if ( nearby ) lwfree(nearby);
5902  if ( nodes ) _lwt_release_nodes(nodes, numnodes);
5903  if ( edges ) _lwt_release_edges(edges, numedges);
5904 
5905  LWDEBUGG(1, noded, "Finally-noded");
5906 
5907  /* 3. For each (now-noded) segment, insert an edge */
5908  col = lwgeom_as_lwcollection(noded);
5909  if ( col )
5910  {
5911  LWDEBUG(1, "Noded line was a collection");
5912  geoms = col->geoms;
5913  ngeoms = col->ngeoms;
5914  }
5915  else
5916  {
5917  LWDEBUG(1, "Noded line was a single geom");
5918  geomsbuf[0] = noded;
5919  geoms = geomsbuf;
5920  ngeoms = 1;
5921  }
5922 
5923  LWDEBUGF(1, "Line was split into %d edges", ngeoms);
5924 
5925  /* TODO: refactor to first add all nodes (re-snapping edges if
5926  * needed) and then check all edges for existing already
5927  * ( so to save a DB scan for each edge to be added )
5928  */
5929  ids = lwalloc(sizeof(LWT_ELEMID)*ngeoms);
5930  num = 0;
5931  for ( i=0; i<ngeoms; ++i )
5932  {
5933  LWT_ELEMID id;
5934  LWGEOM *g = geoms[i];
5935  g->srid = noded->srid;
5936 
5937 #if POSTGIS_DEBUG_LEVEL > 0
5938  {
5939  size_t sz;
5940  char *wkt1 = lwgeom_to_wkt(g, WKT_EXTENDED, 15, &sz);
5941  LWDEBUGF(1, "Component %d of split line is: %s", i, wkt1);
5942  lwfree(wkt1);
5943  }
5944 #endif
5945 
5946  id = _lwt_AddLineEdge( topo, lwgeom_as_lwline(g), tol, handleFaceSplit, &forward );
5947  LWDEBUGF(1, "_lwt_AddLineEdge returned %" LWTFMT_ELEMID, id);
5948  if ( id < 0 )
5949  {
5950  lwgeom_free(noded);
5951  lwfree(ids);
5952  return NULL;
5953  }
5954  if ( ! id )
5955  {
5956  LWDEBUGF(1, "Component %d of split line collapsed", i);
5957  continue;
5958  }
5959 
5960  LWDEBUGF(1, "Component %d of split line is %s edge %" LWTFMT_ELEMID,
5961  i, forward ? "forward" : "backward", id);
5962  ids[num++] = forward ? id : -id; /* TODO: skip duplicates */
5963  }
5964 
5965  LWDEBUGG(1, noded, "Noded before free");
5966  lwgeom_free(noded);
5967 
5968  /* TODO: XXX remove duplicated ids if not done before */
5969 
5970  *nedges = num;
5971  return ids;
5972 }
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: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:1155
LWGEOM * lwgeom_intersection(const LWGEOM *geom1, const LWGEOM *geom2)
#define WKT_EXTENDED
Definition: liblwgeom.h:2186
#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:235
void lwfree(void *mem)
Definition: lwutil.c:242
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:197
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
#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:2181
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:125
static LWGEOM * _lwt_split_by_nodes(const LWGEOM *g, const LWGEOM *nodes)
Definition: lwgeom_topo.c:5587
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:481
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:178
#define _LWT_MINTOLERANCE(topo, geom)
Definition: lwgeom_topo.c:4923
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:184
static LWT_ELEMID _lwt_AddLineEdge(LWT_TOPOLOGY *topo, LWLINE *edge, double tol, int handleFaceSplit, int *forward)
Definition: lwgeom_topo.c:5394
static LWGEOM * _lwt_toposnap(LWGEOM *src, LWGEOM *tgt, double tol)
Definition: lwgeom_topo.c:434
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:43
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:471
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
const LWT_BE_IFACE * be_iface
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(), LWT_TOPOLOGY_T::be_iface, 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(), 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_closed(), 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, 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: