PostGIS  2.2.7dev-r@@SVN_REVISION@@
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 1195 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.

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

Here is the call graph for this function: