PostGIS  2.5.0dev-r@@SVN_REVISION@@
static LWT_ELEMID* _lwt_AddLine ( LWT_TOPOLOGY topo,
LWLINE line,
double  tol,
int *  nedges,
int  handleFaceSplit 
)
static

Definition at line 5719 of file lwgeom_topo.c.

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, LWT_TOPOLOGY_T::srid, LWGEOM::srid, LWLINE::srid, WKT_EXTENDED, GBOX::xmax, GBOX::xmin, GBOX::ymax, and GBOX::ymin.

Referenced by lwt_AddLine(), and lwt_AddLineNoFace().

5721 {
5722  LWGEOM *geomsbuf[1];
5723  LWGEOM **geoms;
5724  int ngeoms;
5725  LWGEOM *noded, *tmp;
5726  LWCOLLECTION *col;
5727  LWT_ELEMID *ids;
5728  LWT_ISO_EDGE *edges;
5729  LWT_ISO_NODE *nodes;
5730  int num, numedges=0, numnodes=0;
5731  int i;
5732  GBOX qbox;
5733 
5734  *nedges = -1; /* error condition, by default */
5735 
5736  /* Get tolerance, if 0 was given */
5737  if ( ! tol ) tol = _LWT_MINTOLERANCE( topo, (LWGEOM*)line );
5738  LWDEBUGF(1, "Working tolerance:%.15g", tol);
5739  LWDEBUGF(1, "Input line has srid=%d", line->srid);
5740 
5741  /* Remove consecutive vertices below given tolerance upfront */
5742  if ( tol )
5743  {{
5745  tmp = lwline_as_lwgeom(clean); /* NOTE: might collapse to non-simple */
5746  LWDEBUGG(1, tmp, "Repeated-point removed");
5747  }} else tmp=(LWGEOM*)line;
5748 
5749  /* 1. Self-node */
5750  noded = lwgeom_node((LWGEOM*)tmp);
5751  if ( tmp != (LWGEOM*)line ) lwgeom_free(tmp);
5752  if ( ! noded ) return NULL; /* should have called lwerror already */
5753  LWDEBUGG(1, noded, "Noded");
5754 
5755  qbox = *lwgeom_get_bbox( lwline_as_lwgeom(line) );
5756  LWDEBUGF(1, "Line BOX is %.15g %.15g, %.15g %.15g", qbox.xmin, qbox.ymin,
5757  qbox.xmax, qbox.ymax);
5758  gbox_expand(&qbox, tol);
5759  LWDEBUGF(1, "BOX expanded by %g is %.15g %.15g, %.15g %.15g",
5760  tol, qbox.xmin, qbox.ymin, qbox.xmax, qbox.ymax);
5761 
5762  LWGEOM **nearby = 0;
5763  int nearbyindex=0;
5764  int nearbycount = 0;
5765 
5766  /* 2. Node to edges falling within tol distance */
5767  edges = lwt_be_getEdgeWithinBox2D( topo, &qbox, &numedges, LWT_COL_EDGE_ALL, 0 );
5768  if ( numedges == -1 )
5769  {
5770  lwgeom_free(noded);
5771  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5772  return NULL;
5773  }
5774  LWDEBUGF(1, "Line has %d points, its bbox intersects %d edges bboxes",
5775  line->points->npoints, numedges);
5776  if ( numedges )
5777  {{
5778  /* collect those whose distance from us is < tol */
5779  nearbycount += numedges;
5780  nearby = lwalloc(nearbycount * sizeof(LWGEOM *));
5781  for (i=0; i<numedges; ++i)
5782  {
5783  LW_ON_INTERRUPT(return NULL);
5784  LWT_ISO_EDGE *e = &(edges[i]);
5785  LWGEOM *g = lwline_as_lwgeom(e->geom);
5786  LWDEBUGF(2, "Computing distance from edge %d having %d points", i, e->geom->points->npoints);
5787  double dist = lwgeom_mindistance2d(g, noded);
5788  /* must be closer than tolerated, unless distance is zero */
5789  if ( dist && dist >= tol ) continue;
5790  nearby[nearbyindex++] = g;
5791  }
5792  LWDEBUGF(2, "Found %d lines closer than tolerance (%g)", nearbyindex, tol);
5793  if ( nearbyindex )
5794  {{
5795  LWCOLLECTION *col;
5796  LWGEOM *iedges; /* just an alias for col */
5797  LWGEOM *snapped;
5798  LWGEOM *set1, *set2;
5799 
5800  LWDEBUGF(1, "Line intersects %d edges", nearbyindex);
5801 
5803  NULL, nearbyindex, nearby);
5804  iedges = lwcollection_as_lwgeom(col);
5805  LWDEBUGG(1, iedges, "Collected edges");
5806  LWDEBUGF(1, "Snapping noded, with srid=%d "
5807  "to interesecting edges, with srid=%d",
5808  noded->srid, iedges->srid);
5809  snapped = _lwt_toposnap(noded, iedges, tol);
5810  lwgeom_free(noded);
5811  LWDEBUGG(1, snapped, "Snapped");
5812  LWDEBUGF(1, "Diffing snapped, with srid=%d "
5813  "and interesecting edges, with srid=%d",
5814  snapped->srid, iedges->srid);
5815  noded = lwgeom_difference(snapped, iedges);
5816  LWDEBUGG(1, noded, "Differenced");
5817  LWDEBUGF(1, "Intersecting snapped, with srid=%d "
5818  "and interesecting edges, with srid=%d",
5819  snapped->srid, iedges->srid);
5820  set1 = lwgeom_intersection(snapped, iedges);
5821  LWDEBUGG(1, set1, "Intersected");
5822  lwgeom_free(snapped);
5823  LWDEBUGF(1, "Linemerging set1, with srid=%d", set1->srid);
5824  set2 = lwgeom_linemerge(set1);
5825  LWDEBUGG(1, set2, "Linemerged");
5826  LWDEBUGG(1, noded, "Noded");
5827  lwgeom_free(set1);
5828  LWDEBUGF(1, "Unioning noded, with srid=%d "
5829  "and set2, with srid=%d", noded->srid, set2->srid);
5830  set1 = lwgeom_union(noded, set2);
5831  lwgeom_free(set2);
5832  lwgeom_free(noded);
5833  noded = set1;
5834  LWDEBUGG(1, set1, "Unioned");
5835 
5836  /* will not release the geoms array */
5837  lwcollection_release(col);
5838  }}
5839  }}
5840 
5841  /* 2.1. Node with existing nodes within tol
5842  * TODO: check if we should be only considering _isolated_ nodes! */
5843  nodes = lwt_be_getNodeWithinBox2D( topo, &qbox, &numnodes, LWT_COL_NODE_ALL, 0 );
5844  if ( numnodes == -1 )
5845  {
5846  lwgeom_free(noded);
5847  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
5848  return NULL;
5849  }
5850  LWDEBUGF(1, "Line bbox intersects %d nodes bboxes", numnodes);
5851  int nearbyedgecount = nearbyindex;
5852  if ( numnodes )
5853  {{
5854  /* collect those whose distance from us is < tol */
5855  nearbycount = nearbyindex + numnodes;
5856  nearby = nearby ?
5857  lwrealloc(nearby, nearbycount * sizeof(LWGEOM *))
5858  :
5859  lwalloc(nearbycount * sizeof(LWGEOM *))
5860  ;
5861  int nn = 0;
5862  for (i=0; i<numnodes; ++i)
5863  {
5864  LWT_ISO_NODE *n = &(nodes[i]);
5865  LWGEOM *g = lwpoint_as_lwgeom(n->geom);
5866  double dist = lwgeom_mindistance2d(g, noded);
5867  /* must be closer than tolerated, unless distance is zero */
5868  if ( dist && dist >= tol ) continue;
5869  nearby[nearbyindex++] = g;
5870  ++nn;
5871  }
5872  }}
5873 
5874  LWDEBUGF(1, "Number of nearby elements is %d", nearbyindex);
5875 
5876  if ( numnodes )
5877  {{
5878  LWCOLLECTION *col;
5879  LWGEOM *elems;
5880 
5882  NULL, nearbyindex, nearby);
5883  elems = lwcollection_as_lwgeom(col);
5884 
5885  LWDEBUGG(1, elems, "Collected nearby elements");
5886 
5887  tmp = _lwt_toposnap(noded, elems, tol);
5888  lwgeom_free(noded);
5889  noded = tmp;
5890  LWDEBUGG(1, noded, "Elements-snapped");
5891 
5892  /* will not release the geoms array */
5893  lwcollection_release(col);
5894 
5895  if ( numnodes )
5896  {{
5898  NULL, nearbyindex-nearbyedgecount,
5899  nearby + nearbyedgecount);
5900  LWGEOM *inodes = lwcollection_as_lwgeom(col);
5901  tmp = _lwt_split_by_nodes(noded, inodes);
5902  lwgeom_free(noded);
5903  noded = tmp;
5904  LWDEBUGG(1, noded, "Node-split");
5905  /* will not release the geoms array */
5906  lwcollection_release(col);
5907  }}
5908 
5909  /*
5910  -- re-node to account for ST_Snap introduced self-intersections
5911  -- See http://trac.osgeo.org/postgis/ticket/1714
5912  -- TODO: consider running UnaryUnion once after all noding
5913  */
5914  tmp = lwgeom_unaryunion(noded);
5915  lwgeom_free(noded);
5916  noded = tmp;
5917  LWDEBUGG(1, noded, "Unary-unioned");
5918 
5919  }}
5920 
5921  LWDEBUG(1, "Freeing up nearby elements");
5922 
5923  if ( nearby ) lwfree(nearby);
5924  if ( nodes ) _lwt_release_nodes(nodes, numnodes);
5925  if ( edges ) _lwt_release_edges(edges, numedges);
5926 
5927  LWDEBUGG(1, noded, "Finally-noded");
5928 
5929  /* 3. For each (now-noded) segment, insert an edge */
5930  col = lwgeom_as_lwcollection(noded);
5931  if ( col )
5932  {
5933  LWDEBUG(1, "Noded line was a collection");
5934  geoms = col->geoms;
5935  ngeoms = col->ngeoms;
5936  }
5937  else
5938  {
5939  LWDEBUG(1, "Noded line was a single geom");
5940  geomsbuf[0] = noded;
5941  geoms = geomsbuf;
5942  ngeoms = 1;
5943  }
5944 
5945  LWDEBUGF(1, "Line was split into %d edges", ngeoms);
5946 
5947  /* TODO: refactor to first add all nodes (re-snapping edges if
5948  * needed) and then check all edges for existing already
5949  * ( so to save a DB scan for each edge to be added )
5950  */
5951  ids = lwalloc(sizeof(LWT_ELEMID)*ngeoms);
5952  num = 0;
5953  for ( i=0; i<ngeoms; ++i )
5954  {
5955  LWT_ELEMID id;
5956  LWGEOM *g = geoms[i];
5957  g->srid = noded->srid;
5958 
5959 #if POSTGIS_DEBUG_LEVEL > 0
5960  {
5961  size_t sz;
5962  char *wkt1 = lwgeom_to_wkt(g, WKT_EXTENDED, 15, &sz);
5963  LWDEBUGF(1, "Component %d of split line is: %s", i, wkt1);
5964  lwfree(wkt1);
5965  }
5966 #endif
5967 
5968  id = _lwt_AddLineEdge( topo, lwgeom_as_lwline(g), tol, handleFaceSplit );
5969  LWDEBUGF(1, "_lwt_AddLineEdge returned %" LWTFMT_ELEMID, id);
5970  if ( id < 0 )
5971  {
5972  lwgeom_free(noded);
5973  lwfree(ids);
5974  return NULL;
5975  }
5976  if ( ! id )
5977  {
5978  LWDEBUGF(1, "Component %d of split line collapsed", i);
5979  continue;
5980  }
5981 
5982  LWDEBUGF(1, "Component %d of split line is edge %" LWTFMT_ELEMID,
5983  i, id);
5984  ids[num++] = id; /* TODO: skip duplicates */
5985  }
5986 
5987  LWDEBUGG(1, noded, "Noded before free");
5988  lwgeom_free(noded);
5989 
5990  /* TODO: XXX remove duplicated ids if not done before */
5991 
5992  *nedges = num;
5993  return ids;
5994 }
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:467
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:43
LWPOINT * geom
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:675
void lwfree(void *mem)
Definition: lwutil.c:244
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
double xmax
Definition: liblwgeom.h:292
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
LWLINE * geom
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LW_ON_INTERRUPT(x)
int32_t srid
Definition: liblwgeom.h:420
uint32_t ngeoms
Definition: liblwgeom.h:506
#define _LWT_MINTOLERANCE(topo, geom)
Definition: lwgeom_topo.c:5063
int32_t srid
Definition: liblwgeom.h:398
double ymin
Definition: liblwgeom.h:293
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:329
double xmin
Definition: liblwgeom.h:291
LWGEOM * lwgeom_node(const LWGEOM *lwgeom_in)
const LWT_BE_IFACE * be_iface
LWGEOM ** geoms
Definition: liblwgeom.h:508
LWGEOM * lwgeom_intersection(const LWGEOM *geom1, const LWGEOM *geom2)
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:727
LWGEOM * lwgeom_difference(const LWGEOM *geom1, const LWGEOM *geom2)
#define LWT_COL_EDGE_ALL
double ymax
Definition: liblwgeom.h:294
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:169
#define LWDEBUGG(level, geom, msg)
Definition: lwgeom_log.h:93
void lwcollection_release(LWCOLLECTION *lwcollection)
Definition: lwcollection.c:36
LWGEOM * lwline_remove_repeated_points(const LWLINE *in, double tolerance)
Definition: lwline.c:448
#define WKT_EXTENDED
Definition: liblwgeom.h:2070
static LWT_ELEMID _lwt_AddLineEdge(LWT_TOPOLOGY *topo, LWLINE *edge, double tol, int handleFaceSplit)
Definition: lwgeom_topo.c:5513
#define LWT_COL_NODE_ALL
LWGEOM * lwgeom_unaryunion(const LWGEOM *geom1)
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:223
static LWGEOM * _lwt_split_by_nodes(const LWGEOM *g, const LWGEOM *nodes)
Definition: lwgeom_topo.c:5697
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:237
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:457
double lwgeom_mindistance2d(const LWGEOM *lw1, const LWGEOM *lw2)
Function initialazing min distance calculation.
Definition: measures.c:202
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:334
LWT_INT64 LWT_ELEMID
Identifier of topology element.
LWGEOM * lwgeom_union(const LWGEOM *geom1, const LWGEOM *geom2)
void * lwalloc(size_t size)
Definition: lwutil.c:229
static LWGEOM * _lwt_toposnap(LWGEOM *src, LWGEOM *tgt, double tol)
Definition: lwgeom_topo.c:420
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
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
uint32_t e
Definition: geobuf.h:57
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
const char * lwt_be_lastErrorMessage(const LWT_BE_IFACE *be)
Definition: lwgeom_topo.c:120
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:44
LWGEOM * lwgeom_linemerge(const LWGEOM *geom1)
POINTARRAY * points
Definition: liblwgeom.h:421
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:299
uint32_t npoints
Definition: liblwgeom.h:370

Here is the call graph for this function:

Here is the caller graph for this function: