class Misc::GeoBoundingBoxPoint

Point Class

Public Class Methods

new(lat: nil, lng: nil, radius: nil, latlng: nil, geo_hash: nil) click to toggle source

@params [Numeric] lat latitude @params [Numeric] lng longitude @params [Array|String] latlng

latitude and longitude as array or string
Format in [lon, lat] when array, such as [-70,40]
Format in lat,lon when string, such as '40,-70'

@params [String] geohash point geohash

# 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

Public Instance Methods

geo_hash_expr() click to toggle source

@!visibility protected

# File lib/misc/geo_bounding_box_point.rb, line 62
def geo_hash_expr
  @geo_hash
end
get_bounding_box_point() click to toggle source
# 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_coordinates() click to toggle source
# 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
lat_expr() click to toggle source

@!visibility protected

# File lib/misc/geo_bounding_box_point.rb, line 47
def lat_expr
  @lat
end
latlng_expr() click to toggle source

@!visibility protected

# File lib/misc/geo_bounding_box_point.rb, line 57
def latlng_expr
  @latlng
end
lng_expr() click to toggle source

@!visibility protected

# File lib/misc/geo_bounding_box_point.rb, line 52
def lng_expr
  @lng
end
settings() click to toggle source

@return [Hash] serialized json query for object

# 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