Class: Parse::Constraint::WithinPolygonQueryConstraint

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

Overview

Equivalent to the $geoWithin Parse query operation and $polygon geopoints constraint. The polygon area is defined by a list of GeoPoint objects that make up the enclosed area. A polygon query should have 3 or more geopoints. Please note that some Geo queries that cross the international date lines are not currently supported by Parse.

# As many points as you want, minimum 3 q.where :field.within_polygon => [geopoint1, geopoint2, geopoint3]

# Polygon for the Bermuda Triangle bermuda = Parse::GeoPoint.new 32.3078000,-64.7504999 # Bermuda miami = Parse::GeoPoint.new 25.7823198,-80.2660226 # Miami, FL san_juan = Parse::GeoPoint.new 18.3848232,-66.0933608 # San Juan, PR

# get all sunken ships inside the Bermuda Triangle SunkenShip.all :location.within_polygon => [bermuda, san_juan, miami]

Instance Attribute Summary

Attributes inherited from Parse::Constraint

#operand, #operation, #operator, #value

Instance Method Summary collapse

Methods inherited from Parse::Constraint

#as_json, constraint_keyword, create, formatted_value, #formatted_value, #initialize, #key, #precedence, #regex_unicode_option, register, #to_s

Constructor Details

This class inherits a constructor from Parse::Constraint

Instance Method Details

#buildHash

Returns the compiled constraint.

Returns:

  • (Hash)

    the compiled constraint.



1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
# File 'lib/parse/query/constraints.rb', line 1939

def build
  value = formatted_value
  if value.is_a?(Parse::Polygon)
    # Parse Server's REST `$polygon` operator expects the legacy
    # array-of-GeoPoint wire shape (each element a
    # `{__type: "GeoPoint", latitude:, longitude:}` hash). The
    # `{__type: "Polygon", coordinates: ...}` wrapper that
    # `Parse::Polygon#as_json` produces is the storage / property
    # shape, NOT a valid `$polygon` operand. If it reaches Parse
    # Server it is rejected with a 500; if it ever reached raw
    # MongoDB, `$polygon` requires `[lng, lat]` order while
    # `Parse::Polygon.coordinates` stores `[lat, lng]` — silent
    # axis swap. Convert here.
    geopoints = value.coordinates.map do |(lat, lng)|
      { __type: "GeoPoint", latitude: lat, longitude: lng }
    end
    return { @operation.operand => { :$geoWithin => { :$polygon => geopoints } } }
  end

  unless value.is_a?(Array) &&
         value.all? { |point| point.is_a?(Parse::GeoPoint) } &&
         value.count > 2
    raise ArgumentError, "[Parse::Query] Invalid query value parameter passed to" \
          " `within_polygon` constraint: Value must be a Parse::Polygon, or an array" \
          " with 3 or more `Parse::GeoPoint` objects."
  end

  { @operation.operand => { :$geoWithin => { :$polygon => value } } }
end

#within_polygonWithinPolygonQueryConstraint

A registered method on a symbol to create the constraint. Maps to Parse operator "$geoWithin" with "$polygon" subconstraint. Takes either an array of GeoPoint objects (legacy form) or a Polygon instance (preferred when the polygon is already modeled).

Examples:

# As many points as you want
q.where :field.within_polygon => [geopoint1, geopoint2, geopoint3]

# Or pass an existing Parse::Polygon directly. Parse Server accepts
# the same `{__type: "Polygon", coordinates: [...]}` object form for
# the `$polygon` argument as the legacy GeoPoint-array form.
q.where :field.within_polygon => polygon

Returns:

Version:

  • 1.7.0 (requires Server v2.4.2 or later)



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

constraint_keyword :$geoWithin