|
| 1 | +/* |
| 2 | + * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.hazelcast.repository.query; |
| 17 | + |
| 18 | +import static com.hazelcast.query.impl.IndexUtils.canonicalizeAttribute; |
| 19 | + |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.springframework.data.geo.Distance; |
| 23 | +import org.springframework.data.geo.Metric; |
| 24 | +import org.springframework.data.geo.Metrics; |
| 25 | +import org.springframework.data.geo.Point; |
| 26 | + |
| 27 | +import com.hazelcast.query.Predicate; |
| 28 | +import com.hazelcast.query.impl.Extractable; |
| 29 | +/** |
| 30 | + * Geo Predicate - Used to calculate near and within queries |
| 31 | + * <li>Finds all the Points within the given distance range from source Point. |
| 32 | + * <li>Finds all the Points within given Circle. |
| 33 | + * |
| 34 | + * @param <K> key of map entry |
| 35 | + * @param <V> value of map entry |
| 36 | + * @author Ulhas R Manekar |
| 37 | + */ |
| 38 | +public class GeoPredicate<K, V> |
| 39 | + implements Predicate<K, V> { |
| 40 | + |
| 41 | + private static final double KM_TO_MILES = 0.621371; |
| 42 | + private static final double KM_TO_NEUTRAL = 0.539957; |
| 43 | + private static final double R = 6372.8; |
| 44 | + |
| 45 | + final String attributeName; |
| 46 | + final Point queryPoint; |
| 47 | + final Distance distance; |
| 48 | + |
| 49 | + /** |
| 50 | + * Constructor accepts the name of the attribute which is of type Point. |
| 51 | + * Constructs a new geo predicate on the given point |
| 52 | + * @param attribute the name of the attribute in a object within Map which is of type Point. |
| 53 | + * @param point the source point from where the distance is calculated. |
| 54 | + * @param distance the Distance object with value and unit of distance. |
| 55 | + */ |
| 56 | + public GeoPredicate(String attribute, Point point, Distance distance) { |
| 57 | + this.attributeName = canonicalizeAttribute(attribute); |
| 58 | + this.queryPoint = point; |
| 59 | + this.distance = distance; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean apply(Map.Entry<K, V> mapEntry) { |
| 64 | + Object attributeValue = readAttributeValue(mapEntry); |
| 65 | + if (attributeValue instanceof Point) { |
| 66 | + return compareDistance((Point) attributeValue); |
| 67 | + } else { |
| 68 | + throw new IllegalArgumentException(String.format("Cannot use %s predicate with attribute other than Point", |
| 69 | + getClass().getSimpleName())); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private boolean compareDistance(Point point) { |
| 74 | + double calculatedDistance = calculateDistance(point.getX(), point.getY(), this.queryPoint.getX(), |
| 75 | + this.queryPoint.getY(), this.distance.getMetric()); |
| 76 | + return calculatedDistance < this.distance.getValue(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * This method users Haversine formula to calculate the distance between two points |
| 81 | + * Formula is explained here - https://www.movable-type.co.uk/scripts/gis-faq-5.1.html |
| 82 | + * Sample Java code is here - https://rosettacode.org/wiki/Haversine_formula#Java |
| 83 | + * @param lat1 - Latitude of first point. |
| 84 | + * @param lng1 - Longitude of first point. |
| 85 | + * @param lat2 - Latitude of second point. |
| 86 | + * @param lng2 - Longitude of second point. |
| 87 | + * @param metric - metric to specify where its KILOMETERS, MILES or NEUTRAL |
| 88 | + * @return |
| 89 | + */ |
| 90 | + private double calculateDistance(double lat1, double lng1, double lat2, double lng2, Metric metric) { |
| 91 | + if ((lat1 == lat2) && (lng1 == lng2)) { |
| 92 | + return 0; |
| 93 | + } else { |
| 94 | + double dLat = Math.toRadians(lat2 - lat1); |
| 95 | + double dLon = Math.toRadians(lng2 - lng1); |
| 96 | + double lat1Radians = Math.toRadians(lat1); |
| 97 | + double lat2Radians = Math.toRadians(lat2); |
| 98 | + |
| 99 | + double a = Math.pow(Math.sin(dLat / 2), 2) |
| 100 | + + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1Radians) * Math.cos(lat2Radians); |
| 101 | + double c = 2 * Math.asin(Math.sqrt(a)); |
| 102 | + double dist = R * c; |
| 103 | + |
| 104 | + if (Metrics.MILES.equals(metric)) { |
| 105 | + dist = dist * KM_TO_MILES; |
| 106 | + } else if (Metrics.NEUTRAL.equals(metric)) { |
| 107 | + dist = dist * KM_TO_NEUTRAL; |
| 108 | + } |
| 109 | + |
| 110 | + return dist; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private Object readAttributeValue(Map.Entry<K, V> entry) { |
| 115 | + Extractable extractable = (Extractable) entry; |
| 116 | + return extractable.getAttributeValue(this.attributeName); |
| 117 | + } |
| 118 | +} |
0 commit comments