PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ lwt_ModEdgeSplit()

LWT_ELEMID lwt_ModEdgeSplit ( LWT_TOPOLOGY topo,
LWT_ELEMID  edge,
LWPOINT pt,
int  skipChecks 
)

Split an edge by a node, modifying the original edge and adding a new one.

For ST_ModEdgeSplit

Parameters
topothe topology to operate on
edgeidentifier of the edge to be split
ptgeometry of the new node
skipChecksif non-zero skips consistency checks (coincident node, point not on edge...)
Returns
the id of newly created node, or -1 on error (liblwgeom error handler will be invoked with error message)

Definition at line 1034 of file lwgeom_topo.c.

References _lwt_EdgeSplit(), _lwt_release_edges(), 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, LWCOLLECTION::geoms, lwcollection_free(), lwerror(), lwgeom_as_lwline(), lwt_be_getNextEdgeId(), lwt_be_insertEdges(), lwt_be_insertNodes(), lwt_be_lastErrorMessage(), lwt_be_updateEdges(), lwt_be_updateTopoGeomEdgeSplit(), LWT_COL_EDGE_EDGE_ID, LWT_COL_EDGE_END_NODE, LWT_COL_EDGE_GEOM, LWT_COL_EDGE_NEXT_LEFT, LWT_COL_EDGE_NEXT_RIGHT, LWT_COL_EDGE_START_NODE, LWT_ISO_EDGE::next_left, LWT_ISO_EDGE::next_right, LWT_ISO_NODE::node_id, LWGEOM::srid, LWCOLLECTION::srid, and LWT_ISO_EDGE::start_node.

Referenced by lwt_AddPoint().

1036 {
1037  LWT_ISO_NODE node;
1038  LWT_ISO_EDGE* oldedge = NULL;
1039  LWCOLLECTION *split_col;
1040  const LWGEOM *oldedge_geom;
1041  const LWGEOM *newedge_geom;
1042  LWT_ISO_EDGE newedge1;
1043  LWT_ISO_EDGE seledge, updedge, excedge;
1044  int ret;
1045 
1046  split_col = _lwt_EdgeSplit( topo, edge, pt, skipISOChecks, &oldedge );
1047  if ( ! split_col ) return -1; /* should have raised an exception */
1048  oldedge_geom = split_col->geoms[0];
1049  newedge_geom = split_col->geoms[1];
1050  /* Make sure the SRID is set on the subgeom */
1051  ((LWGEOM*)oldedge_geom)->srid = split_col->srid;
1052  ((LWGEOM*)newedge_geom)->srid = split_col->srid;
1053 
1054  /* Add new node, getting new id back */
1055  node.node_id = -1;
1056  node.containing_face = -1; /* means not-isolated */
1057  node.geom = pt;
1058  if ( ! lwt_be_insertNodes(topo, &node, 1) )
1059  {
1060  _lwt_release_edges(oldedge, 1);
1061  lwcollection_free(split_col);
1062  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1063  return -1;
1064  }
1065  if (node.node_id == -1) {
1066  /* should have been set by backend */
1067  _lwt_release_edges(oldedge, 1);
1068  lwcollection_free(split_col);
1069  lwerror("Backend coding error: "
1070  "insertNodes callback did not return node_id");
1071  return -1;
1072  }
1073 
1074  /* Insert the new edge */
1075  newedge1.edge_id = lwt_be_getNextEdgeId(topo);
1076  if ( newedge1.edge_id == -1 ) {
1077  _lwt_release_edges(oldedge, 1);
1078  lwcollection_free(split_col);
1079  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1080  return -1;
1081  }
1082  newedge1.start_node = node.node_id;
1083  newedge1.end_node = oldedge->end_node;
1084  newedge1.face_left = oldedge->face_left;
1085  newedge1.face_right = oldedge->face_right;
1086  newedge1.next_left = oldedge->next_left == -oldedge->edge_id ?
1087  -newedge1.edge_id : oldedge->next_left;
1088  newedge1.next_right = -oldedge->edge_id;
1089  newedge1.geom = lwgeom_as_lwline(newedge_geom);
1090  /* lwgeom_split of a line should only return lines ... */
1091  if ( ! newedge1.geom ) {
1092  _lwt_release_edges(oldedge, 1);
1093  lwcollection_free(split_col);
1094  lwerror("first geometry in lwgeom_split output is not a line");
1095  return -1;
1096  }
1097  ret = lwt_be_insertEdges(topo, &newedge1, 1);
1098  if ( ret == -1 ) {
1099  _lwt_release_edges(oldedge, 1);
1100  lwcollection_free(split_col);
1101  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1102  return -1;
1103  } else if ( ret == 0 ) {
1104  _lwt_release_edges(oldedge, 1);
1105  lwcollection_free(split_col);
1106  lwerror("Insertion of split edge failed (no reason)");
1107  return -1;
1108  }
1109 
1110  /* Update the old edge */
1111  updedge.geom = lwgeom_as_lwline(oldedge_geom);
1112  /* lwgeom_split of a line should only return lines ... */
1113  if ( ! updedge.geom ) {
1114  _lwt_release_edges(oldedge, 1);
1115  lwcollection_free(split_col);
1116  lwerror("second geometry in lwgeom_split output is not a line");
1117  return -1;
1118  }
1119  updedge.next_left = newedge1.edge_id;
1120  updedge.end_node = node.node_id;
1121  ret = lwt_be_updateEdges(topo,
1122  oldedge, LWT_COL_EDGE_EDGE_ID,
1124  NULL, 0);
1125  if ( ret == -1 ) {
1126  _lwt_release_edges(oldedge, 1);
1127  lwcollection_free(split_col);
1128  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1129  return -1;
1130  } else if ( ret == 0 ) {
1131  _lwt_release_edges(oldedge, 1);
1132  lwcollection_free(split_col);
1133  lwerror("Edge being split (%d) disappeared during operations?", oldedge->edge_id);
1134  return -1;
1135  } else if ( ret > 1 ) {
1136  _lwt_release_edges(oldedge, 1);
1137  lwcollection_free(split_col);
1138  lwerror("More than a single edge found with id %d !", oldedge->edge_id);
1139  return -1;
1140  }
1141 
1142  /* Update all next edge references to match new layout (ST_ModEdgeSplit) */
1143 
1144  updedge.next_right = -newedge1.edge_id;
1145  excedge.edge_id = newedge1.edge_id;
1146  seledge.next_right = -oldedge->edge_id;
1147  seledge.start_node = oldedge->end_node;
1148  ret = lwt_be_updateEdges(topo,
1150  &updedge, LWT_COL_EDGE_NEXT_RIGHT,
1151  &excedge, LWT_COL_EDGE_EDGE_ID);
1152  if ( ret == -1 ) {
1153  _lwt_release_edges(oldedge, 1);
1154  lwcollection_free(split_col);
1155  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1156  return -1;
1157  }
1158 
1159  updedge.next_left = -newedge1.edge_id;
1160  excedge.edge_id = newedge1.edge_id;
1161  seledge.next_left = -oldedge->edge_id;
1162  seledge.end_node = oldedge->end_node;
1163  ret = lwt_be_updateEdges(topo,
1165  &updedge, LWT_COL_EDGE_NEXT_LEFT,
1166  &excedge, LWT_COL_EDGE_EDGE_ID);
1167  if ( ret == -1 ) {
1168  _lwt_release_edges(oldedge, 1);
1169  lwcollection_free(split_col);
1170  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1171  return -1;
1172  }
1173 
1174  /* Update TopoGeometries composition */
1175  ret = lwt_be_updateTopoGeomEdgeSplit(topo, oldedge->edge_id, newedge1.edge_id, -1);
1176  if ( ! ret ) {
1177  _lwt_release_edges(oldedge, 1);
1178  lwcollection_free(split_col);
1179  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1180  return -1;
1181  }
1182 
1183  _lwt_release_edges(oldedge, 1);
1184  lwcollection_free(split_col);
1185 
1186  /* return new node id */
1187  return node.node_id;
1188 }
LWT_ELEMID face_left
LWT_ELEMID containing_face
LWPOINT * geom
int lwt_be_insertNodes(LWT_TOPOLOGY *topo, LWT_ISO_NODE *node, int numelems)
Definition: lwgeom_topo.c:195
#define LWT_COL_EDGE_START_NODE
LWLINE * geom
int32_t srid
Definition: liblwgeom.h:399
LWT_ELEMID next_left
static LWCOLLECTION * _lwt_EdgeSplit(LWT_TOPOLOGY *topo, LWT_ELEMID edge, LWPOINT *pt, int skipISOChecks, LWT_ISO_EDGE **oldedge)
Definition: lwgeom_topo.c:950
int lwt_be_updateEdges(LWT_TOPOLOGY *topo, const LWT_ISO_EDGE *sel_edge, int sel_fields, const LWT_ISO_EDGE *upd_edge, int upd_fields, const LWT_ISO_EDGE *exc_edge, int exc_fields)
Definition: lwgeom_topo.c:274
const LWT_BE_IFACE * be_iface
LWGEOM ** geoms
Definition: liblwgeom.h:509
#define LWT_COL_EDGE_END_NODE
LWT_ELEMID face_right
int32_t srid
Definition: liblwgeom.h:506
LWT_ELEMID node_id
LWT_ELEMID edge_id
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:138
LWT_ELEMID lwt_be_getNextEdgeId(LWT_TOPOLOGY *topo)
Definition: lwgeom_topo.c:219
#define LWT_COL_EDGE_EDGE_ID
Edge fields.
#define LWT_COL_EDGE_NEXT_LEFT
int lwt_be_insertEdges(LWT_TOPOLOGY *topo, LWT_ISO_EDGE *edge, int numelems)
Definition: lwgeom_topo.c:268
#define LWT_COL_EDGE_NEXT_RIGHT
LWT_ELEMID start_node
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:467
LWT_ELEMID next_right
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:340
#define LWT_COL_EDGE_GEOM
LWT_ELEMID end_node
int lwt_be_updateTopoGeomEdgeSplit(LWT_TOPOLOGY *topo, LWT_ELEMID split_edge, LWT_ELEMID new_edge1, LWT_ELEMID new_edge2)
Definition: lwgeom_topo.c:337
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
Here is the call graph for this function:
Here is the caller graph for this function: