use of com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator in project com.revolsys.open by revolsys.
the class LineStringGraph method getLines.
public List<LineString> getLines() {
removeDuplicateEdges();
final EdgeAttributeValueComparator<LineSegment> comparator = new EdgeAttributeValueComparator<>("INDEX");
final List<LineString> lines = new ArrayList<>();
final List<Point> points = new ArrayList<>();
final Consumer<Edge<LineSegment>> action = new Consumer<Edge<LineSegment>>() {
private Node<LineSegment> previousNode = null;
@Override
public void accept(final Edge<LineSegment> edge) {
final LineSegment lineSegment = edge.getObject();
if (lineSegment.getLength() > 0) {
final Node<LineSegment> fromNode = edge.getFromNode();
final Node<LineSegment> toNode = edge.getToNode();
if (this.previousNode == null) {
points.add(lineSegment.getPoint(0));
points.add(lineSegment.getPoint(1));
} else if (fromNode == this.previousNode) {
if (edge.getLength() > 0) {
points.add(toNode);
}
} else {
if (points.size() > 1) {
final LineString line = LineStringGraph.this.geometryFactory.lineString(points);
lines.add(line);
}
points.clear();
;
points.add(lineSegment.getPoint(0));
points.add(lineSegment.getPoint(1));
}
if (points.size() > 1) {
final int toDegree = toNode.getDegree();
if (toDegree != 2) {
final LineString line = LineStringGraph.this.geometryFactory.lineString(points);
lines.add(line);
points.clear();
;
points.add(toNode);
}
}
this.previousNode = toNode;
}
}
};
forEachEdge(comparator, action);
if (points.size() > 1) {
final LineString line = this.geometryFactory.lineString(points);
lines.add(line);
}
return lines;
}
use of com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator in project com.revolsys.open by revolsys.
the class LineStringGraph method removeDuplicateEdges.
private void removeDuplicateEdges() {
final Comparator<Edge<LineSegment>> comparator = new EdgeAttributeValueComparator<>(INDEX);
forEachEdge(comparator, (edge) -> {
final Node<LineSegment> fromNode = edge.getFromNode();
final Node<LineSegment> toNode = edge.getToNode();
final Collection<Edge<LineSegment>> edges = fromNode.getEdgesTo(toNode);
final int duplicateCount = edges.size();
if (duplicateCount > 1) {
edges.remove(edge);
Edge.remove(edges);
}
});
}
use of com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator in project com.revolsys.open by revolsys.
the class GeometryGraph method getGeometry.
/**
* Only currently works for lines and points.
*
* @return
*/
public Geometry getGeometry() {
removeDuplicateLineEdges();
final EdgeAttributeValueComparator<LineSegment> comparator = new EdgeAttributeValueComparator<>("geometryIndex", "partIndex", "segmentIndex");
final List<Geometry> geometries = new ArrayList<>(this.points);
final GeometryFactory geometryFactory = getGeometryFactory();
final List<Point> points = new ArrayList<>();
final Consumer<Edge<LineSegment>> action = new Consumer<Edge<LineSegment>>() {
private Node<LineSegment> previousNode = null;
@Override
public void accept(final Edge<LineSegment> edge) {
final LineSegment lineSegment = edge.getObject();
if (lineSegment.getLength() > 0) {
final Node<LineSegment> fromNode = edge.getFromNode();
final Node<LineSegment> toNode = edge.getToNode();
if (this.previousNode == null) {
points.add(lineSegment.getPoint(0));
points.add(lineSegment.getPoint(1));
} else if (fromNode == this.previousNode) {
if (edge.getLength() > 0) {
points.add(toNode);
}
} else {
if (points.size() > 1) {
final LineString line = geometryFactory.lineString(points);
geometries.add(line);
}
points.clear();
;
points.add(lineSegment.getPoint(0));
points.add(lineSegment.getPoint(1));
}
if (points.size() > 1) {
final int toDegree = toNode.getDegree();
if (toDegree != 2) {
final LineString line = geometryFactory.lineString(points);
geometries.add(line);
points.clear();
;
points.add(toNode);
}
}
this.previousNode = toNode;
}
}
};
forEachEdge(comparator, action);
if (points.size() > 1) {
final LineString line = geometryFactory.lineString(points);
geometries.add(line);
}
return geometryFactory.geometry(geometries);
}
use of com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator in project com.revolsys.open by revolsys.
the class GeometryGraph method removeDuplicateLineEdges.
public void removeDuplicateLineEdges() {
final Comparator<Edge<LineSegment>> comparator = new EdgeAttributeValueComparator<>("geometryIndex", "partIndex", "segmentIndex");
forEachEdge(comparator, (edge) -> {
if (isLineString(edge)) {
final Node<LineSegment> fromNode = edge.getFromNode();
final Node<LineSegment> toNode = edge.getToNode();
final Collection<Edge<LineSegment>> edges = fromNode.getEdgesTo(toNode);
final int duplicateCount = edges.size();
if (duplicateCount > 1) {
edges.remove(edge);
for (final Edge<LineSegment> removeEdge : edges) {
if (isLineString(removeEdge)) {
removeEdge.remove();
}
}
}
}
});
}
use of com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator in project com.revolsys.open by revolsys.
the class LineStringGraph method splitCrossingEdges.
public void splitCrossingEdges() {
final Comparator<Edge<LineSegment>> comparator = new EdgeAttributeValueComparator<>(INDEX);
forEachEdge(comparator, (edge) -> {
final LineSegment line1 = edge.getObject();
final Predicate<LineSegment> lineFilter = new CrossingLineSegmentFilter(line1);
final Predicate<Edge<LineSegment>> filter = new EdgeObjectFilter<>(lineFilter);
final List<Edge<LineSegment>> edges = getEdges(line1, filter);
if (!edges.isEmpty()) {
final List<Point> points = new ArrayList<>();
for (final Edge<LineSegment> edge2 : edges) {
final LineSegment line2 = edge2.getObject();
final Geometry intersections = line1.getIntersection(line2);
if (intersections instanceof Point) {
final Point intersection = (Point) intersections;
points.add(intersection);
edge2.split(intersection);
}
}
if (!points.isEmpty()) {
edge.splitEdge(points);
}
}
});
}
Aggregations