Skip to content

Add sort option to GeoQueries #424

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 1 commit into from
Dec 4, 2018
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
38 changes: 24 additions & 14 deletions src/Parse/ParseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,34 +770,46 @@ public function near($key, $point)
* @param string $key The key of the ParseGeoPoint
* @param ParseGeoPoint $point The ParseGeoPoint that is used.
* @param int $maxDistance Maximum distance (in radians)
* @param bool $sort Return objects sorted by distance
*
* @return ParseQuery Returns this query, so you can chain this call.
*/
public function withinRadians($key, $point, $maxDistance)
public function withinRadians($key, $point, $maxDistance, $sort = true)
{
$this->near($key, $point);
$this->addCondition($key, '$maxDistance', $maxDistance);
if ($sort) {
$this->near($key, $point);
$this->addCondition($key, '$maxDistance', $maxDistance);
} else {
$this->addCondition(
$key,
'$geoWithin',
[
'$centerSphere' => [
[$point->getLongitude(), $point->getLatitude()],
$maxDistance
]
]
);
}

return $this;
}

/**
* Add a proximity based constraint for finding objects with key point
* values near the point given and within the maximum distance given.
* Radius of earth used is 3958.5 miles.
* Radius of earth used is 3958.8 miles.
*
* @param string $key The key of the ParseGeoPoint
* @param ParseGeoPoint $point The ParseGeoPoint that is used.
* @param int $maxDistance Maximum distance (in miles)
* @param bool $sort Return objects sorted by distance
*
* @return ParseQuery Returns this query, so you can chain this call.
*/
public function withinMiles($key, $point, $maxDistance)
public function withinMiles($key, $point, $maxDistance, $sort = true)
{
$this->near($key, $point);
$this->addCondition($key, '$maxDistance', $maxDistance / 3958.8);

return $this;
return $this->withinRadians($key, $point, $maxDistance / 3958.8, $sort);
}

/**
Expand All @@ -808,15 +820,13 @@ public function withinMiles($key, $point, $maxDistance)
* @param string $key The key of the ParseGeoPoint
* @param ParseGeoPoint $point The ParseGeoPoint that is used.
* @param int $maxDistance Maximum distance (in kilometers)
* @param bool $sort Return objects sorted by distance
*
* @return ParseQuery Returns this query, so you can chain this call.
*/
public function withinKilometers($key, $point, $maxDistance)
public function withinKilometers($key, $point, $maxDistance, $sort = true)
{
$this->near($key, $point);
$this->addCondition($key, '$maxDistance', $maxDistance / 6371.0);

return $this;
return $this->withinRadians($key, $point, $maxDistance / 6371.0, $sort);
}

/**
Expand Down
113 changes: 113 additions & 0 deletions tests/Parse/ParseGeoPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,119 @@ public function testGeoMaxDistanceWithUnits()
$this->assertEquals(0, count($results));
}

public function testGeoMaxDistanceWithUnitsUnsorted()
{
Helper::clearClass('PlaceObject');
// [SAC] 38.52 -121.50 Sacramento,CA
$sacramento = new ParseGeoPoint(38.52, -121.50);
$obj = ParseObject::create('PlaceObject');
$obj->set('location', $sacramento);
$obj->set('name', 'Sacramento');
$obj->save();

// [HNL] 21.35 -157.93 Honolulu Int,HI
$honolulu = new ParseGeoPoint(21.35, -157.93);
$obj = ParseObject::create('PlaceObject');
$obj->set('location', $honolulu);
$obj->set('name', 'Honolulu');
$obj->save();

// [51Q] 37.75 -122.68 San Francisco,CA
$sanfran = new ParseGeoPoint(37.75, -122.68);
$obj = ParseObject::create('PlaceObject');
$obj->set('location', $sanfran);
$obj->set('name', 'San Francisco');
$obj->save();

// test point SFO
$point = new ParseGeoPoint(37.6189722, -122.3748889);

// Kilometers
// baseline all
$query = new ParseQuery('PlaceObject');
$query->near('location', $point);
$results = $query->find();
$this->assertEquals(3, count($results));

// max with all
$query = new ParseQuery('PlaceObject');
$query->withinKilometers('location', $point, 4000.0, false);
$results = $query->find();
$this->assertEquals(3, count($results));

// drop hawaii
$query = new ParseQuery('PlaceObject');
$query->withinKilometers('location', $point, 3700.0, false);
$results = $query->find();
$this->assertEquals(2, count($results));

// drop sacramento
$query = new ParseQuery('PlaceObject');
$query->withinKilometers('location', $point, 100.0, false);
$results = $query->find();
$this->assertEquals(1, count($results));
$this->assertEquals('San Francisco', $results[0]->get('name'));

// drop SF
$query = new ParseQuery('PlaceObject');
$query->withinKilometers('location', $point, 10.0, false);
$results = $query->find();
$this->assertEquals(0, count($results));

// Miles
// max with all
$query = new ParseQuery('PlaceObject');
$query->withinMiles('location', $point, 2500.0, false);
$results = $query->find();
$this->assertEquals(3, count($results));

// drop hawaii
$query = new ParseQuery('PlaceObject');
$query->withinMiles('location', $point, 2200.0, false);
$results = $query->find();
$this->assertEquals(2, count($results));

// drop sacramento
$query = new ParseQuery('PlaceObject');
$query->withinMiles('location', $point, 75.0, false);
$results = $query->find();
$this->assertEquals(1, count($results));
$this->assertEquals('San Francisco', $results[0]->get('name'));

// drop SF
$query = new ParseQuery('PlaceObject');
$query->withinMiles('location', $point, 10.0, false);
$results = $query->find();
$this->assertEquals(0, count($results));
}

public function testGeoQueriesUnsorted()
{
Helper::clearClass('PlaceObject');
$sacramento = new ParseGeoPoint(38.52, -121.50);
$obj = ParseObject::create('PlaceObject');
$obj->set('location', $sacramento);
$obj->set('name', 'Sacramento');
$obj->save();

$point = new ParseGeoPoint(37.6189722, -122.3748889);

$query = new ParseQuery('PlaceObject');
$query->withinRadians('location', $point, 3.14 * 2, false);
$this->assertEquals($query->_getOptions(), [
'where' => [
'location' => [
'$geoWithin' => [
'$centerSphere' => [
[-122.3748889, 37.6189722],
3.14 * 2
]
]
]
]
]);
}

public function testBadLatitude()
{
$this->setExpectedException(
Expand Down