Skip to content
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
1 change: 1 addition & 0 deletions .spellcheck.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dblock
de
Defaultable
demongoize
demongoizes
distanceField
distanceMultiplier
emacs
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 7.4.3 (Next)
## 7.5.0 (Next)

* [#66](https://github.com/mongoid/mongoid-geospatial/issues/66): Honor the configured RGeo factory for all geometry conversions - [@dblock](https://github.com/dblock).
* [#73](https://github.com/mongoid/mongoid-geospatial/issues/73): Read persisted GeoJSON Point documents and cover spherical queries for both GeoJSON and coordinate-array storage - [@dblock](https://github.com/dblock).
* [#79](https://github.com/mongoid/mongoid-geospatial/pull/79): Cover bounded spherical searches, coordinate errors and RGeo arrays - [@dblock](https://github.com/dblock).
* [#55](https://github.com/mongoid/mongoid-geospatial/issues/55): Read persisted empty Point arrays as nil so documents can be loaded and repaired - [@dblock](https://github.com/dblock).
* [#57](https://github.com/mongoid/mongoid-geospatial/issues/57): Correct polygon and bounding-box query examples to use Mongoid symbol operators inside where - [@dblock](https://github.com/dblock).
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ cafe.location.to_geo # => GeoRuby::Point
cafe.location.to_rgeo # => RGeo::Point
```

Point fields also accept GeoJSON hashes such as `{ type: 'Point', coordinates: [lng, lat] }` (with symbol or string keys). Assignment retains the existing coordinate-array storage format; Point fields can also read GeoJSON Point documents already stored in MongoDB. Both representations work with a `2dsphere` index: `sphere: true` selects the index, not a different storage format.

Conventions:

This lib uses #x and #y everywhere. It's shorter than lat or lng or another variation that also confuses. A point is a 2D mathematical notation, longitude/latitude is when you use that notation to map an sphere. In other words: all longitudes are 'xs' where not all 'xs' are longitudes.
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoid/geospatial/fields/point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ class << self
# Database -> Object
# Get it back
def demongoize(obj)
return nil if obj == []
coordinates = obj.is_a?(Hash) ? obj['coordinates'] || obj[:coordinates] : obj
return nil if coordinates == []

obj && new(*obj)
obj && new(*coordinates)
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/geospatial/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
module Mongoid
# Mongoid Geospatial version
module Geospatial
VERSION = '7.4.3'
VERSION = '7.5.0'
end
end
34 changes: 34 additions & 0 deletions spec/mongoid/geospatial/fields/point_geojson_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require 'spec_helper'

describe Mongoid::Geospatial::Point do
it 'demongoizes GeoJSON with either key flavour and BSON documents' do
[
{ type: 'Point', coordinates: [10, -9] },
{ 'type' => 'Point', 'coordinates' => [10, -9] },
BSON::Document.new(type: 'Point', coordinates: [10, -9])
].each do |document|
expect(described_class.demongoize(document).to_a).to eq([10, -9])
end
end

it 'persists GeoJSON point input on a spherical field and finds it again' do
Alarm.create_indexes
alarm = Alarm.create!(spot: described_class.new(10, -9).to_geo_json)

expect(alarm.reload.spot.to_a).to eq([10, -9])
expect(Alarm.within([10, -9], 1).to_a).to eq([alarm])
end

it 'loads and queries a GeoJSON point written directly to MongoDB' do
Alarm.create_indexes
alarm = Alarm.create!(name: 'GeoJSON')
Alarm.collection.find(_id: alarm.id).update_one(
'$set' => { spot: { type: 'Point', coordinates: [10, -9] } }
)

expect(alarm.reload.spot.to_a).to eq([10, -9])
expect(Alarm.within([10, -9], 1).to_a.map { |match| match.spot.to_a }).to eq([[10, -9]])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [92/80]

end
end