## jts-topo-suite-user

 [Jts-topo-suite-user] Determining intersection points (was: Problem computing line intersections) From: Mark Coletti - 2012-11-20 01:52:10 Attachments: Message as HTML ```On Mon, Nov 19, 2012 at 6:43 PM, Martin Davis wrote: > > Another option is to simply using Geometry.union() (Unary union). When > executed on a collection of LineStrings, this has the effect of noding them > and dissolving any common linework. Be aware however that currently JTS > overlay operations have a small robustness issue, so that if the input > linework contains line segments that are almost coincident they may not in > fact be noded quite correctly. > > Duly noted. Thanks! I've a follow up question: Given a set of line strings that have been properly noded, what is the best approach to finding the points corresponding to their intersections? One brute force approach would be to pull out their coordinate strings and note the points that occur more than once. That works, but my intuition is that there may be a slick "JTS Way" that would give me those points in one go. If so, how would I use JTS to give me those intersection vertices? Here's an example of the brute force approach: // brokenUpLines contains noded line strings Coordinate [] vertices = brokenUpLines.getCoordinates(); Set intersectionVertices = new HashSet(); Set visitedCoordinates = new HashSet(); for (int i = 0; i < vertices.length; i++) { if (! visitedCoordinates.contains(vertices[i])) { visitedCoordinates.add(vertices[i]); } else // intersection if vertex visited more than once { intersectionVertices.add(vertices[i]); } } for (Coordinate c : intersectionVertices.toArray(new Coordinate[0])) { System.out.println(c); } Cheers! Mark ```
 Re: [Jts-topo-suite-user] Determining intersection points (was: Problem computing line intersections) From: Martin Davis - 2012-11-21 18:38:07 Attachments: Message as HTML ```One note about the "brute-force" method you present is that if the set of lines is fully noded, you don't need to check *all* vertices, just the endpoints of the LineString components. But yes, in fact there is an alternative approach in JTS, which I think counts as slicker. This way uses to ability of JTS to define different BoundardNodeRules, and to use them in the BoundaryOp class. One BoundaryNodeRule is called the MULTIVALENT_ENDPOINT_BOUNDARY_RULE. Under this rule, only endpoints which have valence (occurrence count) > 1 are considered to be on the boundary. So you can invoke the BoundaryOp using this rule, and the computed boundary will contain only the endpoints which occur more than once. If the set of lines is represented as a MultiLineString, this can be done in one line of code: Geometry intPoints = (new BoundaryOp(lines, BoundaryNodeRule.MULTIVALENT_ENDPOINT_BOUNDARY_RULE)).getBoundary(); Another good question for the FAQ! Keep 'em coming... On Mon, Nov 19, 2012 at 5:52 PM, Mark Coletti wrote: > > > I've a follow up question: Given a set of line strings that have been > properly noded, what is the best approach to finding the points > corresponding to their intersections? One brute force approach would be to > pull out their coordinate strings and note the points that occur more than > once. That works, but my intuition is that there may be a slick "JTS Way" > that would give me those points in one go. If so, how would I use JTS to > give me those intersection vertices? > > > ```