@@ -38,7 +38,7 @@ import org.springframework.util.ObjectUtils;
|
||||
*/
|
||||
class CountQuery {
|
||||
|
||||
private Document source;
|
||||
private final Document source;
|
||||
|
||||
private CountQuery(Document source) {
|
||||
this.source = source;
|
||||
@@ -101,7 +101,7 @@ class CountQuery {
|
||||
}
|
||||
|
||||
if (valueToInspect instanceof Collection) {
|
||||
return requiresRewrite((Collection) valueToInspect);
|
||||
return requiresRewrite((Collection<?>) valueToInspect);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -157,6 +157,7 @@ class CountQuery {
|
||||
* @param $and potentially existing {@code $and} condition.
|
||||
* @return the rewritten query {@link Document}.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Document createGeoWithin(String key, Document source, @Nullable Object $and) {
|
||||
|
||||
boolean spheric = source.containsKey("$nearSphere");
|
||||
@@ -181,7 +182,7 @@ class CountQuery {
|
||||
|
||||
if ($and != null) {
|
||||
if ($and instanceof Collection) {
|
||||
Collection andElements = (Collection) $and;
|
||||
Collection<Document> andElements = (Collection<Document>) $and;
|
||||
criteria = new ArrayList<>(andElements.size() + 2);
|
||||
criteria.addAll(andElements);
|
||||
} else {
|
||||
@@ -195,24 +196,32 @@ class CountQuery {
|
||||
|
||||
criteria.add(new Document("$nor", Collections.singletonList(new Document(key, $geoWithinMin))));
|
||||
criteria.add(new Document(key, $geoWithinMax));
|
||||
|
||||
return new Document("$and", criteria);
|
||||
}
|
||||
|
||||
private static Number getMaxDistance(Document source, Object $near, boolean spheric) {
|
||||
|
||||
Number maxDistance = Double.MAX_VALUE;
|
||||
if(source.containsKey("$maxDistance")) { // legacy coordinate pair
|
||||
maxDistance = (Number) source.get("$maxDistance");
|
||||
} else if ($near instanceof Document) {
|
||||
Document nearDoc = (Document)$near;
|
||||
if(nearDoc.containsKey("$maxDistance")) {
|
||||
|
||||
if (source.containsKey("$maxDistance")) { // legacy coordinate pair
|
||||
return (Number) source.get("$maxDistance");
|
||||
}
|
||||
|
||||
if ($near instanceof Document) {
|
||||
|
||||
Document nearDoc = (Document) $near;
|
||||
|
||||
if (nearDoc.containsKey("$maxDistance")) {
|
||||
|
||||
maxDistance = (Number) nearDoc.get("$maxDistance");
|
||||
// geojson is in Meters but we need radians x/(6378.1*1000)
|
||||
if(spheric && nearDoc.containsKey("$geometry")) {
|
||||
if (spheric && nearDoc.containsKey("$geometry")) {
|
||||
maxDistance = MetricConversion.metersToRadians(maxDistance.doubleValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return maxDistance;
|
||||
}
|
||||
|
||||
@@ -239,13 +248,14 @@ class CountQuery {
|
||||
return Arrays.asList(((Point) value).getX(), ((Point) value).getY());
|
||||
}
|
||||
|
||||
if (value instanceof Document ) {
|
||||
if (value instanceof Document) {
|
||||
Document document = (Document) value;
|
||||
if(document.containsKey("x")) {
|
||||
Document point = document;
|
||||
return Arrays.asList(point.get("x"), point.get("y"));
|
||||
|
||||
if (document.containsKey("x")) {
|
||||
return Arrays.asList(document.get("x"), document.get("y"));
|
||||
}
|
||||
else if (document.containsKey("$geometry")) {
|
||||
|
||||
if (document.containsKey("$geometry")) {
|
||||
Document geoJsonPoint = document.get("$geometry", Document.class);
|
||||
return geoJsonPoint.get("coordinates");
|
||||
}
|
||||
|
||||
@@ -67,8 +67,8 @@ public class MetricConversion {
|
||||
* Return {@code distance} in radians (on an earth like sphere).
|
||||
*
|
||||
* @param distance must not be {@literal null}.
|
||||
* @return distance in rads.
|
||||
* @since 3.4
|
||||
* @return distance in radians.
|
||||
* @since 3.4.4
|
||||
*/
|
||||
public static double toRadians(Distance distance) {
|
||||
return metersToRadians(getDistanceInMeters(distance));
|
||||
@@ -78,8 +78,8 @@ public class MetricConversion {
|
||||
* Return {@code distance} in radians (on an earth like sphere).
|
||||
*
|
||||
* @param meters
|
||||
* @return distance in rads.
|
||||
* @since 3.4
|
||||
* @return distance in radians.
|
||||
* @since 3.4.4
|
||||
*/
|
||||
public static double metersToRadians(double meters) {
|
||||
return BigDecimal.valueOf(meters).divide(METERS_MULTIPLIER, MathContext.DECIMAL64).doubleValue();
|
||||
|
||||
Reference in New Issue
Block a user