|
19 | 19 | */ |
20 | 20 | package org.wowtools.neo4j.rtree.spatial; |
21 | 21 |
|
| 22 | +import org.locationtech.jts.geom.Geometry; |
| 23 | +import org.locationtech.jts.io.ParseException; |
| 24 | +import org.locationtech.jts.io.WKBReader; |
22 | 25 | import org.neo4j.graphdb.*; |
23 | 26 | import org.neo4j.graphdb.traversal.Evaluation; |
24 | 27 | import org.neo4j.graphdb.traversal.Evaluator; |
@@ -719,52 +722,29 @@ public void warmUp() { |
719 | 722 | } |
720 | 723 | } |
721 | 724 |
|
722 | | - public Iterable<Node> getAllIndexInternalNodes(Transaction tx) { |
723 | | - TraversalDescription td = tx.traversalDescription() |
724 | | - .breadthFirst() |
725 | | - .relationships(RTreeRelationshipTypes.RTREE_CHILD, Direction.OUTGOING) |
726 | | - .evaluator(Evaluators.all()); |
727 | | - return td.traverse(getIndexRoot(tx)).nodes(); |
728 | | - } |
729 | | - |
730 | | - |
731 | | - public Iterable<Node> getAllIndexedNodes(Transaction tx) { |
732 | | - return new IndexNodeToGeometryNodeIterable(getAllIndexInternalNodes(tx)); |
733 | | - } |
734 | | - |
735 | | - private class SearchEvaluator implements Evaluator { |
736 | | - private SearchFilter filter; |
737 | 725 |
|
738 | | - public SearchEvaluator(SearchFilter filter) { |
739 | | - this.filter = filter; |
740 | | - } |
741 | | - |
742 | | - |
743 | | - public Evaluation evaluate(Path path) { |
744 | | - Relationship rel = path.lastRelationship(); |
745 | | - Node node = path.endNode(); |
746 | | - if (rel == null) { |
747 | | - return Evaluation.EXCLUDE_AND_CONTINUE; |
748 | | - } else if (rel.isType(RTreeRelationshipTypes.RTREE_CHILD)) { |
749 | | - boolean shouldContinue; |
750 | | - try (Transaction tx = database.beginTx()) { |
751 | | - shouldContinue = filter.needsToVisit(getIndexNodeEnvelope(node, tx)); |
| 726 | + public List<Node> getAllIndexedNodes(Transaction tx) { |
| 727 | + Node rtreeNode = getIndexRoot(tx); |
| 728 | + Deque<Node> stack = new ArrayDeque<>();//辅助遍历的栈 |
| 729 | + List<Node> list = new LinkedList<>(); |
| 730 | + stack.push(rtreeNode); |
| 731 | + while (!stack.isEmpty()) { |
| 732 | + rtreeNode = stack.pop(); |
| 733 | + if (rtreeNode.hasRelationship(Direction.OUTGOING, Constant.Relationship.RTREE_CHILD)) { |
| 734 | + //若有下级索引节点,下级索引节点入栈 |
| 735 | + for (Relationship relationship : rtreeNode.getRelationships(Direction.OUTGOING, Constant.Relationship.RTREE_CHILD)) { |
| 736 | + Node child = relationship.getEndNode(); |
| 737 | + stack.push(child); |
| 738 | + } |
| 739 | + } else if (rtreeNode.hasRelationship(Direction.OUTGOING, Constant.Relationship.RTREE_REFERENCE)) { |
| 740 | + //若有下级对象节点,返回结果 |
| 741 | + for (Relationship relationship : rtreeNode.getRelationships(Direction.OUTGOING, Constant.Relationship.RTREE_REFERENCE)) { |
| 742 | + Node objNode = relationship.getEndNode(); |
| 743 | + list.add(objNode); |
752 | 744 | } |
753 | | - if (shouldContinue) monitor.matchedTreeNode(path.length(), node); |
754 | | - monitor.addCase(shouldContinue ? "Index Matches" : "Index Does NOT Match"); |
755 | | - return shouldContinue ? |
756 | | - Evaluation.EXCLUDE_AND_CONTINUE : |
757 | | - Evaluation.EXCLUDE_AND_PRUNE; |
758 | | - } else if (rel.isType(RTreeRelationshipTypes.RTREE_REFERENCE)) { |
759 | | - boolean found = filter.geometryMatches(node); |
760 | | - monitor.addCase(found ? "Geometry Matches" : "Geometry Does NOT Match"); |
761 | | - if (found) monitor.setHeight(path.length()); |
762 | | - return found ? |
763 | | - Evaluation.INCLUDE_AND_PRUNE : |
764 | | - Evaluation.EXCLUDE_AND_PRUNE; |
765 | 745 | } |
766 | | - return null; |
767 | 746 | } |
| 747 | + return list; |
768 | 748 | } |
769 | 749 |
|
770 | 750 |
|
@@ -1452,62 +1432,6 @@ public void onIndexReference(Node geomNode) { |
1452 | 1432 | } |
1453 | 1433 | } |
1454 | 1434 |
|
1455 | | - /** |
1456 | | - * In order to wrap one iterable or iterator in another that converts |
1457 | | - * the objects from one type to another without loading all into memory, |
1458 | | - * we need to use this ugly java-magic. Man, I miss Ruby right now! |
1459 | | - * |
1460 | | - * @author Craig |
1461 | | - */ |
1462 | | - private class IndexNodeToGeometryNodeIterable implements Iterable<Node> { |
1463 | | - |
1464 | | - private Iterator<Node> allIndexNodeIterator; |
1465 | | - |
1466 | | - private class GeometryNodeIterator implements Iterator<Node> { |
1467 | | - |
1468 | | - private Transaction tx; |
1469 | | - |
1470 | | - public GeometryNodeIterator(Transaction tx) { |
1471 | | - this.tx = tx; |
1472 | | - } |
1473 | | - |
1474 | | - Iterator<Node> geometryNodeIterator = null; |
1475 | | - |
1476 | | - public boolean hasNext() { |
1477 | | - checkGeometryNodeIterator(tx); |
1478 | | - return geometryNodeIterator != null && geometryNodeIterator.hasNext(); |
1479 | | - } |
1480 | | - |
1481 | | - public Node next() { |
1482 | | - checkGeometryNodeIterator(tx); |
1483 | | - return geometryNodeIterator == null ? null : geometryNodeIterator.next(); |
1484 | | - } |
1485 | | - |
1486 | | - private void checkGeometryNodeIterator(Transaction tx) { |
1487 | | - TraversalDescription td = tx.traversalDescription() |
1488 | | - .depthFirst() |
1489 | | - .relationships(RTreeRelationshipTypes.RTREE_REFERENCE, Direction.OUTGOING) |
1490 | | - .evaluator(Evaluators.excludeStartPosition()) |
1491 | | - .evaluator(Evaluators.toDepth(1)); |
1492 | | - while ((geometryNodeIterator == null || !geometryNodeIterator.hasNext()) && |
1493 | | - allIndexNodeIterator.hasNext()) { |
1494 | | - geometryNodeIterator = td.traverse(allIndexNodeIterator.next()).nodes().iterator(); |
1495 | | - } |
1496 | | - } |
1497 | | - |
1498 | | - public void remove() { |
1499 | | - } |
1500 | | - } |
1501 | | - |
1502 | | - public IndexNodeToGeometryNodeIterable(Iterable<Node> allIndexNodes) { |
1503 | | - this.allIndexNodeIterator = allIndexNodes.iterator(); |
1504 | | - } |
1505 | | - |
1506 | | - public Iterator<Node> iterator() { |
1507 | | - return new GeometryNodeIterator(database.beginTx()); |
1508 | | - } |
1509 | | - } |
1510 | | - |
1511 | 1435 | private class IndexNodeAreaComparator implements Comparator<NodeWithEnvelope> { |
1512 | 1436 |
|
1513 | 1437 |
|
|
0 commit comments