PostGIS  2.2.8dev-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 789 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.

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