Skip to content

Commit

Permalink
Add circular geo_spacial queries.
Browse files Browse the repository at this point in the history
Added within_circle and within_sphere queries to geo_spacial selector
to handle mongodb's respective $center and $centerSphere queries.
  • Loading branch information
astjohn committed Dec 23, 2014
1 parent 701a8f7 commit 5c59ec4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/origin/selectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]])
#
Expand All @@ -152,6 +150,12 @@ def exists(criterion = nil)
# @example Add a geo within criterion for a polygon.
# 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.
#
# @return [ Selectable ] The cloned selectable.
Expand All @@ -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.
#
Expand Down
40 changes: 40 additions & 0 deletions spec/origin/selectable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 5c59ec4

Please sign in to comment.