diff --git a/lib/origin/selectable.rb b/lib/origin/selectable.rb index a7560ad..0022d9b 100644 --- a/lib/origin/selectable.rb +++ b/lib/origin/selectable.rb @@ -138,8 +138,6 @@ def exists(criterion = nil) # @note The only valid geometry shapes for a $geoIntersects are: # :intersects_line, :intersects_point, and :intersects_polygon. # - # @note The only valid geometry shape for a $geoWithin is :within_polygon - # # @example Add a geo intersect criterion for a line. # query.geo_spacial(:location.intersects_line => [[ 1, 10 ], [ 2, 10 ]]) # @@ -147,10 +145,16 @@ def exists(criterion = nil) # query.geo_spacial(:location.intersects_point => [[ 1, 10 ]]) # # @example Add a geo intersect criterion for a polygon. - # query.geo_spacial(:location.intersects_polygon => [[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]]) + # query.geo_spacial(:location.intersects_polygon => [[[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]]]) # # @example Add a geo within criterion for a polygon. - # query.geo_spacial(:location.within_polygon => [[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]]) + # query.geo_spacial(:location.within_polygon => [[[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]]]) + # + # @example Add a geo within criterian for a circle. + # query.geo_spacial(:location.within_circle => [[x, y], radius]) + # + # @example Add a geo within criterian for a sphere. Radius specified in radians. + # query.geo_spacial(:location.within_sphere => [[x, y], radius]) # # @param [ Hash ] criterion The criterion. # @@ -172,6 +176,8 @@ def geo_spacial(criterion = nil) key :within_polygon, :override, "$geoWithin", "$geometry" do |value| { "type" => POLYGON, "coordinates" => value } end + key :within_circle, :override, "$geoWithin", "$center" + key :within_sphere, :override, "$geoWithin", "$centerSphere" # Add the $gt criterion to the selector. # diff --git a/spec/origin/selectable_spec.rb b/spec/origin/selectable_spec.rb index 6adc7ca..8059c1a 100644 --- a/spec/origin/selectable_spec.rb +++ b/spec/origin/selectable_spec.rb @@ -1075,6 +1075,46 @@ def localized? it_behaves_like "a cloning selection" end + + + context "when the geometry is a circle" do + + let(:selection) do + query.geo_spacial(:location.within_circle => [[ 1, 10 ], 200]) + end + + it "adds the $center expression" do + expect(selection.selector).to eq({ + "location" => { + "$geoWithin" => { + "$center" => [[1,10], 200] + } + } + }) + end + + it_behaves_like "a cloning selection" + end + + context "when the geometry is a spherical circle" do + + let(:selection) do + query.geo_spacial(:location.within_sphere => [[ 1, 10 ], 200]) + end + + it "adds the $center expression" do + expect(selection.selector).to eq({ + "location" => { + "$geoWithin" => { + "$centerSphere" => [[1,10], 200] + } + } + }) + end + + it_behaves_like "a cloning selection" + end + end end