A common task is to iterate through the segments of a LineString. I was
thinking it might be useful to have a LineSegment visitor interface that
could be implemented to perform some action on each line segment. There
would be another method that would iterate through the segments of a
line and invoke the interface for each segment.
The interface would look something like this. It has a boolean return
type which if false was returned the visiting would terminate at that
point. This would allow for algorithms that want to quit after finding
the first match of something.
public interface LineSegmentVisitor {
boolean visit(LineSegment segment);
}
The utility method would look something like. This could either be
direct on a coordinate sequence or the line string class or separately
in a utility class (as shown here)
public static void visitLineSegments(final CoordinateSequence coords,
final LineSegmentVisitor visitor) {
Coordinate previousCoordinate = coords.getCoordinate(0);
for (int i = 1; i < coords.size(); i++) {
Coordinate coordinate = coords.getCoordinate(i);
LineSegment segment = new LineSegment(previousCoordinate, coordinate);
if (!visitor.visit(segment)) {
return;
}
}
}
This is similar to to the visiting of items in a Quadtree.
Also you could have other kinds of visitors for coordinates, for
geometries (in a multi part geometry) etc.
If Java 5 was used you could have a generic visitor method
public interface Visitor<T> {
boolean visit(T item);
}
And then the method above would be
public static void visitLineSegments(final CoordinateSequence coords,
final Visitor<T extends LineSegment> visitor) {
Paul
_______________________________________________
jts-devel mailing list
[email protected]
http://lists.refractions.net/mailman/listinfo/jts-devel