Skip to content

Add withinPolygon to Query #684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Parse/src/main/java/com/parse/OfflineQueryLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,23 @@ private static boolean matchesGeoIntersectsConstraint(Object constraint, Object
return target.containsPoint(point);
}

/**
* Matches $geoWithin constraints.
*/
private static boolean matchesGeoWithinConstraint(Object constraint, Object value)
throws ParseException {
if (value == null || value == JSONObject.NULL) {
return false;
}

@SuppressWarnings("unchecked")
HashMap<String, List<ParseGeoPoint>> constraintMap =
(HashMap<String, List<ParseGeoPoint>>) constraint;
List<ParseGeoPoint> points = constraintMap.get("$polygon");
ParsePolygon polygon = new ParsePolygon(points);
ParseGeoPoint point = (ParseGeoPoint) value;
return polygon.containsPoint(point);
}
/**
* Returns true iff the given value matches the given operator and constraint.
*
Expand Down Expand Up @@ -535,6 +552,9 @@ private static boolean matchesStatelessConstraint(String operator, Object constr
case "$within":
return matchesWithinConstraint(constraint, value);

case "$geoWithin":
return matchesGeoWithinConstraint(constraint, value);

case "$geoIntersects":
return matchesGeoIntersectsConstraint(constraint, value);

Expand Down
28 changes: 28 additions & 0 deletions Parse/src/main/java/com/parse/ParseQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,12 @@ public Builder<T> whereWithin(String key, ParseGeoPoint southwest, ParseGeoPoint
return addCondition(key, "$within", dictionary);
}

public Builder<T> whereGeoWithin(String key, List<ParseGeoPoint> points) {
Map<String, List<ParseGeoPoint>> dictionary = new HashMap<>();
dictionary.put("$polygon", points);
return addCondition(key, "$geoWithin", dictionary);
}

public Builder<T> whereGeoIntersects(String key, ParseGeoPoint point) {
Map<String, ParseGeoPoint> dictionary = new HashMap<>();
dictionary.put("$point", point);
Expand Down Expand Up @@ -1848,6 +1854,28 @@ public ParseQuery<T> whereWithinGeoBox(
return this;
}

/**
* Adds a constraint to the query that requires a particular key's
* coordinates be contained within and on the bounds of a given polygon.
* Supports closed and open (last point is connected to first) paths
*
* Polygon must have at least 3 points
*
* @param key
* The key to be constrained.
* @param value
* List<ParseGeoPoint> or ParsePolygon
* @return this, so you can chain this call.
*/
public ParseQuery<T> whereWithinPolygon(String key, List<ParseGeoPoint> points) {
builder.whereGeoWithin(key, points);
return this;
}

public ParseQuery<T> whereWithinPolygon(String key, ParsePolygon polygon) {
return whereWithinPolygon(key, polygon.getCoordinates());
}

/**
* Add a constraint to the query that requires a particular key's
* coordinates that contains a {@link ParseGeoPoint}s
Expand Down
38 changes: 38 additions & 0 deletions Parse/src/test/java/com/parse/OfflineQueryLogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,44 @@ public void testMatchesGeoIntersects() throws ParseException {
assertFalse(matches(logic, query, object));
}

@Test
public void testMatchesGeoWithin() throws ParseException {
List<ParseGeoPoint> smallBox = new ArrayList<ParseGeoPoint>();
smallBox.add(new ParseGeoPoint(0,0));
smallBox.add(new ParseGeoPoint(0,1));
smallBox.add(new ParseGeoPoint(1,1));
smallBox.add(new ParseGeoPoint(1,0));

List<ParseGeoPoint> largeBox = new ArrayList<ParseGeoPoint>();
largeBox.add(new ParseGeoPoint(0,0));
largeBox.add(new ParseGeoPoint(0,10));
largeBox.add(new ParseGeoPoint(10,10));
largeBox.add(new ParseGeoPoint(10,0));

ParseGeoPoint point = new ParseGeoPoint(5,5);

//ParsePolygon polygon = new ParsePolygon(points);

ParseObject object = new ParseObject("TestObject");
object.put("point", point);

ParseQuery.State<ParseObject> query;
OfflineQueryLogic logic = new OfflineQueryLogic(null);
query = new ParseQuery.State.Builder<>("TestObject")
.whereGeoWithin("point", largeBox)
.build();
assertTrue(matches(logic, query, object));

query = new ParseQuery.State.Builder<>("TestObject")
.whereGeoWithin("point", smallBox)
.build();
assertFalse(matches(logic, query, object));

// Non-existant key
object = new ParseObject("TestObject");
assertFalse(matches(logic, query, object));
}

//endregion

//region compare
Expand Down
44 changes: 44 additions & 0 deletions Parse/src/test/java/com/parse/ParseQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,50 @@ public void testWhereWithinGeoBox() throws Exception {
assertTrue(list.contains(pointAgain));
}

@Test
public void testWhereWithinPolygon() throws Exception {
ParseQuery<ParseObject> query = new ParseQuery<>("Test");
ParseGeoPoint point1 = new ParseGeoPoint(10, 10);
ParseGeoPoint point2 = new ParseGeoPoint(20, 20);
ParseGeoPoint point3 = new ParseGeoPoint(30, 30);

List<ParseGeoPoint> points = Arrays.asList(point1, point2, point3);
query.whereWithinPolygon("key", points);

// We generate a state to verify the content of the builder
ParseQuery.State state = query.getBuilder().build();
ParseQuery.QueryConstraints queryConstraints = state.constraints();
ParseQuery.KeyConstraints keyConstraints = (ParseQuery.KeyConstraints) queryConstraints.get("key");
Map map = (Map) keyConstraints.get("$geoWithin");
List<Object> list = (List<Object>) map.get("$polygon");
assertEquals(3, list.size());
assertTrue(list.contains(point1));
assertTrue(list.contains(point2));
assertTrue(list.contains(point3));
}

@Test
public void testWhereWithinPolygonWithPolygon() throws Exception {
ParseQuery<ParseObject> query = new ParseQuery<>("Test");
ParseGeoPoint point1 = new ParseGeoPoint(10, 10);
ParseGeoPoint point2 = new ParseGeoPoint(20, 20);
ParseGeoPoint point3 = new ParseGeoPoint(30, 30);

List<ParseGeoPoint> points = Arrays.asList(point1, point2, point3);
query.whereWithinPolygon("key", new ParsePolygon(points));

// We generate a state to verify the content of the builder
ParseQuery.State state = query.getBuilder().build();
ParseQuery.QueryConstraints queryConstraints = state.constraints();
ParseQuery.KeyConstraints keyConstraints = (ParseQuery.KeyConstraints) queryConstraints.get("key");
Map map = (Map) keyConstraints.get("$geoWithin");
List<Object> list = (List<Object>) map.get("$polygon");
assertEquals(3, list.size());
assertTrue(list.contains(point1));
assertTrue(list.contains(point2));
assertTrue(list.contains(point3));
}

@Test
public void testWherePolygonContains() throws Exception {
ParseQuery<ParseObject> query = new ParseQuery<>("Test");
Expand Down