Repository navigation
Description
Overview
I am creating ConformingDelaunayTriangulators from coordinates with lat/lon data. In less than 1% of cases triangulation fails, throwing a LocateFailureException during the formInitialDelaunay call. In the example case I provide below, I find that line 304 of QuadEdgeSubdivision.java always evaluates to true for all edges from some origin. The while loop iterates through these same 3 points until it hits the iter > maxIter case and throws.
Visual inspection doesn't reveal to me anything odd about the set of coordinates. Precision reduction to 4 decimal places does not resolve the issue. Increasing the tolerance can resolve. I have other cases that need as much as 102 meters of tolerance to resolve.
Visual Example
Purple are the incremental triangulation at the start of this site insertion. The orange point is the new site. Green are the segments traversed successfully. Red are the 3 segments that, once reached, are looped through until maxIters is reached.
Example Code
I can generate more cases of sites.csv and tolerance values that reproduce the same issue.
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.triangulate.ConformingDelaunayTriangulator;
import org.locationtech.jts.triangulate.ConstraintVertex;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
class Triangulation {
public static void main(String[] args) throws IOException {
String filePath = "/path/to/sites.csv";
List<Coordinate> coordinates;
try (java.util.stream.Stream<String> lines = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
coordinates = lines.map(line -> {
String[] parts = line.split(",");
return new Coordinate(
Double.parseDouble(parts[0].trim()),
Double.parseDouble(parts[1].trim()),
Double.parseDouble(parts[2].trim())
);
}).toList();
}
double tolerance = 8.993216059187306E-5; // 10 meters in degrees
ConformingDelaunayTriangulator conformingDelaunayTriangulator = new ConformingDelaunayTriangulator(
coordinates.stream().map(ConstraintVertex::new).collect(Collectors.toList()),
tolerance
);
conformingDelaunayTriangulator.setConstraints(new ArrayList<>(), new ArrayList<>());
conformingDelaunayTriangulator.formInitialDelaunay();
}
}
Activity
dr-jts commentedon Feb 22, 2025
Analysis
The cause of this is 3 input points which are almost collinear:
The middle point is about 3e-8 units off the line joining the other two points. Perhaps these were originally generated by densifying a line?
For some reason this causes an invalid triangulation to be created when the middle site is inserted (visible in the image below, with Magnify Topology showing that the constructed triangles overlap, which they should not. Once the triangulation has entered an invalid state, searching for triangles containing other sites is likely to fail.
Solution?
Perhaps the situation of an invalid triangulation could be detected after site insertion, and somehow resolved. However, this problem has occurred before (hence the error detection). It's been a long time since I worked on this code, and I can't remember if I this approach before. It may be that correcting the triangulation is either infeasible or too time-consuming.
Workaround
A workaround is to perturb the sites slightly to avoid nearly collinear sites. One way to do this is to reduce the precision of the sites. I reduced to 5 decimal places and this allowed the CDT to be constructed successfully. Another option is to perturb each point by a small random offset. Once the triangulation is constructed, the topology could be mapped back to the original points, if desired. This procedure could be automated and invoked on failure.
dr-jts commentedon Feb 22, 2025
Another observation: Using the
ConformingDelaunayTriangulationBuilderallows this case to complete. The difference is that theConformingDelaunayTriangulationBuildercallsDelaunayTriangulationBuilder.extractUniqueCoordinates(here). This sorts the input site coordinates, which changes the order they are inserted into the triangulation. This allows the triangulation to complete.So another possible workaround is to randomize the order of the input points if a failure is encountered (e.g. using
Collections.shuffle). This worked for me.dr-jts commentedon Feb 24, 2025
@james-willis will you be able to try one of the proposed fixes on your failing datasets? (I suggest the input order randomization).
I'm curious about how you find so many failing datasets. Are they just different extracts from the underlying map?
james-willis commentedon Feb 24, 2025
I'm going to try some of these right now. I appreciate your help.
I'm building an isochrone generation system that sits on top of Apache Spark/Sedona and an open source library using a road network graph built from Overture Map Foundation data. So I'm generating 10s/100s of thousands of isochrones to test the performance of the feature I've built.
If I find one of your fixes is improving the robustness of the isochrone generation from this open source library I'll raise a PR against it.
james-willis commentedon Feb 24, 2025
Shuffling seems to have solved all the issues in my test case which is generating 10 minute isochrones for all all 351524 coffee shops in the Overture map places dataset.
dr-jts commentedon Feb 25, 2025
Excellent!
This heuristic could be provided in
ConformingDelaunayBuilderandDelaunayTriangulationBuilder(controlled by a flag and possibly a parameter controlling number of retry attempts.dr-jts commentedon Feb 25, 2025
@james-willis some questions to help understanding the context:
ConformingDelaunayBuilder, or the code you provided above? As I noted, CDB sorts the input points, which seems to help with robustness.ConformingDelaunayBuilder, does this mean there are cases which failed even with the initial sort?DelaunayTriangulationBuilderjames-willis commentedon Feb 25, 2025
I was using the JTSTriangulator.triangulate method from graphhopper which does not use the
ConformingDelaunayBuilder. The code above comes from the original JTSTriangulator implemenation linked. When I confirmed shuffling fixes, I forked that class with a version that adds code to shuffle if the firstformInitialDelaunayfails.Let me test this solution in my distributed environment. If this solves I can contribute it back to graphhopper
The JTSTriangulator does not set any so they are not used. I will also make this change to the JTSTriangulator class.
james-willis commentedon Feb 25, 2025
Using the DelaunayTriangulationBuilder fixed the issue with no shuffling. I raised a PR against graphhopper to implement the switch to
DelaunayTriangulationBuilderin their JTSTriangulator class.dr-jts commentedon Feb 25, 2025
Good to hear. I think that the initial sorting of input points prevents runs of nearly collinear points, which is probably the cause of the robustness failure. No proof of this, however. But there haven't been reports of this issue for a very long time, if ever, which is reassuring.