|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package apoc.algo; |
| 20 | + |
| 21 | +import apoc.path.RelationshipTypeAndDirections; |
| 22 | +import org.apache.commons.lang3.tuple.Pair; |
| 23 | +import org.neo4j.graphalgo.EstimateEvaluator; |
| 24 | +import org.neo4j.graphdb.Direction; |
| 25 | +import org.neo4j.graphdb.Node; |
| 26 | +import org.neo4j.graphdb.PathExpander; |
| 27 | +import org.neo4j.graphdb.PathExpanderBuilder; |
| 28 | +import org.neo4j.graphdb.RelationshipType; |
| 29 | +import org.neo4j.values.storable.PointValue; |
| 30 | + |
| 31 | +public class CorePathFindingUtils { |
| 32 | + public static class GeoEstimateEvaluatorPointCustom implements EstimateEvaluator<Double> { |
| 33 | + // -- from org.neo4j.graphalgo.impl.util.GeoEstimateEvaluator |
| 34 | + private static final double EARTH_RADIUS = 6371 * 1000; // Meters |
| 35 | + private Node cachedGoal; |
| 36 | + private final String pointPropertyKey; |
| 37 | + private double[] cachedGoalCoordinates; |
| 38 | + |
| 39 | + public GeoEstimateEvaluatorPointCustom(String pointPropertyKey) { |
| 40 | + this.pointPropertyKey = pointPropertyKey; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public Double getCost(Node node, Node goal) { |
| 45 | + double[] nodeCoordinates = getCoordinates(node); |
| 46 | + if (cachedGoal == null || !cachedGoal.equals(goal)) { |
| 47 | + cachedGoalCoordinates = getCoordinates(goal); |
| 48 | + cachedGoal = goal; |
| 49 | + } |
| 50 | + return distance(nodeCoordinates[0], nodeCoordinates[1], cachedGoalCoordinates[0], cachedGoalCoordinates[1]); |
| 51 | + } |
| 52 | + |
| 53 | + private static double distance(double latitude1, double longitude1, double latitude2, double longitude2) { |
| 54 | + latitude1 = Math.toRadians(latitude1); |
| 55 | + longitude1 = Math.toRadians(longitude1); |
| 56 | + latitude2 = Math.toRadians(latitude2); |
| 57 | + longitude2 = Math.toRadians(longitude2); |
| 58 | + double cLa1 = Math.cos(latitude1); |
| 59 | + double xA = EARTH_RADIUS * cLa1 * Math.cos(longitude1); |
| 60 | + double yA = EARTH_RADIUS * cLa1 * Math.sin(longitude1); |
| 61 | + double zA = EARTH_RADIUS * Math.sin(latitude1); |
| 62 | + double cLa2 = Math.cos(latitude2); |
| 63 | + double xB = EARTH_RADIUS * cLa2 * Math.cos(longitude2); |
| 64 | + double yB = EARTH_RADIUS * cLa2 * Math.sin(longitude2); |
| 65 | + double zB = EARTH_RADIUS * Math.sin(latitude2); |
| 66 | + return Math.sqrt((xA - xB) * (xA - xB) + (yA - yB) * (yA - yB) + (zA - zB) * (zA - zB)); |
| 67 | + } |
| 68 | + // -- end from org.neo4j.graphalgo.impl.util.GeoEstimateEvaluator |
| 69 | + |
| 70 | + private double[] getCoordinates(Node node) { |
| 71 | + return ((PointValue) node.getProperty(pointPropertyKey)).coordinate(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public static PathExpander<Double> buildPathExpander(String relationshipsAndDirections) { |
| 76 | + PathExpanderBuilder builder = PathExpanderBuilder.empty(); |
| 77 | + for (Pair<RelationshipType, Direction> pair : RelationshipTypeAndDirections.parse(relationshipsAndDirections)) { |
| 78 | + if (pair.getLeft() == null) { |
| 79 | + if (pair.getRight() == null) { |
| 80 | + builder = PathExpanderBuilder.allTypesAndDirections(); |
| 81 | + } else { |
| 82 | + builder = PathExpanderBuilder.allTypes(pair.getRight()); |
| 83 | + } |
| 84 | + } else { |
| 85 | + if (pair.getRight() == null) { |
| 86 | + builder = builder.add(pair.getLeft()); |
| 87 | + } else { |
| 88 | + builder = builder.add(pair.getLeft(), pair.getRight()); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + return builder.build(); |
| 93 | + } |
| 94 | +} |
0 commit comments