Skip to content

Commit

Permalink
Add withinPolygon to Query (#1150)
Browse files Browse the repository at this point in the history
* Add WithinPolygon to Query

* Update PFQuery.h
  • Loading branch information
dplewis authored and flovilmart committed Jun 26, 2017
1 parent ab5dcc2 commit 1003c36
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
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 parse-server@2.5.0)
*
* 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

0 comments on commit 1003c36

Please sign in to comment.