use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.
the class LineStringGraph method intersects.
public boolean intersects(final LineString line) {
BoundingBox envelope = line.getBoundingBox();
final double scaleXY = this.geometryFactory.getScaleXY();
double maxDistance = 0;
if (scaleXY > 0) {
maxDistance = 1 / scaleXY;
}
envelope = envelope.expand(maxDistance);
if (envelope.intersects(this.envelope)) {
final LineString points = line;
final int numPoints = points.getVertexCount();
final Point fromPoint = points.getPoint(0);
final Point toPoint = points.getPoint(numPoints - 1);
Point previousPoint = fromPoint;
for (int i = 1; i < numPoints; i++) {
final Point nextPoint = points.getPoint(i);
final LineSegment line1 = new LineSegmentDoubleGF(previousPoint, nextPoint);
final List<Edge<LineSegment>> edges = EdgeLessThanDistance.getEdges(this, line1, maxDistance);
for (final Edge<LineSegment> edge2 : edges) {
final LineSegment line2 = edge2.getObject();
final Geometry intersections = line1.getIntersection(line2);
for (final Point intersection : intersections.vertices()) {
if (intersection.equals(fromPoint) || intersection.equals(toPoint)) {
// Point intersection, make sure it's not at the start
final Node<LineSegment> node = findNode(intersection);
final int degree = node.getDegree();
if (node.equals(2, this.fromPoint)) {
if (degree > 2) {
// into account loops
return true;
}
} else if (degree > 1) {
// Intersection not at the start/end of the other line
return true;
}
} else {
// Intersection not at the start/end of the line
return true;
}
}
for (final Point point : line1.vertices()) {
if (line2.distancePoint(point) < maxDistance) {
if (point.equals(fromPoint) || point.equals(toPoint)) {
// Point intersection, make sure it's not at the start
final double maxDistance1 = maxDistance;
for (final Node<LineSegment> node : this.getNodes(point, maxDistance1)) {
final int degree = node.getDegree();
if (node.equals(2, this.fromPoint)) {
if (degree > 2) {
// into account loops
return true;
}
} else if (degree > 1) {
// Intersection not at the start/end of the other line
return true;
}
}
} else {
// Intersection not at the start/end of the line
return true;
}
}
}
}
previousPoint = nextPoint;
}
}
return false;
}
use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.
the class LinearIntersectionNotEqualLineEdgeCleanupVisitor method accept.
@Override
public void accept(final Edge<Record> edge) {
final String typePath = edge.getTypeName();
final Graph<Record> graph = edge.getGraph();
final LineString line = edge.getLineString();
Predicate<Edge<Record>> attributeAndGeometryFilter = new EdgeTypeNameFilter<>(typePath);
final Predicate<Edge<Record>> filter = getPredicate();
if (filter != null) {
attributeAndGeometryFilter = attributeAndGeometryFilter.and(filter);
}
final Predicate<Record> notEqualLineFilter = new RecordGeometryFilter<>(new EqualFilter<>(line)).negate();
final RecordGeometryFilter<LineString> linearIntersectionFilter = new RecordGeometryFilter<>(new LinearIntersectionFilter(line));
attributeAndGeometryFilter = attributeAndGeometryFilter.and(new EdgeObjectFilter<>(notEqualLineFilter.and(linearIntersectionFilter)));
final List<Edge<Record>> intersectingEdges = graph.getEdges(line, attributeAndGeometryFilter);
if (!intersectingEdges.isEmpty()) {
if (intersectingEdges.size() == 1 && line.getLength() > 10) {
if (line.getVertexCount() > 2) {
final Edge<Record> edge2 = intersectingEdges.get(0);
final LineString line2 = edge2.getLineString();
if (middleCoordinatesEqual(line, line2)) {
final boolean firstEqual = line.equalsVertex(2, 0, line2, 0);
if (!firstEqual) {
final Node<Record> fromNode1 = edge.getFromNode();
final Node<Record> fromNode2 = edge2.getFromNode();
if (fromNode1.distancePoint(fromNode2) < 2) {
graph.moveNodesToMidpoint(typePath, fromNode1, fromNode2);
return;
}
}
final boolean lastEqual = line.equalsVertex(2, line.getVertexCount() - 1, line2, line.getVertexCount() - 1);
if (!lastEqual) {
final Node<Record> toNode1 = edge.getToNode();
final Node<Record> toNode2 = edge2.getToNode();
if (toNode1.distancePoint(toNode2) < 2) {
graph.moveNodesToMidpoint(typePath, toNode1, toNode2);
return;
}
}
}
}
}
Logs.error(this, "Has intersecting edges " + line);
}
}
use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.
the class NodeOnEdgeVisitor method getEdges.
public static <T> List<Edge<T>> getEdges(final Graph<T> graph, final Node<T> node, final double maxDistance) {
final CreateListVisitor<Edge<T>> results = new CreateListVisitor<>();
final Point point = node;
BoundingBox boundingBox = point.getBoundingBox();
boundingBox = boundingBox.expand(maxDistance);
final IdObjectIndex<Edge<T>> index = graph.getEdgeIndex();
final NodeOnEdgeVisitor<T> visitor = new NodeOnEdgeVisitor<>(node, boundingBox, maxDistance, results);
index.forEach(boundingBox, visitor);
final List<Edge<T>> edges = results.getList();
Collections.sort(edges);
return edges;
}
use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.
the class PolygonNodeRemovalVisitor method accept.
@Override
public void accept(final Node<Record> node) {
final Set<Edge<Record>> edges = new LinkedHashSet<>(node.getEdges());
while (edges.size() > 1) {
final Edge<Record> edge = edges.iterator().next();
final Record object = edge.getObject();
final Set<Edge<Record>> matchedEdges = new HashSet<>();
final End end = edge.getEnd(node);
for (final Edge<Record> matchEdge : edges) {
final Record matchObject = matchEdge.getObject();
if (edge != matchEdge) {
final End matchEnd = matchEdge.getEnd(node);
if (end != matchEnd) {
if (DataType.equal(object, matchObject, this.excludedAttributes)) {
matchedEdges.add(matchEdge);
}
}
}
}
if (matchedEdges.size() == 1) {
final Edge<Record> matchedEdge = matchedEdges.iterator().next();
if (end.isFrom()) {
this.graph.merge(node, matchedEdge, edge);
} else {
this.graph.merge(node, edge, matchedEdge);
}
}
edges.removeAll(matchedEdges);
edges.remove(edge);
}
}
use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.
the class EdgeIntersectLineVisitor method getEdges.
public static <T> List<Edge<T>> getEdges(final Graph<T> graph, final LineString line) {
final CreateListVisitor<Edge<T>> results = new CreateListVisitor<>();
final BoundingBox env = line.getBoundingBox();
final IdObjectIndex<Edge<T>> index = graph.getEdgeIndex();
index.forEach(env, new EdgeIntersectLineVisitor<>(line, results));
return results.getList();
}
Aggregations