27 #include "../postgis_config.h"
41 # define LWTFMT_ELEMID "lld"
43 # define LWTFMT_ELEMID PRId64
50 #define P2D_SAME_STRICT(a,b) ((a)->x == (b)->x && (a)->y == (b)->y)
83 #define CHECKCB(be, method) do { \
84 if ( ! (be)->cb || ! (be)->cb->method ) \
85 lwerror("Callback " # method " not registered by backend"); \
88 #define CB0(be, method) \
90 return (be)->cb->method((be)->data)
92 #define CB1(be, method, a1) \
94 return (be)->cb->method((be)->data, a1)
96 #define CBT0(to, method) \
97 CHECKCB((to)->be_iface, method);\
98 return (to)->be_iface->cb->method((to)->be_topo)
100 #define CBT1(to, method, a1) \
101 CHECKCB((to)->be_iface, method);\
102 return (to)->be_iface->cb->method((to)->be_topo, a1)
104 #define CBT2(to, method, a1, a2) \
105 CHECKCB((to)->be_iface, method);\
106 return (to)->be_iface->cb->method((to)->be_topo, a1, a2)
108 #define CBT3(to, method, a1, a2, a3) \
109 CHECKCB((to)->be_iface, method);\
110 return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3)
112 #define CBT4(to, method, a1, a2, a3, a4) \
113 CHECKCB((to)->be_iface, method);\
114 return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3, a4)
116 #define CBT5(to, method, a1, a2, a3, a4, a5) \
117 CHECKCB((to)->be_iface, method);\
118 return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3, a4, a5)
120 #define CBT6(to, method, a1, a2, a3, a4, a5, a6) \
121 CHECKCB((to)->be_iface, method);\
122 return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3, a4, a5, a6)
127 CB0(be, lastErrorMessage);
133 CB1(be, loadTopologyByName, name);
139 CBT0(topo, topoGetSRID);
145 CBT0(topo, topoGetPrecision);
151 CBT0(topo, topoHasZ);
157 CBT0(topo, freeTopology);
163 CBT3(topo, getNodeById, ids, numelems, fields);
174 CBT5(topo, getNodeWithinDistance2D, pt, dist, numelems, fields, limit);
180 CBT4(topo, getNodeWithinBox2D, box, numelems, fields, limit);
186 CBT4(topo, getEdgeWithinBox2D, box, numelems, fields, limit);
192 CBT4(topo, getFaceWithinBox2D, box, numelems, fields, limit);
198 CBT2(topo, insertNodes, node, numelems);
204 CBT2(topo, insertFaces, face, numelems);
210 CBT2(topo, deleteFacesById, ids, numelems);
216 CBT2(topo, deleteNodesById, ids, numelems);
222 CBT0(topo, getNextEdgeId);
228 CBT3(topo, getEdgeById, ids, numelems, fields);
234 CBT3(topo, getFaceById, ids, numelems, fields);
240 CBT3(topo, getEdgeByNode, ids, numelems, fields);
246 CBT4(topo, getEdgeByFace, ids, numelems, fields, box);
252 CBT4(topo, getNodeByFace, ids, numelems, fields, box);
263 CBT5(topo, getEdgeWithinDistance2D, pt, dist, numelems, fields, limit);
269 CBT2(topo, insertEdges, edge, numelems);
279 CBT6(topo, updateEdges, sel_edge, sel_fields,
280 upd_edge, upd_fields,
281 exc_edge, exc_fields);
291 CBT6(topo, updateNodes, sel_node, sel_fields,
292 upd_node, upd_fields,
293 exc_node, exc_fields);
301 CBT2(topo, updateFacesById, faces, numfaces);
309 CBT3(topo, updateEdgesById, edges, numedges, upd_fields);
317 CBT3(topo, updateNodesById, nodes, numnodes, upd_fields);
325 CBT2(topo, deleteEdges, sel_edge, sel_fields);
331 CBT3(topo, updateTopoGeomEdgeSplit, split_edge, new_edge1, new_edge2);
338 CBT3(topo, updateTopoGeomFaceSplit, split_face, new_face1, new_face2);
345 CBT3(topo, checkTopoGeomRemEdge, edge_id, face_left, face_right);
351 CBT1(topo, checkTopoGeomRemIsoEdge, edge_id);
358 CBT3(topo, checkTopoGeomRemNode, node_id, eid1, eid2);
364 CBT1(topo, checkTopoGeomRemIsoNode, node_id);
372 CBT3(topo, updateTopoGeomFaceHeal, face1, face2, newface);
380 CBT3(topo, updateTopoGeomEdgeHeal, edge1, edge2, newedge);
386 CBT3(topo, getRingEdges, edge, numedges, limit);
394 if (exists == UINT64_MAX)
407 if (exists == UINT64_MAX)
418 CBT3(topo, getClosestEdge, pt, numelems, fields);
424 CBT1(topo, computeFaceMBR, face);
452 }
while ( changed && iterations <= maxiterations );
454 LWDEBUGF(1,
"It took %d/%d iterations to properly snap",
455 iterations, maxiterations);
464 for ( i=0; i<num_faces; ++i ) {
465 if ( faces[i].mbr )
lwfree(faces[i].mbr);
474 for ( i=0; i<num_edges; ++i ) {
484 for ( i=0; i<num_nodes; ++i ) {
522 lwnotice(
"Could not release backend topology memory: %s",
538 LWPOINT* pt,
int skipISOChecks,
int checkFace )
544 lwerror(
"Cannot add empty point as isolated node");
549 if ( ! skipISOChecks )
553 lwerror(
"SQL/MM Spatial exception - coincident node");
558 lwerror(
"SQL/MM Spatial exception - edge crosses node.");
563 if ( checkFace && ( face == -1 || ! skipISOChecks ) )
566 if ( foundInFace == -1 ) {
570 if ( foundInFace == -1 ) foundInFace = 0;
576 else if ( ! skipISOChecks && foundInFace != face ) {
578 lwerror(
"SQL/MM Spatial exception - within face %d (not %d)",
581 lwerror(
"SQL/MM Spatial exception - not within face");
601 LWPOINT* pt,
int skipISOChecks )
619 uint64_t i, num_nodes, num_edges;
623 GEOSGeometry *edgegg;
638 LWDEBUGF(1,
"lwt_be_getNodeWithinBox2D returned %d nodes", num_nodes);
639 if (num_nodes == UINT64_MAX)
644 for ( i=0; i<num_nodes; ++i )
648 if ( node->
node_id == start_node )
continue;
649 if ( node->
node_id == end_node )
continue;
656 GEOSGeom_destroy(edgegg);
658 lwerror(
"SQL/MM Spatial exception - geometry crosses a node");
667 LWDEBUGF(1,
"lwt_be_getEdgeWithinBox2D returned %d edges", num_edges);
668 if (num_edges == UINT64_MAX)
670 GEOSGeom_destroy(edgegg);
674 for ( i=0; i<num_edges; ++i )
682 if ( edge_id == myself )
continue;
684 if ( ! edge->
geom ) {
686 lwerror(
"Edge %d has NULL geometry!", edge_id);
692 GEOSGeom_destroy(edgegg);
698 LWDEBUGF(2,
"Edge %d converted to GEOS", edge_id);
702 relate = GEOSRelateBoundaryNodeRule(eegg, edgegg, 2);
704 GEOSGeom_destroy(eegg);
705 GEOSGeom_destroy(edgegg);
711 LWDEBUGF(2,
"Edge %d relate pattern is %s", edge_id, relate);
713 match = GEOSRelatePatternMatch(relate,
"F********");
716 GEOSGeom_destroy(eegg);
720 GEOSGeom_destroy(edgegg);
727 match = GEOSRelatePatternMatch(relate,
"1FFF*FFF2");
730 GEOSGeom_destroy(edgegg);
731 GEOSGeom_destroy(eegg);
742 match = GEOSRelatePatternMatch(relate,
"1********");
745 GEOSGeom_destroy(edgegg);
746 GEOSGeom_destroy(eegg);
751 lwerror(
"Spatial exception - geometry intersects edge %"
757 match = GEOSRelatePatternMatch(relate,
"T********");
760 GEOSGeom_destroy(edgegg);
761 GEOSGeom_destroy(eegg);
766 lwerror(
"SQL/MM Spatial exception - geometry crosses edge %"
772 LWDEBUGF(2,
"Edge %d analisys completed, it does no harm", edge_id);
775 GEOSGeom_destroy(eegg);
780 GEOSGeom_destroy(edgegg);
797 int skipISOChecks = 0;
803 if (startNode == endNode)
805 lwerror(
"Closed edges would not be isolated, try lwt_AddEdgeNewFaces");
809 if ( ! skipISOChecks )
814 lwerror(
"SQL/MM Spatial exception - curve not simple");
828 node_ids[0] = startNode;
829 node_ids[1] = endNode;
832 if (num_nodes == UINT64_MAX)
837 else if ( num_nodes < 2 )
840 lwerror(
"SQL/MM Spatial exception - non-existent node");
843 for ( i=0; i<num_nodes; ++i )
849 lwerror(
"SQL/MM Spatial exception - not isolated node");
856 lwerror(
"SQL/MM Spatial exception - nodes in different faces");
860 if ( ! skipISOChecks )
870 lwerror(
"SQL/MM Spatial exception - "
871 "start node not geometry start point.");
883 lwerror(
"SQL/MM Spatial exception - "
884 "end node not geometry end point.");
893 if ( ! skipISOChecks )
913 if ( containing_face == -1 ) containing_face = 0;
926 }
else if ( ret == 0 ) {
927 lwerror(
"Insertion of split edge failed (no reason)");
937 updated_nodes[0].
node_id = startNode;
939 updated_nodes[1].
node_id = endNode;
960 LWDEBUG(1,
"calling lwt_be_getEdgeById");
962 LWDEBUGF(1,
"lwt_be_getEdgeById returned %p", *oldedge);
965 LWDEBUGF(1,
"lwt_be_getEdgeById returned NULL and set i=%d", i);
973 lwerror(
"SQL/MM Spatial exception - non-existent edge");
978 lwerror(
"Backend coding error: getEdgeById callback returned NULL "
979 "but numelements output parameter has value %d "
980 "(expected 0 or 1)", i);
989 if ( ! skipISOChecks )
991 LWDEBUG(1,
"calling lwt_be_ExistsCoincidentNode");
994 LWDEBUG(1,
"lwt_be_ExistsCoincidentNode returned");
996 lwerror(
"SQL/MM Spatial exception - coincident node");
999 LWDEBUG(1,
"lwt_be_ExistsCoincidentNode returned");
1007 lwerror(
"could not split edge by point ?");
1011 if ( ! split_col ) {
1014 lwerror(
"lwgeom_as_lwcollection returned NULL");
1017 if (split_col->
ngeoms < 2) {
1020 lwerror(
"SQL/MM Spatial exception - point not on edge");
1028 LWDEBUGF(1,
"returning split col: %s", wkt);
1037 LWPOINT* pt,
int skipISOChecks )
1042 const LWGEOM *oldedge_geom;
1043 const LWGEOM *newedge_geom;
1048 split_col =
_lwt_EdgeSplit( topo, edge, pt, skipISOChecks, &oldedge );
1049 if ( ! split_col )
return -1;
1050 oldedge_geom = split_col->
geoms[0];
1051 newedge_geom = split_col->
geoms[1];
1054 ((
LWGEOM*)newedge_geom)->srid = split_col->
srid;
1071 lwerror(
"Backend coding error: "
1072 "insertNodes callback did not return node_id");
1078 if ( newedge1.
edge_id == -1 ) {
1093 if ( ! newedge1.
geom ) {
1096 lwerror(
"first geometry in lwgeom_split output is not a line");
1105 }
else if ( ret == 0 ) {
1108 lwerror(
"Insertion of split edge failed (no reason)");
1115 if ( ! updedge.
geom ) {
1118 lwerror(
"second geometry in lwgeom_split output is not a line");
1132 }
else if ( ret == 0 ) {
1135 lwerror(
"Edge being split (%d) disappeared during operations?", oldedge->
edge_id);
1137 }
else if ( ret > 1 ) {
1140 lwerror(
"More than a single edge found with id %d !", oldedge->
edge_id);
1194 LWPOINT* pt,
int skipISOChecks )
1199 const LWGEOM *oldedge_geom;
1200 const LWGEOM *newedge_geom;
1205 split_col =
_lwt_EdgeSplit( topo, edge, pt, skipISOChecks, &oldedge );
1206 if ( ! split_col )
return -1;
1207 oldedge_geom = split_col->
geoms[0];
1208 newedge_geom = split_col->
geoms[1];
1211 ((
LWGEOM*)newedge_geom)->srid = split_col->
srid;
1228 lwerror(
"Backend coding error: "
1229 "insertNodes callback did not return node_id");
1245 if ( newedges[0].edge_id == -1 ) {
1252 if ( newedges[1].edge_id == -1 ) {
1273 if ( ! newedges[0].geom ) {
1276 lwerror(
"first geometry in lwgeom_split output is not a line");
1294 if ( ! newedges[1].geom ) {
1297 lwerror(
"second geometry in lwgeom_split output is not a line");
1307 }
else if ( ret == 0 ) {
1310 lwerror(
"Insertion of split edge failed (no reason)");
1428 LWDEBUGF(1,
"first point is index %d", from);
1430 for ( i = from+inc; i != toofar; i += inc )
1432 LWDEBUGF(1,
"testing point %d", i);
1463 LWDEBUG(1,
"computing azimuth of first edge end");
1466 lwerror(
"Invalid edge (no two distinct vertices exist)");
1470 lwerror(
"error computing azimuth of first edgeend [%.15g %.15g,%.15g %.15g]",
1471 fp->
x, fp->
y, pt.
x, pt.
y);
1474 LWDEBUGF(1,
"azimuth of first edge end [%.15g %.15g,%.15g %.15g] is %g",
1475 fp->
x, fp->
y, pt.
x, pt.
y, fee->
myaz);
1478 LWDEBUG(1,
"computing azimuth of second edge end");
1481 lwerror(
"Invalid edge (no two distinct vertices exist)");
1485 lwerror(
"error computing azimuth of last edgeend [%.15g %.15g,%.15g %.15g]",
1486 lp->
x, lp->
y, pt.
x, pt.
y);
1489 LWDEBUGF(1,
"azimuth of last edge end [%.15g %.15g,%.15g %.15g] is %g",
1490 lp->
x, lp->
y, pt.
x, pt.
y, lee->
myaz);
1510 edgeend *other,
int myedge_id )
1513 uint64_t numedges = 1;
1515 double minaz, maxaz;
1523 if ( azdif < 0 ) azdif += 2 * M_PI;
1524 minaz = maxaz = azdif;
1526 LWDEBUGF(1,
"Other edge end has cwFace=%d and ccwFace=%d",
1533 " and adjacent to azimuth %g", node,
data->myaz);
1537 if (numedges == UINT64_MAX)
1543 LWDEBUGF(1,
"getEdgeByNode returned %d edges, minaz=%g, maxaz=%g",
1544 numedges, minaz, maxaz);
1547 for ( i = 0; i < numedges; ++i )
1557 if ( edge->
edge_id == myedge_id )
continue;
1570 " does not have two distinct points",
id);
1578 lwerror(
"Edge %d has no distinct vertices: [%.15g %.15g,%.15g %.15g]: ",
1584 ", edgeend is [%.15g %.15g,%.15g %.15g]",
1590 lwerror(
"error computing azimuth of edge %d first edgeend [%.15g %.15g,%.15g %.15g]",
1591 id, p1.
x, p1.
y, p2.
x, p2.
y);
1594 azdif = az -
data->myaz;
1596 ": %.15g (diff: %.15g)", edge->
edge_id, az, azdif);
1598 if ( azdif < 0 ) azdif += 2 * M_PI;
1599 if ( minaz == -1 ) {
1600 minaz = maxaz = azdif;
1607 " (face_right is new ccwFace, face_left is new cwFace)",
1611 if ( azdif < minaz ) {
1617 " (previous had minaz=%g, face_left is new cwFace)",
1622 else if ( azdif > maxaz ) {
1628 " (previous had maxaz=%g, face_right is new ccwFace)",
1640 lwerror(
"Edge %d has no distinct vertices: [%.15g %.15g,%.15g %.15g]: ",
1645 ", edgeend is [%.15g %.15g,%.15g %.15g]",
1651 lwerror(
"error computing azimuth of edge %d last edgeend [%.15g %.15g,%.15g %.15g]",
1652 id, p1.
x, p1.
y, p2.
x, p2.
y);
1655 azdif = az -
data->myaz;
1657 ": %.15g (diff: %.15g)", edge->
edge_id, az, azdif);
1658 if ( azdif < 0 ) azdif += 2 * M_PI;
1659 if ( minaz == -1 ) {
1660 minaz = maxaz = azdif;
1667 " (face_right is new cwFace, face_left is new ccwFace)",
1671 if ( azdif < minaz ) {
1677 " (previous had minaz=%g, face_right is new cwFace)",
1682 else if ( azdif > maxaz ) {
1686 ", outgoing, from start point, "
1688 " (previous had maxaz=%g, face_left is new ccwFace)",
1700 LWDEBUGF(1,
"edges adjacent to azimuth %g"
1703 data->myaz, node,
data->nextCW, minaz,
1704 data->nextCCW, maxaz);
1706 if ( myedge_id < 1 && numedges && data->cwFace !=
data->ccwFace )
1708 if (
data->cwFace != -1 &&
data->ccwFace != -1 ) {
1734 if ( pa->
npoints < 2 )
return 0;
1738 for (i=1; i<pa->
npoints-1; ++i)
1754 ip->
x = fp.
x + ( (lp.
x - fp.
x) * 0.5 );
1755 ip->
y = fp.
y + ( (lp.
y - fp.
y) * 0.5 );
1764 uint64_t numedges, i, j;
1770 for (i=0; i<num_signed_edge_ids; ++i) {
1771 int absid = llabs(signed_edge_ids[i]);
1774 for (j=0; j<numedges; ++j) {
1775 if ( edge_ids[j] == absid ) {
1780 if ( ! found ) edge_ids[numedges++] = absid;
1786 if (i == UINT64_MAX)
1791 else if ( i != numedges )
1793 lwfree( signed_edge_ids );
1795 lwerror(
"Unexpected error: %d edges found when expecting %d", i, numedges);
1803 for ( i=0; i<num_signed_edge_ids; ++i )
1809 for ( j=0; j<numedges; ++j )
1811 if ( ring_edges[j].edge_id == llabs(eid) )
1813 edge = &(ring_edges[j]);
1820 lwerror(
"missing edge that was found in ring edges loop");
1878 uint64_t numfaceedges, i, j;
1879 int newface_outside;
1880 uint64_t num_signed_edge_ids;
1884 int forward_edges_count = 0;
1886 int backward_edges_count = 0;
1889 if (!signed_edge_ids)
1896 LWDEBUGF(1,
"getRingEdges returned %d edges", num_signed_edge_ids);
1899 for (i=0; i<num_signed_edge_ids; ++i) {
1900 if ( signed_edge_ids[i] == -sedge ) {
1903 lwfree( signed_edge_ids );
1909 sedge, face, mbr_only);
1913 num_signed_edge_ids);
1915 lwfree( signed_edge_ids );
1924 lwfree( signed_edge_ids );
1926 " is geometrically not-closed", sedge);
1932 sedge, isccw ?
"counter" :
"");
1941 lwfree( signed_edge_ids );
1945 LWDEBUG(1,
"The left face of this clockwise ring is the universe, "
1946 "won't create a new face there");
1951 if ( mbr_only && face != 0 )
1957 updface.
mbr = (
GBOX *)shellbox;
1961 lwfree( signed_edge_ids );
1968 lwfree( signed_edge_ids );
1970 lwerror(
"Unexpected error: %d faces found when expecting 1", ret);
1974 lwfree( signed_edge_ids );
1982 if ( face != 0 && ! isccw)
1985 uint64_t nfaces = 1;
1987 if (nfaces == UINT64_MAX)
1989 lwfree( signed_edge_ids );
1996 lwfree( signed_edge_ids );
1998 lwerror(
"Unexpected error: %d faces found when expecting 1", nfaces);
2001 newface.
mbr = oldface->
mbr;
2005 newface.
mbr = (
GBOX *)shellbox;
2012 lwfree( signed_edge_ids );
2019 lwfree( signed_edge_ids );
2021 lwerror(
"Unexpected error: %d faces inserted when expecting 1", ret);
2032 if ( face != 0 && ! isccw ) {
2034 LWDEBUG(1,
"New face is on the outside of the ring, updating rings in former shell");
2035 newface_outside = 1;
2038 LWDEBUG(1,
"New face is on the inside of the ring, updating forward edges in new ring");
2039 newface_outside = 0;
2052 if (numfaceedges == UINT64_MAX)
2058 LWDEBUGF(1,
"_lwt_AddFaceSplit: lwt_be_getEdgeByFace returned %d edges", numfaceedges);
2063 forward_edges_count = 0;
2065 backward_edges_count = 0;
2068 for ( i=0; i<numfaceedges; ++i )
2076 for ( j=0; j<num_signed_edge_ids; ++j )
2078 int seid = signed_edge_ids[j];
2086 if ( found == 2 )
break;
2088 else if ( -seid == e->
edge_id )
2091 LWDEBUGF(1,
"Edge %d is a backward edge of the new ring", e->
edge_id);
2095 if ( found == 2 )
break;
2098 if ( found )
continue;
2112 lwerror(
"Could not find interior point for edge %d: %s",
2125 if ( newface_outside )
2129 LWDEBUGF(1,
"Edge %d contained in an hole of the new face",
2138 LWDEBUGF(1,
"Edge %d not contained in the face shell",
2162 if ( forward_edges_count )
2165 forward_edges_count,
2169 lwfree( signed_edge_ids );
2173 if ( ret != forward_edges_count )
2175 lwfree( signed_edge_ids );
2176 lwerror(
"Unexpected error: %d edges updated when expecting %d",
2177 ret, forward_edges_count);
2183 if ( backward_edges_count )
2186 backward_edges_count,
2190 lwfree( signed_edge_ids );
2194 if ( ret != backward_edges_count )
2196 lwfree( signed_edge_ids );
2197 lwerror(
"Unexpected error: %d edges updated when expecting %d",
2198 ret, backward_edges_count);
2211 uint64_t numisonodes = 1;
2214 &numisonodes, fields, newface.
mbr);
2215 if (numisonodes == UINT64_MAX)
2221 if ( numisonodes ) {
2223 int nodes_to_update = 0;
2224 for (i=0; i<numisonodes; ++i)
2229 LWDEBUGF(1,
"Node %d is %scontained in new ring, newface is %s",
2231 newface_outside ?
"outside" :
"inside" );
2232 if ( newface_outside )
2236 LWDEBUGF(1,
"Node %d contained in an hole of the new face",
2245 LWDEBUGF(1,
"Node %d not contained in the face shell",
2256 if ( nodes_to_update )
2262 lwfree( signed_edge_ids );
2286 LWLINE *geom,
int skipChecks,
int modFace )
2295 const LWPOINT *start_node_geom = NULL;
2296 const LWPOINT *end_node_geom = NULL;
2310 lwerror(
"SQL/MM Spatial exception - curve not simple");
2317 newedge.
geom = geom;
2326 lwerror(
"Invalid edge (no two distinct vertices exist)");
2339 lwerror(
"Invalid edge (no two distinct vertices exist)");
2344 lwerror(
"error computing azimuth of first edgeend [%.15g %.15g,%.15g %.15g]",
2345 p1.
x, p1.
y, pn.
x, pn.
y);
2348 LWDEBUGF(1,
"edge's start node is %g,%g", p1.
x, p1.
y);
2356 lwerror(
"Invalid clean edge (no two distinct vertices exist) - should not happen");
2361 lwerror(
"error computing azimuth of last edgeend [%.15g %.15g,%.15g %.15g]",
2362 p2.
x, p2.
y, pn.
x, pn.
y);
2365 LWDEBUGF(1,
"edge's end node is %g,%g", p2.
x, p2.
y);
2372 if ( start_node != end_node ) {
2374 node_ids[0] = start_node;
2375 node_ids[1] = end_node;
2378 node_ids[0] = start_node;
2382 if (num_nodes == UINT64_MAX)
2387 for ( i=0; i<num_nodes; ++i )
2399 lwerror(
"SQL/MM Spatial exception - geometry crosses an edge"
2405 LWDEBUGF(1,
"Node %d, with geom %p (looking for %d and %d)",
2407 if ( node->
node_id == start_node ) {
2408 start_node_geom = node->
geom;
2410 if ( node->
node_id == end_node ) {
2411 end_node_geom = node->
geom;
2417 if ( ! start_node_geom )
2420 lwerror(
"SQL/MM Spatial exception - non-existent node");
2425 pa = start_node_geom->
point;
2430 lwerror(
"SQL/MM Spatial exception"
2431 " - start node not geometry start point."
2438 if ( ! end_node_geom )
2441 lwerror(
"SQL/MM Spatial exception - non-existent node");
2446 pa = end_node_geom->
point;
2451 lwerror(
"SQL/MM Spatial exception"
2452 " - end node not geometry end point."
2471 if ( newedge.
edge_id == -1 ) {
2477 int isclosed = start_node == end_node;
2480 isclosed ? &epan : NULL, -1 );
2485 LWDEBUGF(1,
"New edge %d is connected on start node, "
2486 "next_right is %d, prev_left is %d",
2488 if ( modFace != -1 )
2501 LWDEBUGF(1,
"New edge %d is isolated on start node, "
2502 "next_right is %d, prev_left is %d",
2507 isclosed ? &span : NULL, -1 );
2512 LWDEBUGF(1,
"New edge %d is connected on end node, "
2513 "next_left is %d, prev_right is %d",
2515 if ( modFace != -1 )
2521 lwerror(
"Side-location conflict: "
2522 "new edge starts in face"
2533 lwerror(
"Side-location conflict: "
2534 "new edge starts in face"
2546 LWDEBUGF(1,
"New edge %d is isolated on end node, "
2547 "next_left is %d, prev_right is %d",
2561 " faces mismatch: invalid topology ?",
2567 lwerror(
"Could not derive edge face from linked primitives:"
2568 " invalid topology ?");
2581 }
else if ( ret == 0 ) {
2582 lwerror(
"Insertion of split edge failed (no reason)");
2590 if ( llabs(prev_left) != newedge.
edge_id )
2592 if ( prev_left > 0 )
2609 &updedge, updfields,
2619 if ( llabs(prev_right) != newedge.
edge_id )
2621 if ( prev_right > 0 )
2633 seledge.
edge_id = -prev_right;
2638 &updedge, updfields,
2680 if ( modFace > -1 ) {
2684 LWDEBUG(1,
"New edge is dangling, so it cannot split any face");
2696 if ( newface1 == 0 ) {
2697 LWDEBUG(1,
"New edge does not split any face");
2706 if ( newface == 0 ) {
2707 LWDEBUG(1,
"New edge does not split any face");
2717 if ( newface < 0 )
return newedge.
edge_id;
2756 LWLINE *geom,
int skipChecks )
2758 return _lwt_AddEdge( topo, start_node, end_node, geom, skipChecks, 1 );
2764 LWLINE *geom,
int skipChecks )
2766 return _lwt_AddEdge( topo, start_node, end_node, geom, skipChecks, 0 );
2775 int i, validedges = 0;
2777 for ( i=0; i<numfaceedges; ++i )
2791 if ( numfaceedges )
lwfree(geoms);
2792 LWDEBUG(1,
"_lwt_FaceByEdges returning empty polygon");
2809 LWDEBUGF(1,
"_lwt_FaceByEdges returning area: %s", wkt);
2819 uint64_t numfaceedges;
2829 lwerror(
"SQL/MM Spatial exception - universal face has no geometry");
2841 if (numfaceedges == UINT64_MAX)
2846 LWDEBUGF(1,
"lwt_GetFaceGeometry: lwt_be_getEdgeByFace returned %d edges", numfaceedges);
2848 if ( numfaceedges == 0 )
2852 if (i == UINT64_MAX)
2858 lwerror(
"SQL/MM Spatial exception - non-existent face.");
2863 lwerror(
"Corrupted topology: multiple face records have face_id=%"
2869 lwnotice(
"Corrupted topology: face %"
2886 lwnotice(
"Corrupted topology: face %"
2921 LWDEBUGF(1,
"Ring's 'from' point (%d) is %g,%g", from, p1.
x, p1.
y);
2925 for ( i=0; i<numedges; ++i )
2939 ") on both sides, skipping",
2947 " has only %"PRIu32
" points",
2964 LWDEBUGF(1,
"Rings's 'from' point is still %g,%g", p1.
x, p1.
y);
2967 LWDEBUG(1,
"P2D_SAME_STRICT(p1,p2) returned true");
2969 " matches ring vertex %d", isoe->
edge_id, from);
2971 for ( j=1; j<epa->
npoints; ++j )
2980 LWDEBUGF(1,
"Ring's point %d is %g,%g",
2981 from+1, pt.
x, pt.
y);
2985 #if POSTGIS_DEBUG_LEVEL > 0
2988 " matches ring vertex %d", isoe->
edge_id, from+1);
2991 " does not match ring vertex %d", isoe->
edge_id, from+1);
3006 " matches ring vertex %d", isoe->
edge_id, from);
3008 for ( j=2; j<=epa->
npoints; j++ )
3017 LWDEBUGF(1,
"Ring's point %d is %g,%g",
3018 from+1, pt.
x, pt.
y);
3023 #if POSTGIS_DEBUG_LEVEL > 0
3026 " matches ring vertex %d", isoe->
edge_id, from+1);
3029 " does not match ring vertex %d", isoe->
edge_id, from+1);
3034 if ( match )
return i;
3050 ary[from++] = ary[to];
3073 uint64_t numfaceedges;
3088 if (numfaceedges == UINT64_MAX)
3093 if ( ! numfaceedges )
return 0;
3094 LWDEBUGF(1,
"lwt_GetFaceEdges: lwt_be_getEdgeByFace returned %d edges", numfaceedges);
3138 nseid = prevseid = 0;
3142 for ( i=0; i<facepoly->
nrings; ++i )
3151 while ( j < (int32_t) ring->
npoints-1 )
3153 LWDEBUGF(1,
"Looking for edge covering ring %d from vertex %d",
3163 lwerror(
"No edge (among %d) found to be defining geometry of face %"
3168 nextedge = &(edges[edgeno]);
3169 nextline = nextedge->
geom;
3172 " covers ring %d from vertex %d to %d",
3188 seid[nseid++] = nextedge->
face_left == face_id ?
3199 if ( (nseid - prevseid) > 1 )
3203 LWDEBUGF(1,
"Looking for smallest id among the %d edges "
3204 "composing ring %d", (nseid-prevseid), i);
3205 for ( j=prevseid; j<nseid; ++j )
3209 if ( ! minid ||
id < minid )
3216 " at position %d", minid, minidx);
3217 if ( minidx != prevseid )
3245 lwerror(
"SQL/MM Spatial exception - curve not simple");
3254 "lwt_be_getEdgeById returned NULL and set i=%d", i);
3255 if (i == UINT64_MAX)
3262 lwerror(
"SQL/MM Spatial exception - non-existent edge %"
3268 lwerror(
"Backend coding error: getEdgeById callback returned NULL "
3269 "but numelements output parameter has value %d "
3270 "(expected 0 or 1)", i);
3276 "old edge has %d points, new edge has %d points",
3287 lwerror(
"SQL/MM Spatial exception - "
3288 "start node not geometry start point.");
3299 " has less than 2 vertices", oldedge->
edge_id);
3306 lwerror(
"Invalid edge: less than 2 vertices");
3313 lwerror(
"SQL/MM Spatial exception - "
3314 "end node not geometry end point.");
3330 lwerror(
"Invalid edge (no two distinct vertices exist)");
3339 lwerror(
"Edge twist at node POINT(%g %g)", p1.
x, p1.
y);
3345 oldedge->
end_node, geom, edge_id ) )
3352 LWDEBUG(1,
"lwt_ChangeEdgeGeom: "
3353 "edge crossing check passed ");
3371 LWDEBUGF(1,
"lwt_be_getNodeWithinBox2D returned %d nodes", numnodes);
3372 if (numnodes == UINT64_MAX)
3379 if ( numnodes > ( 1 + isclosed ? 0 : 1 ) )
3382 for (i=0; i<numnodes; ++i)
3395 lwerror(
"Edge motion collision at %s", wkt);
3403 LWDEBUG(1,
"nodes containment check passed");
3416 isclosed ? &epan_pre : NULL, edge_id );
3418 isclosed ? &span_pre : NULL, edge_id );
3428 newedge.
geom = geom;
3439 lwerror(
"Unexpected error: %d edges updated when expecting 1", i);
3452 isclosed ? &epan_post : NULL, edge_id );
3454 isclosed ? &span_post : NULL, edge_id );
3469 lwerror(
"Edge changed disposition around start node %"
3480 lwerror(
"Edge changed disposition around end node %"
3498 uint64_t facestoupdate = 0;
3505 lwerror(
"Corrupted topology: face %d, left of edge %d, has no bbox",
3511 faces[facestoupdate++].
mbr = updatedBox;
3520 lwerror(
"Corrupted topology: face %d, right of edge %d, has no bbox",
3526 faces[facestoupdate++].
mbr = updatedBox;
3528 LWDEBUGF(1,
"%d faces to update", facestoupdate);
3529 if ( facestoupdate )
3532 if (updatedFaces != facestoupdate)
3534 while ( facestoupdate-- )
lwfree(faces[facestoupdate].mbr);
3536 if (updatedFaces == UINT64_MAX)
3539 lwerror(
"Unexpected error: %d faces updated when expecting 1", updatedFaces);
3543 while ( facestoupdate-- )
lwfree(faces[facestoupdate].mbr);
3547 LWDEBUG(1,
"BBOX of changed edge did not change");
3550 LWDEBUG(1,
"all done, cleaning up edges");
3564 if (n == UINT64_MAX)
3570 lwerror(
"SQL/MM Spatial exception - non-existent node");
3576 lwerror(
"SQL/MM Spatial exception - not isolated node");
3591 if ( ! node )
return -1;
3596 lwerror(
"SQL/MM Spatial exception - coincident node");
3603 lwerror(
"SQL/MM Spatial exception - edge crosses node.");
3610 if ( newPointFace == -1 ) {
3617 lwerror(
"Cannot move isolated node across faces");
3642 if ( ! node )
return -1;
3654 lwerror(
"Unexpected error: %d nodes deleted when expecting 1", n);
3691 lwerror(
"SQL/MM Spatial exception - non-existent edge");
3697 lwerror(
"Corrupted topology: more than a single edge have id %"
3702 if ( edge[0].face_left != edge[0].face_right )
3705 lwerror(
"SQL/MM Spatial exception - not isolated edge");
3716 if ((n == UINT64_MAX) || (edge == NULL))
3721 for (i = 0; i < n; ++i)
3723 if (edge[i].edge_id !=
id)
3726 lwerror(
"SQL/MM Spatial exception - not isolated edge");
3734 if (n == UINT64_MAX)
3741 lwerror(
"Unexpected error: %d edges deleted when expecting 1", n);
3748 if ( nid[1] != nid[0] ) {
3755 if (n == UINT64_MAX)
3793 if ( ret == -1 )
return -1;
3801 if ( ret == -1 )
return -1;
3826 if ( ret == -1 )
return -1;
3840 uint64_t i, nedges, nfaces, fields;
3846 int nedge_right = 0;
3853 int fnode_edges = 0;
3855 int lnode_edges = 0;
3864 LWDEBUGF(1,
"lwt_be_getEdgeById returned NULL and set i=%d", i);
3865 if (i == UINT64_MAX)
3878 "Backend coding error: getEdgeById callback returned NULL "
3879 "but numelements output parameter has value %d "
3880 "(expected 0 or 1)",
3893 LWDEBUG(1,
"Updating next_{right,left}_face of ring edges...");
3901 node_ids[nedges++] = edge->
end_node;
3907 if (nedges == UINT64_MAX)
3912 nedge_left = nedge_right = 0;
3913 for ( i=0; i<nedges; ++i )
3916 if ( e->
edge_id == edge_id )
continue;
3954 LWDEBUGF(1,
"updating %d 'next_left' edges", nedge_left);
3967 LWDEBUGF(1,
"updating %d 'next_right' edges", nedge_right);
3978 LWDEBUGF(1,
"releasing %d updateable edges in %p", nedges, upd_edge);
3996 LWDEBUG(1,
"floodface is universe");
4010 if (nfaces == UINT64_MAX)
4017 for ( i=0; i<nfaces; ++i )
4019 if ( faces[i].face_id == edge->
face_left )
4021 if ( ! box1 ) box1 = faces[i].
mbr;
4027 lwerror(
"corrupted topology: more than 1 face have face_id=%"
4032 else if ( faces[i].face_id == edge->
face_right )
4034 if ( ! box2 ) box2 = faces[i].
mbr;
4040 lwerror(
"corrupted topology: more than 1 face have face_id=%"
4050 lwerror(
"Backend coding error: getFaceById returned face "
4059 lwerror(
"corrupted topology: no face have face_id=%"
4068 lwerror(
"corrupted topology: no face have face_id=%"
4089 lwerror(
"Unexpected error: %d faces updated when expecting 1", i);
4108 lwerror(
"Unexpected error: %d faces inserted when expecting 1",
result);
4173 if ( ! fnode_edges )
4215 return modFace ? floodface : newface.
face_id;
4238 uint64_t num_node_edges;
4245 int e2sign, e2freenode;
4249 size_t bufleft = 256;
4257 " with itself, try with another", eid1);
4264 if ((nedges == UINT64_MAX) || (edges == NULL))
4269 for ( i=0; i<nedges; ++i )
4271 if ( edges[i].edge_id == eid1 ) {
4274 lwerror(
"Corrupted topology: multiple edges have id %"
4280 else if ( edges[i].edge_id == eid2 ) {
4283 lwerror(
"Corrupted topology: multiple edges have id %"
4294 "SQL/MM Spatial exception - non-existent edge %" LWTFMT_ELEMID,
4302 "SQL/MM Spatial exception - non-existent edge %" LWTFMT_ELEMID,
4336 if ( commonnode != -1 )
4341 if (num_node_edges == UINT64_MAX)
4347 for (i=0; i<num_node_edges; ++i)
4350 if ( node_edges[i].edge_id == eid1 )
continue;
4351 if ( node_edges[i].edge_id == eid2 )
continue;
4354 if ( bufleft > 0 ) {
4356 ( ptr==buf ?
"" :
"," ), node_edges[i].edge_id);
4357 if (
r >= (
int) bufleft )
4375 if ( commonnode == -1 )
4388 if ( commonnode != -1 )
4393 if (num_node_edges == UINT64_MAX)
4399 for (i=0; i<num_node_edges; ++i)
4402 if ( node_edges[i].edge_id == eid1 )
continue;
4403 if ( node_edges[i].edge_id == eid2 )
continue;
4406 if ( bufleft > 0 ) {
4408 ( ptr==buf ?
"" :
"," ), node_edges[i].edge_id);
4409 if (
r >= (
int) bufleft )
4424 if ( num_node_edges )
lwfree(node_edges);
4428 if ( commonnode == -1 )
4433 lwerror(
"SQL/MM Spatial exception - other edges connected (%s)",
4438 lwerror(
"SQL/MM Spatial exception - non-connected edges");
4515 lwerror(
"Coding error: caseno=%d should never happen", caseno);
4541 lwerror(
"Unexpected error: %d edges updated when expecting 1", i);
4563 lwerror(
"Insertion of split edge failed (no reason)");
4667 eid1, eid2, newedge.
edge_id) )
4673 return modEdge ? commonnode : newedge.
edge_id;
4703 if (num == UINT64_MAX)
4713 lwerror(
"Two or more nodes found");
4738 if (num == UINT64_MAX)
4743 for (i=0; i<num;++i)
4754 " has null geometry", e->
edge_id);
4762 if ( dist > tol )
continue;
4768 lwerror(
"Two or more edges found");
4807 LWDEBUG(1,
"No face properly contains query point,"
4808 " looking for edges");
4815 if (num == UINT64_MAX)
4820 for (i=0; i<num; ++i)
4831 " has null geometry", e->
edge_id);
4839 " is dangling, won't consider it", e->
edge_id);
4847 " is %g (tol=%g)", e->
edge_id, dist, tol);
4850 if ( dist > tol )
continue;
4859 lwerror(
"Two or more faces found");
4863 if (
id &&
id != eface )
4866 lwerror(
"Two or more faces found"
4883 static inline double
4886 double ret = 3.6 * pow(10, - ( 15 - log10(d?d:1.0) ) );
4912 if ( ! gbox )
return 0;
4923 #define _LWT_MINTOLERANCE( topo, geom ) ( \
4924 topo->precision ? topo->precision : _lwt_minTolerance(geom) )
4953 findFace,
int *moved)
4956 double mindist = FLT_MAX;
4977 if (num == UINT64_MAX)
4984 LWDEBUGF(1,
"New point is within %.15g units of %d nodes", tol, num);
4989 for (i=0; i<num; ++i)
4991 sorted[i].
ptr = nodes+i;
4994 ((
LWT_ISO_NODE*)(sorted[i].ptr))->node_id, sorted[i].score);
4998 for (i=0; i<num; ++i)
5007 for ( i=0; i<num; ++i )
5014 if ( dist && dist >= tol )
continue;
5015 if ( !
id || dist < mindist )
5025 if ( moved ) *moved = mindist == 0 ? 0 : 1;
5039 if (num == UINT64_MAX)
5046 LWDEBUGF(1,
"New point is within %.15g units of %d edges", tol, num);
5053 for (i=0; i<num; ++i)
5055 sorted[i].
ptr = edges+i;
5058 ((
LWT_ISO_EDGE*)(sorted[i].ptr))->edge_id, sorted[i].score);
5062 for (j=0, i=0; i<num; ++i)
5064 if ( sorted[i].score == sorted[0].score )
5079 for (i=0; i<num; ++i)
5092 if ( moved ) *moved =
lwgeom_same(prj,pt) ? 0 : 1;
5124 " does not contain projected point to it",
5131 LWDEBUG(1,
"But there's another to check");
5146 LWDEBUGF(1,
"Edge snapped with tolerance %g", snaptol);
5149 #if POSTGIS_DEBUG_LEVEL > 0
5154 LWDEBUGF(1,
"Edge %s snapped became %s", wkt1, wkt2);
5167 LWDEBUGF(1,
"Edge first point is %g %g, "
5168 "snapline first point is %g %g",
5169 p1.
x, p1.
y, p2.
x, p2.
y);
5170 if ( p1.
x != p2.
x || p1.
y != p2.
y )
5172 LWDEBUG(1,
"Snapping moved first point, re-adding it");
5181 #if POSTGIS_DEBUG_LEVEL > 0
5185 LWDEBUGF(1,
"Tweaked snapline became %s", wkt1);
5190 #if POSTGIS_DEBUG_LEVEL > 0
5192 LWDEBUG(1,
"Snapping did not move first point");
5202 lwerror(
"lwt_ChangeEdgeGeom failed");
5207 #if POSTGIS_DEBUG_LEVEL > 0
5213 LWDEBUGF(1,
"Edge %s contains projected point %s", wkt1, wkt2);
5226 lwerror(
"lwt_ModEdgeSplit failed");
5254 if ( moved ) *moved = 0;
5258 lwerror(
"lwt_AddIsoNode failed");
5286 GEOSGeometry *edgeg;
5290 if (num == UINT64_MAX)
5306 for (i=0; i<num; ++i)
5315 GEOSGeom_destroy(edgeg);
5320 equals = GEOSEquals(gg, edgeg);
5321 GEOSGeom_destroy(gg);
5324 GEOSGeom_destroy(edgeg);
5366 GEOSGeom_destroy(edgeg);
5371 GEOSGeom_destroy(edgeg);
5395 int handleFaceSplit,
int *forward )
5398 LWPOINT *start_point, *end_point;
5408 LWDEBUGF(1,
"_lwtAddLineEdge with tolerance %g", tol);
5411 if ( ! start_point )
5413 lwnotice(
"Empty component of noded line");
5418 handleFaceSplit, &mm );
5420 if ( nid[0] == -1 )
return -1;
5427 lwerror(
"could not get last point of line "
5428 "after successfully getting first point !?");
5433 handleFaceSplit, &mm );
5436 if ( nid[1] == -1 )
return -1;
5445 nn = nid[0] == nid[1] ? 1 : 2;
5448 if (nn == UINT64_MAX)
5453 start_point = NULL; end_point = NULL;
5454 for (i=0; i<nn; ++i)
5456 if ( node[i].node_id == nid[0] ) start_point = node[i].
geom;
5457 if ( node[i].node_id == nid[1] ) end_point = node[i].
geom;
5459 if ( ! start_point || ! end_point )
5487 if ( colex->
ngeoms == 0 )
5491 LWDEBUG(1,
"Made-valid snapped edge collapsed");
5503 lwerror(
"lwcollection_extract(LINETYPE) returned a non-line?");
5512 LWDEBUGF(1,
"Made-valid snapped edge collapsed to %s",
5541 LWDEBUGG(1, tmp2,
"Repeated-point removed");
5550 LWDEBUG(1,
"Repeated-point removed edge collapsed");
5571 id =
_lwt_AddEdge( topo, nid[0], nid[1], edge, 0, handleFaceSplit ? 1 : -1 );
5594 if ( ! col->
ngeoms )
return bg;
5596 for (i=0; i<col->
ngeoms; ++i)
5610 int handleFaceSplit)
5620 uint64_t num, numedges = 0, numnodes = 0;
5624 int input_was_closed = 0;
5629 input_was_closed = 1;
5631 LWDEBUGF(1,
"Input line is closed, original point is %g,%g", originalStartPoint.
x, originalStartPoint.
y);
5638 LWDEBUGF(1,
"Working tolerance:%.15g", tol);
5646 LWDEBUGG(1, tmp,
"Repeated-point removed");
5647 }}
else tmp=(
LWGEOM*)line;
5652 if ( ! noded )
return NULL;
5659 LWDEBUGF(1,
"BOX expanded by %g is %.15g %.15g, %.15g %.15g",
5663 int nearbyindex = 0;
5664 int nearbycount = 0;
5668 if (numedges == UINT64_MAX)
5674 LWDEBUGF(1,
"Line has %d points, its bbox intersects %d edges bboxes",
5679 nearbycount += numedges;
5681 for (i=0; i<numedges; ++i)
5689 if ( dist && dist >= tol )
continue;
5690 nearby[nearbyindex++] = g;
5692 LWDEBUGF(1,
"Found %d edges closer than tolerance (%g)", nearbyindex, tol);
5694 int nearbyedgecount = nearbyindex;
5701 if (numnodes == UINT64_MAX)
5707 LWDEBUGF(1,
"Line bbox intersects %d nodes bboxes", numnodes);
5711 nearbycount = nearbyedgecount + numnodes;
5718 for (i=0; i<numnodes; ++i)
5725 if ( dist && dist >= tol )
5727 LWDEBUGF(1,
"Node %d is %g units away, we tolerate only %g", n->
node_id, dist, tol);
5730 nearby[nearbyindex++] = g;
5733 LWDEBUGF(1,
"Found %d isolated nodes closer than tolerance (%g)", nn, tol);
5735 int nearbynodecount = nearbyindex - nearbyedgecount;
5736 nearbycount = nearbyindex;
5738 LWDEBUGF(1,
"Number of nearby elements is %d", nearbycount);
5747 NULL, nearbycount, nearby);
5750 LWDEBUGG(1, elems,
"Collected nearby elements");
5755 LWDEBUGG(1, noded,
"Elements-snapped");
5756 if ( input_was_closed )
5763 LWDEBUGF(1,
"Closed input line start point after snap %g,%g", originalStartPoint.
x, originalStartPoint.
y);
5778 LWDEBUGG(1, noded,
"Unary-unioned");
5782 if ( nearbyedgecount )
5788 LWDEBUGF(1,
"Line intersects %d edges", nearbyedgecount);
5791 NULL, nearbyedgecount, nearby);
5793 LWDEBUGG(1, iedges,
"Collected edges");
5795 LWDEBUGF(1,
"Diffing noded, with srid=%d "
5796 "and interesecting edges, with srid=%d",
5801 LWDEBUGF(1,
"Intersecting noded, with srid=%d "
5802 "and interesecting edges, with srid=%d",
5822 LWDEBUG(1,
"Linemerging intersection");
5832 LWDEBUG(1,
"Unioning difference and (linemerged) intersection");
5834 LWDEBUGG(1, noded,
"Diff-Xset Unioned");
5841 if ( input_was_closed )
5853 LWDEBUGG(1, xset,
"Linemerged intersected input is not a line anymore");
5866 if ( nearbyedgecount )
5868 nearbycount += nearbyedgecount * 2;
5870 for (
int i=0; i<nearbyedgecount; i++)
5882 if ( nearbynodecount )
5885 NULL, nearbynodecount,
5886 nearby + nearbyedgecount);
5898 LWDEBUG(1,
"Freeing up nearby elements");
5901 if ( nearby )
lwfree(nearby);
5905 LWDEBUGG(1, noded,
"Finally-noded");
5911 LWDEBUG(1,
"Noded line was a collection");
5917 LWDEBUG(1,
"Noded line was a single geom");
5918 geomsbuf[0] = noded;
5923 LWDEBUGF(1,
"Line was split into %d edges", ngeoms);
5931 for ( i=0; i<ngeoms; ++i )
5937 #if POSTGIS_DEBUG_LEVEL > 0
5941 LWDEBUGF(1,
"Component %d of split line is: %s", i, wkt1);
5956 LWDEBUGF(1,
"Component %d of split line collapsed", i);
5961 i, forward ?
"forward" :
"backward",
id);
5962 ids[num++] = forward ? id : -id;
5965 LWDEBUGG(1, noded,
"Noded before free");
5993 uint64_t nfacesinbox;
5997 const GEOSPreparedGeometry *ppoly;
5998 GEOSGeometry *polyg;
6002 LWDEBUGF(1,
"Working tolerance:%.15g", tol);
6005 for ( i=0; i<poly->
nrings; ++i )
6018 lwerror(
"Error adding ring %d of polygon", i);
6033 if (nfacesinbox == UINT64_MAX)
6050 ppoly = GEOSPrepare(polyg);
6052 for ( j=0; j<nfacesinbox; ++j )
6056 GEOSGeometry *fgg, *sp;
6064 GEOSPreparedGeom_destroy(ppoly);
6065 GEOSGeom_destroy(polyg);
6076 GEOSPreparedGeom_destroy(ppoly);
6077 GEOSGeom_destroy(polyg);
6082 sp = GEOSPointOnSurface(fgg);
6083 GEOSGeom_destroy(fgg);
6086 GEOSPreparedGeom_destroy(ppoly);
6087 GEOSGeom_destroy(polyg);
6092 covers = GEOSPreparedCovers( ppoly, sp );
6093 GEOSGeom_destroy(sp);
6096 GEOSPreparedGeom_destroy(ppoly);
6097 GEOSGeom_destroy(polyg);
6110 GEOSPreparedGeom_destroy(ppoly);
6111 GEOSGeom_destroy(polyg);
6151 void *match = bsearch( &key, tab->
edges, tab->
size,
6179 #define LWT_EDGERING_INIT(a) { \
6181 (a)->capacity = 1; \
6182 (a)->elems = lwalloc(sizeof(LWT_EDGERING_ELEM *) * (a)->capacity); \
6187 #define LWT_EDGERING_PUSH(a, r) { \
6188 if ( (a)->size + 1 > (a)->capacity ) { \
6189 (a)->capacity *= 2; \
6190 (a)->elems = lwrealloc((a)->elems, sizeof(LWT_EDGERING_ELEM *) * (a)->capacity); \
6193 (a)->elems[(a)->size++] = (r); \
6196 #define LWT_EDGERING_CLEAN(a) { \
6197 int i; for (i=0; i<(a)->size; ++i) { \
6198 if ( (a)->elems[i] ) { \
6200 lwfree((a)->elems[i]); \
6203 if ( (a)->elems ) { lwfree((a)->elems); (a)->elems = NULL; } \
6205 (a)->capacity = 0; \
6206 if ( (a)->env ) { lwfree((a)->env); (a)->env = NULL; } \
6207 if ( (a)->genv ) { GEOSGeom_destroy((a)->genv); (a)->genv = NULL; } \
6218 #define LWT_EDGERING_ARRAY_INIT(a) { \
6220 (a)->capacity = 1; \
6221 (a)->rings = lwalloc(sizeof(LWT_EDGERING *) * (a)->capacity); \
6227 #define LWT_EDGERING_ARRAY_CLEAN(a) { \
6228 int j; for (j=0; j<(a)->size; ++j) { \
6229 LWT_EDGERING_CLEAN((a)->rings[j]); \
6231 if ( (a)->capacity ) lwfree((a)->rings); \
6232 if ( (a)->tree ) { \
6233 GEOSSTRtree_destroy( (a)->tree ); \
6238 #define LWT_EDGERING_ARRAY_PUSH(a, r) { \
6239 if ( (a)->size + 1 > (a)->capacity ) { \
6240 (a)->capacity *= 2; \
6241 (a)->rings = lwrealloc((a)->rings, sizeof(LWT_EDGERING *) * (a)->capacity); \
6243 (a)->rings[(a)->size++] = (r); \
6259 if ( ! el )
return 0;
6271 if ( it->
curidx < 0 ) tonext = 1;
6276 LWDEBUG(3,
"iterator moving to next element");
6307 #define LWT_HOLES_FACE_PLACEHOLDER INT32_MIN
6313 from < etab->size &&
6317 return from < etab->
size ? from : -1;
6325 uint64_t nelems = 1;
6329 if (nelems == UINT64_MAX)
6351 int forward_edges_count = 0;
6353 int backward_edges_count = 0;
6359 forward_edges_count = 0;
6361 backward_edges_count = 0;
6363 for ( i=0; i<ring->
size; ++i )
6370 LWDEBUGF(3,
"Forward edge %d is %d", forward_edges_count,
id);
6371 forward_edges[forward_edges_count].
edge_id = id;
6372 forward_edges[forward_edges_count++].
face_left = face;
6377 LWDEBUGF(3,
"Backward edge %d is %d", forward_edges_count,
id);
6378 backward_edges[backward_edges_count].
edge_id = id;
6379 backward_edges[backward_edges_count++].
face_right = face;
6385 if ( forward_edges_count )
6388 forward_edges_count,
6393 lwfree( backward_edges );
6397 if ( ret != forward_edges_count )
6400 lwfree( backward_edges );
6401 lwerror(
"Unexpected error: %d edges updated when expecting %d (forward)",
6402 ret, forward_edges_count);
6408 if ( backward_edges_count )
6411 backward_edges_count,
6416 lwfree( backward_edges );
6420 if ( ret != backward_edges_count )
6423 lwfree( backward_edges );
6424 lwerror(
"Unexpected error: %d edges updated when expecting %d (backward)",
6425 ret, backward_edges_count);
6431 lwfree( backward_edges );
6454 LWDEBUGF(2,
"Building rings for edge %d (side %d)",
cur->edge_id, side);
6461 elem->
left = ( curside == 1 );
6468 next = elem->
left ?
cur->next_left :
cur->next_right;
6470 LWDEBUGF(3,
" next edge is %d", next);
6472 if ( next > 0 ) curside = 1;
6473 else { curside = -1; next = -next; }
6477 lwerror(
"Could not find edge with id %d", next);
6480 }
while (
cur != edge || curside != side);
6494 double x0,
x, y1, y2;
6499 LWDEBUG(2,
"_lwt_EdgeRingSignedArea");
6524 LWDEBUGF(2,
"_lwt_EdgeRingIsCCW, ring has %d elems", ring->
size);
6527 LWDEBUGF(2,
"_lwt_EdgeRingIsCCW, signed area is %g", sa);
6529 if ( sa >= 0 )
return 0;
6552 ((v1.
y <= p->
y) && (v2.
y > p->
y))
6554 || ((v1.
y > p->
y) && (v2.
y <= p->
y))
6558 vt = (double)(p->
y - v1.
y) / (v2.
y - v1.
y);
6561 if (p->
x < v1.
x + vt * (v2.
x - v1.
x))
6570 LWDEBUGF(3,
"_lwt_EdgeRingCrossingCount returning %d", cn);
6573 if ( memcmp(&v1, &v0,
sizeof(
POINT2D)) )
6575 lwerror(
"_lwt_EdgeRingCrossingCount: V[n] != V[0] (%g %g != %g %g)",
6576 v1.
x, v1.
y, v0.
x, v0.
y);
6603 LWDEBUGF(2,
"Computing GBOX for ring %p", ring);
6604 for (i=0; i<ring->
size; ++i)
6665 LWDEBUG(2,
"Ring built, calling EdgeRingIsCCW");
6690 lwerror(
"Unexpected error: %d faces inserted when expecting 1", ret);
6694 *registered = newface.
face_id;
6701 lwerror(
"Errors updating edgering side face: %s",
6710 *registered = placeholder_faceid;
6731 const GBOX *minenv = NULL;
6733 const GBOX *testbox;
6734 GEOSGeometry *ghole;
6752 if ( ! shells->
tree )
6755 LWDEBUG(1,
"Building STRtree");
6757 if (shells->
tree == NULL)
6762 for (i=0; i<shells->
size; ++i)
6766 LWDEBUGF(2,
"GBOX of shell %p for edge %d is %g %g,%g %g",
6772 pt.
x = shellbox->
xmin;
6773 pt.
y = shellbox->
ymin;
6775 pt.
x = shellbox->
xmax;
6776 pt.
y = shellbox->
ymax;
6783 GEOSSTRtree_insert(shells->
tree, sring->
genv, sring);
6785 LWDEBUG(1,
"STRtree build completed");
6791 LWDEBUGF(1,
"Found %d candidate shells containing first point of ring's originating edge %d",
6796 for (i=0; i<candidates.
size; ++i)
6804 LWDEBUGF(1,
"Shell %d is on other side of ring",
6812 LWDEBUGF(1,
"Bbox of shell %d equals that of hole ring",
6820 LWDEBUGF(1,
"Bbox of shell %d does not contain bbox of ring point",
6829 LWDEBUGF(2,
"Bbox of shell %d (face %d) not contained by bbox "
6830 "of last shell found to contain the point",
6841 LWDEBUGF(1,
"Shell %d contains hole of edge %d",
6848 if ( foundInFace == -1 ) foundInFace = 0;
6850 candidates.
size = 0;
6853 GEOSGeom_destroy(ghole);
6868 uint64_t nelems = 1;
6874 if (nelems == UINT64_MAX)
6921 if ( numfaces != 0 ) {
6922 if ( numfaces > 0 ) {
6924 lwerror(
"Polygonize: face table is not empty.");
6932 if ( ! edgetable.
edges ) {
6933 if (edgetable.
size == 0) {
6945 for (i=0; i<edgetable.
size; ++i)
6953 edge = &(edgetable.
edges[i]);
6957 LWDEBUGF(1,
"Next face-missing edge has id:%d, face_left:%d, face_right:%d",
6962 &holes, &shells, &newface);
6964 LWDEBUGF(1,
"New face on the left of edge %d is %d",
6971 &holes, &shells, &newface);
6973 LWDEBUGF(1,
"New face on the right of edge %d is %d",
6984 lwerror(
"Errors fetching or registering face-missing edges: %s",
6994 for (i=0; i<holes.
size; ++i)
7001 if ( containing_face == -1 )
7006 lwerror(
"Errors finding face containing ring: %s",
7016 lwerror(
"Errors updating edgering side face: %s",
7022 LWDEBUG(1,
"All holes assigned, cleaning up");
7038 uint64_t numedges, i;
7040 const POINT2D *closestPointOnEdge = NULL;
7041 uint32_t closestSegmentIndex;
7042 int closestSegmentSide;
7043 uint32_t closestPointVertex;
7044 const POINT2D *closestSegmentP0, *closestSegmentP1;
7047 int containingFace = -1;
7057 if (numedges == UINT64_MAX)
7079 closestSegmentP0->
x,
7080 closestSegmentP0->
y,
7081 closestSegmentP1->
x,
7099 const POINT2D *p = queryPoint;
7100 const POINT2D *A = closestSegmentP0;
7101 const POINT2D *B = closestSegmentP1;
7102 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) );
7105 closestPointOnEdge = A;
7106 closestPointVertex = closestSegmentIndex;
7107 if ( closestSegmentIndex == 0 )
7114 closestPointOnEdge = B;
7115 closestPointVertex = closestSegmentIndex + 1;
7118 closestNode = closestEdge->
end_node;
7126 if ( closestNode != 0 )
7128 LWDEBUGF(1,
"Closest point is node %d", closestNode);
7131 LWDEBUGF(1,
"Query point is node %d", closestNode);
7142 lwerror(
"Two or more faces found");
7145 containingFace = closestEdge->
face_left;
7150 if (numedges == UINT64_MAX)
7157 for (i=0; i<numedges; ++i)
7159 if ( edges[i].face_left != containingFace ||
7160 edges[i].face_right != containingFace )
7164 lwerror(
"Two or more faces found");
7170 lwerror(
"Unexpected backend return: getEdgeByNode(%d) returns no edges when we previously found edge %d ending on that node",
7171 closestNode, closestEdge->
edge_id);
7176 LWDEBUGF(1,
"lwt_be_getEdgeByNode returned %d edges", numedges);
7179 return containingFace;
7187 lwerror(
"error computing azimuth of query point [%.15g %.15g,%.15g %.15g]",
7188 closestPointOnEdge->
x, closestPointOnEdge->
y,
7189 queryPoint->
x, queryPoint->
y);
7198 lwerror(
"Unexpected backend return: _lwt_FindAdjacentEdges(%d) found no edges when we previously found edge %d ending on that node",
7199 closestNode, closestEdge->
edge_id);
7209 LWDEBUG(1,
"Closest point is NOT a node");
7215 containingFace = closestEdge->
face_left;
7217 return containingFace;
7224 lwerror(
"Two or more faces found");
7232 LWDEBUGF(1,
"Closest point is vertex %d of closest segment", closestPointVertex);
7243 uint32_t prevVertexIndex = closestPointVertex > 0 ?
7244 closestPointVertex - 1u :
7252 LWDEBUGF(1,
"Previous vertex %u is POINT(%.15g %.15g)",
7258 uint32_t nextVertexIndex = closestPointVertex == closestEdge->
geom->
points->
npoints - 1u ?
7260 closestPointVertex + 1u;
7267 LWDEBUGF(1,
"Next vertex %u is POINT(%.15g %.15g)",
7278 if ( !
azimuth_pt_pt(closestPointOnEdge, prevVertex, &azS0)) {
7279 lwerror(
"error computing azimuth of segment to closest point [%.15g %.15g,%.15g %.15g]",
7280 closestPointOnEdge->
x, closestPointOnEdge->
y,
7281 prevVertex->
x, prevVertex->
y);
7285 if ( !
azimuth_pt_pt(closestPointOnEdge, nextVertex, &azS1)) {
7286 lwerror(
"error computing azimuth of segment from closest point [%.15g %.15g,%.15g %.15g]",
7287 closestPointOnEdge->
x, closestPointOnEdge->
y,
7288 nextVertex->
x, nextVertex->
y);
7292 if ( !
azimuth_pt_pt(closestPointOnEdge, queryPoint, &azSL) ) {
7293 lwerror(
"error computing azimuth of queryPoint [%.15g %.15g,%.15g %.15g]",
7294 closestPointOnEdge->
x, closestPointOnEdge->
y,
7295 queryPoint->
x, queryPoint->
y);
7300 double angle_S0_S1 = azS1 - azS0;
7301 if ( angle_S0_S1 < 0 ) angle_S0_S1 += 2 * M_PI;
7303 double angle_S0_SL = azSL - azS0;
7304 if ( angle_S0_SL < 0 ) angle_S0_SL += 2 * M_PI;
7306 LWDEBUGF(1,
"Azimuths from closest (vertex) point: P:%g, N:%g (+%g), Q:%g (+%g)",
7311 if ( angle_S0_SL < angle_S0_S1 )
7314 containingFace = closestEdge->
face_left;
7327 LWDEBUGF(1,
"Closest point is internal to closest segment, calling lw_segment_side((%g,%g),(%g,%g),(%g,%g)",
7328 closestSegmentP0->
x,
7329 closestSegmentP0->
y,
7330 closestSegmentP1->
x,
7331 closestSegmentP1->
y,
7336 closestSegmentSide =
lw_segment_side(closestSegmentP0, closestSegmentP1, queryPoint);
7337 LWDEBUGF(1,
"Side of closest segment query point falls on: %d", closestSegmentSide);
7339 if ( closestSegmentSide == -1 )
7341 containingFace = closestEdge->
face_left;
7343 else if ( closestSegmentSide == 1 )
7349 lwerror(
"Unexpected collinearity reported from lw_segment_side");
7357 return containingFace;
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)
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)
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)
#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 1 if the point is inside the POINTARRAY, -1 if it is outside, and 0 if it is on the boundary.
int ptarray_isccw(const POINTARRAY *pa)
int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
lw_segment_side()
#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 LWDEBUGGF(level, geom, fmt,...)
#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)
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 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)
static int lwt_be_checkTopoGeomRemIsoEdge(LWT_TOPOLOGY *topo, LWT_ELEMID edge_id)
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)
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)
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)
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)
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 LWT_ELEMID _lwt_GetEqualEdge(LWT_TOPOLOGY *topo, LWLINE *edge, int *forward)
static void _lwt_RotateElemidArray(LWT_ELEMID *ary, int from, int to, int rotidx)
#define LWT_EDGERING_ARRAY_INIT(a)
struct LWT_EDGERING_ELEM_T LWT_EDGERING_ELEM
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)
#define LWT_EDGERING_PUSH(a, r)
static LWT_ELEMID _lwt_AddFaceSplit(LWT_TOPOLOGY *topo, LWT_ELEMID sedge, LWT_ELEMID face, int mbr_only)
static int lwt_be_checkTopoGeomRemIsoNode(LWT_TOPOLOGY *topo, LWT_ELEMID node_id)
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)
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)
#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)
#define P2D_SAME_STRICT(a, b)
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_ISO_EDGE * lwt_be_getEdgeWithinDistance2D(LWT_TOPOLOGY *topo, const LWPOINT *pt, double dist, uint64_t *numelems, int fields, int64_t limit)
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)
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