use of com.revolsys.geometry.planargraph.DirectedEdge in project com.revolsys.open by revolsys.
the class LineMergeGraph method addEdge.
/**
* Adds an Edge, DirectedEdges, and Nodes for the given LineString representation
* of an edge.
* Empty lines or lines with all coordinates equal are not added.
*
* @param line the linestring to add to the graph
*/
public void addEdge(final LineString line) {
if (line.isEmpty()) {
return;
}
final LineString points = line.removeDuplicatePoints();
final int vertexCount = points.getVertexCount();
if (vertexCount > 1) {
final Point fromPoint = points.getPoint(0);
final Point toPoint = points.getPoint(vertexCount - 1);
final Node startNode = getNode(fromPoint);
final Node endNode = getNode(toPoint);
final DirectedEdge directedEdge0 = new LineMergeDirectedEdge(startNode, endNode, points.getPoint(1).newPoint(), true);
final DirectedEdge directedEdge1 = new LineMergeDirectedEdge(endNode, startNode, points.getPoint(vertexCount - 2).newPoint(), false);
final Edge edge = new LineMergeEdge(line);
edge.setDirectedEdges(directedEdge0, directedEdge1);
add(edge);
}
}
use of com.revolsys.geometry.planargraph.DirectedEdge in project com.revolsys.open by revolsys.
the class LineSequencer method orient.
/**
* Computes a version of the sequence which is optimally
* oriented relative to the underlying geometry.
* <p>
* Heuristics used are:
* <ul>
* <li>If the path has a degree-1 node which is the start
* node of an linestring, use that node as the start of the sequence
* <li>If the path has a degree-1 node which is the end
* node of an linestring, use that node as the end of the sequence
* <li>If the sequence has no degree-1 nodes, use any node as the start
* (NOTE: in this case could orient the sequence according to the majority of the
* linestring orientations)
* </ul>
*
* @param seq a List of DirectedEdges
* @return a List of DirectedEdges oriented appropriately
*/
private List orient(final List seq) {
final DirectedEdge startEdge = (DirectedEdge) seq.get(0);
final DirectedEdge endEdge = (DirectedEdge) seq.get(seq.size() - 1);
final Node startNode = startEdge.getFromNode();
final Node endNode = endEdge.getToNode();
boolean flipSeq = false;
final boolean hasDegree1Node = startNode.getDegree() == 1 || endNode.getDegree() == 1;
if (hasDegree1Node) {
boolean hasObviousStartNode = false;
// (ie. if both are good starts, pick the actual start
if (endEdge.getToNode().getDegree() == 1 && endEdge.getEdgeDirection() == false) {
hasObviousStartNode = true;
flipSeq = true;
}
if (startEdge.getFromNode().getDegree() == 1 && startEdge.getEdgeDirection() == true) {
hasObviousStartNode = true;
flipSeq = false;
}
// since there is no obvious start node, use any node of degree 1
if (!hasObviousStartNode) {
// check if the start node should actually be the end node
if (startEdge.getFromNode().getDegree() == 1) {
flipSeq = true;
// if the end node is of degree 1, it is properly the end node
}
}
}
if (flipSeq) {
return reverse(seq);
}
return seq;
}
use of com.revolsys.geometry.planargraph.DirectedEdge in project com.revolsys.open by revolsys.
the class LineSequencer method buildSequencedGeometry.
/**
* Builds a geometry ({@link Lineal} )
* representing the sequence.
*
* @param sequences a List of Lists of DirectedEdges with
* LineMergeEdges as their parent edges.
* @return the sequenced geometry, or <code>null</code> if no sequence exists
*/
private Geometry buildSequencedGeometry(final List sequences) {
final List lines = new ArrayList();
for (final Iterator i1 = sequences.iterator(); i1.hasNext(); ) {
final List seq = (List) i1.next();
for (final Iterator i2 = seq.iterator(); i2.hasNext(); ) {
final DirectedEdge de = (DirectedEdge) i2.next();
final LineMergeEdge e = (LineMergeEdge) de.getEdge();
final LineString line = e.getLine();
LineString lineToAdd = line;
if (!de.getEdgeDirection() && !line.isClosed()) {
lineToAdd = line.reverse();
}
lines.add(lineToAdd);
}
}
if (lines.size() == 0) {
return this.factory.lineString();
} else {
return this.factory.buildGeometry(lines);
}
}
use of com.revolsys.geometry.planargraph.DirectedEdge in project com.revolsys.open by revolsys.
the class LineSequencer method reverse.
/**
* Reverse the sequence.
* This requires reversing the order of the dirEdges, and flipping
* each dirEdge as well
*
* @param seq a List of DirectedEdges, in sequential order
* @return the reversed sequence
*/
private List reverse(final List seq) {
final LinkedList newSeq = new LinkedList();
for (final Iterator i = seq.iterator(); i.hasNext(); ) {
final DirectedEdge de = (DirectedEdge) i.next();
newSeq.addFirst(de.getSym());
}
return newSeq;
}
use of com.revolsys.geometry.planargraph.DirectedEdge in project com.revolsys.open by revolsys.
the class LineSequencer method addReverseSubpath.
private void addReverseSubpath(DirectedEdge de, final ListIterator lit, final boolean expectedClosed) {
// trace an unvisited path *backwards* from this de
final Node endNode = de.getToNode();
Node fromNode = null;
while (true) {
lit.add(de.getSym());
de.getEdge().setVisited(true);
fromNode = de.getFromNode();
final DirectedEdge unvisitedOutDE = findUnvisitedBestOrientedDE(fromNode);
// this must terminate, since we are continually marking edges as visited
if (unvisitedOutDE == null) {
break;
}
de = unvisitedOutDE.getSym();
}
if (expectedClosed) {
// the path should end at the toNode of this de, otherwise we have an
// error
Assert.isTrue(fromNode == endNode, "path not contiguous");
}
}
Aggregations