PostGIS  2.5.7dev-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 1176 of file lwgeom_topo.c.

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

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.

Here is the call graph for this function: