27 #include "../postgis_config.h"
41 # define LWTFMT_ELEMID "lld"
43 # define LWTFMT_ELEMID PRId64
77 #define CHECKCB(be, method) do { \
78 if ( ! (be)->cb || ! (be)->cb->method ) \
79 lwerror("Callback " # method " not registered by backend"); \
82 #define CB0(be, method) \
84 return (be)->cb->method((be)->data)
86 #define CB1(be, method, a1) \
88 return (be)->cb->method((be)->data, a1)
90 #define CBT0(to, method) \
91 CHECKCB((to)->be_iface, method);\
92 return (to)->be_iface->cb->method((to)->be_topo)
94 #define CBT1(to, method, a1) \
95 CHECKCB((to)->be_iface, method);\
96 return (to)->be_iface->cb->method((to)->be_topo, a1)
98 #define CBT2(to, method, a1, a2) \
99 CHECKCB((to)->be_iface, method);\
100 return (to)->be_iface->cb->method((to)->be_topo, a1, a2)
102 #define CBT3(to, method, a1, a2, a3) \
103 CHECKCB((to)->be_iface, method);\
104 return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3)
106 #define CBT4(to, method, a1, a2, a3, a4) \
107 CHECKCB((to)->be_iface, method);\
108 return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3, a4)
110 #define CBT5(to, method, a1, a2, a3, a4, a5) \
111 CHECKCB((to)->be_iface, method);\
112 return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3, a4, a5)
114 #define CBT6(to, method, a1, a2, a3, a4, a5, a6) \
115 CHECKCB((to)->be_iface, method);\
116 return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3, a4, a5, a6)
121 CB0(be, lastErrorMessage);
127 CB1(be, loadTopologyByName, name);
133 CBT0(topo, topoGetSRID);
139 CBT0(topo, topoGetPrecision);
145 CBT0(topo, topoHasZ);
151 CBT0(topo, freeTopology);
157 CBT3(topo, getNodeById, ids, numelems, fields);
168 CBT5(topo, getNodeWithinDistance2D, pt, dist, numelems, fields, limit);
174 CBT4(topo, getNodeWithinBox2D, box, numelems, fields, limit);
180 CBT4(topo, getEdgeWithinBox2D, box, numelems, fields, limit);
186 CBT4(topo, getFaceWithinBox2D, box, numelems, fields, limit);
192 CBT2(topo, insertNodes, node, numelems);
198 CBT2(topo, insertFaces, face, numelems);
204 CBT2(topo, deleteFacesById, ids, numelems);
210 CBT2(topo, deleteNodesById, ids, numelems);
216 CBT0(topo, getNextEdgeId);
222 CBT3(topo, getEdgeById, ids, numelems, fields);
228 CBT3(topo, getFaceById, ids, numelems, fields);
234 CBT3(topo, getEdgeByNode, ids, numelems, fields);
240 CBT4(topo, getEdgeByFace, ids, numelems, fields, box);
246 CBT4(topo, getNodeByFace, ids, numelems, fields, box);
257 CBT5(topo, getEdgeWithinDistance2D, pt, dist, numelems, fields, limit);
263 CBT2(topo, insertEdges, edge, numelems);
273 CBT6(topo, updateEdges, sel_edge, sel_fields,
274 upd_edge, upd_fields,
275 exc_edge, exc_fields);
285 CBT6(topo, updateNodes, sel_node, sel_fields,
286 upd_node, upd_fields,
287 exc_node, exc_fields);
295 CBT2(topo, updateFacesById, faces, numfaces);
303 CBT3(topo, updateEdgesById, edges, numedges, upd_fields);
311 CBT3(topo, updateNodesById, nodes, numnodes, upd_fields);
319 CBT2(topo, deleteEdges, sel_edge, sel_fields);
325 CBT1(topo, getFaceContainingPoint, pt);
332 CBT3(topo, updateTopoGeomEdgeSplit, split_edge, new_edge1, new_edge2);
339 CBT3(topo, updateTopoGeomFaceSplit, split_face, new_face1, new_face2);
346 CBT3(topo, checkTopoGeomRemEdge, edge_id, face_left, face_right);
353 CBT3(topo, checkTopoGeomRemNode, node_id, eid1, eid2);
361 CBT3(topo, updateTopoGeomFaceHeal, face1, face2, newface);
369 CBT3(topo, updateTopoGeomEdgeHeal, edge1, edge2, newedge);
375 CBT3(topo, getRingEdges, edge, numedges, limit);
386 if (exists == UINT64_MAX)
399 if (exists == UINT64_MAX)
432 }
while ( changed && iterations <= maxiterations );
434 LWDEBUGF(1,
"It took %d/%d iterations to properly snap",
435 iterations, maxiterations);
444 for ( i=0; i<num_faces; ++i ) {
445 if ( faces[i].mbr )
lwfree(faces[i].mbr);
454 for ( i=0; i<num_edges; ++i ) {
464 for ( i=0; i<num_nodes; ++i ) {
502 lwnotice(
"Could not release backend topology memory: %s",
518 LWPOINT* pt,
int skipISOChecks,
int checkFace )
524 lwerror(
"Cannot add empty point as isolated node");
529 if ( ! skipISOChecks )
533 lwerror(
"SQL/MM Spatial exception - coincident node");
538 lwerror(
"SQL/MM Spatial exception - edge crosses node.");
543 if ( checkFace && ( face == -1 || ! skipISOChecks ) )
546 if ( foundInFace == -2 ) {
550 if ( foundInFace == -1 ) foundInFace = 0;
556 else if ( ! skipISOChecks && foundInFace != face ) {
558 lwerror(
"SQL/MM Spatial exception - within face %d (not %d)",
561 lwerror(
"SQL/MM Spatial exception - not within face");
581 LWPOINT* pt,
int skipISOChecks )
601 uint64_t i, num_nodes, num_edges;
605 GEOSGeometry *edgegg;
620 LWDEBUGF(1,
"lwt_be_getNodeWithinBox2D returned %d nodes", num_nodes);
621 if (num_nodes == UINT64_MAX)
626 for ( i=0; i<num_nodes; ++i )
630 if ( node->
node_id == start_node )
continue;
631 if ( node->
node_id == end_node )
continue;
638 GEOSGeom_destroy(edgegg);
640 lwerror(
"SQL/MM Spatial exception - geometry crosses a node");
649 LWDEBUGF(1,
"lwt_be_getEdgeWithinBox2D returned %d edges", num_edges);
650 if (num_edges == UINT64_MAX)
652 GEOSGeom_destroy(edgegg);
656 for ( i=0; i<num_edges; ++i )
664 if ( edge_id == myself )
continue;
666 if ( ! edge->
geom ) {
668 lwerror(
"Edge %d has NULL geometry!", edge_id);
674 GEOSGeom_destroy(edgegg);
680 LWDEBUGF(2,
"Edge %d converted to GEOS", edge_id);
684 relate = GEOSRelateBoundaryNodeRule(eegg, edgegg, 2);
686 GEOSGeom_destroy(eegg);
687 GEOSGeom_destroy(edgegg);
693 LWDEBUGF(2,
"Edge %d relate pattern is %s", edge_id, relate);
695 match = GEOSRelatePatternMatch(relate,
"FF*F*****");
698 GEOSGeom_destroy(eegg);
702 GEOSGeom_destroy(edgegg);
709 match = GEOSRelatePatternMatch(relate,
"1FFF*FFF2");
712 GEOSGeom_destroy(edgegg);
713 GEOSGeom_destroy(eegg);
724 match = GEOSRelatePatternMatch(relate,
"1********");
727 GEOSGeom_destroy(edgegg);
728 GEOSGeom_destroy(eegg);
733 lwerror(
"Spatial exception - geometry intersects edge %"
739 match = GEOSRelatePatternMatch(relate,
"T********");
742 GEOSGeom_destroy(edgegg);
743 GEOSGeom_destroy(eegg);
748 lwerror(
"SQL/MM Spatial exception - geometry crosses edge %"
754 match = GEOSRelatePatternMatch(relate,
"*T*******");
757 GEOSGeom_destroy(edgegg);
758 GEOSGeom_destroy(eegg);
763 lwerror(
"Spatial exception - geometry boundary touches interior of edge %"
769 match = GEOSRelatePatternMatch(relate,
"***T*****");
772 GEOSGeom_destroy(edgegg);
773 GEOSGeom_destroy(eegg);
778 lwerror(
"Spatial exception - boundary of edge % touches interior of geometry"
784 LWDEBUGF(2,
"Edge %d analisys completed, it does no harm", edge_id);
787 GEOSGeom_destroy(eegg);
789 LWDEBUGF(1,
"No edge crossing detected among the %d candidate edges", num_edges);
793 GEOSGeom_destroy(edgegg);
810 int skipISOChecks = 0;
816 if (startNode == endNode)
818 lwerror(
"Closed edges would not be isolated, try lwt_AddEdgeNewFaces");
822 if ( ! skipISOChecks )
827 lwerror(
"SQL/MM Spatial exception - curve not simple");
841 node_ids[0] = startNode;
842 node_ids[1] = endNode;
845 if (num_nodes == UINT64_MAX)
850 else if ( num_nodes < 2 )
853 lwerror(
"SQL/MM Spatial exception - non-existent node");
856 for ( i=0; i<num_nodes; ++i )
862 lwerror(
"SQL/MM Spatial exception - not isolated node");
869 lwerror(
"SQL/MM Spatial exception - nodes in different faces");
873 if ( ! skipISOChecks )
883 lwerror(
"SQL/MM Spatial exception - "
884 "start node not geometry start point.");
896 lwerror(
"SQL/MM Spatial exception - "
897 "end node not geometry end point.");
906 if ( ! skipISOChecks )
926 if ( containing_face == -1 ) containing_face = 0;
939 }
else if ( ret == 0 ) {
940 lwerror(
"Insertion of split edge failed (no reason)");
950 updated_nodes[0].
node_id = startNode;
952 updated_nodes[1].
node_id = endNode;
973 LWDEBUG(1,
"calling lwt_be_getEdgeById");
975 LWDEBUGF(1,
"lwt_be_getEdgeById returned %p", *oldedge);
978 LWDEBUGF(1,
"lwt_be_getEdgeById returned NULL and set i=%d", i);
986 lwerror(
"SQL/MM Spatial exception - non-existent edge");
991 lwerror(
"Backend coding error: getEdgeById callback returned NULL "
992 "but numelements output parameter has value %d "
993 "(expected 0 or 1)", i);
1002 if ( ! skipISOChecks )
1004 LWDEBUG(1,
"calling lwt_be_ExistsCoincidentNode");
1007 LWDEBUG(1,
"lwt_be_ExistsCoincidentNode returned");
1009 lwerror(
"SQL/MM Spatial exception - coincident node");
1012 LWDEBUG(1,
"lwt_be_ExistsCoincidentNode returned");
1020 lwerror(
"could not split edge by point ?");
1024 if ( ! split_col ) {
1027 lwerror(
"lwgeom_as_lwcollection returned NULL");
1030 if (split_col->
ngeoms < 2) {
1033 lwerror(
"SQL/MM Spatial exception - point not on edge");
1041 LWDEBUGF(1,
"returning split col: %s", wkt);
1050 LWPOINT* pt,
int skipISOChecks )
1055 const LWGEOM *oldedge_geom;
1056 const LWGEOM *newedge_geom;
1061 split_col =
_lwt_EdgeSplit( topo, edge, pt, skipISOChecks, &oldedge );
1062 if ( ! split_col )
return -1;
1063 oldedge_geom = split_col->
geoms[0];
1064 newedge_geom = split_col->
geoms[1];
1067 ((
LWGEOM*)newedge_geom)->srid = split_col->
srid;
1084 lwerror(
"Backend coding error: "
1085 "insertNodes callback did not return node_id");
1091 if ( newedge1.
edge_id == -1 ) {
1106 if ( ! newedge1.
geom ) {
1109 lwerror(
"first geometry in lwgeom_split output is not a line");
1118 }
else if ( ret == 0 ) {
1121 lwerror(
"Insertion of split edge failed (no reason)");
1128 if ( ! updedge.
geom ) {
1131 lwerror(
"second geometry in lwgeom_split output is not a line");
1145 }
else if ( ret == 0 ) {
1148 lwerror(
"Edge being split (%d) disappeared during operations?", oldedge->
edge_id);
1150 }
else if ( ret > 1 ) {
1153 lwerror(
"More than a single edge found with id %d !", oldedge->
edge_id);
1207 LWPOINT* pt,
int skipISOChecks )
1212 const LWGEOM *oldedge_geom;
1213 const LWGEOM *newedge_geom;
1218 split_col =
_lwt_EdgeSplit( topo, edge, pt, skipISOChecks, &oldedge );
1219 if ( ! split_col )
return -1;
1220 oldedge_geom = split_col->
geoms[0];
1221 newedge_geom = split_col->
geoms[1];
1224 ((
LWGEOM*)newedge_geom)->srid = split_col->
srid;
1241 lwerror(
"Backend coding error: "
1242 "insertNodes callback did not return node_id");
1258 if ( newedges[0].edge_id == -1 ) {
1265 if ( newedges[1].edge_id == -1 ) {
1286 if ( ! newedges[0].geom ) {
1289 lwerror(
"first geometry in lwgeom_split output is not a line");
1307 if ( ! newedges[1].geom ) {
1310 lwerror(
"second geometry in lwgeom_split output is not a line");
1320 }
else if ( ret == 0 ) {
1323 lwerror(
"Insertion of split edge failed (no reason)");
1441 LWDEBUGF(1,
"first point is index %d", from);
1443 for ( i = from+inc; i != toofar; i += inc )
1445 LWDEBUGF(1,
"testing point %d", i);
1476 LWDEBUG(1,
"computing azimuth of first edge end");
1479 lwerror(
"Invalid edge (no two distinct vertices exist)");
1483 lwerror(
"error computing azimuth of first edgeend [%.15g %.15g,%.15g %.15g]",
1484 fp->
x, fp->
y, pt.
x, pt.
y);
1487 LWDEBUGF(1,
"azimuth of first edge end [%.15g %.15g,%.15g %.15g] is %g",
1488 fp->
x, fp->
y, pt.
x, pt.
y, fee->
myaz);
1491 LWDEBUG(1,
"computing azimuth of second edge end");
1494 lwerror(
"Invalid edge (no two distinct vertices exist)");
1498 lwerror(
"error computing azimuth of last edgeend [%.15g %.15g,%.15g %.15g]",
1499 lp->
x, lp->
y, pt.
x, pt.
y);
1502 LWDEBUGF(1,
"azimuth of last edge end [%.15g %.15g,%.15g %.15g] is %g",
1503 lp->
x, lp->
y, pt.
x, pt.
y, lee->
myaz);
1523 edgeend *other,
int myedge_id )
1526 uint64_t numedges = 1;
1528 double minaz, maxaz;
1536 if ( azdif < 0 ) azdif += 2 * M_PI;
1537 minaz = maxaz = azdif;
1539 LWDEBUGF(1,
"Other edge end has cwFace=%d and ccwFace=%d",
1546 " and adjacent to azimuth %g", node,
data->myaz);
1550 if (numedges == UINT64_MAX)
1556 LWDEBUGF(1,
"getEdgeByNode returned %d edges, minaz=%g, maxaz=%g",
1557 numedges, minaz, maxaz);
1560 for ( i = 0; i < numedges; ++i )
1570 if ( edge->
edge_id == myedge_id )
continue;
1583 " does not have two distinct points",
id);
1591 lwerror(
"Edge %d has no distinct vertices: [%.15g %.15g,%.15g %.15g]: ",
1597 ", edgeend is %g,%g-%g,%g",
1603 lwerror(
"error computing azimuth of edge %d first edgeend [%.15g %.15g,%.15g %.15g]",
1604 id, p1.
x, p1.
y, p2.
x, p2.
y);
1607 azdif = az -
data->myaz;
1609 ": %g (diff: %g)", edge->
edge_id, az, azdif);
1611 if ( azdif < 0 ) azdif += 2 * M_PI;
1612 if ( minaz == -1 ) {
1613 minaz = maxaz = azdif;
1620 " (face_right is new ccwFace, face_left is new cwFace)",
1624 if ( azdif < minaz ) {
1630 " (previous had minaz=%g, face_left is new cwFace)",
1635 else if ( azdif > maxaz ) {
1641 " (previous had maxaz=%g, face_right is new ccwFace)",
1653 lwerror(
"Edge %d has no distinct vertices: [%.15g %.15g,%.15g %.15g]: ",
1658 ", edgeend is %g,%g-%g,%g",
1664 lwerror(
"error computing azimuth of edge %d last edgeend [%.15g %.15g,%.15g %.15g]",
1665 id, p1.
x, p1.
y, p2.
x, p2.
y);
1668 azdif = az -
data->myaz;
1670 ": %g (diff: %g)", edge->
edge_id, az, azdif);
1671 if ( azdif < 0 ) azdif += 2 * M_PI;
1672 if ( minaz == -1 ) {
1673 minaz = maxaz = azdif;
1680 " (face_right is new cwFace, face_left is new ccwFace)",
1684 if ( azdif < minaz ) {
1690 " (previous had minaz=%g, face_right is new cwFace)",
1695 else if ( azdif > maxaz ) {
1699 ", outgoing, from start point, "
1701 " (previous had maxaz=%g, face_left is new ccwFace)",
1713 LWDEBUGF(1,
"edges adjacent to azimuth %g"
1716 data->myaz, node,
data->nextCW, minaz,
1717 data->nextCCW, maxaz);
1719 if ( myedge_id < 1 && numedges && data->cwFace !=
data->ccwFace )
1721 if (
data->cwFace != -1 &&
data->ccwFace != -1 ) {
1747 if ( pa->
npoints < 2 )
return 0;
1751 for (i=1; i<pa->
npoints-1; ++i)
1754 if (
p2d_same(&tp, &fp) )
continue;
1755 if (
p2d_same(&tp, &lp) )
continue;
1765 if (
p2d_same(&fp, &lp) )
return 0;
1767 ip->
x = fp.
x + ( (lp.
x - fp.
x) * 0.5 );
1768 ip->
y = fp.
y + ( (lp.
y - fp.
y) * 0.5 );
1777 uint64_t numedges, i, j;
1783 for (i=0; i<num_signed_edge_ids; ++i) {
1784 int absid = llabs(signed_edge_ids[i]);
1787 for (j=0; j<numedges; ++j) {
1788 if ( edge_ids[j] == absid ) {
1793 if ( ! found ) edge_ids[numedges++] = absid;
1799 if (i == UINT64_MAX)
1804 else if ( i != numedges )
1806 lwfree( signed_edge_ids );
1808 lwerror(
"Unexpected error: %d edges found when expecting %d", i, numedges);
1816 for ( i=0; i<num_signed_edge_ids; ++i )
1822 for ( j=0; j<numedges; ++j )
1824 if ( ring_edges[j].edge_id == llabs(eid) )
1826 edge = &(ring_edges[j]);
1833 lwerror(
"missing edge that was found in ring edges loop");
1891 uint64_t numfaceedges, i, j;
1892 int newface_outside;
1893 uint64_t num_signed_edge_ids;
1897 int forward_edges_count = 0;
1899 int backward_edges_count = 0;
1902 if (!signed_edge_ids)
1909 LWDEBUGF(1,
"getRingEdges returned %d edges", num_signed_edge_ids);
1912 for (i=0; i<num_signed_edge_ids; ++i) {
1913 if ( signed_edge_ids[i] == -sedge ) {
1916 lwfree( signed_edge_ids );
1922 sedge, face, mbr_only);
1931 num_signed_edge_ids);
1933 lwfree( signed_edge_ids );
1942 lwfree( signed_edge_ids );
1944 " is geometrically not-closed", sedge);
1950 sedge, isccw ?
"counter" :
"");
1959 lwfree( signed_edge_ids );
1963 LWDEBUG(1,
"The left face of this clockwise ring is the universe, "
1964 "won't create a new face there");
1969 if ( mbr_only && face != 0 )
1975 updface.
mbr = (
GBOX *)shellbox;
1979 lwfree( signed_edge_ids );
1986 lwfree( signed_edge_ids );
1988 lwerror(
"Unexpected error: %d faces found when expecting 1", ret);
1992 lwfree( signed_edge_ids );
2000 if ( face != 0 && ! isccw)
2003 uint64_t nfaces = 1;
2005 if (nfaces == UINT64_MAX)
2007 lwfree( signed_edge_ids );
2014 lwfree( signed_edge_ids );
2016 lwerror(
"Unexpected error: %d faces found when expecting 1", nfaces);
2019 newface.
mbr = oldface->
mbr;
2023 newface.
mbr = (
GBOX *)shellbox;
2030 lwfree( signed_edge_ids );
2037 lwfree( signed_edge_ids );
2039 lwerror(
"Unexpected error: %d faces inserted when expecting 1", ret);
2050 if ( face != 0 && ! isccw ) {
2052 LWDEBUG(1,
"New face is on the outside of the ring, updating rings in former shell");
2053 newface_outside = 1;
2056 LWDEBUG(1,
"New face is on the inside of the ring, updating forward edges in new ring");
2057 newface_outside = 0;
2070 if (numfaceedges == UINT64_MAX)
2076 LWDEBUGF(1,
"_lwt_AddFaceSplit: lwt_be_getEdgeByFace(%d) returned %d edges", face, numfaceedges);
2081 forward_edges_count = 0;
2083 backward_edges_count = 0;
2086 for ( i=0; i<numfaceedges; ++i )
2094 for ( j=0; j<num_signed_edge_ids; ++j )
2096 int seid = signed_edge_ids[j];
2100 LWDEBUGF(1,
"Edge %d is a known forward edge of the new ring", e->
edge_id);
2104 if ( found == 2 )
break;
2106 else if ( -seid == e->
edge_id )
2109 LWDEBUGF(1,
"Edge %d is a known backward edge of the new ring", e->
edge_id);
2113 if ( found == 2 )
break;
2116 if ( found )
continue;
2117 LWDEBUGF(1,
"Edge %d is not a known edge of the new ring", e->
edge_id);
2136 LWDEBUGF(1,
"Edge %d first point %s new ring",
2141 if ( newface_outside )
2145 LWDEBUGF(1,
"Edge %d not outside of the new ring, not updating it",
2154 LWDEBUGF(1,
"Edge %d not inside the new ring, not updating it",
2178 if ( forward_edges_count )
2181 forward_edges_count,
2185 lwfree( signed_edge_ids );
2189 if ( ret != forward_edges_count )
2191 lwfree( signed_edge_ids );
2192 lwerror(
"Unexpected error: %d edges updated when expecting %d",
2193 ret, forward_edges_count);
2199 if ( backward_edges_count )
2202 backward_edges_count,
2206 lwfree( signed_edge_ids );
2210 if ( ret != backward_edges_count )
2212 lwfree( signed_edge_ids );
2213 lwerror(
"Unexpected error: %d edges updated when expecting %d",
2214 ret, backward_edges_count);
2227 uint64_t numisonodes = 1;
2230 &numisonodes, fields, newface.
mbr);
2231 if (numisonodes == UINT64_MAX)
2237 if ( numisonodes ) {
2239 int nodes_to_update = 0;
2240 for (i=0; i<numisonodes; ++i)
2245 LWDEBUGF(1,
"Node %d is %scontained in new ring, newface is %s",
2247 newface_outside ?
"outside" :
"inside" );
2248 if ( newface_outside )
2252 LWDEBUGF(1,
"Node %d contained in an hole of the new face",
2261 LWDEBUGF(1,
"Node %d not contained in the face shell",
2272 if ( nodes_to_update )
2278 lwfree( signed_edge_ids );
2302 LWLINE *geom,
int skipChecks,
int modFace )
2311 const LWPOINT *start_node_geom = NULL;
2312 const LWPOINT *end_node_geom = NULL;
2326 lwerror(
"SQL/MM Spatial exception - curve not simple");
2333 newedge.
geom = geom;
2342 lwerror(
"Invalid edge (no two distinct vertices exist)");
2355 lwerror(
"Invalid edge (no two distinct vertices exist)");
2360 lwerror(
"error computing azimuth of first edgeend [%.15g %.15g,%.15g %.15g]",
2361 p1.
x, p1.
y, pn.
x, pn.
y);
2364 LWDEBUGF(1,
"edge's start node is %g,%g", p1.
x, p1.
y);
2372 lwerror(
"Invalid clean edge (no two distinct vertices exist) - should not happen");
2377 lwerror(
"error computing azimuth of last edgeend [%.15g %.15g,%.15g %.15g]",
2378 p2.
x, p2.
y, pn.
x, pn.
y);
2381 LWDEBUGF(1,
"edge's end node is %g,%g", p2.
x, p2.
y);
2388 if ( start_node != end_node ) {
2390 node_ids[0] = start_node;
2391 node_ids[1] = end_node;
2394 node_ids[0] = start_node;
2398 if (num_nodes == UINT64_MAX)
2403 for ( i=0; i<num_nodes; ++i )
2415 lwerror(
"SQL/MM Spatial exception - geometry crosses an edge"
2421 LWDEBUGF(1,
"Node %d, with geom %p (looking for %d and %d)",
2423 if ( node->
node_id == start_node ) {
2424 start_node_geom = node->
geom;
2426 if ( node->
node_id == end_node ) {
2427 end_node_geom = node->
geom;
2433 if ( ! start_node_geom )
2436 lwerror(
"SQL/MM Spatial exception - non-existent node");
2441 pa = start_node_geom->
point;
2446 lwerror(
"SQL/MM Spatial exception"
2447 " - start node not geometry start point."
2454 if ( ! end_node_geom )
2457 lwerror(
"SQL/MM Spatial exception - non-existent node");
2462 pa = end_node_geom->
point;
2467 lwerror(
"SQL/MM Spatial exception"
2468 " - end node not geometry end point."
2487 if ( newedge.
edge_id == -1 ) {
2493 int isclosed = start_node == end_node;
2496 isclosed ? &epan : NULL, -1 );
2501 LWDEBUGF(1,
"New edge %d is connected on start node, "
2502 "next_right is %d, prev_left is %d",
2514 LWDEBUGF(1,
"New edge %d is isolated on start node, "
2515 "next_right is %d, prev_left is %d",
2520 isclosed ? &span : NULL, -1 );
2525 LWDEBUGF(1,
"New edge %d is connected on end node, "
2526 "next_left is %d, prev_right is %d",
2532 lwerror(
"Side-location conflict: "
2533 "new edge starts in face"
2544 lwerror(
"Side-location conflict: "
2545 "new edge starts in face"
2556 LWDEBUGF(1,
"New edge %d is isolated on end node, "
2557 "next_left is %d, prev_right is %d",
2569 "faces mismatch: invalid topology ?",
2573 else if ( newedge.
face_left == -1 && modFace > -1 )
2575 lwerror(
"Could not derive edge face from linked primitives:"
2576 " invalid topology ?");
2588 }
else if ( ret == 0 ) {
2589 lwerror(
"Insertion of split edge failed (no reason)");
2597 if ( llabs(prev_left) != newedge.
edge_id )
2599 if ( prev_left > 0 )
2616 &updedge, updfields,
2626 if ( llabs(prev_right) != newedge.
edge_id )
2628 if ( prev_right > 0 )
2640 seledge.
edge_id = -prev_right;
2645 &updedge, updfields,
2687 if ( modFace > -1 ) {
2691 LWDEBUG(1,
"New edge is dangling, so it cannot split any face");
2703 if ( newface1 == 0 ) {
2704 LWDEBUG(1,
"New edge does not split any face");
2713 if ( newface == 0 ) {
2714 LWDEBUG(1,
"New edge does not split any face");
2724 if ( newface < 0 )
return newedge.
edge_id;
2763 LWLINE *geom,
int skipChecks )
2765 return _lwt_AddEdge( topo, start_node, end_node, geom, skipChecks, 1 );
2771 LWLINE *geom,
int skipChecks )
2773 return _lwt_AddEdge( topo, start_node, end_node, geom, skipChecks, 0 );
2782 int i, validedges = 0;
2784 for ( i=0; i<numfaceedges; ++i )
2798 if ( numfaceedges )
lwfree(geoms);
2799 LWDEBUG(1,
"_lwt_FaceByEdges returning empty polygon");
2816 LWDEBUGF(1,
"_lwt_FaceByEdges returning area: %s", wkt);
2826 uint64_t numfaceedges;
2836 lwerror(
"SQL/MM Spatial exception - universal face has no geometry");
2848 if (numfaceedges == UINT64_MAX)
2854 if ( numfaceedges == 0 )
2858 if (i == UINT64_MAX)
2864 lwerror(
"SQL/MM Spatial exception - non-existent face.");
2869 lwerror(
"Corrupted topology: multiple face records have face_id=%"
2875 lwnotice(
"Corrupted topology: face %"
2892 lwnotice(
"Corrupted topology: face %"
2927 LWDEBUGF(1,
"Ring's 'from' point (%d) is %g,%g", from, p1.
x, p1.
y);
2931 for ( i=0; i<numedges; ++i )
2945 ") on both sides, skipping",
2953 " has only %"PRIu32
" points",
2970 LWDEBUGF(1,
"Rings's 'from' point is still %g,%g", p1.
x, p1.
y);
2973 LWDEBUG(1,
"p2d_same(p1,p2) returned true");
2975 " matches ring vertex %d", isoe->
edge_id, from);
2977 for ( j=1; j<epa->
npoints; ++j )
2983 if (
p2d_same(&p1, &p2) )
continue;
2986 LWDEBUGF(1,
"Ring's point %d is %g,%g",
2987 from+1, pt.
x, pt.
y);
2991 #if POSTGIS_DEBUG_LEVEL > 0
2994 " matches ring vertex %d", isoe->
edge_id, from+1);
2997 " does not match ring vertex %d", isoe->
edge_id, from+1);
3012 " matches ring vertex %d", isoe->
edge_id, from);
3014 for ( j=2; j<=epa->
npoints; j++ )
3020 if (
p2d_same(&p1, &p2) )
continue;
3023 LWDEBUGF(1,
"Ring's point %d is %g,%g",
3024 from+1, pt.
x, pt.
y);
3029 #if POSTGIS_DEBUG_LEVEL > 0
3032 " matches ring vertex %d", isoe->
edge_id, from+1);
3035 " does not match ring vertex %d", isoe->
edge_id, from+1);
3040 if ( match )
return i;
3056 ary[from++] = ary[to];
3079 uint64_t numfaceedges;
3094 if (numfaceedges == UINT64_MAX)
3099 if ( ! numfaceedges )
return 0;
3108 lwerror(
"Corrupted topology: unable to build geometry of face %"
3109 LWTFMT_ELEMID " from its %"PRIu64
" edges", face_id, numfaceedges);
3145 nseid = prevseid = 0;
3149 for ( i=0; i<facepoly->
nrings; ++i )
3158 while ( j < (int32_t) ring->
npoints-1 )
3160 LWDEBUGF(1,
"Looking for edge covering ring %d from vertex %d",
3170 lwerror(
"No edge (among %d) found to be defining geometry of face %"
3175 nextedge = &(edges[edgeno]);
3176 nextline = nextedge->
geom;
3179 " covers ring %d from vertex %d to %d",
3195 seid[nseid++] = nextedge->
face_left == face_id ?
3206 if ( (nseid - prevseid) > 1 )
3210 LWDEBUGF(1,
"Looking for smallest id among the %d edges "
3211 "composing ring %d", (nseid-prevseid), i);
3212 for ( j=prevseid; j<nseid; ++j )
3216 if ( ! minid ||
id < minid )
3223 " at position %d", minid, minidx);
3224 if ( minidx != prevseid )
3252 lwerror(
"SQL/MM Spatial exception - curve not simple");
3261 "lwt_be_getEdgeById returned NULL and set i=%d", i);
3262 if (i == UINT64_MAX)
3269 lwerror(
"SQL/MM Spatial exception - non-existent edge %"
3275 lwerror(
"Backend coding error: getEdgeById callback returned NULL "
3276 "but numelements output parameter has value %d "
3277 "(expected 0 or 1)", i);
3283 "old edge has %d points, new edge has %d points",
3294 lwerror(
"SQL/MM Spatial exception - "
3295 "start node not geometry start point.");
3306 " has less than 2 vertices", oldedge->
edge_id);
3313 lwerror(
"Invalid edge: less than 2 vertices");
3320 lwerror(
"SQL/MM Spatial exception - "
3321 "end node not geometry end point.");
3337 lwerror(
"Invalid edge (no two distinct vertices exist)");
3346 lwerror(
"Edge twist at node POINT(%g %g)", p1.
x, p1.
y);
3352 oldedge->
end_node, geom, edge_id ) )
3359 LWDEBUG(1,
"lwt_ChangeEdgeGeom: "
3360 "edge crossing check passed ");
3378 LWDEBUGF(1,
"lwt_be_getNodeWithinBox2D returned %d nodes", numnodes);
3379 if (numnodes == UINT64_MAX)
3386 if ( numnodes > ( 1 + isclosed ? 0 : 1 ) )
3389 for (i=0; i<numnodes; ++i)
3402 lwerror(
"Edge motion collision at %s", wkt);
3410 LWDEBUG(1,
"nodes containment check passed");
3423 isclosed ? &epan_pre : NULL, edge_id );
3425 isclosed ? &span_pre : NULL, edge_id );
3435 newedge.
geom = geom;
3446 lwerror(
"Unexpected error: %d edges updated when expecting 1", i);
3459 isclosed ? &epan_post : NULL, edge_id );
3461 isclosed ? &span_post : NULL, edge_id );
3476 lwerror(
"Edge changed disposition around start node %"
3487 lwerror(
"Edge changed disposition around end node %"
3504 uint64_t facestoupdate = 0;
3513 lwerror(
"lwt_ChangeEdgeGeom could not construct face %"
3522 LWDEBUGF(1,
"new geometry of face left (%d): %s", (
int)oldedge->
face_left, wkt);
3527 if ( ! nface1->
bbox )
3529 lwerror(
"Corrupted topology: face %d, left of edge %d, has no bbox",
3535 faces[facestoupdate++].
mbr = nface1->
bbox;
3544 lwerror(
"lwt_ChangeEdgeGeom could not construct face %"
3558 if ( ! nface2->
bbox )
3560 lwerror(
"Corrupted topology: face %d, right of edge %d, has no bbox",
3565 faces[facestoupdate++].
mbr = nface2->
bbox;
3567 LWDEBUGF(1,
"%d faces to update", facestoupdate);
3568 if ( facestoupdate )
3571 if (updatedFaces != facestoupdate)
3578 if (updatedFaces == UINT64_MAX)
3581 lwerror(
"Unexpected error: %d faces found when expecting 1", i);
3590 LWDEBUG(1,
"BBOX of changed edge did not change");
3593 LWDEBUG(1,
"all done, cleaning up edges");
3607 if (n == UINT64_MAX)
3613 lwerror(
"SQL/MM Spatial exception - non-existent node");
3619 lwerror(
"SQL/MM Spatial exception - not isolated node");
3633 if ( ! node )
return -1;
3638 lwerror(
"SQL/MM Spatial exception - coincident node");
3645 lwerror(
"SQL/MM Spatial exception - edge crosses node.");
3674 if ( ! node )
return -1;
3686 lwerror(
"Unexpected error: %d nodes deleted when expecting 1", n);
3720 lwerror(
"SQL/MM Spatial exception - non-existent edge");
3726 lwerror(
"Corrupted topology: more than a single edge have id %"
3731 if ( edge[0].face_left != edge[0].face_right )
3734 lwerror(
"SQL/MM Spatial exception - not isolated edge");
3745 if ((n == UINT64_MAX) || (edge == NULL))
3750 for (i = 0; i < n; ++i)
3752 if (edge[i].edge_id !=
id)
3755 lwerror(
"SQL/MM Spatial exception - not isolated edge");
3763 if (n == UINT64_MAX)
3770 lwerror(
"Unexpected error: %d edges deleted when expecting 1", n);
3777 if ( nid[1] != nid[0] ) {
3784 if (n == UINT64_MAX)
3817 if ( ret == -1 )
return -1;
3825 if ( ret == -1 )
return -1;
3850 if ( ret == -1 )
return -1;
3864 uint64_t i, nedges, nfaces, fields;
3870 int nedge_right = 0;
3877 int fnode_edges = 0;
3879 int lnode_edges = 0;
3888 LWDEBUGF(1,
"lwt_be_getEdgeById returned NULL and set i=%d", i);
3889 if (i == UINT64_MAX)
3902 "Backend coding error: getEdgeById callback returned NULL "
3903 "but numelements output parameter has value %d "
3904 "(expected 0 or 1)",
3917 LWDEBUG(1,
"Updating next_{right,left}_face of ring edges...");
3925 node_ids[nedges++] = edge->
end_node;
3931 if (nedges == UINT64_MAX)
3936 nedge_left = nedge_right = 0;
3937 for ( i=0; i<nedges; ++i )
3940 if ( e->
edge_id == edge_id )
continue;
3978 LWDEBUGF(1,
"updating %d 'next_left' edges", nedge_left);
3991 LWDEBUGF(1,
"updating %d 'next_right' edges", nedge_right);
4002 LWDEBUGF(1,
"releasing %d updateable edges in %p", nedges, upd_edge);
4020 LWDEBUG(1,
"floodface is universe");
4034 if (nfaces == UINT64_MAX)
4041 for ( i=0; i<nfaces; ++i )
4043 if ( faces[i].face_id == edge->
face_left )
4045 if ( ! box1 ) box1 = faces[i].
mbr;
4051 lwerror(
"corrupted topology: more than 1 face have face_id=%"
4056 else if ( faces[i].face_id == edge->
face_right )
4058 if ( ! box2 ) box2 = faces[i].
mbr;
4064 lwerror(
"corrupted topology: more than 1 face have face_id=%"
4074 lwerror(
"Backend coding error: getFaceById returned face "
4083 lwerror(
"corrupted topology: no face have face_id=%"
4092 lwerror(
"corrupted topology: no face have face_id=%"
4113 lwerror(
"Unexpected error: %d faces updated when expecting 1", i);
4132 lwerror(
"Unexpected error: %d faces inserted when expecting 1",
result);
4197 if ( ! fnode_edges )
4239 return modFace ? floodface : newface.
face_id;
4262 uint64_t num_node_edges;
4269 int e2sign, e2freenode;
4273 size_t bufleft = 256;
4281 " with itself, try with another", eid1);
4288 if ((nedges == UINT64_MAX) || (edges == NULL))
4293 for ( i=0; i<nedges; ++i )
4295 if ( edges[i].edge_id == eid1 ) {
4298 lwerror(
"Corrupted topology: multiple edges have id %"
4304 else if ( edges[i].edge_id == eid2 ) {
4307 lwerror(
"Corrupted topology: multiple edges have id %"
4318 "SQL/MM Spatial exception - non-existent edge %" LWTFMT_ELEMID,
4326 "SQL/MM Spatial exception - non-existent edge %" LWTFMT_ELEMID,
4360 if ( commonnode != -1 )
4365 if (num_node_edges == UINT64_MAX)
4371 for (i=0; i<num_node_edges; ++i)
4374 if ( node_edges[i].edge_id == eid1 )
continue;
4375 if ( node_edges[i].edge_id == eid2 )
continue;
4378 if ( bufleft > 0 ) {
4380 ( ptr==buf ?
"" :
"," ), node_edges[i].edge_id);
4381 if (
r >= (
int) bufleft )
4399 if ( commonnode == -1 )
4412 if ( commonnode != -1 )
4417 if (num_node_edges == UINT64_MAX)
4423 for (i=0; i<num_node_edges; ++i)
4426 if ( node_edges[i].edge_id == eid1 )
continue;
4427 if ( node_edges[i].edge_id == eid2 )
continue;
4430 if ( bufleft > 0 ) {
4432 ( ptr==buf ?
"" :
"," ), node_edges[i].edge_id);
4433 if (
r >= (
int) bufleft )
4448 if ( num_node_edges )
lwfree(node_edges);
4452 if ( commonnode == -1 )
4457 lwerror(
"SQL/MM Spatial exception - other edges connected (%s)",
4462 lwerror(
"SQL/MM Spatial exception - non-connected edges");
4539 lwerror(
"Coding error: caseno=%d should never happen", caseno);
4564 lwerror(
"Unexpected error: %d edges updated when expecting 1", i);
4586 lwerror(
"Insertion of split edge failed (no reason)");
4690 eid1, eid2, newedge.
edge_id) )
4696 return modEdge ? commonnode : newedge.
edge_id;
4726 if (num == UINT64_MAX)
4736 lwerror(
"Two or more nodes found");
4761 if (num == UINT64_MAX)
4766 for (i=0; i<num;++i)
4777 " has null geometry", e->
edge_id);
4785 if ( dist > tol )
continue;
4791 lwerror(
"Two or more edges found");
4826 LWDEBUG(1,
"No face properly contains query point,"
4827 " looking for edges");
4834 if (num == UINT64_MAX)
4839 for (i=0; i<num; ++i)
4850 " has null geometry", e->
edge_id);
4858 " is dangling, won't consider it", e->
edge_id);
4866 " is %g (tol=%g)", e->
edge_id, dist, tol);
4869 if ( dist > tol )
continue;
4878 lwerror(
"Two or more faces found");
4882 if (
id &&
id != eface )
4885 lwerror(
"Two or more faces found"
4902 static inline double
4905 double ret = 3.6 * pow(10, - ( 15 - log10(d?d:1.0) ) );
4931 if ( ! gbox )
return 0;
4942 #define _LWT_MINTOLERANCE( topo, geom ) ( \
4943 topo->precision ? topo->precision : _lwt_minTolerance(geom) )
4972 findFace,
int *moved)
4975 double mindist = FLT_MAX;
4996 if (num == UINT64_MAX)
5003 LWDEBUGF(1,
"New point is within %.15g units of %d nodes", tol, num);
5008 for (i=0; i<num; ++i)
5010 sorted[i].
ptr = nodes+i;
5013 ((
LWT_ISO_NODE*)(sorted[i].ptr))->node_id, sorted[i].score);
5017 for (i=0; i<num; ++i)
5026 for ( i=0; i<num; ++i )
5033 if ( dist && dist >= tol )
continue;
5034 if ( !
id || dist < mindist )
5044 if ( moved ) *moved = mindist == 0 ? 0 : 1;
5058 if (num == UINT64_MAX)
5065 LWDEBUGF(1,
"New point is within %.15g units of %d edges", tol, num);
5072 for (i=0; i<num; ++i)
5074 sorted[i].
ptr = edges+i;
5077 ((
LWT_ISO_EDGE*)(sorted[i].ptr))->edge_id, sorted[i].score);
5081 for (j=0, i=0; i<num; ++i)
5083 if ( sorted[i].score == sorted[0].score )
5098 for (i=0; i<num; ++i)
5111 if ( moved ) *moved =
lwgeom_same(prj,pt) ? 0 : 1;
5143 " does not contain projected point to it",
5150 LWDEBUG(1,
"But there's another to check");
5165 LWDEBUGF(1,
"Edge snapped with tolerance %g", snaptol);
5168 #if POSTGIS_DEBUG_LEVEL > 0
5173 LWDEBUGF(1,
"Edge %s snapped became %s", wkt1, wkt2);
5186 LWDEBUGF(1,
"Edge first point is %g %g, "
5187 "snapline first point is %g %g",
5188 p1.
x, p1.
y, p2.
x, p2.
y);
5189 if ( p1.
x != p2.
x || p1.
y != p2.
y )
5191 LWDEBUG(1,
"Snapping moved first point, re-adding it");
5200 #if POSTGIS_DEBUG_LEVEL > 0
5204 LWDEBUGF(1,
"Tweaked snapline became %s", wkt1);
5209 #if POSTGIS_DEBUG_LEVEL > 0
5211 LWDEBUG(1,
"Snapping did not move first point");
5221 lwerror(
"lwt_ChangeEdgeGeom failed");
5226 #if POSTGIS_DEBUG_LEVEL > 0
5232 LWDEBUGF(1,
"Edge %s contains projected point %s", wkt1, wkt2);
5245 lwerror(
"lwt_ModEdgeSplit failed");
5273 if ( moved ) *moved = 0;
5277 lwerror(
"lwt_AddIsoNode failed");
5301 GEOSGeometry *edgeg;
5305 if (num == UINT64_MAX)
5321 for (i=0; i<num; ++i)
5330 GEOSGeom_destroy(edgeg);
5335 equals = GEOSEquals(gg, edgeg);
5336 GEOSGeom_destroy(gg);
5339 GEOSGeom_destroy(edgeg);
5347 GEOSGeom_destroy(edgeg);
5352 GEOSGeom_destroy(edgeg);
5370 int handleFaceSplit )
5373 LWPOINT *start_point, *end_point;
5383 LWDEBUGF(1,
"_lwtAddLineEdge with tolerance %g", tol);
5386 if ( ! start_point )
5388 lwnotice(
"Empty component of noded line");
5393 handleFaceSplit, &mm );
5395 if ( nid[0] == -1 )
return -1;
5402 lwerror(
"could not get last point of line "
5403 "after successfully getting first point !?");
5408 handleFaceSplit, &mm );
5411 if ( nid[1] == -1 )
return -1;
5420 nn = nid[0] == nid[1] ? 1 : 2;
5423 if (nn == UINT64_MAX)
5428 start_point = NULL; end_point = NULL;
5429 for (i=0; i<nn; ++i)
5431 if ( node[i].node_id == nid[0] ) start_point = node[i].
geom;
5432 if ( node[i].node_id == nid[1] ) end_point = node[i].
geom;
5434 if ( ! start_point || ! end_point )
5462 if ( colex->
ngeoms == 0 )
5466 LWDEBUG(1,
"Made-valid snapped edge collapsed");
5478 lwerror(
"lwcollection_extract(LINETYPE) returned a non-line?");
5487 LWDEBUGF(1,
"Made-valid snapped edge collapsed to %s",
5516 LWDEBUGG(1, tmp2,
"Repeated-point removed");
5525 LWDEBUG(1,
"Repeated-point removed edge collapsed");
5546 id =
_lwt_AddEdge( topo, nid[0], nid[1], edge, 0, handleFaceSplit ? 1 : -1 );
5568 if ( ! col->
ngeoms )
return bg;
5570 for (i=0; i<col->
ngeoms; ++i)
5584 int handleFaceSplit)
5594 uint64_t num, numedges = 0, numnodes = 0;
5608 LWDEBUGF(1,
"Working tolerance:%.15g", tol);
5616 LWDEBUGG(1, tmp,
"Repeated-point removed");
5617 }}
else tmp=(
LWGEOM*)line;
5622 if ( ! noded )
return NULL;
5629 LWDEBUGF(1,
"BOX expanded by %g is %.15g %.15g, %.15g %.15g",
5633 int nearbyindex = 0;
5634 int nearbycount = 0;
5638 if (numedges == UINT64_MAX)
5644 LWDEBUGF(1,
"Line has %d points, its bbox intersects %d edges bboxes",
5649 nearbycount += numedges;
5651 for (i=0; i<numedges; ++i)
5659 if ( dist && dist >= tol )
continue;
5660 nearby[nearbyindex++] = g;
5662 LWDEBUGF(1,
"Found %d edges closer than tolerance (%g)", nearbyindex, tol);
5664 int nearbyedgecount = nearbyindex;
5671 if (numnodes == UINT64_MAX)
5677 LWDEBUGF(1,
"Line bbox intersects %d nodes bboxes", numnodes);
5681 nearbycount = nearbyedgecount + numnodes;
5689 for (i=0; i<numnodes; ++i)
5696 if ( dist && dist >= tol )
5698 LWDEBUGF(1,
"Node %d is %g units away, we tolerate only %g", n->
node_id, dist, tol);
5701 nearby[nearbyindex++] = g;
5704 LWDEBUGF(1,
"Found %d isolated nodes closer than tolerance (%g)", nn, tol);
5706 int nearbynodecount = nearbyindex - nearbyedgecount;
5707 nearbycount = nearbyindex;
5709 LWDEBUGF(1,
"Number of nearby elements is %d", nearbycount);
5718 NULL, nearbycount, nearby);
5721 LWDEBUGG(1, elems,
"Collected nearby elements");
5726 LWDEBUGG(1, noded,
"Elements-snapped");
5739 LWDEBUGG(1, noded,
"Unary-unioned");
5743 if ( nearbyedgecount )
5749 LWDEBUGF(1,
"Line intersects %d edges", nearbyedgecount);
5752 NULL, nearbyedgecount, nearby);
5754 LWDEBUGG(1, iedges,
"Collected edges");
5756 LWDEBUGF(1,
"Diffing noded, with srid=%d "
5757 "and interesecting edges, with srid=%d",
5762 LWDEBUGF(1,
"Intersecting noded, with srid=%d "
5763 "and interesecting edges, with srid=%d",
5783 LWDEBUG(1,
"Linemerging intersection");
5793 LWDEBUG(1,
"Unioning difference and (linemerged) intersection");
5795 LWDEBUGG(1, noded,
"Diff-Xset Unioned");
5809 if ( nearbyedgecount )
5811 nearbycount += nearbyedgecount * 2;
5813 for (
int i=0; i<nearbyedgecount; i++)
5825 if ( nearbynodecount )
5828 NULL, nearbynodecount,
5829 nearby + nearbyedgecount);
5841 LWDEBUG(1,
"Freeing up nearby elements");
5844 if ( nearby )
lwfree(nearby);
5848 LWDEBUGG(1, noded,
"Finally-noded");
5854 LWDEBUG(1,
"Noded line was a collection");
5860 LWDEBUG(1,
"Noded line was a single geom");
5861 geomsbuf[0] = noded;
5866 LWDEBUGF(1,
"Line was split into %d edges", ngeoms);
5874 for ( i=0; i<ngeoms; ++i )
5880 #if POSTGIS_DEBUG_LEVEL > 0
5884 LWDEBUGF(1,
"Component %d of split line is: %s", i, wkt1);
5899 LWDEBUGF(1,
"Component %d of split line collapsed", i);
5908 LWDEBUGG(1, noded,
"Noded before free");
5936 uint64_t nfacesinbox;
5940 const GEOSPreparedGeometry *ppoly;
5941 GEOSGeometry *polyg;
5952 LWDEBUGF(1,
"Working tolerance:%.15g", tol);
5955 for ( i=0; i<poly->
nrings; ++i )
5968 lwerror(
"Error adding ring %d of polygon", i);
5983 if (nfacesinbox == UINT64_MAX)
6000 ppoly = GEOSPrepare(polyg);
6002 for ( j=0; j<nfacesinbox; ++j )
6006 GEOSGeometry *fgg, *sp;
6014 GEOSPreparedGeom_destroy(ppoly);
6015 GEOSGeom_destroy(polyg);
6026 GEOSPreparedGeom_destroy(ppoly);
6027 GEOSGeom_destroy(polyg);
6032 sp = GEOSPointOnSurface(fgg);
6033 GEOSGeom_destroy(fgg);
6036 GEOSPreparedGeom_destroy(ppoly);
6037 GEOSGeom_destroy(polyg);
6042 covers = GEOSPreparedCovers( ppoly, sp );
6043 GEOSGeom_destroy(sp);
6046 GEOSPreparedGeom_destroy(ppoly);
6047 GEOSGeom_destroy(polyg);
6060 GEOSPreparedGeom_destroy(ppoly);
6061 GEOSGeom_destroy(polyg);
6101 void *match = bsearch( &key, tab->
edges, tab->
size,
6129 #define LWT_EDGERING_INIT(a) { \
6131 (a)->capacity = 1; \
6132 (a)->elems = lwalloc(sizeof(LWT_EDGERING_ELEM *) * (a)->capacity); \
6137 #define LWT_EDGERING_PUSH(a, r) { \
6138 if ( (a)->size + 1 > (a)->capacity ) { \
6139 (a)->capacity *= 2; \
6140 (a)->elems = lwrealloc((a)->elems, sizeof(LWT_EDGERING_ELEM *) * (a)->capacity); \
6143 (a)->elems[(a)->size++] = (r); \
6146 #define LWT_EDGERING_CLEAN(a) { \
6147 int i; for (i=0; i<(a)->size; ++i) { \
6148 if ( (a)->elems[i] ) { \
6150 lwfree((a)->elems[i]); \
6153 if ( (a)->elems ) { lwfree((a)->elems); (a)->elems = NULL; } \
6155 (a)->capacity = 0; \
6156 if ( (a)->env ) { lwfree((a)->env); (a)->env = NULL; } \
6157 if ( (a)->genv ) { GEOSGeom_destroy((a)->genv); (a)->genv = NULL; } \
6168 #define LWT_EDGERING_ARRAY_INIT(a) { \
6170 (a)->capacity = 1; \
6171 (a)->rings = lwalloc(sizeof(LWT_EDGERING *) * (a)->capacity); \
6177 #define LWT_EDGERING_ARRAY_CLEAN(a) { \
6178 int j; for (j=0; j<(a)->size; ++j) { \
6179 LWT_EDGERING_CLEAN((a)->rings[j]); \
6181 if ( (a)->capacity ) lwfree((a)->rings); \
6182 if ( (a)->tree ) { \
6183 GEOSSTRtree_destroy( (a)->tree ); \
6188 #define LWT_EDGERING_ARRAY_PUSH(a, r) { \
6189 if ( (a)->size + 1 > (a)->capacity ) { \
6190 (a)->capacity *= 2; \
6191 (a)->rings = lwrealloc((a)->rings, sizeof(LWT_EDGERING *) * (a)->capacity); \
6193 (a)->rings[(a)->size++] = (r); \
6209 if ( ! el )
return 0;
6221 if ( it->
curidx < 0 ) tonext = 1;
6226 LWDEBUG(3,
"iterator moving to next element");
6257 #define LWT_HOLES_FACE_PLACEHOLDER INT32_MIN
6263 from < etab->size &&
6267 return from < etab->
size ? from : -1;
6275 uint64_t nelems = 1;
6279 if (nelems == UINT64_MAX)
6301 int forward_edges_count = 0;
6303 int backward_edges_count = 0;
6309 forward_edges_count = 0;
6311 backward_edges_count = 0;
6313 for ( i=0; i<ring->
size; ++i )
6320 LWDEBUGF(3,
"Forward edge %d is %d", forward_edges_count,
id);
6321 forward_edges[forward_edges_count].
edge_id = id;
6322 forward_edges[forward_edges_count++].
face_left = face;
6327 LWDEBUGF(3,
"Backward edge %d is %d", forward_edges_count,
id);
6328 backward_edges[backward_edges_count].
edge_id = id;
6329 backward_edges[backward_edges_count++].
face_right = face;
6335 if ( forward_edges_count )
6338 forward_edges_count,
6343 lwfree( backward_edges );
6347 if ( ret != forward_edges_count )
6350 lwfree( backward_edges );
6351 lwerror(
"Unexpected error: %d edges updated when expecting %d (forward)",
6352 ret, forward_edges_count);
6358 if ( backward_edges_count )
6361 backward_edges_count,
6366 lwfree( backward_edges );
6370 if ( ret != backward_edges_count )
6373 lwfree( backward_edges );
6374 lwerror(
"Unexpected error: %d edges updated when expecting %d (backward)",
6375 ret, backward_edges_count);
6381 lwfree( backward_edges );
6404 LWDEBUGF(2,
"Building rings for edge %d (side %d)",
cur->edge_id, side);
6411 elem->
left = ( curside == 1 );
6418 next = elem->
left ?
cur->next_left :
cur->next_right;
6420 LWDEBUGF(3,
" next edge is %d", next);
6422 if ( next > 0 ) curside = 1;
6423 else { curside = -1; next = -next; }
6427 lwerror(
"Could not find edge with id %d", next);
6430 }
while (
cur != edge || curside != side);
6444 double x0,
x, y1, y2;
6449 LWDEBUG(2,
"_lwt_EdgeRingSignedArea");
6474 LWDEBUGF(2,
"_lwt_EdgeRingIsCCW, ring has %d elems", ring->
size);
6477 LWDEBUGF(2,
"_lwt_EdgeRingIsCCW, signed area is %g", sa);
6479 if ( sa >= 0 )
return 0;
6502 ((v1.
y <= p->
y) && (v2.
y > p->
y))
6504 || ((v1.
y > p->
y) && (v2.
y <= p->
y))
6508 vt = (double)(p->
y - v1.
y) / (v2.
y - v1.
y);
6511 if (p->
x < v1.
x + vt * (v2.
x - v1.
x))
6520 LWDEBUGF(3,
"_lwt_EdgeRingCrossingCount returning %d", cn);
6523 if ( memcmp(&v1, &v0,
sizeof(
POINT2D)) )
6525 lwerror(
"_lwt_EdgeRingCrossingCount: V[n] != V[0] (%g %g != %g %g)",
6526 v1.
x, v1.
y, v0.
x, v0.
y);
6553 LWDEBUGF(2,
"Computing GBOX for ring %p", ring);
6554 for (i=0; i<ring->
size; ++i)
6615 LWDEBUG(2,
"Ring built, calling EdgeRingIsCCW");
6640 lwerror(
"Unexpected error: %d faces inserted when expecting 1", ret);
6644 *registered = newface.
face_id;
6651 lwerror(
"Errors updating edgering side face: %s",
6660 *registered = placeholder_faceid;
6681 const GBOX *minenv = NULL;
6683 const GBOX *testbox;
6684 GEOSGeometry *ghole;
6702 if ( ! shells->
tree )
6705 LWDEBUG(1,
"Building STRtree");
6707 if (shells->
tree == NULL)
6712 for (i=0; i<shells->
size; ++i)
6716 LWDEBUGF(2,
"GBOX of shell %p for edge %d is %g %g,%g %g",
6722 pt.
x = shellbox->
xmin;
6723 pt.
y = shellbox->
ymin;
6725 pt.
x = shellbox->
xmax;
6726 pt.
y = shellbox->
ymax;
6733 GEOSSTRtree_insert(shells->
tree, sring->
genv, sring);
6735 LWDEBUG(1,
"STRtree build completed");
6741 LWDEBUGF(1,
"Found %d candidate shells containing first point of ring's originating edge %d",
6746 for (i=0; i<candidates.
size; ++i)
6754 LWDEBUGF(1,
"Shell %d is on other side of ring",
6762 LWDEBUGF(1,
"Bbox of shell %d equals that of hole ring",
6770 LWDEBUGF(1,
"Bbox of shell %d does not contain bbox of ring point",
6779 LWDEBUGF(2,
"Bbox of shell %d (face %d) not contained by bbox "
6780 "of last shell found to contain the point",
6791 LWDEBUGF(1,
"Shell %d contains hole of edge %d",
6798 if ( foundInFace == -1 ) foundInFace = 0;
6800 candidates.
size = 0;
6803 GEOSGeom_destroy(ghole);
6818 uint64_t nelems = 1;
6824 if (nelems == UINT64_MAX)
6871 if ( numfaces != 0 ) {
6872 if ( numfaces > 0 ) {
6874 lwerror(
"Polygonize: face table is not empty.");
6882 if ( ! edgetable.
edges ) {
6883 if (edgetable.
size == 0) {
6895 for (i=0; i<edgetable.
size; ++i)
6903 edge = &(edgetable.
edges[i]);
6907 LWDEBUGF(1,
"Next face-missing edge has id:%d, face_left:%d, face_right:%d",
6912 &holes, &shells, &newface);
6914 LWDEBUGF(1,
"New face on the left of edge %d is %d",
6921 &holes, &shells, &newface);
6923 LWDEBUGF(1,
"New face on the right of edge %d is %d",
6934 lwerror(
"Errors fetching or registering face-missing edges: %s",
6944 for (i=0; i<holes.
size; ++i)
6951 if ( containing_face == -1 )
6956 lwerror(
"Errors finding face containing ring: %s",
6966 lwerror(
"Errors updating edgering side face: %s",
6972 LWDEBUG(1,
"All holes assigned, cleaning up");
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.
GBOX * gbox_clone(const GBOX *gbox)
int gbox_contains_2d(const GBOX *g1, const GBOX *g2)
Return LW_TRUE if the first GBOX contains the second on the 2d plane, LW_FALSE otherwise.
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
void lwgeom_geos_error(const char *fmt,...)
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)
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)
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
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)
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
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.
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)
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
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)
POINTARRAY * ptarray_clone(const POINTARRAY *ptarray)
Clone a POINTARRAY object.
int lwpoint_is_empty(const LWPOINT *point)
int ptarray_contains_point(const POINTARRAY *pa, const POINT2D *pt)
Return 1 if the point is inside the POINTARRAY, -1 if it is outside, and 0 if it is on the boundary.
int lwpoly_is_empty(const LWPOLY *poly)
int ptarray_isccw(const POINTARRAY *pa)
int p2d_same(const POINT2D *p1, const POINT2D *p2)
#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
static const int STRTREE_NODE_CAPACITY
#define LWDEBUG(level, msg)
#define LWDEBUGF(level, msg,...)
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
#define LWDEBUGG(level, geom, msg)
static LWT_ISO_FACE * lwt_be_getFaceWithinBox2D(const LWT_TOPOLOGY *topo, const GBOX *box, uint64_t *numelems, int fields, uint64_t limit)
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.
#define CBT2(to, method, a1, a2)
int lwt_be_ExistsEdgeIntersectingPoint(LWT_TOPOLOGY *topo, LWPOINT *pt)
struct LWT_ISO_EDGE_TABLE_T LWT_ISO_EDGE_TABLE
static uint64_t lwt_be_updateFacesById(LWT_TOPOLOGY *topo, const LWT_ISO_FACE *faces, uint64_t numfaces)
static double _lwt_EdgeRingSignedArea(LWT_EDGERING_POINT_ITERATOR *it)
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.
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.
struct LWT_EDGERING_POINT_ITERATOR_T LWT_EDGERING_POINT_ITERATOR
#define CB1(be, method, a1)
static int _lwt_EdgeRingContainsPoint(LWT_EDGERING *ring, POINT2D *p)
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)
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 double lwt_be_topoGetPrecision(LWT_TOPOLOGY *topo)
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)
struct LWT_EDGERING_ARRAY_T LWT_EDGERING_ARRAY
#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)
#define LWT_EDGERING_INIT(a)
static LWT_ISO_EDGE * lwt_be_getEdgeByNode(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t *numelems, int fields)
static int _lwt_FirstDistinctVertex2D(const POINTARRAY *pa, POINT2D *ref, int from, int dir, POINT2D *op)
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_EDGERING * _lwt_BuildEdgeRing(__attribute__((__unused__)) LWT_TOPOLOGY *topo, LWT_ISO_EDGE_TABLE *edges, LWT_ISO_EDGE *edge, int side)
static LWT_ELEMID _lwt_AddIsoNode(LWT_TOPOLOGY *topo, LWT_ELEMID face, LWPOINT *pt, int skipISOChecks, int checkFace)
static LWT_ELEMID _lwt_EdgeRingGetFace(LWT_EDGERING *ring)
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)
static int lwt_be_updateTopoGeomFaceSplit(LWT_TOPOLOGY *topo, LWT_ELEMID split_face, LWT_ELEMID new_face1, LWT_ELEMID new_face2)
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_be_getFaceContainingPoint(LWT_TOPOLOGY *topo, LWPOINT *pt)
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)
static int lwt_be_insertFaces(LWT_TOPOLOGY *topo, LWT_ISO_FACE *face, uint64_t numelems)
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 int _lwt_UpdateEdgeRingSideFace(LWT_TOPOLOGY *topo, LWT_EDGERING *ring, LWT_ELEMID face)
static int _lwt_EdgeRingIsCCW(LWT_EDGERING *ring)
static LWT_ISO_EDGE * lwt_be_getEdgeByFace(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t *numelems, int fields, const GBOX *box)
int lwt_be_insertNodes(LWT_TOPOLOGY *topo, LWT_ISO_NODE *node, uint64_t numelems)
LWT_ISO_NODE * lwt_be_getNodeWithinDistance2D(LWT_TOPOLOGY *topo, LWPOINT *pt, double dist, uint64_t *numelems, int fields, int64_t limit)
int lwt_Polygonize(LWT_TOPOLOGY *topo)
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)
static LWT_EDGERING_POINT_ITERATOR * _lwt_EdgeRingIterator_begin(LWT_EDGERING *er)
LWT_TOPOLOGY * lwt_LoadTopology(LWT_BE_IFACE *iface, const char *name)
Loads an existing topology by name from the database.
static int _lwt_EdgeRingIterator_next(LWT_EDGERING_POINT_ITERATOR *it, POINT2D *pt)
#define LWT_HOLES_FACE_PLACEHOLDER
static int _lwt_CheckFacesExist(LWT_TOPOLOGY *topo)
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 GBOX * _lwt_EdgeRingGetBbox(LWT_EDGERING *ring)
static void _lwt_RotateElemidArray(LWT_ELEMID *ary, int from, int to, int rotidx)
static LWT_ELEMID _lwt_GetEqualEdge(LWT_TOPOLOGY *topo, LWLINE *edge)
#define LWT_EDGERING_ARRAY_INIT(a)
struct LWT_EDGERING_ELEM_T LWT_EDGERING_ELEM
static LWT_ELEMID _lwt_AddLineEdge(LWT_TOPOLOGY *topo, LWLINE *edge, double tol, int handleFaceSplit)
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)
static LWT_ISO_EDGE * _lwt_getIsoEdgeById(LWT_ISO_EDGE_TABLE *tab, LWT_ELEMID id)
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.
static int _lwt_FindAdjacentEdges(LWT_TOPOLOGY *topo, LWT_ELEMID node, edgeend *data, edgeend *other, int myedge_id)
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)
static int lwt_be_topoHasZ(LWT_TOPOLOGY *topo)
static int _lwt_RegisterFaceOnEdgeSide(LWT_TOPOLOGY *topo, LWT_ISO_EDGE *edge, int side, LWT_ISO_EDGE_TABLE *edges, LWT_EDGERING_ARRAY *holes, LWT_EDGERING_ARRAY *shells, LWT_ELEMID *registered)
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.
static void _lwt_AccumulateCanditates(void *item, void *userdata)
static void _lwt_release_faces(LWT_ISO_FACE *faces, int num_faces)
LWT_ISO_EDGE * lwt_be_getEdgeWithinDistance2D(LWT_TOPOLOGY *topo, LWPOINT *pt, double dist, uint64_t *numelems, int fields, int64_t limit)
#define LWT_EDGERING_PUSH(a, r)
static LWT_ELEMID _lwt_AddFaceSplit(LWT_TOPOLOGY *topo, LWT_ELEMID sedge, LWT_ELEMID face, int mbr_only)
static LWT_ELEMID _lwt_FindFaceContainingRing(LWT_TOPOLOGY *topo, LWT_EDGERING *ring, LWT_EDGERING_ARRAY *shells)
#define _LWT_MINTOLERANCE(topo, geom)
#define CBT6(to, method, a1, a2, a3, a4, a5, a6)
static LWT_ISO_EDGE * lwt_be_getEdgeWithinBox2D(const LWT_TOPOLOGY *topo, const GBOX *box, uint64_t *numelems, int fields, uint64_t limit)
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)
#define CBT5(to, method, a1, a2, a3, a4, a5)
int lwt_be_insertEdges(LWT_TOPOLOGY *topo, LWT_ISO_EDGE *edge, uint64_t numelems)
#define LWT_EDGERING_ARRAY_CLEAN(a)
int lwt_be_ExistsCoincidentNode(LWT_TOPOLOGY *topo, LWPOINT *pt)
static LWT_ISO_EDGE * _lwt_FetchAllEdges(LWT_TOPOLOGY *topo, int *numedges)
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 LWGEOM * _lwt_FaceByEdges(LWT_TOPOLOGY *topo, LWT_ISO_EDGE *edges, int numfaceedges)
static int compare_iso_edges_by_id(const void *si1, const void *si2)
LWT_BE_TOPOLOGY * lwt_be_loadTopologyByName(LWT_BE_IFACE *be, const char *name)
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)
static int _lwt_FetchNextUnvisitedEdge(__attribute__((__unused__)) LWT_TOPOLOGY *topo, LWT_ISO_EDGE_TABLE *etab, int from)
struct scored_pointer_t scored_pointer
#define LWT_EDGERING_ARRAY_PUSH(a, r)
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.
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)
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
static int _lwt_EdgeRingCrossingCount(const POINT2D *p, LWT_EDGERING_POINT_ITERATOR *it)
int lwt_ChangeEdgeGeom(LWT_TOPOLOGY *topo, LWT_ELEMID edge_id, LWLINE *geom)
Changes the shape of an edge without affecting the topology structure.
static int lwt_be_updateEdgesById(LWT_TOPOLOGY *topo, const LWT_ISO_EDGE *edges, int numedges, int upd_fields)
struct LWT_EDGERING_T LWT_EDGERING
static LWT_ELEMID _lwt_RemEdge(LWT_TOPOLOGY *topo, LWT_ELEMID edge_id, int modFace)
LWT_ELEMID lwt_GetFaceByPoint(LWT_TOPOLOGY *topo, LWPOINT *pt, double tol)
Find the face-id of a face containing a given point.
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 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)
Datum contains(PG_FUNCTION_ARGS)
Datum covers(PG_FUNCTION_ARGS)
Structure containing base backend callbacks.
const LWT_BE_CALLBACKS * cb
LWT_EDGERING_ELEM * curelem
LWT_EDGERING_ELEM ** elems
LWT_ELEMID containing_face
LWT_BE_TOPOLOGY * be_topo
const LWT_BE_IFACE * be_iface