PostGIS  3.0.0dev-r@@SVN_REVISION@@

◆ lwt_NewEdgesSplit()

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

Split an edge by a node, replacing it with two new edges.

For ST_NewEdgesSplit

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

Definition at line 1175 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(), lwcollection_release(), lwerror(), lwgeom_as_lwline(), lwt_be_deleteEdges(), 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_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.

1177 {
1178  LWT_ISO_NODE node;
1179  LWT_ISO_EDGE* oldedge = NULL;
1180  LWCOLLECTION *split_col;
1181  const LWGEOM *oldedge_geom;
1182  const LWGEOM *newedge_geom;
1183  LWT_ISO_EDGE newedges[2];
1184  LWT_ISO_EDGE seledge, updedge;
1185  int ret;
1186 
1187  split_col = _lwt_EdgeSplit( topo, edge, pt, skipISOChecks, &oldedge );
1188  if ( ! split_col ) return -1; /* should have raised an exception */
1189  oldedge_geom = split_col->geoms[0];
1190  newedge_geom = split_col->geoms[1];
1191  /* Make sure the SRID is set on the subgeom */
1192  ((LWGEOM*)oldedge_geom)->srid = split_col->srid;
1193  ((LWGEOM*)newedge_geom)->srid = split_col->srid;
1194 
1195  /* Add new node, getting new id back */
1196  node.node_id = -1;
1197  node.containing_face = -1; /* means not-isolated */
1198  node.geom = pt;
1199  if ( ! lwt_be_insertNodes(topo, &node, 1) )
1200  {
1201  _lwt_release_edges(oldedge, 1);
1202  lwcollection_free(split_col);
1203  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1204  return -1;
1205  }
1206  if (node.node_id == -1) {
1207  _lwt_release_edges(oldedge, 1);
1208  lwcollection_free(split_col);
1209  /* should have been set by backend */
1210  lwerror("Backend coding error: "
1211  "insertNodes callback did not return node_id");
1212  return -1;
1213  }
1214 
1215  /* Delete the old edge */
1216  seledge.edge_id = edge;
1217  ret = lwt_be_deleteEdges(topo, &seledge, LWT_COL_EDGE_EDGE_ID);
1218  if ( ret == -1 ) {
1219  _lwt_release_edges(oldedge, 1);
1220  lwcollection_free(split_col);
1221  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1222  return -1;
1223  }
1224 
1225  /* Get new edges identifiers */
1226  newedges[0].edge_id = lwt_be_getNextEdgeId(topo);
1227  if ( newedges[0].edge_id == -1 ) {
1228  _lwt_release_edges(oldedge, 1);
1229  lwcollection_free(split_col);
1230  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1231  return -1;
1232  }
1233  newedges[1].edge_id = lwt_be_getNextEdgeId(topo);
1234  if ( newedges[1].edge_id == -1 ) {
1235  _lwt_release_edges(oldedge, 1);
1236  lwcollection_free(split_col);
1237  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1238  return -1;
1239  }
1240 
1241  /* Define the first new edge (to new node) */
1242  newedges[0].start_node = oldedge->start_node;
1243  newedges[0].end_node = node.node_id;
1244  newedges[0].face_left = oldedge->face_left;
1245  newedges[0].face_right = oldedge->face_right;
1246  newedges[0].next_left = newedges[1].edge_id;
1247  if ( oldedge->next_right == edge )
1248  newedges[0].next_right = newedges[0].edge_id;
1249  else if ( oldedge->next_right == -edge )
1250  newedges[0].next_right = -newedges[1].edge_id;
1251  else
1252  newedges[0].next_right = oldedge->next_right;
1253  newedges[0].geom = lwgeom_as_lwline(oldedge_geom);
1254  /* lwgeom_split of a line should only return lines ... */
1255  if ( ! newedges[0].geom ) {
1256  _lwt_release_edges(oldedge, 1);
1257  lwcollection_free(split_col);
1258  lwerror("first geometry in lwgeom_split output is not a line");
1259  return -1;
1260  }
1261 
1262  /* Define the second new edge (from new node) */
1263  newedges[1].start_node = node.node_id;
1264  newedges[1].end_node = oldedge->end_node;
1265  newedges[1].face_left = oldedge->face_left;
1266  newedges[1].face_right = oldedge->face_right;
1267  newedges[1].next_right = -newedges[0].edge_id;
1268  if ( oldedge->next_left == -edge )
1269  newedges[1].next_left = -newedges[1].edge_id;
1270  else if ( oldedge->next_left == edge )
1271  newedges[1].next_left = newedges[0].edge_id;
1272  else
1273  newedges[1].next_left = oldedge->next_left;
1274  newedges[1].geom = lwgeom_as_lwline(newedge_geom);
1275  /* lwgeom_split of a line should only return lines ... */
1276  if ( ! newedges[1].geom ) {
1277  _lwt_release_edges(oldedge, 1);
1278  lwcollection_free(split_col);
1279  lwerror("second geometry in lwgeom_split output is not a line");
1280  return -1;
1281  }
1282 
1283  /* Insert both new edges */
1284  ret = lwt_be_insertEdges(topo, newedges, 2);
1285  if ( ret == -1 ) {
1286  _lwt_release_edges(oldedge, 1);
1287  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1288  return -1;
1289  } else if ( ret == 0 ) {
1290  _lwt_release_edges(oldedge, 1);
1291  lwcollection_free(split_col);
1292  lwerror("Insertion of split edge failed (no reason)");
1293  return -1;
1294  }
1295 
1296  /* Update all next edge references pointing to old edge id */
1297 
1298  updedge.next_right = newedges[1].edge_id;
1299  seledge.next_right = edge;
1300  seledge.start_node = oldedge->start_node;
1301  ret = lwt_be_updateEdges(topo,
1303  &updedge, LWT_COL_EDGE_NEXT_RIGHT,
1304  NULL, 0);
1305  if ( ret == -1 ) {
1306  _lwt_release_edges(oldedge, 1);
1307  lwcollection_free(split_col);
1308  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1309  return -1;
1310  }
1311 
1312  updedge.next_right = -newedges[0].edge_id;
1313  seledge.next_right = -edge;
1314  seledge.start_node = oldedge->end_node;
1315  ret = lwt_be_updateEdges(topo,
1317  &updedge, LWT_COL_EDGE_NEXT_RIGHT,
1318  NULL, 0);
1319  if ( ret == -1 ) {
1320  _lwt_release_edges(oldedge, 1);
1321  lwcollection_free(split_col);
1322  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1323  return -1;
1324  }
1325 
1326  updedge.next_left = newedges[0].edge_id;
1327  seledge.next_left = edge;
1328  seledge.end_node = oldedge->start_node;
1329  ret = lwt_be_updateEdges(topo,
1331  &updedge, LWT_COL_EDGE_NEXT_LEFT,
1332  NULL, 0);
1333  if ( ret == -1 ) {
1334  _lwt_release_edges(oldedge, 1);
1335  lwcollection_free(split_col);
1336  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1337  return -1;
1338  }
1339 
1340  updedge.next_left = -newedges[1].edge_id;
1341  seledge.next_left = -edge;
1342  seledge.end_node = oldedge->end_node;
1343  ret = lwt_be_updateEdges(topo,
1345  &updedge, LWT_COL_EDGE_NEXT_LEFT,
1346  NULL, 0);
1347  if ( ret == -1 ) {
1348  _lwt_release_edges(oldedge, 1);
1349  lwcollection_release(split_col);
1350  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1351  return -1;
1352  }
1353 
1354  /* Update TopoGeometries composition */
1355  ret = lwt_be_updateTopoGeomEdgeSplit(topo, oldedge->edge_id, newedges[0].edge_id, newedges[1].edge_id);
1356  if ( ! ret ) {
1357  _lwt_release_edges(oldedge, 1);
1358  lwcollection_free(split_col);
1359  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1360  return -1;
1361  }
1362 
1363  _lwt_release_edges(oldedge, 1);
1364  lwcollection_free(split_col);
1365 
1366  /* return new node id */
1367  return node.node_id;
1368 }
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:194
int lwt_be_deleteEdges(LWT_TOPOLOGY *topo, const LWT_ISO_EDGE *sel_edge, int sel_fields)
Definition: lwgeom_topo.c:321
#define LWT_COL_EDGE_START_NODE
LWLINE * geom
int32_t srid
Definition: liblwgeom.h:401
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:934
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:273
const LWT_BE_IFACE * be_iface
LWGEOM ** geoms
Definition: liblwgeom.h:511
#define LWT_COL_EDGE_END_NODE
LWT_ELEMID face_right
int32_t srid
Definition: liblwgeom.h:508
LWT_ELEMID node_id
LWT_ELEMID edge_id
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:161
LWT_ELEMID lwt_be_getNextEdgeId(LWT_TOPOLOGY *topo)
Definition: lwgeom_topo.c:218
#define LWT_COL_EDGE_EDGE_ID
Edge fields.
void lwcollection_release(LWCOLLECTION *lwcollection)
Definition: lwcollection.c:36
#define LWT_COL_EDGE_NEXT_LEFT
int lwt_be_insertEdges(LWT_TOPOLOGY *topo, LWT_ISO_EDGE *edge, int numelems)
Definition: lwgeom_topo.c:267
#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:456
LWT_ELEMID next_right
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:356
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:336
const char * lwt_be_lastErrorMessage(const LWT_BE_IFACE *be)
Definition: lwgeom_topo.c:119
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: