PostGIS 3.6.2dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ lwt_AddIsoEdge()

LWT_ELEMID lwt_AddIsoEdge ( LWT_TOPOLOGY topo,
LWT_ELEMID  startNode,
LWT_ELEMID  endNode,
const LWLINE geom 
)

Add an isolated edge connecting two existing isolated nodes.

For ST_AddIsoEdge

Parameters
topothe topology to operate on
start_nodeidentifier of the starting node
end_nodeidentifier of the ending node
geomthe edge geometry
Returns
ID of the newly added edge, or -1 on error (liblwgeom error handler will be invoked with error message)

Definition at line 801 of file lwgeom_topo.c.

803{
804 uint64_t num_nodes;
805 uint64_t i;
806 LWT_ISO_EDGE newedge;
807 LWT_ISO_NODE *endpoints;
808 LWT_ELEMID containing_face = -1;
809 LWT_ELEMID node_ids[2];
810 LWT_ISO_NODE updated_nodes[2];
811 int skipISOChecks = 0;
812 POINT2D p1, p2;
813
814 /* NOT IN THE SPECS:
815 * A closed edge is never isolated (as it forms a face)
816 */
817 if (startNode == endNode)
818 {
819 lwerror("Closed edges would not be isolated, try lwt_AddEdgeNewFaces");
820 return -1;
821 }
822
823 if ( ! skipISOChecks )
824 {
825 /* Acurve must be simple */
826 if ( ! lwgeom_is_simple(lwline_as_lwgeom(geom)) )
827 {
828 lwerror("SQL/MM Spatial exception - curve not simple");
829 return -1;
830 }
831 }
832
833 /*
834 * Check for:
835 * existence of nodes
836 * nodes faces match
837 * Extract:
838 * nodes face id
839 * nodes geoms
840 */
841 num_nodes = 2;
842 node_ids[0] = startNode;
843 node_ids[1] = endNode;
844 endpoints = lwt_be_getNodeById( topo, node_ids, &num_nodes,
846 if (num_nodes == UINT64_MAX)
847 {
849 return -1;
850 }
851 else if ( num_nodes < 2 )
852 {
853 if ( num_nodes ) _lwt_release_nodes(endpoints, num_nodes);
854 lwerror("SQL/MM Spatial exception - non-existent node");
855 return -1;
856 }
857 for ( i=0; i<num_nodes; ++i )
858 {
859 const LWT_ISO_NODE *n = &(endpoints[i]);
860 if ( n->containing_face == -1 )
861 {
862 _lwt_release_nodes(endpoints, num_nodes);
863 lwerror("SQL/MM Spatial exception - not isolated node");
864 return -1;
865 }
866 if ( containing_face == -1 ) containing_face = n->containing_face;
867 else if ( containing_face != n->containing_face )
868 {
869 _lwt_release_nodes(endpoints, num_nodes);
870 lwerror("SQL/MM Spatial exception - nodes in different faces");
871 return -1;
872 }
873
874 if ( ! skipISOChecks )
875 {
876 if ( n->node_id == startNode )
877 {
878 /* l) Check that start point of acurve match start node geoms. */
879 getPoint2d_p(geom->points, 0, &p1);
880 getPoint2d_p(n->geom->point, 0, &p2);
881 if ( ! P2D_SAME_STRICT(&p1, &p2) )
882 {
883 _lwt_release_nodes(endpoints, num_nodes);
884 lwerror("SQL/MM Spatial exception - "
885 "start node not geometry start point.");
886 return -1;
887 }
888 }
889 else
890 {
891 /* m) Check that end point of acurve match end node geoms. */
892 getPoint2d_p(geom->points, geom->points->npoints-1, &p1);
893 getPoint2d_p(n->geom->point, 0, &p2);
894 if ( ! P2D_SAME_STRICT(&p1, &p2) )
895 {
896 _lwt_release_nodes(endpoints, num_nodes);
897 lwerror("SQL/MM Spatial exception - "
898 "end node not geometry end point.");
899 return -1;
900 }
901 }
902 }
903 }
904
905 if ( num_nodes ) _lwt_release_nodes(endpoints, num_nodes);
906
907 if ( ! skipISOChecks )
908 {
909 if ( _lwt_CheckEdgeCrossing( topo, startNode, endNode, geom, 0 ) )
910 {
911 /* would have called lwerror already, leaking :( */
912 return -1;
913 }
914 }
915
916 /*
917 * All checks passed, time to prepare the new edge
918 */
919
920 newedge.edge_id = lwt_be_getNextEdgeId( topo );
921 if ( newedge.edge_id == -1 ) {
923 return -1;
924 }
925
926 /* TODO: this should likely be an exception instead ! */
927 if ( containing_face == -1 ) containing_face = 0;
928
929 newedge.start_node = startNode;
930 newedge.end_node = endNode;
931 newedge.face_left = newedge.face_right = containing_face;
932 newedge.next_left = -newedge.edge_id;
933 newedge.next_right = newedge.edge_id;
934 newedge.geom = (LWLINE *)geom; /* const cast.. */
935
936 int ret = lwt_be_insertEdges(topo, &newedge, 1);
937 if ( ret == -1 ) {
939 return -1;
940 } else if ( ret == 0 ) {
941 lwerror("Insertion of split edge failed (no reason)");
942 return -1;
943 }
944
945 /*
946 * Update Node containing_face values
947 *
948 * the nodes anode and anothernode are no more isolated
949 * because now there is an edge connecting them
950 */
951 updated_nodes[0].node_id = startNode;
952 updated_nodes[0].containing_face = -1;
953 updated_nodes[1].node_id = endNode;
954 updated_nodes[1].containing_face = -1;
955 ret = lwt_be_updateNodesById(topo, updated_nodes, 2,
957 if ( ret == -1 ) {
959 return -1;
960 }
961
962 return newedge.edge_id;
963}
int lwgeom_is_simple(const LWGEOM *lwgeom)
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition lwgeom_api.c:342
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition lwgeom.c:339
#define P2D_SAME_STRICT(a, b)
LWT_INT64 LWT_ELEMID
Identifier of topology element.
#define LWT_COL_NODE_CONTAINING_FACE
#define LWT_COL_NODE_ALL
#define PGTOPO_BE_ERROR()
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
LWT_ELEMID lwt_be_getNextEdgeId(LWT_TOPOLOGY *topo)
static int lwt_be_updateNodesById(LWT_TOPOLOGY *topo, const LWT_ISO_NODE *nodes, int numnodes, int upd_fields)
static int _lwt_CheckEdgeCrossing(LWT_TOPOLOGY *topo, LWT_ELEMID start_node, LWT_ELEMID end_node, const LWLINE *geom, LWT_ELEMID myself)
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
int lwt_be_insertEdges(LWT_TOPOLOGY *topo, LWT_ISO_EDGE *edge, uint64_t numelems)
LWT_ISO_NODE * lwt_be_getNodeById(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t *numelems, int fields)
POINTARRAY * points
Definition liblwgeom.h:483
POINTARRAY * point
Definition liblwgeom.h:471
LWT_ELEMID face_right
LWT_ELEMID next_right
LWT_ELEMID end_node
LWT_ELEMID face_left
LWT_ELEMID next_left
LWT_ELEMID edge_id
LWT_ELEMID start_node
LWT_ELEMID node_id
LWT_ELEMID containing_face
LWPOINT * geom
uint32_t npoints
Definition liblwgeom.h:427

References _lwt_CheckEdgeCrossing(), _lwt_release_nodes(), 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, getPoint2d_p(), lwerror(), lwgeom_is_simple(), lwline_as_lwgeom(), lwt_be_getNextEdgeId(), lwt_be_getNodeById(), lwt_be_insertEdges(), lwt_be_updateNodesById(), LWT_COL_NODE_ALL, LWT_COL_NODE_CONTAINING_FACE, LWT_ISO_EDGE::next_left, LWT_ISO_EDGE::next_right, LWT_ISO_NODE::node_id, POINTARRAY::npoints, P2D_SAME_STRICT, PGTOPO_BE_ERROR, LWPOINT::point, LWLINE::points, and LWT_ISO_EDGE::start_node.

Here is the call graph for this function: