Skip to content

Add withinPolygon to Query #1150

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 4 commits into from
Jun 26, 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
2 changes: 2 additions & 0 deletions Parse/Internal/Query/PFQueryConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern NSString *const PFQueryKeyNotContainedIn;
extern NSString *const PFQueryKeyContainsAll;
extern NSString *const PFQueryKeyNearSphere;
extern NSString *const PFQueryKeyWithin;
extern NSString *const PFQueryKeyGeoWithin;
extern NSString *const PFQueryKeyRegex;
extern NSString *const PFQueryKeyExists;
extern NSString *const PFQueryKeyInQuery;
Expand All @@ -35,6 +36,7 @@ extern NSString *const PFQueryKeyObject;

extern NSString *const PFQueryOptionKeyMaxDistance;
extern NSString *const PFQueryOptionKeyBox;
extern NSString *const PFQueryOptionKeyPolygon;
extern NSString *const PFQueryOptionKeyRegexOptions;

NS_ASSUME_NONNULL_END
2 changes: 2 additions & 0 deletions Parse/Internal/Query/PFQueryConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
NSString *const PFQueryKeyContainsAll = @"$all";
NSString *const PFQueryKeyNearSphere = @"$nearSphere";
NSString *const PFQueryKeyWithin = @"$within";
NSString *const PFQueryKeyGeoWithin = @"$geoWithin";
NSString *const PFQueryKeyRegex = @"$regex";
NSString *const PFQueryKeyExists = @"$exists";
NSString *const PFQueryKeyInQuery = @"$inQuery";
Expand All @@ -33,4 +34,5 @@

NSString *const PFQueryOptionKeyMaxDistance = @"$maxDistance";
NSString *const PFQueryOptionKeyBox = @"$box";
NSString *const PFQueryOptionKeyPolygon = @"$polygon";
NSString *const PFQueryOptionKeyRegexOptions = @"$options";
15 changes: 15 additions & 0 deletions Parse/PFQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,21 @@ typedef void (^PFQueryArrayResultBlock)(NSArray<PFGenericObject> *_Nullable obje
*/
- (instancetype)whereKey:(NSString *)key withinGeoBoxFromSouthwest:(PFGeoPoint *)southwest toNortheast:(PFGeoPoint *)northeast;

/**
* Add 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.
* (Requires [email protected])
*
* Polygon must have at least 3 points
*
* @param key The key to be constrained.
* @param points The polygon points as an Array of `PFGeoPoint`'s.
*
* @return The same instance of `PFQuery` as the receiver. This allows method chaining.
*/
- (instancetype)whereKey:(NSString *)key withinPolygon:(NSArray<PFGeoPoint *> *)points;

///--------------------------------------
#pragma mark - Adding String Constraints
///--------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions Parse/PFQuery.m
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ - (instancetype)whereKey:(NSString *)key withinGeoBoxFromSouthwest:(PFGeoPoint *
return [self whereKey:key condition:PFQueryKeyWithin object:dictionary];
}

- (instancetype)whereKey:(NSString *)key withinPolygon:(NSArray<PFGeoPoint *> *)points {
NSDictionary *dictionary = @{ PFQueryOptionKeyPolygon : points };
return [self whereKey:key condition:PFQueryKeyGeoWithin object:dictionary];
}

- (instancetype)whereKey:(NSString *)key matchesRegex:(NSString *)regex {
return [self whereKey:key condition:PFQueryKeyRegex object:regex];
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/Unit/QueryUnitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,16 @@ - (void)testWhereKeyWithinGeobox {
XCTAssertEqualObjects(query.state.conditions, (@{ @"yolo" : @{@"$within" : @{@"$box" : @[ geoPoint1, geoPoint2 ]}} }));
}

- (void)testWhereKeyWithinPolygon {
PFGeoPoint *geoPoint1 = [PFGeoPoint geoPointWithLatitude:10.0 longitude:20.0];
PFGeoPoint *geoPoint2 = [PFGeoPoint geoPointWithLatitude:20.0 longitude:30.0];
PFGeoPoint *geoPoint3 = [PFGeoPoint geoPointWithLatitude:30.0 longitude:40.0];

PFQuery *query = [PFQuery queryWithClassName:@"a"];
[query whereKey:@"yolo" withinPolygon:@[geoPoint1, geoPoint2, geoPoint3]];
XCTAssertEqualObjects(query.state.conditions, (@{ @"yolo" : @{@"$geoWithin" : @{@"$polygon" : @[ geoPoint1, geoPoint2, geoPoint3 ]}} }));
}

- (void)testWhereKeyMatchesRegex {
PFQuery *query = [PFQuery queryWithClassName:@"a"];
[query whereKey:@"yolo" matchesRegex:@"yarr"];
Expand Down