Skip to content

Commit

Permalink
Merge pull request #761 from mongodb/points
Browse files Browse the repository at this point in the history
Support more than 2 dimensions when defining a point
  • Loading branch information
evanchooly committed Apr 30, 2015
2 parents fb9290c + 11fcda0 commit a929b33
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 71 deletions.
47 changes: 17 additions & 30 deletions morphia/src/main/java/org/mongodb/morphia/geo/Point.java
Expand Up @@ -3,7 +3,7 @@
import org.mongodb.morphia.annotations.Embedded;
import org.mongodb.morphia.annotations.Entity;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -17,27 +17,20 @@
@Embedded
@Entity(noClassnameStored = true)
public class Point implements Geometry {
private final double latitude;
private final double longitude;

@SuppressWarnings("unused") //needed for Morphia serialisation
private Point() {
longitude = 0;
latitude = 0;
}

private final List<Double> coordinates = new ArrayList<Double>();

Point(final double latitude, final double longitude) {
this.latitude = latitude;
this.longitude = longitude;
coordinates.add(longitude);
coordinates.add(latitude);
}

Point(final List<Double> coordinates) {
this(coordinates.get(1), coordinates.get(0));
this.coordinates.addAll(coordinates);
}

@Override
public List<Double> getCoordinates() {
return Arrays.asList(longitude, latitude);
return coordinates;
}

/**
Expand All @@ -46,7 +39,7 @@ public List<Double> getCoordinates() {
* @return the Point's latitude
*/
public double getLatitude() {
return latitude;
return coordinates.get(1);
}

/**
Expand All @@ -55,7 +48,7 @@ public double getLatitude() {
* @return the Point's longitude
*/
public double getLongitude() {
return longitude;
return coordinates.get(0);
}

/* equals, hashCode and toString. Useful primarily for testing and debugging. Don't forget to re-create when changing this class */
Expand All @@ -70,32 +63,26 @@ public boolean equals(final Object o) {

Point point = (Point) o;

if (Double.compare(point.latitude, latitude) != 0) {
if (getCoordinates().size() != point.getCoordinates().size()) {
return false;
}
if (Double.compare(point.longitude, longitude) != 0) {
return false;
for (int i = 0; i < coordinates.size(); i++) {
final Double coordinate = coordinates.get(i);
if (Double.compare(coordinate, point.getCoordinates().get(i)) != 0) {
return false;
}
}

return true;
}

@Override
public int hashCode() {
int result;
long temp;
temp = Double.doubleToLongBits(latitude);
result = (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(longitude);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
return coordinates.hashCode();
}

@Override
public String toString() {
return "Point{"
+ "latitude=" + latitude
+ ", longitude=" + longitude
+ '}';
return String.format("Point{coordinates=%s}", coordinates);
}
}
79 changes: 38 additions & 41 deletions morphia/src/main/java/org/mongodb/morphia/query/FieldEnd.java
Expand Up @@ -8,6 +8,7 @@

/**
* Represents a document field in a query and presents the operations available to querying against that field.
*
* @param <T>
*/
public interface FieldEnd<T> {
Expand Down Expand Up @@ -40,19 +41,22 @@ public interface FieldEnd<T> {

T hasThisOne(Object val);

T lessThan(Object val);

T lessThanOrEq(Object val);

T in(Iterable<?> values);

T mod(long divisor, long remainder);
/**
* This performs a $geoIntersects query, searching documents containing any sort of GeoJson field and returning those where the given
* geometry intersects with the document shape. This includes cases where the data and the specified object share an edge.
*
* @param geometry the shape to use to query for any stored shapes that intersect
* @return any documents where the GeoJson intersects with a specified {@code geometry}.
*/
T intersects(Geometry geometry);

FieldEnd<T> not();
T lessThan(Object val);

T notEqual(Object val);
T lessThanOrEq(Object val);

T notIn(Iterable<?> values);
T mod(long divisor, long remainder);

T near(double x, double y);

Expand All @@ -62,19 +66,6 @@ public interface FieldEnd<T> {

T near(double x, double y, double radius, boolean spherical);

T sizeEq(int val);

T startsWith(String prefix);

T startsWithIgnoreCase(final String prefix);

/**
* This implements the $geoWithin operator and is only compatible with mongo 2.4 or greater.
*/
T within(Shape shape);

T type(Type type);

/**
* This runs a $near query to check for documents geographically close to the given Point - this Point represents a GeoJSON point type.
* These queries are only supported by MongoDB version 2.4 or greater.
Expand All @@ -94,12 +85,29 @@ public interface FieldEnd<T> {
*/
T near(Point point);

FieldEnd<T> not();

T notEqual(Object val);

T notIn(Iterable<?> values);

T sizeEq(int val);

T startsWith(String prefix);

T startsWithIgnoreCase(final String prefix);

T type(Type type);

/**
* This runs the $geoWithin query, returning documents with GeoJson fields
* whose area falls within the given boundary. When determining
* inclusion, MongoDB considers the border of a shape to be part of the
* shape, subject to the precision of floating point numbers.
*
* This implements the $geoWithin operator and is only compatible with mongo 2.4 or greater.
*/
T within(Shape shape);

/**
* This runs the $geoWithin query, returning documents with GeoJson fields whose area falls within the given boundary. When determining
* inclusion, MongoDB considers the border of a shape to be part of the shape, subject to the precision of floating point numbers.
* <p/>
* These queries are only compatible with MongoDB 2.4 or greater.
*
* @param boundary a polygon describing the boundary to search within.
Expand All @@ -108,25 +116,14 @@ public interface FieldEnd<T> {
T within(Polygon boundary);

/**
* This runs the $geoWithin query, returning documents with GeoJson fields
* whose area falls within the given boundaries. When determining
* inclusion, MongoDB considers the border of a shape to be part of the
* shape, subject to the precision of floating point numbers.
*
* This runs the $geoWithin query, returning documents with GeoJson fields whose area falls within the given boundaries. When
* determining inclusion, MongoDB considers the border of a shape to be part of the shape, subject to the precision of floating point
* numbers.
* <p/>
* These queries are only compatible with MongoDB 2.6 or greater.
*
* @param boundaries a multi-polygon describing the areas to search within.
* @return T
*/
T within(MultiPolygon boundaries);

/**
* This performs a $geoIntersects query, searching documents containing any sort of GeoJson field and returning
* those where the given geometry intersects with the document shape. This includes cases where the data and the
* specified object share an edge.
*
* @param geometry the shape to use to query for any stored shapes that intersect
* @return any documents where the GeoJson intersects with a specified {@code geometry}.
*/
T intersects(Geometry geometry);
}

0 comments on commit a929b33

Please sign in to comment.