Class: Mongoid::Geospatial::GeometryField
- Inherits:
-
Array
- Object
- Array
- Mongoid::Geospatial::GeometryField
- Defined in:
- lib/mongoid/geospatial/geometry_field.rb
Overview
Main Geometry Array
All multi point classes inherit from this one: LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon
Direct Known Subclasses
Class Method Summary collapse
-
.demongoize(obj) ⇒ Object
Database -> Object.
Instance Method Summary collapse
-
#bounding_box ⇒ Array
(also: #bbox)
Determines the 2 points geometry bounding box.
-
#center_point ⇒ Array
(also: #center)
Determines the center point of a multi point geometry.
-
#geom_box ⇒ Array
Determines the 5 points geometry bounding box.
-
#radius(r = 1) ⇒ Array
Generates a radius from the point.
-
#radius_sphere(r = 1, unit = :km) ⇒ Array
Generates a spherical radius from the point.
Class Method Details
.demongoize(obj) ⇒ Object
Database -> Object
89 90 91 |
# File 'lib/mongoid/geospatial/geometry_field.rb', line 89 def demongoize(obj) obj && new(obj) end |
Instance Method Details
#bounding_box ⇒ Array Also known as: bbox
Determines the 2 points geometry bounding box. Useful to find map boundaries, and fit to screen. Returns [bottom left, top right]
21 22 23 24 25 26 27 |
# File 'lib/mongoid/geospatial/geometry_field.rb', line 21 def bounding_box return nil if empty? min_x, max_x = map { |point| point[0] }.minmax min_y, max_y = map { |point| point[1] }.minmax [[min_x, min_y], [max_x, max_y]] end |
#center_point ⇒ Array Also known as: center
Determines the center point of a multi point geometry. Geometry may be closed or not.
52 53 54 55 56 57 |
# File 'lib/mongoid/geospatial/geometry_field.rb', line 52 def center_point return nil unless (box = bbox) min, max = *box [(min[0] + max[0]) / 2.0, (min[1] + max[1]) / 2.0] end |
#geom_box ⇒ Array
Determines the 5 points geometry bounding box. Useful to use with Mongoid #within_geometry
Returns a closed ring: [bottom left, top left, top right, bottom right, bottom left]
39 40 41 42 43 44 |
# File 'lib/mongoid/geospatial/geometry_field.rb', line 39 def geom_box return nil unless (box = bounding_box) xl, yl = box [xl, [xl[0], yl[1]], yl, [yl[0], xl[1]], xl] end |
#radius(r = 1) ⇒ Array
Generates a radius from the point
66 67 68 69 70 |
# File 'lib/mongoid/geospatial/geometry_field.rb', line 66 def radius(r = 1) # rubocop:disable Naming/MethodParameterName return nil unless (mid = center) [mid, r] end |
#radius_sphere(r = 1, unit = :km) ⇒ Array
Generates a spherical radius from the point
point.radius(x) -> [point, x / earth radius]
80 81 82 |
# File 'lib/mongoid/geospatial/geometry_field.rb', line 80 def radius_sphere(r = 1, unit = :km) # rubocop:disable Naming/MethodParameterName radius r.to_f / Mongoid::Geospatial.earth_radius.fetch(unit) end |