PostGIS  2.4.9dev-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 1191 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.

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