27 #include "../postgis_config.h"
37 #include "lwgeom_geos.h"
72 #define CHECKCB(be, method) do { \
73 if ( ! (be)->cb || ! (be)->cb->method ) \
74 lwerror("Callback " # method " not registered by backend"); \
77 #define CB0(be, method) \
79 return (be)->cb->method((be)->data)
81 #define CB1(be, method, a1) \
83 return (be)->cb->method((be)->data, a1)
85 #define CBT0(to, method) \
86 CHECKCB((to)->be_iface, method);\
87 return (to)->be_iface->cb->method((to)->be_topo)
89 #define CBT1(to, method, a1) \
90 CHECKCB((to)->be_iface, method);\
91 return (to)->be_iface->cb->method((to)->be_topo, a1)
93 #define CBT2(to, method, a1, a2) \
94 CHECKCB((to)->be_iface, method);\
95 return (to)->be_iface->cb->method((to)->be_topo, a1, a2)
97 #define CBT3(to, method, a1, a2, a3) \
98 CHECKCB((to)->be_iface, method);\
99 return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3)
101 #define CBT4(to, method, a1, a2, a3, a4) \
102 CHECKCB((to)->be_iface, method);\
103 return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3, a4)
105 #define CBT5(to, method, a1, a2, a3, a4, a5) \
106 CHECKCB((to)->be_iface, method);\
107 return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3, a4, a5)
109 #define CBT6(to, method, a1, a2, a3, a4, a5, a6) \
110 CHECKCB((to)->be_iface, method);\
111 return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3, a4, a5, a6)
116 CB0(be, lastErrorMessage);
122 CB1(be, loadTopologyByName, name);
128 CBT0(topo, topoGetSRID);
134 CBT0(topo, topoGetPrecision);
140 CBT0(topo, topoHasZ);
146 CBT0(topo, freeTopology);
152 CBT3(topo, getNodeById, ids, numelems, fields);
163 CBT5(topo, getNodeWithinDistance2D, pt, dist, numelems, fields, limit);
169 CBT4(topo, getNodeWithinBox2D, box, numelems, fields, limit);
175 CBT4(topo, getEdgeWithinBox2D, box, numelems, fields, limit);
181 CBT4(topo, getFaceWithinBox2D, box, numelems, fields, limit);
187 CBT2(topo, insertNodes, node, numelems);
193 CBT2(topo, insertFaces, face, numelems);
199 CBT2(topo, deleteFacesById, ids, numelems);
205 CBT2(topo, deleteNodesById, ids, numelems);
211 CBT0(topo, getNextEdgeId);
217 CBT3(topo, getEdgeById, ids, numelems, fields);
223 CBT3(topo, getFaceById, ids, numelems, fields);
229 CBT3(topo, getEdgeByNode, ids, numelems, fields);
235 CBT4(topo, getEdgeByFace, ids, numelems, fields, box);
241 CBT4(topo, getNodeByFace, ids, numelems, fields, box);
252 CBT5(topo, getEdgeWithinDistance2D, pt, dist, numelems, fields, limit);
258 CBT2(topo, insertEdges, edge, numelems);
268 CBT6(topo, updateEdges, sel_edge, sel_fields,
269 upd_edge, upd_fields,
270 exc_edge, exc_fields);
280 CBT6(topo, updateNodes, sel_node, sel_fields,
281 upd_node, upd_fields,
282 exc_node, exc_fields);
290 CBT2(topo, updateFacesById, faces, numfaces);
298 CBT3(topo, updateEdgesById, edges, numedges, upd_fields);
306 CBT3(topo, updateNodesById, nodes, numnodes, upd_fields);
314 CBT2(topo, deleteEdges, sel_edge, sel_fields);
320 CBT3(topo, updateTopoGeomEdgeSplit, split_edge, new_edge1, new_edge2);
327 CBT3(topo, updateTopoGeomFaceSplit, split_face, new_face1, new_face2);
334 CBT3(topo, checkTopoGeomRemEdge, edge_id, face_left, face_right);
340 CBT1(topo, checkTopoGeomRemIsoEdge, edge_id);
347 CBT3(topo, checkTopoGeomRemNode, node_id, eid1, eid2);
353 CBT1(topo, checkTopoGeomRemIsoNode, node_id);
361 CBT3(topo, updateTopoGeomFaceHeal, face1, face2, newface);
369 CBT3(topo, updateTopoGeomEdgeHeal, edge1, edge2, newedge);
375 CBT3(topo, getRingEdges, edge, numedges, limit);
383 if (exists == UINT64_MAX)
396 if (exists == UINT64_MAX)
407 CBT3(topo, getClosestEdge, pt, numelems, fields);
413 CBT1(topo, computeFaceMBR, face);
441 }
while ( changed && iterations <= maxiterations );
443 LWDEBUGF(1,
"It took %d/%d iterations to properly snap",
444 iterations, maxiterations);
453 for ( i=0; i<num_faces; ++i ) {
454 if ( faces[i].mbr )
lwfree(faces[i].mbr);
463 for ( i=0; i<num_edges; ++i ) {
473 for ( i=0; i<num_nodes; ++i ) {
511 lwnotice(
"Could not release backend topology memory: %s",
527 LWPOINT* pt,
int skipISOChecks,
int checkFace )
533 lwerror(
"Cannot add empty point as isolated node");
538 if ( ! skipISOChecks )
542 lwerror(
"SQL/MM Spatial exception - coincident node");
547 lwerror(
"SQL/MM Spatial exception - edge crosses node.");
552 if ( checkFace && ( face == -1 || ! skipISOChecks ) )
555 if ( foundInFace == -1 ) {
559 if ( foundInFace == -1 ) foundInFace = 0;
565 else if ( ! skipISOChecks && foundInFace != face ) {
567 lwerror(
"SQL/MM Spatial exception - within face %d (not %d)",
570 lwerror(
"SQL/MM Spatial exception - not within face");
590 LWPOINT* pt,
int skipISOChecks )
610 uint64_t i, num_nodes, num_edges;
614 GEOSGeometry *edgegg;
629 LWDEBUGF(1,
"lwt_be_getNodeWithinBox2D returned %llu nodes", num_nodes);
630 if (num_nodes == UINT64_MAX)
635 for ( i=0; i<num_nodes; ++i )
639 if ( node->
node_id == start_node )
continue;
640 if ( node->
node_id == end_node )
continue;
647 GEOSGeom_destroy(edgegg);
649 lwerror(
"SQL/MM Spatial exception - geometry crosses a node");
658 LWDEBUGF(1,
"lwt_be_getEdgeWithinBox2D returned %llu edges", num_edges);
659 if (num_edges == UINT64_MAX)
661 GEOSGeom_destroy(edgegg);
665 for ( i=0; i<num_edges; ++i )
673 if ( edge_id == myself )
continue;
675 if ( ! edge->
geom ) {
683 GEOSGeom_destroy(edgegg);
693 relate = GEOSRelateBoundaryNodeRule(eegg, edgegg, 2);
694 GEOSGeom_destroy(eegg);
696 GEOSGeom_destroy(edgegg);
704 match = GEOSRelatePatternMatch(relate,
"FF*F*****");
710 GEOSGeom_destroy(edgegg);
717 match = GEOSRelatePatternMatch(relate,
"1FFF*FFF2");
720 GEOSGeom_destroy(edgegg);
731 match = GEOSRelatePatternMatch(relate,
"1********");
734 GEOSGeom_destroy(edgegg);
739 lwerror(
"Spatial exception - geometry intersects edge %"
745 match = GEOSRelatePatternMatch(relate,
"T********");
748 GEOSGeom_destroy(edgegg);
753 lwerror(
"SQL/MM Spatial exception - geometry crosses edge %"
759 match = GEOSRelatePatternMatch(relate,
"*T*******");
762 GEOSGeom_destroy(edgegg);
767 lwerror(
"Spatial exception - geometry boundary touches interior of edge %"
773 match = GEOSRelatePatternMatch(relate,
"***T*****");
776 GEOSGeom_destroy(edgegg);
781 lwerror(
"Spatial exception - boundary of edge %" LWTFMT_ELEMID" touches interior of geometry", edge_id);
790 LWDEBUGF(1,
"No edge crossing detected among the %llu candidate edges", num_edges);
794 GEOSGeom_destroy(edgegg);
811 int skipISOChecks = 0;
817 if (startNode == endNode)
819 lwerror(
"Closed edges would not be isolated, try lwt_AddEdgeNewFaces");
823 if ( ! skipISOChecks )
828 lwerror(
"SQL/MM Spatial exception - curve not simple");
842 node_ids[0] = startNode;
843 node_ids[1] = endNode;
846 if (num_nodes == UINT64_MAX)
851 else if ( num_nodes < 2 )
854 lwerror(
"SQL/MM Spatial exception - non-existent node");
857 for ( i=0; i<num_nodes; ++i )
863 lwerror(
"SQL/MM Spatial exception - not isolated node");
870 lwerror(
"SQL/MM Spatial exception - nodes in different faces");
874 if ( ! skipISOChecks )
884 lwerror(
"SQL/MM Spatial exception - "
885 "start node not geometry start point.");
897 lwerror(
"SQL/MM Spatial exception - "
898 "end node not geometry end point.");
907 if ( ! skipISOChecks )
927 if ( containing_face == -1 ) containing_face = 0;
940 }
else if ( ret == 0 ) {
941 lwerror(
"Insertion of split edge failed (no reason)");
951 updated_nodes[0].
node_id = startNode;
953 updated_nodes[1].
node_id = endNode;
974 LWDEBUG(1,
"calling lwt_be_getEdgeById");
976 LWDEBUGF(1,
"lwt_be_getEdgeById returned %p", *oldedge);
980 LWDEBUGF(1,
"lwt_be_getEdgeById returned NULL and set i=%llu", i);
988 lwerror(
"SQL/MM Spatial exception - non-existent edge");
993 lwerror(
"Backend coding error: getEdgeById callback returned NULL "
994 "but numelements output parameter has value %" PRIu64
995 "(expected 0 or 1)", i);
1004 if ( ! skipISOChecks )
1006 LWDEBUG(1,
"calling lwt_be_ExistsCoincidentNode");
1009 LWDEBUG(1,
"lwt_be_ExistsCoincidentNode returned");
1011 lwerror(
"SQL/MM Spatial exception - coincident node");
1014 LWDEBUG(1,
"lwt_be_ExistsCoincidentNode returned");
1022 lwerror(
"could not split edge by point ?");
1026 if ( ! split_col ) {
1029 lwerror(
"lwgeom_as_lwcollection returned NULL");
1032 if (split_col->
ngeoms < 2) {
1036 lwerror(
"SQL/MM Spatial exception - point not on edge");
1040 #if POSTGIS_DEBUG_LEVEL > 1
1044 LWDEBUGF(1,
"returning split col: %s", wkt);
1053 LWPOINT* pt,
int skipISOChecks )
1058 const LWGEOM *oldedge_geom;
1059 const LWGEOM *newedge_geom;
1064 split_col =
_lwt_EdgeSplit( topo, edge, pt, skipISOChecks, &oldedge );
1065 if ( ! split_col )
return -1;
1066 oldedge_geom = split_col->
geoms[0];
1067 newedge_geom = split_col->
geoms[1];
1070 ((
LWGEOM*)newedge_geom)->srid = split_col->
srid;
1087 lwerror(
"Backend coding error: "
1088 "insertNodes callback did not return node_id");
1094 if ( newedge1.
edge_id == -1 ) {
1109 if ( ! newedge1.
geom ) {
1112 lwerror(
"first geometry in lwgeom_split output is not a line");
1121 }
else if ( ret == 0 ) {
1124 lwerror(
"Insertion of split edge failed (no reason)");
1131 if ( ! updedge.
geom ) {
1134 lwerror(
"second geometry in lwgeom_split output is not a line");
1148 }
else if ( ret == 0 ) {
1153 }
else if ( ret > 1 ) {
1210 LWPOINT* pt,
int skipISOChecks )
1215 const LWGEOM *oldedge_geom;
1216 const LWGEOM *newedge_geom;
1221 split_col =
_lwt_EdgeSplit( topo, edge, pt, skipISOChecks, &oldedge );
1222 if ( ! split_col )
return -1;
1223 oldedge_geom = split_col->
geoms[0];
1224 newedge_geom = split_col->
geoms[1];
1227 ((
LWGEOM*)newedge_geom)->srid = split_col->
srid;
1244 lwerror(
"Backend coding error: "
1245 "insertNodes callback did not return node_id");
1261 if ( newedges[0].edge_id == -1 ) {
1268 if ( newedges[1].edge_id == -1 ) {
1289 if ( ! newedges[0].geom ) {
1292 lwerror(
"first geometry in lwgeom_split output is not a line");
1310 if ( ! newedges[1].geom ) {
1313 lwerror(
"second geometry in lwgeom_split output is not a line");
1323 }
else if ( ret == 0 ) {
1326 lwerror(
"Insertion of split edge failed (no reason)");
1444 LWDEBUGF(2,
"first point is index %d", from);
1446 for ( i = from+inc; i != toofar; i += inc )
1448 LWDEBUGF(2,
"testing point %d", i);
1478 LWDEBUG(1,
"computing azimuth of first edge end");
1481 lwerror(
"Invalid edge (no two distinct vertices exist)");
1485 lwerror(
"error computing azimuth of first edgeend [%.15g %.15g,%.15g %.15g]",
1486 fp->
x, fp->
y, pt.
x, pt.
y);
1489 LWDEBUGF(1,
"azimuth of first edge end [%.15g %.15g,%.15g %.15g] is %g",
1490 fp->
x, fp->
y, pt.
x, pt.
y, fee->
myaz);
1493 LWDEBUG(1,
"computing azimuth of second edge end");
1496 lwerror(
"Invalid edge (no two distinct vertices exist)");
1500 lwerror(
"error computing azimuth of last edgeend [%.15g %.15g,%.15g %.15g]",
1501 lp->
x, lp->
y, pt.
x, pt.
y);
1504 LWDEBUGF(1,
"azimuth of last edge end [%.15g %.15g,%.15g %.15g] is %g",
1505 lp->
x, lp->
y, pt.
x, pt.
y, lee->
myaz);
1528 uint64_t numedges = 1;
1530 double minaz, maxaz;
1538 if ( azdif < 0 ) azdif += 2 * M_PI;
1539 minaz = maxaz = azdif;
1549 " and adjacent to azimuth %g", node,
data->myaz);
1553 if (numedges == UINT64_MAX)
1559 LWDEBUGF(1,
"getEdgeByNode returned %llu edges, minaz=%g, maxaz=%g",
1560 numedges, minaz, maxaz);
1563 for ( i = 0; i < numedges; ++i )
1573 if ( edge->
edge_id == myedge_id )
continue;
1586 " does not have two distinct points",
id);
1600 ", edgeend is [%.15g %.15g,%.15g %.15g]",
1606 lwerror(
"error computing azimuth of edge %"
1608 id, p1.
x, p1.
y, p2.
x, p2.
y);
1611 azdif = az -
data->myaz;
1613 ": %.15g (diff: %.15g)", edge->
edge_id, az, azdif);
1615 if ( azdif < 0 ) azdif += 2 * M_PI;
1616 if ( minaz == -1 ) {
1617 minaz = maxaz = azdif;
1624 " (face_right is new ccwFace, face_left is new cwFace)",
1628 if ( azdif < minaz ) {
1634 " (previous had minaz=%g, face_left is new cwFace)",
1639 else if ( azdif > maxaz ) {
1645 " (previous had maxaz=%g, face_right is new ccwFace)",
1662 ", edgeend is [%.15g %.15g,%.15g %.15g]",
1668 lwerror(
"error computing azimuth of edge %"
1670 id, p1.
x, p1.
y, p2.
x, p2.
y);
1673 azdif = az -
data->myaz;
1675 ": %.15g (diff: %.15g)", edge->
edge_id, az, azdif);
1676 if ( azdif < 0 ) azdif += 2 * M_PI;
1677 if ( minaz == -1 ) {
1678 minaz = maxaz = azdif;
1685 " (face_right is new cwFace, face_left is new ccwFace)",
1689 if ( azdif < minaz ) {
1695 " (previous had minaz=%g, face_right is new cwFace)",
1700 else if ( azdif > maxaz ) {
1704 ", outgoing, from start point, "
1706 " (previous had maxaz=%g, face_left is new ccwFace)",
1718 LWDEBUGF(1,
"edges adjacent to azimuth %g"
1721 data->myaz, node,
data->nextCW, minaz,
1722 data->nextCCW, maxaz);
1724 if ( myedge_id < 1 && numedges && data->cwFace !=
data->ccwFace )
1726 if (
data->cwFace != -1 &&
data->ccwFace != -1 ) {
1752 if ( pa->
npoints < 2 )
return 0;
1756 for (i=1; i<pa->
npoints-1; ++i)
1772 ip->
x = fp.
x + ( (lp.
x - fp.
x) * 0.5 );
1773 ip->
y = fp.
y + ( (lp.
y - fp.
y) * 0.5 );
1782 uint64_t numedges, i, j;
1788 for (i=0; i<num_signed_edge_ids; ++i) {
1789 int absid = llabs(signed_edge_ids[i]);
1792 for (j=0; j<numedges; ++j) {
1793 if ( edge_ids[j] == absid ) {
1798 if ( ! found ) edge_ids[numedges++] = absid;
1804 if (i == UINT64_MAX)
1809 else if ( i != numedges )
1811 lwfree( signed_edge_ids );
1814 " edges found when expecting %" PRIu64, i, numedges);
1822 for ( i=0; i<num_signed_edge_ids; ++i )
1828 for ( j=0; j<numedges; ++j )
1830 if ( ring_edges[j].edge_id == llabs(eid) )
1832 edge = &(ring_edges[j]);
1839 lwerror(
"missing edge that was found in ring edges loop");
1897 uint64_t numfaceedges, i, j;
1898 int newface_outside;
1899 uint64_t num_signed_edge_ids;
1903 int forward_edges_count = 0;
1905 int backward_edges_count = 0;
1908 if (!signed_edge_ids)
1914 LWDEBUGF(1,
"getRingEdges returned %llu edges", num_signed_edge_ids);
1917 for (i=0; i<num_signed_edge_ids; ++i) {
1918 if ( signed_edge_ids[i] == -sedge ) {
1921 lwfree( signed_edge_ids );
1927 sedge, face, mbr_only);
1936 num_signed_edge_ids);
1938 lwfree( signed_edge_ids );
1947 lwfree( signed_edge_ids );
1949 " is geometrically not-closed", sedge);
1955 sedge, isccw ?
"counter" :
"");
1964 lwfree( signed_edge_ids );
1968 LWDEBUG(1,
"The left face of this clockwise ring is the universe, "
1969 "won't create a new face there");
1974 if ( mbr_only && face != 0 )
1980 updface.
mbr = (
GBOX *)shellbox;
1984 lwfree( signed_edge_ids );
1991 lwfree( signed_edge_ids );
1993 lwerror(
"Unexpected error: %d faces found when expecting 1", ret);
1997 lwfree( signed_edge_ids );
2005 if ( face != 0 && ! isccw)
2008 uint64_t nfaces = 1;
2010 if (nfaces == UINT64_MAX)
2012 lwfree( signed_edge_ids );
2019 lwfree( signed_edge_ids );
2021 lwerror(
"Unexpected error: %" PRIu64
" faces found when expecting 1", nfaces);
2024 newface.
mbr = oldface->
mbr;
2028 newface.
mbr = (
GBOX *)shellbox;
2035 lwfree( signed_edge_ids );
2042 lwfree( signed_edge_ids );
2044 lwerror(
"Unexpected error: %d faces inserted when expecting 1", ret);
2055 if ( face != 0 && ! isccw ) {
2057 LWDEBUG(1,
"New face is on the outside of the ring, updating rings in former shell");
2058 newface_outside = 1;
2061 LWDEBUG(1,
"New face is on the inside of the ring, updating forward edges in new ring");
2062 newface_outside = 0;
2075 if (numfaceedges == UINT64_MAX)
2081 LWDEBUGF(1,
"_lwt_AddFaceSplit: lwt_be_getEdgeByFace(%" LWTFMT_ELEMID ") returned %llu edges", face, numfaceedges);
2086 forward_edges_count = 0;
2088 backward_edges_count = 0;
2091 for ( i=0; i<numfaceedges; ++i )
2099 for ( j=0; j<num_signed_edge_ids; ++j )
2101 int seid = signed_edge_ids[j];
2109 if ( found == 2 )
break;
2111 else if ( -seid == e->
edge_id )
2118 if ( found == 2 )
break;
2121 if ( found )
continue;
2145 if ( newface_outside )
2182 if ( forward_edges_count )
2185 forward_edges_count,
2189 lwfree( signed_edge_ids );
2193 if ( ret != forward_edges_count )
2195 lwfree( signed_edge_ids );
2196 lwerror(
"Unexpected error: %d edges updated when expecting %d",
2197 ret, forward_edges_count);
2203 if ( backward_edges_count )
2206 backward_edges_count,
2210 lwfree( signed_edge_ids );
2214 if ( ret != backward_edges_count )
2216 lwfree( signed_edge_ids );
2217 lwerror(
"Unexpected error: %d edges updated when expecting %d",
2218 ret, backward_edges_count);
2230 uint64_t numisonodes = 1;
2233 &numisonodes, fields, newface.
mbr);
2234 if (numisonodes == UINT64_MAX)
2240 if ( numisonodes ) {
2242 int nodes_to_update = 0;
2243 for (i=0; i<numisonodes; ++i)
2250 newface_outside ?
"outside" :
"inside" );
2251 if ( newface_outside )
2275 if ( nodes_to_update )
2281 lwfree( signed_edge_ids );
2305 LWLINE *geom,
int skipChecks,
int modFace )
2314 const LWPOINT *start_node_geom = NULL;
2315 const LWPOINT *end_node_geom = NULL;
2329 lwerror(
"SQL/MM Spatial exception - curve not simple");
2336 newedge.
geom = geom;
2345 lwerror(
"Invalid edge (no two distinct vertices exist)");
2358 lwerror(
"Invalid edge (no two distinct vertices exist)");
2363 lwerror(
"error computing azimuth of first edgeend [%.15g %.15g,%.15g %.15g]",
2364 p1.
x, p1.
y, pn.
x, pn.
y);
2367 LWDEBUGF(1,
"edge's start node is %g,%g", p1.
x, p1.
y);
2375 lwerror(
"Invalid clean edge (no two distinct vertices exist) - should not happen");
2380 lwerror(
"error computing azimuth of last edgeend [%.15g %.15g,%.15g %.15g]",
2381 p2.
x, p2.
y, pn.
x, pn.
y);
2384 LWDEBUGF(1,
"edge's end node is %g,%g", p2.
x, p2.
y);
2391 if ( start_node != end_node ) {
2393 node_ids[0] = start_node;
2394 node_ids[1] = end_node;
2397 node_ids[0] = start_node;
2401 if (num_nodes == UINT64_MAX)
2406 for ( i=0; i<num_nodes; ++i )
2418 lwerror(
"SQL/MM Spatial exception - geometry crosses an edge"
2427 if ( node->
node_id == start_node ) {
2428 start_node_geom = node->
geom;
2430 if ( node->
node_id == end_node ) {
2431 end_node_geom = node->
geom;
2437 if ( ! start_node_geom )
2440 lwerror(
"SQL/MM Spatial exception - non-existent node");
2445 pa = start_node_geom->
point;
2450 lwerror(
"SQL/MM Spatial exception"
2451 " - start node not geometry start point."
2458 if ( ! end_node_geom )
2461 lwerror(
"SQL/MM Spatial exception - non-existent node");
2466 pa = end_node_geom->
point;
2471 lwerror(
"SQL/MM Spatial exception"
2472 " - end node not geometry end point."
2491 if ( newedge.
edge_id == -1 ) {
2497 int isclosed = start_node == end_node;
2500 isclosed ? &epan : NULL, -1 );
2509 if ( modFace != -1 )
2529 isclosed ? &span : NULL, -1 );
2538 if ( modFace != -1 )
2544 lwerror(
"Side-location conflict: "
2545 "new edge starts in face"
2556 lwerror(
"Side-location conflict: "
2557 "new edge starts in face"
2585 " faces mismatch: invalid topology ?",
2591 lwerror(
"Could not derive edge face from linked primitives:"
2592 " invalid topology ?");
2605 }
else if ( ret == 0 ) {
2606 lwerror(
"Insertion of split edge failed (no reason)");
2614 if ( llabs(prev_left) != newedge.
edge_id )
2616 if ( prev_left > 0 )
2633 &updedge, updfields,
2643 if ( llabs(prev_right) != newedge.
edge_id )
2645 if ( prev_right > 0 )
2657 seledge.
edge_id = -prev_right;
2662 &updedge, updfields,
2704 if ( modFace > -1 ) {
2708 LWDEBUG(1,
"New edge is dangling, so it cannot split any face");
2720 if ( newface1 == 0 ) {
2721 LWDEBUG(1,
"New edge does not split any face");
2730 if ( newface == 0 ) {
2731 LWDEBUG(1,
"New edge does not split any face");
2741 if ( newface < 0 )
return newedge.
edge_id;
2780 LWLINE *geom,
int skipChecks )
2782 return _lwt_AddEdge( topo, start_node, end_node, geom, skipChecks, 1 );
2788 LWLINE *geom,
int skipChecks )
2790 return _lwt_AddEdge( topo, start_node, end_node, geom, skipChecks, 0 );
2799 int i, validedges = 0;
2801 for ( i=0; i<numfaceedges; ++i )
2815 if ( numfaceedges )
lwfree(geoms);
2816 LWDEBUG(1,
"_lwt_FaceByEdges returning empty polygon");
2833 LWDEBUGF(1,
"_lwt_FaceByEdges returning area: %s", wkt);
2843 uint64_t numfaceedges;
2853 lwerror(
"SQL/MM Spatial exception - universal face has no geometry");
2865 if (numfaceedges == UINT64_MAX)
2870 LWDEBUGF(1,
"lwt_GetFaceGeometry: lwt_be_getEdgeByFace returned %llu edges", numfaceedges);
2872 if ( numfaceedges == 0 )
2876 if (i == UINT64_MAX)
2882 lwerror(
"SQL/MM Spatial exception - non-existent face.");
2887 lwerror(
"Corrupted topology: multiple face records have face_id=%"
2893 lwnotice(
"Corrupted topology: face %"
2910 lwnotice(
"Corrupted topology: face %"
2945 LWDEBUGF(1,
"Ring's 'from' point (%d) is %g,%g", from, p1.
x, p1.
y);
2949 for ( i=0; i<numedges; ++i )
2963 ") on both sides, skipping",
2971 " has only %"PRIu32
" points",
2988 LWDEBUGF(1,
"Rings's 'from' point is still %g,%g", p1.
x, p1.
y);
2991 LWDEBUG(1,
"P2D_SAME_STRICT(p1,p2) returned true");
2993 " matches ring vertex %d", isoe->
edge_id, from);
2995 for ( j=1; j<epa->
npoints; ++j )
3004 LWDEBUGF(1,
"Ring's point %d is %g,%g",
3005 from+1, pt.
x, pt.
y);
3009 #if POSTGIS_DEBUG_LEVEL > 0
3012 " matches ring vertex %d", isoe->
edge_id, from+1);
3015 " does not match ring vertex %d", isoe->
edge_id, from+1);
3030 " matches ring vertex %d", isoe->
edge_id, from);
3032 for ( j=2; j<=epa->
npoints; j++ )
3041 LWDEBUGF(1,
"Ring's point %d is %g,%g",
3042 from+1, pt.
x, pt.
y);
3047 #if POSTGIS_DEBUG_LEVEL > 0
3050 " matches ring vertex %d", isoe->
edge_id, from+1);
3053 " does not match ring vertex %d", isoe->
edge_id, from+1);
3058 if ( match )
return i;
3074 ary[from++] = ary[to];
3097 uint64_t numfaceedges;
3112 if (numfaceedges == UINT64_MAX)
3117 if ( ! numfaceedges )
return 0;
3118 LWDEBUGF(1,
"lwt_GetFaceEdges: lwt_be_getEdgeByFace returned %llu edges", numfaceedges);
3127 lwerror(
"Corrupted topology: unable to build geometry of face %"
3128 LWTFMT_ELEMID " from its %"PRIu64
" edges", face_id, numfaceedges);
3164 nseid = prevseid = 0;
3168 for ( i=0; i<facepoly->
nrings; ++i )
3177 while ( j < (int32_t) ring->
npoints-1 )
3179 LWDEBUGF(1,
"Looking for edge covering ring %d from vertex %d",
3189 lwerror(
"No edge (among %" PRIu64
") found to be defining geometry of face %"
3194 nextedge = &(edges[edgeno]);
3195 nextline = nextedge->
geom;
3198 " covers ring %d from vertex %d to %d",
3214 seid[nseid++] = nextedge->
face_left == face_id ?
3225 if ( (nseid - prevseid) > 1 )
3229 LWDEBUGF(1,
"Looking for smallest id among the %d edges "
3230 "composing ring %d", (nseid-prevseid), i);
3231 for ( j=prevseid; j<nseid; ++j )
3235 if ( ! minid ||
id < minid )
3242 " at position %d", minid, minidx);
3243 if ( minidx != prevseid )
3267 int leftRingIsCCW = -1;
3272 lwerror(
"SQL/MM Spatial exception - curve not simple");
3281 "lwt_be_getEdgeById returned NULL and set i=%llu", i);
3282 if (i == UINT64_MAX)
3289 lwerror(
"SQL/MM Spatial exception - non-existent edge %"
3295 lwerror(
"Backend coding error: getEdgeById callback returned NULL "
3296 "but numelements output parameter has value %" PRIu64
" "
3297 "(expected 0 or 1)", i);
3303 "old edge has %d points, new edge has %d points",
3314 lwerror(
"SQL/MM Spatial exception - "
3315 "start node not geometry start point.");
3326 " has less than 2 vertices", oldedge->
edge_id);
3333 lwerror(
"Invalid edge: less than 2 vertices");
3340 lwerror(
"SQL/MM Spatial exception - "
3341 "end node not geometry end point.");
3357 lwerror(
"Invalid edge (no two distinct vertices exist)");
3366 lwerror(
"Edge twist at node POINT(%g %g)", p1.
x, p1.
y);
3372 oldedge->
end_node, geom, edge_id ) )
3379 LWDEBUG(1,
"lwt_ChangeEdgeGeom: "
3380 "edge crossing check passed ");
3398 LWDEBUGF(1,
"lwt_be_getNodeWithinBox2D returned %llu nodes", numnodes);
3399 if (numnodes == UINT64_MAX)
3406 if ( numnodes > ( 1 + isclosed ? 0 : 1 ) )
3409 for (i=0; i<numnodes; ++i)
3422 lwerror(
"Edge motion collision at %s", wkt);
3430 LWDEBUG(1,
"nodes containment check passed");
3443 isclosed ? &epan_pre : NULL, edge_id );
3445 isclosed ? &span_pre : NULL, edge_id );
3468 uint64_t num_signed_edge_ids;
3472 LWDEBUG(1,
"Twist check before");
3475 if (!signed_edge_ids)
3481 LWDEBUGF(1,
"getRingEdges returned %llu edges", num_signed_edge_ids);
3485 lwfree( signed_edge_ids );
3495 lwfree( signed_edge_ids );
3497 " is geometrically not-closed", edge_id);
3503 lwfree( signed_edge_ids );
3505 LWDEBUGF(1,
"Ring of edge %" LWTFMT_ELEMID " is %sclockwise", edge_id, leftRingIsCCW ?
"counter" :
"");
3511 newedge.
geom = geom;
3522 lwerror(
"Unexpected error: %" PRIu64
" edges updated when expecting 1", i);
3535 isclosed ? &epan_post : NULL, edge_id );
3537 isclosed ? &span_post : NULL, edge_id );
3552 lwerror(
"Edge changed disposition around start node %"
3563 lwerror(
"Edge changed disposition around end node %"
3569 if ( leftRingIsCCW != -1 )
3571 uint64_t num_signed_edge_ids;
3576 LWDEBUG(1,
"Twist check after");
3579 if (!signed_edge_ids)
3585 LWDEBUGF(1,
"getRingEdges returned %llu edges", num_signed_edge_ids);
3589 lwfree( signed_edge_ids );
3599 lwfree( signed_edge_ids );
3601 " is geometrically not-closed", edge_id);
3607 lwfree( signed_edge_ids );
3609 if ( isCCW != leftRingIsCCW )
3612 lwerror(
"Edge ring changes winding");
3634 uint64_t facestoupdate = 0;
3648 faces[facestoupdate++].
mbr = updatedBox;
3657 lwerror(
"Corrupted topology: face %"
3664 faces[facestoupdate++].
mbr = updatedBox;
3666 LWDEBUGF(1,
"%llu faces to update", facestoupdate);
3667 if ( facestoupdate )
3670 if (updatedFaces != facestoupdate)
3672 while ( facestoupdate-- )
lwfree(faces[facestoupdate].mbr);
3674 if (updatedFaces == UINT64_MAX)
3677 lwerror(
"Unexpected error: %" PRIu64
" faces updated when expecting 1", updatedFaces);
3681 while ( facestoupdate-- )
lwfree(faces[facestoupdate].mbr);
3685 LWDEBUG(1,
"BBOX of changed edge did not change");
3688 LWDEBUG(1,
"all done, cleaning up edges");
3702 if (n == UINT64_MAX)
3708 lwerror(
"SQL/MM Spatial exception - non-existent node");
3714 lwerror(
"SQL/MM Spatial exception - not isolated node");
3729 if ( ! node )
return -1;
3734 lwerror(
"SQL/MM Spatial exception - coincident node");
3741 lwerror(
"SQL/MM Spatial exception - edge crosses node.");
3748 if ( newPointFace == -1 ) {
3755 lwerror(
"Cannot move isolated node across faces");
3780 if ( ! node )
return -1;
3792 lwerror(
"Unexpected error: %d nodes deleted when expecting 1", n);
3829 lwerror(
"SQL/MM Spatial exception - non-existent edge");
3835 lwerror(
"Corrupted topology: more than a single edge have id %"
3840 if ( edge[0].face_left != edge[0].face_right )
3843 lwerror(
"SQL/MM Spatial exception - not isolated edge");
3854 if ((n == UINT64_MAX) || (edge == NULL))
3859 for (i = 0; i < n; ++i)
3861 if (edge[i].edge_id !=
id)
3864 lwerror(
"SQL/MM Spatial exception - not isolated edge");
3872 if (n == UINT64_MAX)
3879 lwerror(
"Unexpected error: %" PRIu64
" edges deleted when expecting 1", n);
3886 if ( nid[1] != nid[0] ) {
3893 if (n == UINT64_MAX)
3931 if ( ret == -1 )
return -1;
3939 if ( ret == -1 )
return -1;
3964 if ( ret == -1 )
return -1;
3978 uint64_t i, nedges, nfaces, fields;
3984 int nedge_right = 0;
3991 int fnode_edges = 0;
3993 int lnode_edges = 0;
4003 LWDEBUGF(1,
"lwt_be_getEdgeById returned NULL and set i=%llu", i);
4004 if (i == UINT64_MAX)
4017 "Backend coding error: getEdgeById callback returned NULL "
4018 "but numelements output parameter has value %" PRIu64
" "
4019 "(expected 0 or 1)",
4032 LWDEBUG(1,
"Updating next_{right,left}_face of ring edges...");
4040 node_ids[nedges++] = edge->
end_node;
4046 if (nedges == UINT64_MAX)
4051 nedge_left = nedge_right = 0;
4052 for ( i=0; i<nedges; ++i )
4055 if ( e->
edge_id == edge_id )
continue;
4093 LWDEBUGF(1,
"updating %d 'next_left' edges", nedge_left);
4106 LWDEBUGF(1,
"updating %d 'next_right' edges", nedge_right);
4117 LWDEBUGF(1,
"releasing %llu updateable edges in %p", nedges, upd_edge);
4135 LWDEBUG(1,
"floodface is universe");
4149 if (nfaces == UINT64_MAX)
4156 for ( i=0; i<nfaces; ++i )
4158 if ( faces[i].face_id == edge->
face_left )
4160 if ( ! box1 ) box1 = faces[i].
mbr;
4166 lwerror(
"corrupted topology: more than 1 face have face_id=%"
4171 else if ( faces[i].face_id == edge->
face_right )
4173 if ( ! box2 ) box2 = faces[i].
mbr;
4179 lwerror(
"corrupted topology: more than 1 face have face_id=%"
4189 lwerror(
"Backend coding error: getFaceById returned face "
4198 lwerror(
"corrupted topology: no face have face_id=%"
4207 lwerror(
"corrupted topology: no face have face_id=%"
4228 lwerror(
"Unexpected error: %" PRIu64
" faces updated when expecting 1", i);
4247 lwerror(
"Unexpected error: %d faces inserted when expecting 1",
result);
4312 if ( ! fnode_edges )
4354 return modFace ? floodface : newface.
face_id;
4377 uint64_t num_node_edges;
4384 int e2sign, e2freenode;
4388 size_t bufleft = 256;
4396 " with itself, try with another", eid1);
4403 if ((nedges == UINT64_MAX) || (edges == NULL))
4408 for ( i=0; i<nedges; ++i )
4410 if ( edges[i].edge_id == eid1 ) {
4413 lwerror(
"Corrupted topology: multiple edges have id %"
4419 else if ( edges[i].edge_id == eid2 ) {
4422 lwerror(
"Corrupted topology: multiple edges have id %"
4433 "SQL/MM Spatial exception - non-existent edge %" LWTFMT_ELEMID,
4441 "SQL/MM Spatial exception - non-existent edge %" LWTFMT_ELEMID,
4475 if ( commonnode != -1 )
4480 if (num_node_edges == UINT64_MAX)
4486 for (i=0; i<num_node_edges; ++i)
4489 if ( node_edges[i].edge_id == eid1 )
continue;
4490 if ( node_edges[i].edge_id == eid2 )
continue;
4493 if ( bufleft > 0 ) {
4495 ( ptr==buf ?
"" :
"," ), node_edges[i].edge_id);
4496 if (
r >= (
int) bufleft )
4514 if ( commonnode == -1 )
4527 if ( commonnode != -1 )
4532 if (num_node_edges == UINT64_MAX)
4538 for (i=0; i<num_node_edges; ++i)
4541 if ( node_edges[i].edge_id == eid1 )
continue;
4542 if ( node_edges[i].edge_id == eid2 )
continue;
4545 if ( bufleft > 0 ) {
4547 ( ptr==buf ?
"" :
"," ), node_edges[i].edge_id);
4548 if (
r >= (
int) bufleft )
4563 if ( num_node_edges )
lwfree(node_edges);
4567 if ( commonnode == -1 )
4572 lwerror(
"SQL/MM Spatial exception - other edges connected (%s)",
4577 lwerror(
"SQL/MM Spatial exception - non-connected edges");
4654 lwerror(
"Coding error: caseno=%d should never happen", caseno);
4680 lwerror(
"Unexpected error: %" PRIu64
" edges updated when expecting 1", i);
4702 lwerror(
"Insertion of split edge failed (no reason)");
4806 eid1, eid2, newedge.
edge_id) )
4812 return modEdge ? commonnode : newedge.
edge_id;
4842 if (num == UINT64_MAX)
4852 lwerror(
"Two or more nodes found");
4877 if (num == UINT64_MAX)
4882 for (i=0; i<num;++i)
4893 " has null geometry", e->
edge_id);
4901 if ( dist > tol )
continue;
4907 lwerror(
"Two or more edges found");
4946 LWDEBUG(1,
"No face properly contains query point,"
4947 " looking for edges");
4954 if (num == UINT64_MAX)
4959 for (i=0; i<num; ++i)
4970 " has null geometry", e->
edge_id);
4978 " is dangling, won't consider it", e->
edge_id);
4986 " is %g (tol=%g)", e->
edge_id, dist, tol);
4989 if ( dist > tol )
continue;
4998 lwerror(
"Two or more faces found");
5002 if (
id &&
id != eface )
5005 lwerror(
"Two or more faces found"
5022 static inline double
5025 double ret = 3.6 * pow(10, - ( 15 - log10(d?d:1.0) ) );
5051 if ( ! gbox )
return 0;
5062 #define _LWT_MINTOLERANCE( topo, geom ) ( \
5063 topo->precision ? topo->precision : _lwt_minTolerance(geom) )
5097 GEOSGeometry *edgeg;
5101 if (num == UINT64_MAX)
5117 for (i=0; i<num; ++i)
5126 GEOSGeom_destroy(edgeg);
5131 equals = GEOSEquals(gg, edgeg);
5132 GEOSGeom_destroy(gg);
5135 GEOSGeom_destroy(edgeg);
5177 GEOSGeom_destroy(edgeg);
5182 GEOSGeom_destroy(edgeg);
5223 LWDEBUGG(1, motionPoly,
"Motion range");
5228 GEOSGeometry *motionPolyG = NULL;
5229 for ( uint64_t t=0; t<splitNodeEdges->
numEdges; t++ )
5233 if ( e == existingEdge )
continue;
5234 if ( e == edge )
continue;
5235 if ( ! motionPolyG ) {
5237 if ( ! motionPolyG )
5246 GEOSGeom_destroy(motionPolyG);
5252 char *relate = GEOSRelate( motionPolyG, eg );
5253 GEOSGeom_destroy(eg);
5256 GEOSGeom_destroy(motionPolyG);
5262 int match = GEOSRelatePatternMatch(relate,
"FF*F*****");
5266 GEOSGeom_destroy(motionPolyG);
5273 GEOSGeom_destroy(motionPolyG);
5282 if ( motionPolyG ) GEOSGeom_destroy(motionPolyG);
5309 if ( splitC->
ngeoms != 2 )
5311 lwerror(
"Split of edge resulted in %d components", splitC->
ngeoms);
5316 if ( ! firstNodeEdges ) {
5317 lwerror(
"No edges found in DB to be incident to split edge's first node");
5321 if ( ! lastNodeEdges ) {
5322 lwerror(
"No edges found in DB to be incident to split edge's last node");
5326 if ( ! splitNodeEdges ) {
5327 lwerror(
"No edges found in DB to be incident to split node");
5332 for ( uint64_t t=0; t<firstNodeEdges->
numEdges; t++ )
5336 edge = &(firstNodeEdges->
edges[t]);
5365 for ( n=0; n<2; ++n)
5370 if ( existingEdgeId == -1 )
5375 existingEdges[n] = NULL;
5376 if ( existingEdgeId == 0 )
5378 LWDEBUGF(1,
"Split component %llu is a new edge, computing edgeEndInfo", n);
5394 lwerror(
"No distinct vertices found on second part of split edge");
5408 lwerror(
"No distinct vertices found on first part of split edge");
5412 if ( !
azimuth_pt_pt(pt, &op, &(splitNodeEdgeEnds[n].myaz)) ) {
5413 lwerror(
"error computing azimuth of split endpoint [%.15g %.15g,%.15g %.15g]",
5419 LWDEBUGF(1,
"Azimuth of split component %llu edgeend [%.15g %.15g,%.15g %.15g] is %.15g",
5420 n, op.
x, op.
y, pt->
x, pt->
y, splitNodeEdgeEnds[n].
myaz);
5423 lwerror(
"Unexpected backend return: _lwt_FindAdjacentEdges(%"
5424 LWTFMT_ELEMID ") found no edges when we previously split edge %"
5437 " (%s)", n, existingEdgeId, forward ?
"forward" :
"backward" );
5439 for ( uint64_t t=0; t<splitNodeEdges->
numEdges; t++ )
5441 if ( splitNodeEdges->
edges[t].
edge_id == existingEdgeId )
5443 existingEdges[n] = &(splitNodeEdges->
edges[t]);
5447 if (existingEdges[n] == NULL)
5449 lwerror(
"could not find edge %" LWTFMT_ELEMID " in database, but _lwt_GetEqualEdges said it was there", existingEdgeId );
5454 replacedBy[n] = existingEdgeId;
5462 if ( ( replacedBy[0] != 0 && replacedBy[1] == 0 ) ||
5463 ( replacedBy[1] != 0 && replacedBy[0] == 0 ) )
5468 edgeend *splitNodeEdgeEndInfo;
5469 LWLINE *newSplitEdgeLine;
5470 int splitNodeNewEdgeOutgoing;
5472 if ( replacedBy[1] == 0 )
5475 existingEdge = existingEdges[0];
5477 splitNodeEdgeEndInfo = &(splitNodeEdgeEnds[1]);
5478 splitNodeNewEdgeOutgoing = 1;
5483 existingEdge = existingEdges[1];
5485 splitNodeEdgeEndInfo = &(splitNodeEdgeEnds[0]);
5486 splitNodeNewEdgeOutgoing = 0;
5518 if ( splitNodeNewEdgeOutgoing ) {
5528 splitNodeEdgeEndInfo->
nextCCW,
5532 if ( splitNodeEdgeEndInfo->
nextCCW > 0 )
5561 &updatedEdge, updateFlags,
5566 }
else if ( ret == 0 ) {
5569 }
else if ( ret > 1 ) {
5570 lwerror(
"More than a single edge found with id %"
5579 if ( splitNodeNewEdgeOutgoing ) {
5587 splitNodeEdgeEndInfo->
nextCW, sideFace
5590 if ( splitNodeEdgeEndInfo->
nextCW > 0 )
5596 ", outgoing next CW of new split edge on split node",
5605 ", incoming next CW of new split edge on split node",
5611 &updatedEdge, updateFlags,
5616 }
else if ( ret == 0 ) {
5619 }
else if ( ret > 1 ) {
5620 lwerror(
"More than a single edge found with edge_id %"
5633 int nextCCWEdgeIsIncoming = 0;
5634 int collapsedEdgeIsIncoming;
5639 if ( replacedBy[0] ) {
5640 commonNodeEdges = firstNodeEdges;
5642 collapsedEdgeIsIncoming = 0;
5644 commonNodeEdges = lastNodeEdges;
5646 collapsedEdgeIsIncoming = 1;
5654 LWDEBUGF(1,
"Looking for next CCW edge of split edge %"
5656 " having %llu attached edges",
5658 commonNodeID, commonNodeEdges->
numEdges
5662 for ( uint64_t t=0; t<commonNodeEdges->
numEdges; t++ )
5676 if ( et->
next_right == signedCollapsedEdgeID )
5679 nextCCWEdgeIsIncoming = 0;
5683 else if ( et->
end_node == commonNodeID )
5685 if ( et->
next_left == signedCollapsedEdgeID )
5688 nextCCWEdgeIsIncoming = 1;
5693 if ( ! nextCCWEdge )
5701 LWDEBUGF(1,
"Next CCW edge of split edge %"
5706 nextCCWEdgeIsIncoming ?
"incoming" :
"outgoing",
5712 LWDEBUG(1,
"Next CCW edge is existing/collapse edge, will not update here");
5729 if ( nextCCWEdgeIsIncoming ) {
5732 LWDEBUGF(1,
"Updating next_left of incoming next CCW edge %"
5741 LWDEBUGF(1,
"Updating next_right of outgoing next CCW edge %"
5750 &updatedEdge, updateFlags,
5755 }
else if ( ret == 0 ) {
5758 }
else if ( ret > 1 ) {
5759 lwerror(
"More than a single edge found with next_left %"
5778 LWDEBUGF(1,
"Will update next_left of existing edge %"
5788 LWDEBUGF(1,
"Will update next_right of existing edge %"
5798 LWDEBUGF(1,
"Will update next_left of existing edge %"
5808 LWDEBUGF(1,
"Will update next_right of existing edge %"
5818 &updatedEdge, updateFlags,
5823 }
else if ( ret == 0 ) {
5826 }
else if ( ret > 1 ) {
5827 lwerror(
"More than a single edge found with id %"
5840 updatedEdge.
geom = newSplitEdgeLine;
5842 if ( splitNodeNewEdgeOutgoing ) {
5871 &updatedEdge, updateFlags,
5876 }
else if ( ret == 0 ) {
5879 }
else if ( ret > 1 ) {
5880 lwerror(
"More than a single edge found with id %"
5969 else if ( replacedBy[0] == 0 && replacedBy[1] == 0 )
5977 if ( newEdge.
edge_id == -1 ) {
6001 for ( uint64_t t=0; t<splitNodeEdges->
numEdges; t++ )
6023 int splitFaceOnLeft = 1;
6026 splitFaceOnLeft = 0;
6054 }
else if ( ret == 0 ) {
6057 }
else if ( ret > 1 ) {
6058 lwerror(
"More than a single edge found with id %"
6082 &updatedEdge, updateFlags,
6087 }
else if ( ret == 0 ) {
6090 }
else if ( ret > 1 ) {
6091 lwerror(
"More than a single edge found with id %"
6116 &updatedEdge, updateFlags,
6121 }
else if ( ret == 0 ) {
6124 }
else if ( ret > 1 ) {
6125 lwerror(
"More than a single edge found with id %"
6134 for ( uint64_t t=0; t<lastNodeEdges->
numEdges; t++ )
6156 if ( updateFlags != 0 )
6163 &updatedEdge, updateFlags,
6168 }
else if ( ret == 0 ) {
6171 }
else if ( ret > 1 ) {
6172 lwerror(
"More than a single edge found with id %"
6182 if ( splitFaceOnLeft ) {
6206 -newEdge.
edge_id, newFaceId1);
6231 newFaceId1, newFaceId2, oldFaceId);
6232 if ( newFaceId1 || newFaceId2 )
6236 newFaceId1 ? newFaceId1 : oldFaceId,
6237 newFaceId2 ? newFaceId2 : oldFaceId
6244 if ( newFaceId1 && newFaceId2 )
6258 else if ( replacedBy[0] != 0 && replacedBy[1] != 0 )
6292 for ( uint64_t t=0; t<firstNodeEdges->
numEdges; t++ )
6321 if ( updateFlags != 0 )
6328 &updatedEdge, updateFlags,
6333 }
else if ( ret == 0 ) {
6336 }
else if ( ret > 1 ) {
6337 lwerror(
"More than a single edge found with id %"
6345 for ( uint64_t t=0; t<lastNodeEdges->
numEdges; t++ )
6372 if ( updateFlags != 0 )
6379 &updatedEdge, updateFlags,
6384 }
else if ( ret == 0 ) {
6387 }
else if ( ret > 1 ) {
6388 lwerror(
"More than a single edge found with id %"
6414 &updatedEdge, updateFlags,
6419 }
else if ( ret == 0 ) {
6422 }
else if ( ret > 1 ) {
6423 lwerror(
"More than a single edge found with id %"
6448 &updatedEdge, updateFlags,
6453 }
else if ( ret == 0 ) {
6456 }
else if ( ret > 1 ) {
6457 lwerror(
"More than a single edge found with id %"
6527 for (i=0; i<num; ++i)
6534 if ( dist >= tol )
continue;
6536 sorted[j++].
score = dist;
6547 for (j=0, i=0; i<num; ++i)
6549 if ( sorted[i].score == sorted[0].score )
6565 if ( moved ) *moved =
lwgeom_same(prj, lwpoint) ? 0 : 1;
6588 LWDEBUGG(1, prj,
"Projected point");
6592 for (i=0; i<num; ++i)
6608 lwerror(
"Something went wrong with _lwt_SnapEdgeToExistingNode");
6625 " does not contain projected point to it",
6633 LWDEBUG(1,
"But there's another to check");
6642 LWDEBUGF(1,
"Edge snapped with tolerance %g", tol);
6645 #if POSTGIS_DEBUG_LEVEL > 0
6650 LWDEBUGF(1,
"Edge %s snapped became %s", wkt1, wkt2);
6663 LWDEBUGF(1,
"Edge first point is %g %g, "
6664 "snapline first point is %g %g",
6665 p1.
x, p1.
y, p2.
x, p2.
y);
6666 if ( p1.
x != p2.
x || p1.
y != p2.
y )
6668 LWDEBUG(1,
"Snapping moved first point, re-adding it");
6676 #if POSTGIS_DEBUG_LEVEL > 0
6680 LWDEBUGF(1,
"Tweaked snapline became %s", wkt1);
6685 #if POSTGIS_DEBUG_LEVEL > 0
6687 LWDEBUG(1,
"Snapping did not move first point");
6696 lwerror(
"lwt_ChangeEdgeGeom failed");
6701 #if POSTGIS_DEBUG_LEVEL > 0
6707 LWDEBUGF(1,
"Edge %s contains projected point %s", wkt1, wkt2);
6719 lwerror(
"lwt_ModEdgeSplit failed");
6740 if ( edges2 )
lwfree(edges2);
6753 findFace,
int *moved)
6756 double mindist = FLT_MAX;
6777 if (num == UINT64_MAX)
6784 LWDEBUGF(1,
"New point is within %.15g units of %llu nodes", tol, num);
6789 for (i=0; i<num; ++i)
6791 sorted[i].
ptr = nodes+i;
6794 ((
LWT_ISO_NODE*)(sorted[i].ptr))->node_id, sorted[i].score);
6798 for (i=0; i<num; ++i)
6807 for ( i=0; i<num; ++i )
6814 if ( dist && dist >= tol )
continue;
6815 if ( !
id || dist < mindist )
6825 if ( moved ) *moved = mindist == 0 ? 0 : 1;
6839 if (num == UINT64_MAX)
6844 LWDEBUGF(1,
"New point is within %.15g units of %llu edges", tol, num);
6856 if ( moved ) *moved = 0;
6861 lwerror(
"lwt_AddIsoNode failed");
6892 int handleFaceSplit,
int *forward )
6895 LWPOINT *start_point, *end_point;
6905 LWDEBUGF(1,
"_lwtAddLineEdge with tolerance %g", tol);
6908 if ( ! start_point )
6910 lwnotice(
"Empty component of noded line");
6915 handleFaceSplit, &mm );
6917 if ( nid[0] == -1 )
return -1;
6919 LWDEBUGF(1,
"node for start point added or found to be %" LWTFMT_ELEMID " (moved ? %d)", nid[0], mm);
6925 lwerror(
"could not get last point of line "
6926 "after successfully getting first point !?");
6931 handleFaceSplit, &mm );
6933 if ( nid[1] == -1 )
return -1;
6944 LWDEBUG(1,
"One or both line endpoints moved by snap, updating line");
6946 nn = nid[0] == nid[1] ? 1 : 2;
6949 if (nn == UINT64_MAX)
6954 start_point = NULL; end_point = NULL;
6955 for (i=0; i<nn; ++i)
6957 if ( node[i].node_id == nid[0] ) start_point = node[i].
geom;
6958 if ( node[i].node_id == nid[1] ) end_point = node[i].
geom;
6960 if ( ! start_point || ! end_point )
6983 LWDEBUGG(2, tmp,
"Made-valid after snap to drifted endpoints");
6992 if ( colex->
ngeoms == 0 )
6996 LWDEBUG(1,
"Made-valid snapped edge collapsed");
7008 lwerror(
"lwcollection_extract(LINETYPE) returned a non-line?");
7017 LWDEBUGF(1,
"Made-valid snapped edge collapsed to %s",
7047 LWDEBUGG(2, tmp2,
"Repeated-point removed");
7056 LWDEBUG(1,
"Repeated-point removed edge collapsed");
7077 id =
_lwt_AddEdge( topo, nid[0], nid[1], edge, 0, handleFaceSplit ? 1 : -1 );
7100 if ( ! col->
ngeoms )
return bg;
7102 for (i=0; i<col->
ngeoms; ++i)
7116 int handleFaceSplit)
7126 uint64_t num, numedges = 0, numnodes = 0;
7130 int input_was_closed = 0;
7141 input_was_closed = 1;
7143 LWDEBUGF(1,
"Input line is closed, original point is %g,%g", originalStartPoint.
x, originalStartPoint.
y);
7150 LWDEBUGF(1,
"Working tolerance:%.15g", tol);
7158 LWDEBUGG(1, tmp,
"Repeated-point removed");
7159 }}
else tmp=(
LWGEOM*)line;
7164 if ( ! noded )
return NULL;
7171 LWDEBUGF(1,
"BOX expanded by %g is %.15g %.15g, %.15g %.15g",
7175 int nearbyindex = 0;
7176 int nearbycount = 0;
7180 if (numedges == UINT64_MAX)
7186 LWDEBUGF(1,
"Line has %u points, its bbox intersects %llu edges bboxes",
7193 nearbycount += numedges;
7195 for (i=0; i<numedges; ++i)
7203 if ( 0 == GEOSDistanceIndexed(edge_g, noded_g, &dist) ) {
7204 GEOSGeom_destroy(edge_g);
7205 GEOSGeom_destroy(noded_g);
7210 GEOSGeom_destroy(edge_g);
7211 if ( dist && dist >= tol )
continue;
7212 nearby[nearbyindex++] = g;
7214 LWDEBUGF(1,
"Found %d edges closer than tolerance (%g)", nearbyindex, tol);
7215 GEOSGeom_destroy(noded_g);
7217 int nearbyedgecount = nearbyindex;
7224 if (numnodes == UINT64_MAX)
7230 LWDEBUGF(1,
"Line bbox intersects %llu nodes bboxes", numnodes);
7234 nearbycount = nearbyedgecount + numnodes;
7241 for (i=0; i<numnodes; ++i)
7248 if ( dist && dist >= tol )
7253 nearby[nearbyindex++] = g;
7256 LWDEBUGF(1,
"Found %d isolated nodes closer than tolerance (%g)", nn, tol);
7258 int nearbynodecount = nearbyindex - nearbyedgecount;
7259 nearbycount = nearbyindex;
7261 LWDEBUGF(1,
"Number of nearby elements is %d", nearbycount);
7270 NULL, nearbycount, nearby);
7273 LWDEBUGG(1, elems,
"Collected nearby elements");
7278 LWDEBUGG(1, noded,
"Elements-snapped");
7279 if ( input_was_closed )
7286 LWDEBUGF(1,
"Closed input line start point after snap %g,%g", originalStartPoint.
x, originalStartPoint.
y);
7301 LWDEBUGG(1, noded,
"Unary-unioned");
7305 if ( nearbyedgecount )
7311 LWDEBUGF(1,
"Line intersects %d edges", nearbyedgecount);
7314 NULL, nearbyedgecount, nearby);
7316 LWDEBUGG(1, iedges,
"Collected edges");
7318 LWDEBUGF(1,
"Diffing noded, with srid=%d "
7319 "and intersecting edges, with srid=%d",
7324 LWDEBUGF(1,
"Intersecting noded, with srid=%d "
7325 "and intersecting edges, with srid=%d",
7345 LWDEBUG(1,
"Linemerging intersection");
7355 LWDEBUG(1,
"Unioning difference and (linemerged) intersection");
7357 LWDEBUGG(1, noded,
"Diff-Xset Unioned");
7364 if ( input_was_closed )
7376 LWDEBUGG(1, noded,
"Diff-Xset Unioned cannot be scrolled");
7389 if ( nearbyedgecount )
7391 nearbycount += nearbyedgecount * 2;
7393 for (
int i=0; i<nearbyedgecount; i++)
7405 if ( nearbynodecount )
7408 NULL, nearbynodecount,
7409 nearby + nearbyedgecount);
7421 LWDEBUG(1,
"Freeing up nearby elements");
7424 if ( nearby )
lwfree(nearby);
7428 LWDEBUGG(2, noded,
"Finally-noded");
7434 LWDEBUG(1,
"Noded line was a collection");
7440 LWDEBUG(1,
"Noded line was a single geom");
7441 geomsbuf[0] = noded;
7446 LWDEBUGF(1,
"Line was split into %d edges", ngeoms);
7454 for ( i=0; i<ngeoms; ++i )
7460 #if POSTGIS_DEBUG_LEVEL > 0
7464 LWDEBUGF(1,
"Component %llu of split line is: %s", i, wkt1);
7479 LWDEBUGF(1,
"Component %llu of split line collapsed", i);
7484 i, forward ?
"forward" :
"backward",
id);
7485 ids[num++] = forward ? id : -id;
7488 LWDEBUGG(2, noded,
"Noded before free");
7523 if ( nedges > 0 )
lwfree(ids);
7532 for ( i=0; i<poly->
nrings; ++i )
7551 uint64_t nfacesinbox;
7555 const GEOSPreparedGeometry *ppoly;
7556 GEOSGeometry *polyg;
7567 LWDEBUGF(1,
"Working tolerance:%.15g", tol);
7579 if (nfacesinbox == UINT64_MAX)
7596 ppoly = GEOSPrepare(polyg);
7598 for ( j=0; j<nfacesinbox; ++j )
7602 GEOSGeometry *fgg, *sp;
7610 GEOSPreparedGeom_destroy(ppoly);
7611 GEOSGeom_destroy(polyg);
7622 GEOSPreparedGeom_destroy(ppoly);
7623 GEOSGeom_destroy(polyg);
7628 sp = GEOSPointOnSurface(fgg);
7629 GEOSGeom_destroy(fgg);
7632 GEOSPreparedGeom_destroy(ppoly);
7633 GEOSGeom_destroy(polyg);
7638 covers = GEOSPreparedCovers( ppoly, sp );
7639 GEOSGeom_destroy(sp);
7642 GEOSPreparedGeom_destroy(ppoly);
7643 GEOSGeom_destroy(polyg);
7656 GEOSPreparedGeom_destroy(ppoly);
7657 GEOSGeom_destroy(polyg);
7673 uint64_t numedges, i;
7675 const POINT2D *closestPointOnEdge = NULL;
7676 uint32_t closestSegmentIndex;
7677 int closestSegmentSide;
7678 uint32_t closestPointVertex;
7679 const POINT2D *closestSegmentP0, *closestSegmentP1;
7692 if (numedges == UINT64_MAX)
7738 closestSegmentP0->
x,
7739 closestSegmentP0->
y,
7740 closestSegmentP1->
x,
7758 const POINT2D *p = queryPoint;
7759 const POINT2D *A = closestSegmentP0;
7760 const POINT2D *B = closestSegmentP1;
7761 double r = ( (p->
x-A->
x) * (B->
x-A->
x) + (p->
y-A->
y) * (B->
y-A->
y) )/( (B->
x-A->
x)*(B->
x-A->
x) +(B->
y-A->
y)*(B->
y-A->
y) );
7764 closestPointOnEdge = A;
7765 closestPointVertex = closestSegmentIndex;
7766 if ( closestSegmentIndex == 0 )
7773 closestPointOnEdge = B;
7774 closestPointVertex = closestSegmentIndex + 1;
7777 closestNode = closestEdge->
end_node;
7785 if ( closestNode != 0 )
7801 lwerror(
"Two or more faces found");
7804 containingFace = closestEdge->
face_left;
7809 if (numedges == UINT64_MAX)
7816 for (i=0; i<numedges; ++i)
7818 if ( edges[i].face_left != containingFace ||
7819 edges[i].face_right != containingFace )
7823 lwerror(
"Two or more faces found");
7830 ") returns no edges when we previously found edge %"
7832 closestNode, closestEdge->
edge_id);
7837 LWDEBUGF(1,
"lwt_be_getEdgeByNode returned %" PRIu64
" edges", numedges);
7840 return containingFace;
7848 lwerror(
"error computing azimuth of query point [%.15g %.15g,%.15g %.15g]",
7849 closestPointOnEdge->
x, closestPointOnEdge->
y,
7850 queryPoint->
x, queryPoint->
y);
7859 lwerror(
"Unexpected backend return: _lwt_FindAdjacentEdges(%"
7861 ") found no edges when we previously found edge %"
7863 closestNode, closestEdge->
edge_id);
7873 LWDEBUG(1,
"Closest point is NOT a node");
7880 " on both sides", closestEdge->
face_left);
7882 containingFace = closestEdge->
face_left;
7884 return containingFace;
7891 lwerror(
"Two or more faces found");
7899 LWDEBUGF(1,
"Closest point is vertex %d of closest segment", closestPointVertex);
7910 uint32_t prevVertexIndex = closestPointVertex > 0 ?
7911 closestPointVertex - 1u :
7919 LWDEBUGF(1,
"Previous vertex %u is POINT(%.15g %.15g)",
7925 uint32_t nextVertexIndex = closestPointVertex == closestEdge->
geom->
points->
npoints - 1u ?
7927 closestPointVertex + 1u;
7934 LWDEBUGF(1,
"Next vertex %u is POINT(%.15g %.15g)",
7945 if ( !
azimuth_pt_pt(closestPointOnEdge, prevVertex, &azS0)) {
7946 lwerror(
"error computing azimuth of segment to closest point [%.15g %.15g,%.15g %.15g]",
7947 closestPointOnEdge->
x, closestPointOnEdge->
y,
7948 prevVertex->
x, prevVertex->
y);
7952 if ( !
azimuth_pt_pt(closestPointOnEdge, nextVertex, &azS1)) {
7953 lwerror(
"error computing azimuth of segment from closest point [%.15g %.15g,%.15g %.15g]",
7954 closestPointOnEdge->
x, closestPointOnEdge->
y,
7955 nextVertex->
x, nextVertex->
y);
7959 if ( !
azimuth_pt_pt(closestPointOnEdge, queryPoint, &azSL) ) {
7960 lwerror(
"error computing azimuth of queryPoint [%.15g %.15g,%.15g %.15g]",
7961 closestPointOnEdge->
x, closestPointOnEdge->
y,
7962 queryPoint->
x, queryPoint->
y);
7967 double angle_S0_S1 = azS1 - azS0;
7968 if ( angle_S0_S1 < 0 ) angle_S0_S1 += 2 * M_PI;
7970 double angle_S0_SL = azSL - azS0;
7971 if ( angle_S0_SL < 0 ) angle_S0_SL += 2 * M_PI;
7973 LWDEBUGF(1,
"Azimuths from closest (vertex) point: P:%g, N:%g (+%g), Q:%g (+%g)",
7978 if ( angle_S0_SL < angle_S0_S1 )
7981 containingFace = closestEdge->
face_left;
7994 LWDEBUGF(1,
"Closest point is internal to closest segment, calling lw_segment_side((%g,%g),(%g,%g),(%g,%g)",
7995 closestSegmentP0->
x,
7996 closestSegmentP0->
y,
7997 closestSegmentP1->
x,
7998 closestSegmentP1->
y,
8003 closestSegmentSide =
lw_segment_side(closestSegmentP0, closestSegmentP1, queryPoint);
8004 LWDEBUGF(1,
"Side of closest segment query point falls on: %d (%s)",
8005 closestSegmentSide, closestSegmentSide == -1 ?
"left" : closestSegmentSide == 1 ?
"right" :
"collinear" );
8007 if ( closestSegmentSide == -1 )
8009 containingFace = closestEdge->
face_left;
8011 else if ( closestSegmentSide == 1 )
8017 lwerror(
"Unexpected collinearity reported from lw_segment_side");
8025 return containingFace;
8053 for (i=0; i<coll->
ngeoms; i++)
8060 lwerror(
"%s: Unsupported geometry type: %s", __func__,
char result[OUT_DOUBLE_BUFFER_SIZE]
int gbox_merge(const GBOX *new_box, GBOX *merge_box)
Update the merged GBOX to be large enough to include itself and the new box.
int gbox_same(const GBOX *g1, const GBOX *g2)
Check if 2 given Gbox are the same.
int gbox_union(const GBOX *g1, const GBOX *g2, GBOX *gout)
Update the output GBOX to be large enough to include both inputs.
void gbox_expand(GBOX *g, double d)
Move the box minimums down and the maximums up by the distance provided.
int gbox_contains_point2d(const GBOX *g, const POINT2D *p)
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
void lwgeom_geos_error(const char *fmt,...)
void lwgeom_refresh_bbox(LWGEOM *lwgeom)
Drop current bbox and calculate a fresh one.
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
char lwgeom_same(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
geom1 same as geom2 iff
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
int ptarray_closest_segment_2d(const POINTARRAY *pa, const POINT2D *qp, double *dist)
LWGEOM * lwgeom_closest_point(const LWGEOM *lw1, const LWGEOM *lw2)
int azimuth_pt_pt(const POINT2D *p1, const POINT2D *p2, double *ret)
Compute the azimuth of segment AB in radians.
LWGEOM * lwgeom_node(const LWGEOM *lwgeom_in)
int ptarray_append_ptarray(POINTARRAY *pa1, POINTARRAY *pa2, double gap_tolerance)
Append a POINTARRAY, pa2 to the end of an existing POINTARRAY, pa1.
void lwpoint_free(LWPOINT *pt)
void lwgeom_free(LWGEOM *geom)
LWGEOM * lwgeom_split(const LWGEOM *lwgeom_in, const LWGEOM *blade_in)
LWGEOM * lwgeom_intersection(const LWGEOM *geom1, const LWGEOM *geom2)
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
int lwgeom_is_simple(const LWGEOM *lwgeom)
LWGEOM * lwgeom_snap(const LWGEOM *geom1, const LWGEOM *geom2, double tolerance)
Snap vertices and segments of a geometry to another using a given tolerance.
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
LWGEOM * lwgeom_difference(const LWGEOM *geom1, const LWGEOM *geom2)
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
LWGEOM * lwgeom_remove_repeated_points(const LWGEOM *in, double tolerance)
LWGEOM * lwgeom_force_3dz(const LWGEOM *geom, double zval)
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
int ptarray_insert_point(POINTARRAY *pa, const POINT4D *p, uint32_t where)
Insert a point into an existing POINTARRAY.
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
void * lwrealloc(void *mem, size_t size)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
double lwgeom_mindistance2d_tolerance(const LWGEOM *lw1, const LWGEOM *lw2, double tolerance)
Function handling min distance calculations and dwithin calculations.
LWGEOM * lwgeom_unaryunion(const LWGEOM *geom1)
LWCOLLECTION * lwcollection_extract(const LWCOLLECTION *col, uint32_t type)
LWGEOM * lwgeom_buildarea(const LWGEOM *geom)
Take a geometry and return an areal geometry (Polygon or MultiPolygon).
void lwcollection_release(LWCOLLECTION *lwcollection)
double lwgeom_mindistance2d(const LWGEOM *lw1, const LWGEOM *lw2)
Function initializing min distance calculation.
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
void lwcollection_free(LWCOLLECTION *col)
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
void ptarray_free(POINTARRAY *pa)
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
const GBOX * lwgeom_get_bbox(const LWGEOM *lwgeom)
Get a non-empty geometry bounding box, computing and caching it if not already there.
void lwline_setPoint4d(LWLINE *line, uint32_t which, POINT4D *newpoint)
LWGEOM * lwgeom_linemerge(const LWGEOM *geom1)
void * lwalloc(size_t size)
LWCOLLECTION * lwcollection_construct(uint8_t type, int32_t srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
int ptarray_is_closed_2d(const POINTARRAY *pa)
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
void lwpoly_free(LWPOLY *poly)
#define LW_TRUE
Return types for functions with status returns.
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
LWPOLY * lwpoly_from_lwlines(const LWLINE *shell, uint32_t nholes, const LWLINE **holes)
LWPOLY * lwpoly_construct_empty(int32_t srid, char hasz, char hasm)
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
void lwline_free(LWLINE *line)
LWGEOM * lwgeom_union(const LWGEOM *geom1, const LWGEOM *geom2)
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
void lwgeom_reverse_in_place(LWGEOM *lwgeom)
Reverse vertex order of LWGEOM.
LWPOINT * lwline_get_lwpoint(const LWLINE *line, uint32_t where)
Returns freshly allocated LWPOINT that corresponds to the index where.
int ptarray_contains_point_partial(const POINTARRAY *pa, const POINT2D *pt, int check_closed, int *winding_number)
#define LW_INSIDE
Constants for point-in-polygon return values.
LWGEOM * lwline_remove_repeated_points(const LWLINE *in, double tolerance)
void ptarray_reverse_in_place(POINTARRAY *pa)
int lwline_is_empty(const LWLINE *line)
#define LW_ON_INTERRUPT(x)
int lwline_is_closed(const LWLINE *line)
POINTARRAY * ptarray_clone(const POINTARRAY *ptarray)
Clone a POINTARRAY object.
int ptarray_scroll_in_place(POINTARRAY *pa, const POINT4D *newbase)
int lwpoint_is_empty(const LWPOINT *point)
int ptarray_contains_point(const POINTARRAY *pa, const POINT2D *pt)
Return LW_INSIDE if the point is inside the POINTARRAY, LW_OUTSIDE if it is outside,...
int lwpoly_is_empty(const LWPOLY *poly)
int ptarray_isccw(const POINTARRAY *pa)
int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
lw_segment_side()
#define P2D_SAME_STRICT(a, b)
#define LWT_COL_EDGE_FACE_RIGHT
#define LWT_COL_FACE_FACE_ID
Face fields.
LWT_INT64 LWT_ELEMID
Identifier of topology element.
struct LWT_BE_TOPOLOGY_T LWT_BE_TOPOLOGY
Topology handler.
#define LWT_COL_EDGE_START_NODE
#define LWT_COL_EDGE_FACE_LEFT
#define LWT_COL_EDGE_NEXT_RIGHT
#define LWT_COL_NODE_CONTAINING_FACE
#define LWT_COL_EDGE_EDGE_ID
Edge fields.
#define LWT_COL_NODE_GEOM
#define LWT_COL_EDGE_END_NODE
#define LWT_COL_EDGE_NEXT_LEFT
#define LWT_COL_NODE_NODE_ID
Node fields.
struct LWT_BE_DATA_T LWT_BE_DATA
Backend private data pointer.
#define LWT_COL_EDGE_GEOM
#define PGTOPO_BE_ERROR()
Datum contains(PG_FUNCTION_ARGS)
Datum covers(PG_FUNCTION_ARGS)
#define LWDEBUG(level, msg)
#define LWDEBUGF(level, msg,...)
void lwnotice(const char *fmt,...) __attribute__((format(printf
Write a notice out to the notice handler.
#define LWDEBUGGF(level, geom, fmt,...)
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
#define LWDEBUGG(level, geom, msg)
static LWT_ELEMID * lwt_be_getRingEdges(LWT_TOPOLOGY *topo, LWT_ELEMID edge, uint64_t *numedges, uint64_t limit)
LWT_ELEMID lwt_AddPoint(LWT_TOPOLOGY *topo, LWPOINT *point, double tol)
Adds a point to the topology.
LWT_ELEMID lwt_be_ExistsCoincidentNode(LWT_TOPOLOGY *topo, const LWPOINT *pt)
LWT_ELEMID lwt_be_ExistsEdgeIntersectingPoint(LWT_TOPOLOGY *topo, const LWPOINT *pt)
#define CBT2(to, method, a1, a2)
static uint64_t lwt_be_updateFacesById(LWT_TOPOLOGY *topo, const LWT_ISO_FACE *faces, uint64_t numfaces)
void lwt_BackendIfaceRegisterCallbacks(LWT_BE_IFACE *iface, const LWT_BE_CALLBACKS *cb)
Register backend callbacks into the opaque iface handler.
const char * lwt_be_lastErrorMessage(const LWT_BE_IFACE *be)
static int _lwt_FindNextRingEdge(const POINTARRAY *ring, int from, const LWT_ISO_EDGE *edges, int numedges)
LWT_BE_IFACE * lwt_CreateBackendIface(const LWT_BE_DATA *data)
Create a new backend interface.
LWT_ISO_EDGE * lwt_be_getEdgeByNode(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t *numelems, int fields)
static int lwt_be_deleteNodesById(const LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t numelems)
int lwt_RemoveIsoNode(LWT_TOPOLOGY *topo, LWT_ELEMID nid)
Remove an isolated node.
#define CB1(be, method, a1)
static int lwt_be_deleteFacesById(const LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t numelems)
static int lwt_be_checkTopoGeomRemEdge(LWT_TOPOLOGY *topo, LWT_ELEMID edge_id, LWT_ELEMID face_left, LWT_ELEMID face_right)
static LWT_ELEMID _lwt_AddEdge(LWT_TOPOLOGY *topo, LWT_ELEMID start_node, LWT_ELEMID end_node, LWLINE *geom, int skipChecks, int modFace)
LWT_ELEMID lwt_GetFaceContainingPoint(LWT_TOPOLOGY *topo, const LWPOINT *pt)
Find the face-id of the face properly containing a given point.
static int _lwt_InitEdgeEndByLine(edgeend *fee, edgeend *lee, LWLINE *edge, POINT2D *fp, POINT2D *lp)
LWT_ISO_EDGE * lwt_be_getEdgeById(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t *numelems, int fields)
static int _lwt_SnapEdgeToExistingNode(LWT_TOPOLOGY *topo, LWT_ISO_NODE *node, LWT_ISO_EDGE *edge, double tol)
static double lwt_be_topoGetPrecision(LWT_TOPOLOGY *topo)
void lwt_LoadGeometry(LWT_TOPOLOGY *topo, LWGEOM *geom, double tol)
Load a geometry into the topology.
static int _lwt_UpdateEdgeFaceRef(LWT_TOPOLOGY *topo, LWT_ELEMID of, LWT_ELEMID nf)
void lwt_FreeBackendIface(LWT_BE_IFACE *iface)
Release memory associated with an LWT_BE_IFACE.
static int _lwt_GetInteriorEdgePoint(const LWLINE *edge, POINT2D *ip)
LWGEOM * lwt_GetFaceGeometry(LWT_TOPOLOGY *topo, LWT_ELEMID faceid)
Return the geometry of a face.
LWT_ELEMID lwt_GetEdgeByPoint(LWT_TOPOLOGY *topo, LWPOINT *pt, double tol)
Find the edge-id of an edge that intersects a given point.
LWT_ELEMID lwt_AddEdgeModFace(LWT_TOPOLOGY *topo, LWT_ELEMID start_node, LWT_ELEMID end_node, LWLINE *geom, int skipChecks)
Add a new edge possibly splitting a face (modifying it)
static int _lwt_SnapEdge_checkMotion(LWT_TOPOLOGY *topo, const LWCOLLECTION *splitC, const LWT_ISO_EDGE *edge, LWT_ISO_EDGE *existingEdge, const LWT_NODE_EDGES *splitNodeEdges)
Check the motion of a snapped edge, invoke lwerror if the movement hits any other edge or node.
static int lwt_be_checkTopoGeomRemIsoEdge(LWT_TOPOLOGY *topo, LWT_ELEMID edge_id)
#define CBT3(to, method, a1, a2, a3)
LWT_ELEMID lwt_AddIsoNode(LWT_TOPOLOGY *topo, LWT_ELEMID face, LWPOINT *pt, int skipISOChecks)
Add an isolated node.
LWT_ELEMID lwt_ModEdgeSplit(LWT_TOPOLOGY *topo, LWT_ELEMID edge, LWPOINT *pt, int skipISOChecks)
Split an edge by a node, modifying the original edge and adding a new one.
static int lwt_be_updateTopoGeomEdgeHeal(LWT_TOPOLOGY *topo, LWT_ELEMID edge1, LWT_ELEMID edge2, LWT_ELEMID newedge)
#define CBT1(to, method, a1)
static void lwt_LoadPoint(LWT_TOPOLOGY *topo, LWPOINT *point, double tol)
static LWGEOM * _lwt_split_by_nodes(const LWGEOM *g, const LWGEOM *nodes)
LWT_ELEMID lwt_be_getNextEdgeId(LWT_TOPOLOGY *topo)
static LWT_ISO_NODE * _lwt_GetIsoNode(LWT_TOPOLOGY *topo, LWT_ELEMID nid)
static LWT_ELEMID _lwt_AddIsoNode(LWT_TOPOLOGY *topo, LWT_ELEMID face, LWPOINT *pt, int skipISOChecks, int checkFace)
static void lwt_LoadLine(LWT_TOPOLOGY *topo, LWLINE *line, double tol)
void _lwt_release_faces(LWT_ISO_FACE *faces, int num_faces)
LWT_ELEMID * lwt_AddLine(LWT_TOPOLOGY *topo, LWLINE *line, double tol, int *nedges)
Adds a linestring to the topology.
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.
static LWPOLY * _lwt_MakeRingShell(LWT_TOPOLOGY *topo, LWT_ELEMID *signed_edge_ids, uint64_t num_signed_edge_ids)
LWT_ELEMID lwt_GetFaceByPoint(LWT_TOPOLOGY *topo, const LWPOINT *pt, double tol)
Find the face-id of a face containing a given point.
static int lwt_be_updateTopoGeomFaceSplit(LWT_TOPOLOGY *topo, LWT_ELEMID split_face, LWT_ELEMID new_face1, LWT_ELEMID new_face2)
static GBOX * lwt_be_computeFaceMBR(const LWT_TOPOLOGY *topo, LWT_ELEMID face)
static int lwt_be_updateNodesById(LWT_TOPOLOGY *topo, const LWT_ISO_NODE *nodes, int numnodes, int upd_fields)
LWT_ELEMID lwt_NewEdgesSplit(LWT_TOPOLOGY *topo, LWT_ELEMID edge, LWPOINT *pt, int skipISOChecks)
Split an edge by a node, replacing it with two new edges.
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)
LWT_ELEMID lwt_RemEdgeNewFace(LWT_TOPOLOGY *topo, LWT_ELEMID edge_id)
Remove an edge, possibly merging two faces (replacing both with a new one)
static double _lwt_minToleranceDouble(double d)
static int lwt_be_checkTopoGeomRemNode(LWT_TOPOLOGY *topo, LWT_ELEMID node_id, LWT_ELEMID eid1, LWT_ELEMID eid2)
static int lwt_be_updateTopoGeomFaceHeal(LWT_TOPOLOGY *topo, LWT_ELEMID face1, LWT_ELEMID face2, LWT_ELEMID newface)
int lwt_be_updateTopoGeomEdgeSplit(LWT_TOPOLOGY *topo, LWT_ELEMID split_edge, LWT_ELEMID new_edge1, LWT_ELEMID new_edge2)
static double _lwt_minTolerance(LWGEOM *g)
static LWT_ISO_EDGE * lwt_be_getEdgeByFace(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t *numelems, int fields, const GBOX *box)
LWT_ISO_EDGE * lwt_be_getEdgeWithinBox2D(const LWT_TOPOLOGY *topo, const GBOX *box, uint64_t *numelems, int fields, uint64_t limit)
static LWT_ISO_EDGE * lwt_be_getClosestEdge(const LWT_TOPOLOGY *topo, const LWPOINT *pt, uint64_t *numelems, int fields)
int lwt_be_insertNodes(LWT_TOPOLOGY *topo, LWT_ISO_NODE *node, uint64_t numelems)
void _lwt_LoadGeometryRecursive(LWT_TOPOLOGY *topo, LWGEOM *geom, double tol)
LWT_ELEMID lwt_AddEdgeNewFaces(LWT_TOPOLOGY *topo, LWT_ELEMID start_node, LWT_ELEMID end_node, LWLINE *geom, int skipChecks)
Add a new edge possibly splitting a face (replacing with two new faces)
LWT_TOPOLOGY * lwt_LoadTopology(LWT_BE_IFACE *iface, const char *name)
Loads an existing topology by name from the database.
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)
static LWT_ELEMID _lwt_GetEqualEdge(LWT_TOPOLOGY *topo, LWLINE *edge, int *forward)
static void _lwt_RotateElemidArray(LWT_ELEMID *ary, int from, int to, int rotidx)
static int compare_scored_pointer(const void *si1, const void *si2)
LWT_ELEMID lwt_RemEdgeModFace(LWT_TOPOLOGY *topo, LWT_ELEMID edge_id)
Remove an edge, possibly merging two faces (replacing one with the other)
LWT_ELEMID * lwt_AddPolygon(LWT_TOPOLOGY *topo, LWPOLY *poly, double tol, int *nfaces)
Adds a polygon to the topology.
int lwt_be_deleteEdges(LWT_TOPOLOGY *topo, const LWT_ISO_EDGE *sel_edge, int sel_fields)
int lwt_GetFaceEdges(LWT_TOPOLOGY *topo, LWT_ELEMID face_id, LWT_ELEMID **out)
Return the list of directed edges bounding a face.
static int _lwt_UpdateNodeFaceRef(LWT_TOPOLOGY *topo, LWT_ELEMID of, LWT_ELEMID nf)
#define CBT4(to, method, a1, a2, a3, a4)
int lwt_MoveIsoNode(LWT_TOPOLOGY *topo, LWT_ELEMID nid, LWPOINT *pt)
Move an isolated node.
LWT_ELEMID * lwt_AddLineNoFace(LWT_TOPOLOGY *topo, LWLINE *line, double tol, int *nedges)
Adds a linestring to the topology without determining generated faces.
LWT_ISO_NODE * lwt_be_getNodeById(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t *numelems, int fields)
void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
static int lwt_be_topoHasZ(LWT_TOPOLOGY *topo)
LWT_ISO_FACE * lwt_be_getFaceWithinBox2D(const LWT_TOPOLOGY *topo, const GBOX *box, uint64_t *numelems, int fields, uint64_t limit)
static LWT_ISO_NODE * lwt_be_getNodeWithinBox2D(const LWT_TOPOLOGY *topo, const GBOX *box, uint64_t *numelems, int fields, uint64_t limit)
LWT_ELEMID lwt_ModEdgeHeal(LWT_TOPOLOGY *topo, LWT_ELEMID e1, LWT_ELEMID e2)
Merge two edges, modifying the first and deleting the second.
LWT_ISO_NODE * lwt_be_getNodeWithinDistance2D(LWT_TOPOLOGY *topo, const LWPOINT *pt, double dist, uint64_t *numelems, int fields, int64_t limit)
static void lwt_LoadPolygon(LWT_TOPOLOGY *topo, const LWPOLY *poly, double tol)
static LWT_ELEMID _lwt_AddFaceSplit(LWT_TOPOLOGY *topo, LWT_ELEMID sedge, LWT_ELEMID face, int mbr_only)
int lwt_be_updateEdgesById(LWT_TOPOLOGY *topo, const LWT_ISO_EDGE *edges, int numedges, int upd_fields)
static int lwt_be_checkTopoGeomRemIsoNode(LWT_TOPOLOGY *topo, LWT_ELEMID node_id)
#define _LWT_MINTOLERANCE(topo, geom)
#define CBT6(to, method, a1, a2, a3, a4, a5, a6)
int lwt_be_freeTopology(LWT_TOPOLOGY *topo)
void lwt_FreeTopology(LWT_TOPOLOGY *topo)
Release memory associated with an LWT_TOPOLOGY.
LWT_ELEMID lwt_NewEdgeHeal(LWT_TOPOLOGY *topo, LWT_ELEMID e1, LWT_ELEMID e2)
Merge two edges, replacing both with a new one.
static LWCOLLECTION * _lwt_EdgeSplit(LWT_TOPOLOGY *topo, LWT_ELEMID edge, LWPOINT *pt, int skipISOChecks, LWT_ISO_EDGE **oldedge)
static LWT_ELEMID _lwt_AddLineEdge(LWT_TOPOLOGY *topo, LWLINE *edge, double tol, int handleFaceSplit, int *forward)
#define CBT5(to, method, a1, a2, a3, a4, a5)
int lwt_be_insertEdges(LWT_TOPOLOGY *topo, LWT_ISO_EDGE *edge, uint64_t numelems)
static LWT_ELEMID _lwt_HealEdges(LWT_TOPOLOGY *topo, LWT_ELEMID eid1, LWT_ELEMID eid2, int modEdge)
static LWT_ELEMID * _lwt_AddLine(LWT_TOPOLOGY *topo, LWLINE *line, double tol, int *nedges, int handleFaceSplit)
static LWT_ELEMID _lwt_SplitAllEdgesToNewNode(LWT_TOPOLOGY *topo, LWT_ISO_EDGE *edges, uint64_t num, LWPOINT *point, double tol, int *moved)
static LWGEOM * _lwt_FaceByEdges(LWT_TOPOLOGY *topo, LWT_ISO_EDGE *edges, int numfaceedges)
LWT_BE_TOPOLOGY * lwt_be_loadTopologyByName(LWT_BE_IFACE *be, const char *name)
static int _lwt_FirstDistinctVertex2D(const POINTARRAY *pa, const POINT2D *ref, int from, int dir, POINT2D *op)
static LWT_ISO_FACE * lwt_be_getFaceById(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t *numelems, int fields)
static LWT_ISO_NODE * lwt_be_getNodeByFace(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t *numelems, int fields, const GBOX *box)
static LWGEOM * _lwt_toposnap(LWGEOM *src, LWGEOM *tgt, double tol)
struct scored_pointer_t scored_pointer
LWT_ELEMID lwt_GetNodeByPoint(LWT_TOPOLOGY *topo, LWPOINT *pt, double tol)
Retrieve the id of a node at a point location.
static int lwt_be_topoGetSRID(LWT_TOPOLOGY *topo)
static void _lwt_ReverseElemidArray(LWT_ELEMID *ary, int from, int to)
static LWT_ELEMID _lwt_AddPoint(LWT_TOPOLOGY *topo, LWPOINT *point, double tol, int findFace, int *moved)
int lwt_RemIsoEdge(LWT_TOPOLOGY *topo, LWT_ELEMID id)
Remove an isolated edge.
int lwt_be_insertFaces(LWT_TOPOLOGY *topo, LWT_ISO_FACE *face, uint64_t numelems)
static int lwt_be_updateNodes(LWT_TOPOLOGY *topo, const LWT_ISO_NODE *sel_node, int sel_fields, const LWT_ISO_NODE *upd_node, int upd_fields, const LWT_ISO_NODE *exc_node, int exc_fields)
int lwt_ChangeEdgeGeom(LWT_TOPOLOGY *topo, LWT_ELEMID edge_id, LWLINE *geom)
Changes the shape of an edge without affecting the topology structure.
static LWT_ELEMID _lwt_RemEdge(LWT_TOPOLOGY *topo, LWT_ELEMID edge_id, int modFace)
LWT_ISO_EDGE * lwt_be_getEdgeWithinDistance2D(LWT_TOPOLOGY *topo, const LWPOINT *pt, double dist, uint64_t *numelems, int fields, int64_t limit)
static int _lwt_FindAdjacentEdges(LWT_TOPOLOGY *topo, LWT_ELEMID node, edgeend *data, edgeend *other, LWT_ELEMID myedge_id)
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
static uint8_t * getPoint_internal(const POINTARRAY *pa, uint32_t n)
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
const LWT_EDGEEND * lwt_edgeEndStar_getNextCW(LWT_EDGEEND_STAR *star, LWT_ISO_EDGE *edge, int outgoing)
LWT_EDGEEND_STAR * lwt_edgeEndStar_init(LWT_ELEMID nodeID)
const LWT_EDGEEND * lwt_edgeEndStar_getNextCCW(LWT_EDGEEND_STAR *star, LWT_ISO_EDGE *edge, int outgoing)
void lwt_EdgeEndStar_debugPrint(const LWT_EDGEEND_STAR *star)
void lwt_edgeEndStar_addEdge(LWT_EDGEEND_STAR *star, const LWT_ISO_EDGE *edge)
LWT_NODE_EDGES * lwt_nodeEdges_loadFromDB(LWT_TOPOLOGY *topo, LWT_ELEMID node_id, int fields)
Structure containing base backend callbacks.
const LWT_BE_CALLBACKS * cb
const LWT_ISO_EDGE * edge
LWT_ELEMID containing_face
LWT_BE_TOPOLOGY * be_topo
const LWT_BE_IFACE * be_iface