PostGIS  3.1.6dev-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 800 of file lwgeom_topo.c.

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

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.

Here is the call graph for this function: