Skip to content

Commit 1003c36

Browse files
dplewisflovilmart
authored andcommitted
Add withinPolygon to Query (#1150)
* Add WithinPolygon to Query * Update PFQuery.h
1 parent ab5dcc2 commit 1003c36

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

Parse/Internal/Query/PFQueryConstants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern NSString *const PFQueryKeyNotContainedIn;
2121
extern NSString *const PFQueryKeyContainsAll;
2222
extern NSString *const PFQueryKeyNearSphere;
2323
extern NSString *const PFQueryKeyWithin;
24+
extern NSString *const PFQueryKeyGeoWithin;
2425
extern NSString *const PFQueryKeyRegex;
2526
extern NSString *const PFQueryKeyExists;
2627
extern NSString *const PFQueryKeyInQuery;
@@ -35,6 +36,7 @@ extern NSString *const PFQueryKeyObject;
3536

3637
extern NSString *const PFQueryOptionKeyMaxDistance;
3738
extern NSString *const PFQueryOptionKeyBox;
39+
extern NSString *const PFQueryOptionKeyPolygon;
3840
extern NSString *const PFQueryOptionKeyRegexOptions;
3941

4042
NS_ASSUME_NONNULL_END

Parse/Internal/Query/PFQueryConstants.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
NSString *const PFQueryKeyContainsAll = @"$all";
2020
NSString *const PFQueryKeyNearSphere = @"$nearSphere";
2121
NSString *const PFQueryKeyWithin = @"$within";
22+
NSString *const PFQueryKeyGeoWithin = @"$geoWithin";
2223
NSString *const PFQueryKeyRegex = @"$regex";
2324
NSString *const PFQueryKeyExists = @"$exists";
2425
NSString *const PFQueryKeyInQuery = @"$inQuery";
@@ -33,4 +34,5 @@
3334

3435
NSString *const PFQueryOptionKeyMaxDistance = @"$maxDistance";
3536
NSString *const PFQueryOptionKeyBox = @"$box";
37+
NSString *const PFQueryOptionKeyPolygon = @"$polygon";
3638
NSString *const PFQueryOptionKeyRegexOptions = @"$options";

Parse/PFQuery.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,21 @@ typedef void (^PFQueryArrayResultBlock)(NSArray<PFGenericObject> *_Nullable obje
308308
*/
309309
- (instancetype)whereKey:(NSString *)key withinGeoBoxFromSouthwest:(PFGeoPoint *)southwest toNortheast:(PFGeoPoint *)northeast;
310310

311+
/**
312+
* Add a constraint to the query that requires a particular key's
313+
* coordinates be contained within and on the bounds of a given polygon
314+
* Supports closed and open (last point is connected to first) paths.
315+
* (Requires [email protected])
316+
*
317+
* Polygon must have at least 3 points
318+
*
319+
* @param key The key to be constrained.
320+
* @param points The polygon points as an Array of `PFGeoPoint`'s.
321+
*
322+
* @return The same instance of `PFQuery` as the receiver. This allows method chaining.
323+
*/
324+
- (instancetype)whereKey:(NSString *)key withinPolygon:(NSArray<PFGeoPoint *> *)points;
325+
311326
///--------------------------------------
312327
#pragma mark - Adding String Constraints
313328
///--------------------------------------

Parse/PFQuery.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ - (instancetype)whereKey:(NSString *)key withinGeoBoxFromSouthwest:(PFGeoPoint *
301301
return [self whereKey:key condition:PFQueryKeyWithin object:dictionary];
302302
}
303303

304+
- (instancetype)whereKey:(NSString *)key withinPolygon:(NSArray<PFGeoPoint *> *)points {
305+
NSDictionary *dictionary = @{ PFQueryOptionKeyPolygon : points };
306+
return [self whereKey:key condition:PFQueryKeyGeoWithin object:dictionary];
307+
}
308+
304309
- (instancetype)whereKey:(NSString *)key matchesRegex:(NSString *)regex {
305310
return [self whereKey:key condition:PFQueryKeyRegex object:regex];
306311
}

Tests/Unit/QueryUnitTests.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,16 @@ - (void)testWhereKeyWithinGeobox {
415415
XCTAssertEqualObjects(query.state.conditions, (@{ @"yolo" : @{@"$within" : @{@"$box" : @[ geoPoint1, geoPoint2 ]}} }));
416416
}
417417

418+
- (void)testWhereKeyWithinPolygon {
419+
PFGeoPoint *geoPoint1 = [PFGeoPoint geoPointWithLatitude:10.0 longitude:20.0];
420+
PFGeoPoint *geoPoint2 = [PFGeoPoint geoPointWithLatitude:20.0 longitude:30.0];
421+
PFGeoPoint *geoPoint3 = [PFGeoPoint geoPointWithLatitude:30.0 longitude:40.0];
422+
423+
PFQuery *query = [PFQuery queryWithClassName:@"a"];
424+
[query whereKey:@"yolo" withinPolygon:@[geoPoint1, geoPoint2, geoPoint3]];
425+
XCTAssertEqualObjects(query.state.conditions, (@{ @"yolo" : @{@"$geoWithin" : @{@"$polygon" : @[ geoPoint1, geoPoint2, geoPoint3 ]}} }));
426+
}
427+
418428
- (void)testWhereKeyMatchesRegex {
419429
PFQuery *query = [PFQuery queryWithClassName:@"a"];
420430
[query whereKey:@"yolo" matchesRegex:@"yarr"];

0 commit comments

Comments
 (0)