PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ lwt_AddIsoEdge()

LWT_ELEMID lwt_AddIsoEdge ( LWT_TOPOLOGY topo,
LWT_ELEMID  startNode,
LWT_ELEMID  endNode,
const LWLINE geom 
)

Add an isolated edge connecting two existing isolated nodes.

For ST_AddIsoEdge

Parameters
topothe topology to operate on
start_nodeidentifier of the starting node
end_nodeidentifier of the ending node
geomthe edge geometry
Returns
ID of the newly added edge, or -1 on error (liblwgeom error handler will be invoked with error message)

Definition at line 785 of file lwgeom_topo.c.

References _lwt_CheckEdgeCrossing(), _lwt_release_nodes(), LWT_TOPOLOGY_T::be_iface, LWT_ISO_NODE::containing_face, LWT_ISO_EDGE::edge_id, LWT_ISO_EDGE::end_node, LWT_ISO_EDGE::face_left, LWT_ISO_EDGE::face_right, LWT_ISO_NODE::geom, LWT_ISO_EDGE::geom, getPoint2d_p(), lwerror(), lwgeom_is_simple(), lwline_as_lwgeom(), lwt_be_getNextEdgeId(), lwt_be_getNodeById(), lwt_be_insertEdges(), lwt_be_lastErrorMessage(), lwt_be_updateNodesById(), LWT_COL_NODE_ALL, LWT_COL_NODE_CONTAINING_FACE, LWT_ISO_EDGE::next_left, LWT_ISO_EDGE::next_right, LWT_ISO_NODE::node_id, POINTARRAY::npoints, p2d_same(), LWPOINT::point, LWLINE::points, and LWT_ISO_EDGE::start_node.

787 {
788  int num_nodes;
789  int i;
790  LWT_ISO_EDGE newedge;
791  LWT_ISO_NODE *endpoints;
792  LWT_ELEMID containing_face = -1;
793  LWT_ELEMID node_ids[2];
794  LWT_ISO_NODE updated_nodes[2];
795  int skipISOChecks = 0;
796  POINT2D p1, p2;
797 
798  /* NOT IN THE SPECS:
799  * A closed edge is never isolated (as it forms a face)
800  */
801  if ( startNode == endNode )
802  {
803  lwerror("Closed edges would not be isolated, try lwt_AddEdgeNewFaces");
804  return -1;
805  }
806 
807  if ( ! skipISOChecks )
808  {
809  /* Acurve must be simple */
810  if ( ! lwgeom_is_simple(lwline_as_lwgeom(geom)) )
811  {
812  lwerror("SQL/MM Spatial exception - curve not simple");
813  return -1;
814  }
815  }
816 
817  /*
818  * Check for:
819  * existence of nodes
820  * nodes faces match
821  * Extract:
822  * nodes face id
823  * nodes geoms
824  */
825  num_nodes = 2;
826  node_ids[0] = startNode;
827  node_ids[1] = endNode;
828  endpoints = lwt_be_getNodeById( topo, node_ids, &num_nodes,
830  if ( num_nodes < 0 )
831  {
832  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
833  return -1;
834  }
835  else if ( num_nodes < 2 )
836  {
837  if ( num_nodes ) _lwt_release_nodes(endpoints, num_nodes);
838  lwerror("SQL/MM Spatial exception - non-existent node");
839  return -1;
840  }
841  for ( i=0; i<num_nodes; ++i )
842  {
843  const LWT_ISO_NODE *n = &(endpoints[i]);
844  if ( n->containing_face == -1 )
845  {
846  _lwt_release_nodes(endpoints, num_nodes);
847  lwerror("SQL/MM Spatial exception - not isolated node");
848  return -1;
849  }
850  if ( containing_face == -1 ) containing_face = n->containing_face;
851  else if ( containing_face != n->containing_face )
852  {
853  _lwt_release_nodes(endpoints, num_nodes);
854  lwerror("SQL/MM Spatial exception - nodes in different faces");
855  return -1;
856  }
857 
858  if ( ! skipISOChecks )
859  {
860  if ( n->node_id == startNode )
861  {
862  /* l) Check that start point of acurve match start node geoms. */
863  getPoint2d_p(geom->points, 0, &p1);
864  getPoint2d_p(n->geom->point, 0, &p2);
865  if ( ! p2d_same(&p1, &p2) )
866  {
867  _lwt_release_nodes(endpoints, num_nodes);
868  lwerror("SQL/MM Spatial exception - "
869  "start node not geometry start point.");
870  return -1;
871  }
872  }
873  else
874  {
875  /* m) Check that end point of acurve match end node geoms. */
876  getPoint2d_p(geom->points, geom->points->npoints-1, &p1);
877  getPoint2d_p(n->geom->point, 0, &p2);
878  if ( ! p2d_same(&p1, &p2) )
879  {
880  _lwt_release_nodes(endpoints, num_nodes);
881  lwerror("SQL/MM Spatial exception - "
882  "end node not geometry end point.");
883  return -1;
884  }
885  }
886  }
887  }
888 
889  if ( num_nodes ) _lwt_release_nodes(endpoints, num_nodes);
890 
891  if ( ! skipISOChecks )
892  {
893  if ( _lwt_CheckEdgeCrossing( topo, startNode, endNode, geom, 0 ) )
894  {
895  /* would have called lwerror already, leaking :( */
896  return -1;
897  }
898  }
899 
900  /*
901  * All checks passed, time to prepare the new edge
902  */
903 
904  newedge.edge_id = lwt_be_getNextEdgeId( topo );
905  if ( newedge.edge_id == -1 ) {
906  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
907  return -1;
908  }
909 
910  /* TODO: this should likely be an exception instead ! */
911  if ( containing_face == -1 ) containing_face = 0;
912 
913  newedge.start_node = startNode;
914  newedge.end_node = endNode;
915  newedge.face_left = newedge.face_right = containing_face;
916  newedge.next_left = -newedge.edge_id;
917  newedge.next_right = newedge.edge_id;
918  newedge.geom = (LWLINE *)geom; /* const cast.. */
919 
920  int ret = lwt_be_insertEdges(topo, &newedge, 1);
921  if ( ret == -1 ) {
922  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
923  return -1;
924  } else if ( ret == 0 ) {
925  lwerror("Insertion of split edge failed (no reason)");
926  return -1;
927  }
928 
929  /*
930  * Update Node containing_face values
931  *
932  * the nodes anode and anothernode are no more isolated
933  * because now there is an edge connecting them
934  */
935  updated_nodes[0].node_id = startNode;
936  updated_nodes[0].containing_face = -1;
937  updated_nodes[1].node_id = endNode;
938  updated_nodes[1].containing_face = -1;
939  ret = lwt_be_updateNodesById(topo, updated_nodes, 2,
941  if ( ret == -1 ) {
942  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
943  return -1;
944  }
945 
946  return newedge.edge_id;
947 }
static int _lwt_CheckEdgeCrossing(LWT_TOPOLOGY *topo, LWT_ELEMID start_node, LWT_ELEMID end_node, const LWLINE *geom, LWT_ELEMID myself)
Definition: lwgeom_topo.c:589
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:477
LWT_ELEMID face_left
#define LWT_COL_NODE_CONTAINING_FACE
LWT_ELEMID containing_face
LWPOINT * geom
int npoints
Definition: liblwgeom.h:371
LWLINE * geom
POINTARRAY * point
Definition: liblwgeom.h:411
LWT_ELEMID next_left
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:49
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:298
const LWT_BE_IFACE * be_iface
LWT_ELEMID face_right
LWT_ELEMID node_id
LWT_ELEMID edge_id
int getPoint2d_p(const POINTARRAY *pa, int n, POINT2D *point)
Definition: lwgeom_api.c:347
static int lwt_be_updateNodesById(LWT_TOPOLOGY *topo, const LWT_ISO_NODE *nodes, int numnodes, int upd_fields)
Definition: lwgeom_topo.c:314
LWT_ELEMID lwt_be_getNextEdgeId(LWT_TOPOLOGY *topo)
Definition: lwgeom_topo.c:219
int lwt_be_insertEdges(LWT_TOPOLOGY *topo, LWT_ISO_EDGE *edge, int numelems)
Definition: lwgeom_topo.c:268
#define LWT_COL_NODE_ALL
LWT_ELEMID start_node
int lwgeom_is_simple(const LWGEOM *lwgeom)
LWT_ELEMID next_right
LWT_INT64 LWT_ELEMID
Identifier of topology element.
LWT_ISO_NODE * lwt_be_getNodeById(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, int *numelems, int fields)
Definition: lwgeom_topo.c:156
LWT_ELEMID end_node
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
POINTARRAY * points
Definition: liblwgeom.h:422
Here is the call graph for this function: