PostGIS  2.5.7dev-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 5517 of file lwgeom_topo.c.

5519 {
5520  LWGEOM *geomsbuf[1];
5521  LWGEOM **geoms;
5522  int ngeoms;
5523  LWGEOM *noded, *tmp;
5524  LWCOLLECTION *col;
5525  LWT_ELEMID *ids;
5526  LWT_ISO_EDGE *edges;
5527  LWT_ISO_NODE *nodes;
5528  int num, numedges=0, numnodes=0;
5529  int i;
5530  GBOX qbox;
5531 
5532  *nedges = -1; /* error condition, by default */
5533 
5534  /* Get tolerance, if 0 was given */
5535  if ( ! tol ) tol = _LWT_MINTOLERANCE( topo, (LWGEOM*)line );
5536  LWDEBUGF(1, "Working tolerance:%.15g", tol);
5537  LWDEBUGF(1, "Input line has srid=%d", line->srid);
5538 
5539  /* Remove consecutive vertices below given tolerance upfront */
5540  if ( tol )
5541  {{
5543  tmp = lwline_as_lwgeom(clean); /* NOTE: might collapse to non-simple */
5544  LWDEBUGG(1, tmp, "Repeated-point removed");
5545  }} else tmp=(LWGEOM*)line;
5546 
5547  /* 1. Self-node */
5548  noded = lwgeom_node((LWGEOM*)tmp);
5549  if ( tmp != (LWGEOM*)line ) lwgeom_free(tmp);
5550  if ( ! noded ) return NULL; /* should have called lwerror already */
5551  LWDEBUGG(1, noded, "Noded");
5552 
5553  qbox = *lwgeom_get_bbox( lwline_as_lwgeom(line) );
5554  LWDEBUGF(1, "Line BOX is %.15g %.15g, %.15g %.15g", qbox.xmin, qbox.ymin,
5555  qbox.xmax, qbox.ymax);
5556  gbox_expand(&qbox, tol);
5557  LWDEBUGF(1, "BOX expanded by %g is %.15g %.15g, %.15g %.15g",
5558  tol, qbox.xmin, qbox.ymin, qbox.xmax, qbox.ymax);
5559 
5560  LWGEOM **nearby = 0;
5561  int nearbyindex=0;
5562  int nearbycount = 0;
5563 
5564  /* 2. Node to edges falling within tol distance */
5565  edges = lwt_be_getEdgeWithinBox2D( topo, &qbox, &numedges, LWT_COL_EDGE_ALL, 0 );
5566  if ( numedges == -1 )
5567  {
5568  lwgeom_free(noded);
5569  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5570  return NULL;
5571  }
5572  LWDEBUGF(1, "Line has %d points, its bbox intersects %d edges bboxes",
5573  line->points->npoints, numedges);
5574  if ( numedges )
5575  {{
5576  /* collect those whose distance from us is < tol */
5577  nearbycount += numedges;
5578  nearby = lwalloc(nearbycount * sizeof(LWGEOM *));
5579  for (i=0; i<numedges; ++i)
5580  {
5581  LW_ON_INTERRUPT(return NULL);
5582  LWT_ISO_EDGE *e = &(edges[i]);
5583  LWGEOM *g = lwline_as_lwgeom(e->geom);
5584  LWDEBUGF(2, "Computing distance from edge %d having %d points", i, e->geom->points->npoints);
5585  double dist = lwgeom_mindistance2d(g, noded);
5586  /* must be closer than tolerated, unless distance is zero */
5587  if ( dist && dist >= tol ) continue;
5588  nearby[nearbyindex++] = g;
5589  }
5590  LWDEBUGF(2, "Found %d lines closer than tolerance (%g)", nearbyindex, tol);
5591  if ( nearbyindex )
5592  {{
5593  LWCOLLECTION *col;
5594  LWGEOM *iedges; /* just an alias for col */
5595  LWGEOM *snapped;
5596  LWGEOM *set1, *set2;
5597 
5598  LWDEBUGF(1, "Line intersects %d edges", nearbyindex);
5599 
5601  NULL, nearbyindex, nearby);
5602  iedges = lwcollection_as_lwgeom(col);
5603  LWDEBUGG(1, iedges, "Collected edges");
5604  LWDEBUGF(1, "Snapping noded, with srid=%d "
5605  "to interesecting edges, with srid=%d",
5606  noded->srid, iedges->srid);
5607  snapped = _lwt_toposnap(noded, iedges, tol);
5608  lwgeom_free(noded);
5609  LWDEBUGG(1, snapped, "Snapped");
5610  LWDEBUGF(1, "Diffing snapped, with srid=%d "
5611  "and interesecting edges, with srid=%d",
5612  snapped->srid, iedges->srid);
5613  noded = lwgeom_difference(snapped, iedges);
5614  LWDEBUGG(1, noded, "Differenced");
5615  LWDEBUGF(1, "Intersecting snapped, with srid=%d "
5616  "and interesecting edges, with srid=%d",
5617  snapped->srid, iedges->srid);
5618  set1 = lwgeom_intersection(snapped, iedges);
5619  LWDEBUGG(1, set1, "Intersected");
5620  lwgeom_free(snapped);
5621  LWDEBUGF(1, "Linemerging set1, with srid=%d", set1->srid);
5622  set2 = lwgeom_linemerge(set1);
5623  LWDEBUGG(1, set2, "Linemerged");
5624  LWDEBUGG(1, noded, "Noded");
5625  lwgeom_free(set1);
5626  LWDEBUGF(1, "Unioning noded, with srid=%d "
5627  "and set2, with srid=%d", noded->srid, set2->srid);
5628  set1 = lwgeom_union(noded, set2);
5629  lwgeom_free(set2);
5630  lwgeom_free(noded);
5631  noded = set1;
5632  LWDEBUGG(1, set1, "Unioned");
5633 
5634  /* will not release the geoms array */
5635  lwcollection_release(col);
5636  }}
5637  }}
5638 
5639  /* 2.1. Node with existing nodes within tol
5640  * TODO: check if we should be only considering _isolated_ nodes! */
5641  nodes = lwt_be_getNodeWithinBox2D( topo, &qbox, &numnodes, LWT_COL_NODE_ALL, 0 );
5642  if ( numnodes == -1 )
5643  {
5644  lwgeom_free(noded);
5645  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5646  return NULL;
5647  }
5648  LWDEBUGF(1, "Line bbox intersects %d nodes bboxes", numnodes);
5649  int nearbyedgecount = nearbyindex;
5650  if ( numnodes )
5651  {{
5652  /* collect those whose distance from us is < tol */
5653  nearbycount = nearbyindex + numnodes;
5654  nearby = nearby ?
5655  lwrealloc(nearby, nearbycount * sizeof(LWGEOM *))
5656  :
5657  lwalloc(nearbycount * sizeof(LWGEOM *))
5658  ;
5659  int nn = 0;
5660  for (i=0; i<numnodes; ++i)
5661  {
5662  LWT_ISO_NODE *n = &(nodes[i]);
5663  LWGEOM *g = lwpoint_as_lwgeom(n->geom);
5664  double dist = lwgeom_mindistance2d(g, noded);
5665  /* must be closer than tolerated, unless distance is zero */
5666  if ( dist && dist >= tol ) continue;
5667  nearby[nearbyindex++] = g;
5668  ++nn;
5669  }
5670  }}
5671 
5672  LWDEBUGF(1, "Number of nearby elements is %d", nearbyindex);
5673 
5674  if ( numnodes )
5675  {{
5676  LWCOLLECTION *col;
5677  LWGEOM *elems;
5678 
5680  NULL, nearbyindex, nearby);
5681  elems = lwcollection_as_lwgeom(col);
5682 
5683  LWDEBUGG(1, elems, "Collected nearby elements");
5684 
5685  tmp = _lwt_toposnap(noded, elems, tol);
5686  lwgeom_free(noded);
5687  noded = tmp;
5688  LWDEBUGG(1, noded, "Elements-snapped");
5689 
5690  /* will not release the geoms array */
5691  lwcollection_release(col);
5692 
5693  if ( numnodes )
5694  {{
5696  NULL, nearbyindex-nearbyedgecount,
5697  nearby + nearbyedgecount);
5698  LWGEOM *inodes = lwcollection_as_lwgeom(col);
5699  tmp = _lwt_split_by_nodes(noded, inodes);
5700  lwgeom_free(noded);
5701  noded = tmp;
5702  LWDEBUGG(1, noded, "Node-split");
5703  /* will not release the geoms array */
5704  lwcollection_release(col);
5705  }}
5706 
5707  /*
5708  -- re-node to account for ST_Snap introduced self-intersections
5709  -- See http://trac.osgeo.org/postgis/ticket/1714
5710  -- TODO: consider running UnaryUnion once after all noding
5711  */
5712  tmp = lwgeom_unaryunion(noded);
5713  lwgeom_free(noded);
5714  noded = tmp;
5715  LWDEBUGG(1, noded, "Unary-unioned");
5716 
5717  }}
5718 
5719  LWDEBUG(1, "Freeing up nearby elements");
5720 
5721  if ( nearby ) lwfree(nearby);
5722  if ( nodes ) _lwt_release_nodes(nodes, numnodes);
5723  if ( edges ) _lwt_release_edges(edges, numedges);
5724 
5725  LWDEBUGG(1, noded, "Finally-noded");
5726 
5727  /* 3. For each (now-noded) segment, insert an edge */
5728  col = lwgeom_as_lwcollection(noded);
5729  if ( col )
5730  {
5731  LWDEBUG(1, "Noded line was a collection");
5732  geoms = col->geoms;
5733  ngeoms = col->ngeoms;
5734  }
5735  else
5736  {
5737  LWDEBUG(1, "Noded line was a single geom");
5738  geomsbuf[0] = noded;
5739  geoms = geomsbuf;
5740  ngeoms = 1;
5741  }
5742 
5743  LWDEBUGF(1, "Line was split into %d edges", ngeoms);
5744 
5745  /* TODO: refactor to first add all nodes (re-snapping edges if
5746  * needed) and then check all edges for existing already
5747  * ( so to save a DB scan for each edge to be added )
5748  */
5749  ids = lwalloc(sizeof(LWT_ELEMID)*ngeoms);
5750  num = 0;
5751  for ( i=0; i<ngeoms; ++i )
5752  {
5753  LWT_ELEMID id;
5754  LWGEOM *g = geoms[i];
5755  g->srid = noded->srid;
5756 
5757 #if POSTGIS_DEBUG_LEVEL > 0
5758  {
5759  size_t sz;
5760  char *wkt1 = lwgeom_to_wkt(g, WKT_EXTENDED, 15, &sz);
5761  LWDEBUGF(1, "Component %d of split line is: %s", i, wkt1);
5762  lwfree(wkt1);
5763  }
5764 #endif
5765 
5766  id = _lwt_AddLineEdge( topo, lwgeom_as_lwline(g), tol, handleFaceSplit );
5767  LWDEBUGF(1, "_lwt_AddLineEdge returned %" LWTFMT_ELEMID, id);
5768  if ( id < 0 )
5769  {
5770  lwgeom_free(noded);
5771  lwfree(ids);
5772  return NULL;
5773  }
5774  if ( ! id )
5775  {
5776  LWDEBUGF(1, "Component %d of split line collapsed", i);
5777  continue;
5778  }
5779 
5780  LWDEBUGF(1, "Component %d of split line is edge %" LWTFMT_ELEMID,
5781  i, id);
5782  ids[num++] = id; /* TODO: skip duplicates */
5783  }
5784 
5785  LWDEBUGG(1, noded, "Noded before free");
5786  lwgeom_free(noded);
5787 
5788  /* TODO: XXX remove duplicated ids if not done before */
5789 
5790  *nedges = num;
5791  return ids;
5792 }
void gbox_expand(GBOX *g, double d)
Move the box minimums down and the maximums up by the distance provided.
Definition: g_box.c:104
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:170
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:330
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:300
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
LWGEOM * lwgeom_node(const LWGEOM *lwgeom_in)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
LWGEOM * lwgeom_intersection(const LWGEOM *geom1, const LWGEOM *geom2)
#define WKT_EXTENDED
Definition: liblwgeom.h:2077
#define MULTIPOINTTYPE
Definition: liblwgeom.h:88
LWGEOM * lwgeom_difference(const LWGEOM *geom1, const LWGEOM *geom2)
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:237
void lwfree(void *mem)
Definition: lwutil.c:244
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:335
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:202
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:676
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:734
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:43
LWGEOM * lwgeom_linemerge(const LWGEOM *geom1)
void * lwalloc(size_t size)
Definition: lwutil.c:229
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:224
LWGEOM * lwgeom_union(const LWGEOM *geom1, const LWGEOM *geom2)
LWGEOM * lwline_remove_repeated_points(const LWLINE *in, double tolerance)
Definition: lwline.c:448
#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:120
static LWGEOM * _lwt_split_by_nodes(const LWGEOM *g, const LWGEOM *nodes)
Definition: lwgeom_topo.c:5495
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:467
static LWT_ISO_NODE * lwt_be_getNodeWithinBox2D(const LWT_TOPOLOGY *topo, const GBOX *box, int *numelems, int fields, int limit)
Definition: lwgeom_topo.c:171
static LWT_ELEMID _lwt_AddLineEdge(LWT_TOPOLOGY *topo, LWLINE *edge, double tol, int handleFaceSplit)
Definition: lwgeom_topo.c:5303
#define _LWT_MINTOLERANCE(topo, geom)
Definition: lwgeom_topo.c:4877
static LWGEOM * _lwt_toposnap(LWGEOM *src, LWGEOM *tgt, double tol)
Definition: lwgeom_topo.c:420
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:44
static LWT_ISO_EDGE * lwt_be_getEdgeWithinBox2D(const LWT_TOPOLOGY *topo, const GBOX *box, int *numelems, int fields, int limit)
Definition: lwgeom_topo.c:179
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:457
double ymax
Definition: liblwgeom.h:298
double xmax
Definition: liblwgeom.h:296
double ymin
Definition: liblwgeom.h:297
double xmin
Definition: liblwgeom.h:295
uint32_t ngeoms
Definition: liblwgeom.h:510
LWGEOM ** geoms
Definition: liblwgeom.h:512
int32_t srid
Definition: liblwgeom.h:402
POINTARRAY * points
Definition: liblwgeom.h:425
int32_t srid
Definition: liblwgeom.h:424
LWLINE * geom
LWPOINT * geom
const LWT_BE_IFACE * be_iface
uint32_t npoints
Definition: liblwgeom.h:374

References _lwt_AddLineEdge(), _LWT_MINTOLERANCE, _lwt_release_edges(), _lwt_release_nodes(), _lwt_split_by_nodes(), _lwt_toposnap(), LWT_TOPOLOGY_T::be_iface, COLLECTIONTYPE, 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_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, 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: