PostGIS  3.2.2dev-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 5669 of file lwgeom_topo.c.

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