Class: Mongoid::Geospatial::GeometryField

Inherits:
Array
  • Object
show all
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

Box, Circle, LineString, Polygon

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.demongoize(obj) ⇒ Object

Database -> Object

Returns:

  • (Object)


87
88
89
# File 'lib/mongoid/geospatial/geometry_field.rb', line 87

def demongoize(obj)
  obj && new(obj)
end

Instance Method Details

#bounding_boxArray 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]

Returns:

  • (Array)

    containing 2 points



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid/geospatial/geometry_field.rb', line 19

def bounding_box
  max_x = -Float::MAX
  min_x = Float::MAX
  max_y = -Float::MAX
  min_y = Float::MAX
  each do |point|
    max_y = point[1] if point[1] > max_y
    min_y = point[1] if point[1] < min_y
    max_x = point[0] if point[0] > max_x
    min_x = point[0] if point[0] < min_x
  end
  [[min_x, min_y], [max_x, max_y]]
end

#center_pointArray Also known as: center

Determines the center point of a multi point geometry. Geometry may be closed or not.

Returns:

  • (Array)

    containing 1 point [x,y]



54
55
56
57
# File 'lib/mongoid/geospatial/geometry_field.rb', line 54

def center_point
  min, max = *bbox
  [(min[0] + max[0]) / 2.0, (min[1] + max[1]) / 2.0]
end

#geom_boxArray

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

Returns:

  • (Array)

    containing 5 points



43
44
45
46
# File 'lib/mongoid/geospatial/geometry_field.rb', line 43

def geom_box
  xl, yl = bounding_box
  [xl, [xl[0], yl[1]], yl, [yl[0], xl[1]], xl]
end

#radius(r = 1) ⇒ Array

Generates a radius from the point

Parameters:

  • r (Numeric) (defaults to: 1)

    radius

Returns:

  • (Array)
    point, r

    point and radius in mongoid format



66
67
68
# File 'lib/mongoid/geospatial/geometry_field.rb', line 66

def radius(r = 1)
  [center, r]
end

#radius_sphere(r = 1, unit = :km) ⇒ Array

Generates a spherical radius from the point

point.radius(x) -> [point, x / earth radius]

Returns:

  • (Array)

See Also:



78
79
80
# File 'lib/mongoid/geospatial/geometry_field.rb', line 78

def radius_sphere(r = 1, unit = :km)
  radius r.to_f / Mongoid::Geospatial.earth_radius[unit]
end