Class: Parse::Constraint::NearSphereQueryConstraint

Inherits:
Constraint
  • Object
show all
Defined in:
lib/parse/query/constraints.rb

Overview

TODO:

Add support $maxDistanceInKilometers (for kms) and $maxDistanceInRadians (for radian angle).

Equivalent to the $nearSphere Parse query operation. This is only applicable if the field is of type GeoPoint. This will query Parse and return a list of results ordered by distance with the nearest object being first.

q.where :field.near => geopoint

geopoint = Parse::GeoPoint.new(30.0, -20.0) PlaceObject.all :location.near => geopoint If you wish to constrain the geospatial query to a maximum number of miles, you can utilize the max_miles method on a Parse::GeoPoint object. This is equivalent to the $maxDistanceInMiles constraint used with $nearSphere.

q.where :field.near => geopoint.max_miles(distance) # or provide a triplet includes max miles constraint q.where :field.near => [lat, lng, miles]

geopoint = Parse::GeoPoint.new(30.0, -20.0) PlaceObject.all :location.near => geopoint.max_miles(10)

Constant Summary collapse

KM_PER_RADIAN =

Conversion factors mirror WithinSphereQueryConstraint so the cap below is unit-agnostic.

6371.0
MILES_PER_RADIAN =
3958.8
MAX_RADIANS =

Whole-sphere cap (π radians ≈ 6371π km ≈ 3958.8π miles). A $nearSphere with a larger $maxDistanceIn* defeats the 2dsphere early-termination optimization and degenerates to a full geo scan.

Math::PI

Instance Method Summary collapse

Instance Method Details

#build ⇒ Hash

Returns the compiled constraint.

Returns:

  • (Hash) —

    the compiled constraint.



2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
# File 'lib/parse/query/constraints.rb', line 2552

def build
  point = formatted_value
  max_distance = nil
  unit = :miles
  if point.is_a?(Array) && point.count > 1
    if point.count >= 3
      max_distance = point[2]
      # max_kilometers / max_radians tag the array with their unit
      # symbol in the 4th slot so we can dispatch to the right
      # Parse operator key. max_miles leaves slot 3 nil and the
      # default :miles applies.
      unit = point[3] if [:km, :radians].include?(point[3])
    end
    point = { __type: "GeoPoint", latitude: point[0], longitude: point[1] }
  end
  if max_distance.present? && max_distance > 0
    # Cap radius at whole-sphere coverage (π radians) regardless
    # of the chosen unit. Without this cap, an attacker-controlled
    # `max_*` (e.g. user-supplied km) can submit a huge value and
    # force a full-collection scan.
    radians_for_check = case unit
      when :km then max_distance.to_f / KM_PER_RADIAN
      when :radians then max_distance.to_f
      else max_distance.to_f / MILES_PER_RADIAN
      end
    if radians_for_check > MAX_RADIANS
      raise ArgumentError, "[Parse::Query] `near` max distance #{max_distance} #{unit} " \
                           "(#{radians_for_check} radians) exceeds whole-sphere coverage " \
                           "(π radians). A radius that large defeats the 2dsphere index " \
                           "and degenerates $nearSphere into a full collection scan."
    end

    distance_key = case unit
      when :km then :$maxDistanceInKilometers
      when :radians then :$maxDistance
      else :$maxDistanceInMiles
      end
    return { @operation.operand => { key => point, distance_key => max_distance.to_f } }
  end
  { @operation.operand => { key => point } }
end

#near ⇒ NearSphereQueryConstraint

A registered method on a symbol to create the constraint. Maps to Parse operator "$nearSphere".

Examples:

q.where :field.near => geopoint
q.where :field.near => geopoint.max_miles(distance)
q.where :field.near => geopoint.max_kilometers(distance)

Returns:



2538
# File 'lib/parse/query/constraints.rb', line 2538

constraint_keyword :$nearSphere