Class: Misc::GeoBoundingBoxPoint

Inherits:
Object
  • Object
show all
Includes:
AttributesReader
Defined in:
lib/misc/geo_bounding_box_point.rb

Overview

Point Class

Instance Method Summary collapse

Methods included from AttributesReader

#attributes

Constructor Details

#initialize(lat: nil, lng: nil, radius: nil, latlng: nil, geo_hash: nil) ⇒ GeoBoundingBoxPoint

Returns a new instance of GeoBoundingBoxPoint.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/misc/geo_bounding_box_point.rb', line 16

def initialize(lat: nil, lng: nil, radius: nil, latlng: nil, geo_hash: nil)
  if lat.present? && lng.present?
    @lat = lat.to_f
    @lng = lng.to_f
    @radius = radius.to_f
    @type = :float
  elsif latlng.present?
    @latlng = latlng
    @type = latlng.class.name.downcase.intern
  elsif geo_hash.present?
    @geo_hash = geo_hash
    @type = :geohash
  else
    raise 'Provide Point as floating values latitude and longitude
           or a string or an array or a geohash'
  end
end

Instance Method Details

#get_bounding_box_pointObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/misc/geo_bounding_box_point.rb', line 92

def get_bounding_box_point
  direction_coordinates = get_coordinates
  ne_lat_lng, sw_lat_lng =  direction_coordinates[:ne_coordinates], direction_coordinates[:sw_coordinates]
  {
    top_right: {lat: ne_lat_lng[:latitude].to_f,
               lon: ne_lat_lng[:longitude].to_f},
    bottom_left: {lat: sw_lat_lng[:latitude].to_f,
                 lon: sw_lat_lng[:longitude].to_f}
  }
end

#get_coordinatesObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/misc/geo_bounding_box_point.rb', line 66

def get_coordinates
  radius_of_earth = Constants::RADIUS_OF_EARTH
  pi = Math::PI
  deg_to_rad =  pi/180
  lat_in_rad = @lat * deg_to_rad
  lon_in_rad = @lng * deg_to_rad
  delta_rad = @radius/radius_of_earth
  ne_coordinates = {
      latitude: (lat_in_rad + delta_rad)/ deg_to_rad,
      longitude: (lon_in_rad + delta_rad)/ deg_to_rad
  }
  sw_coordinates = {
      latitude: (lat_in_rad - delta_rad)/ deg_to_rad,
      longitude: (lon_in_rad - delta_rad)/ deg_to_rad
  }
  nw_coordinates = {
      latitude: (lat_in_rad - delta_rad)/ deg_to_rad,
      longitude: (lon_in_rad + delta_rad)/ deg_to_rad
  }
  se_coordinates = {
      latitude: (lat_in_rad + delta_rad)/ deg_to_rad,
      longitude: (lon_in_rad - delta_rad)/ deg_to_rad
  }
  {ne_coordinates: ne_coordinates, sw_coordinates: sw_coordinates, nw_coordinates: nw_coordinates, se_coordinates: se_coordinates}
end

#settingsHash

Returns serialized json query for object.

Returns:

  • (Hash)

    serialized json query for object



35
36
37
38
39
40
41
42
43
44
# File 'lib/misc/geo_bounding_box_point.rb', line 35

def settings
  case @type
  when :float
    get_bounding_box_point
  when :array || :string
    @latlng
  when :geohash
    @geohash
  end
end