Class: Graticule::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/graticule/location.rb

Overview

A geographic location

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Location

Returns a new instance of Location


11
12
13
14
15
16
# File 'lib/graticule/location.rb', line 11

def initialize(attrs = {})
  attrs.each do |key,value|
    self.send("#{key}=", value.respond_to?(:force_encoding) ? value.force_encoding('UTF-8') : value)
  end
  self.precision ||= :unknown
end

Instance Attribute Details

#countryObject

Returns the value of attribute country


6
7
8
# File 'lib/graticule/location.rb', line 6

def country
  @country
end

#latitudeObject

Returns the value of attribute latitude


6
7
8
# File 'lib/graticule/location.rb', line 6

def latitude
  @latitude
end

#localityObject Also known as: city

Returns the value of attribute locality


6
7
8
# File 'lib/graticule/location.rb', line 6

def locality
  @locality
end

#longitudeObject

Returns the value of attribute longitude


6
7
8
# File 'lib/graticule/location.rb', line 6

def longitude
  @longitude
end

#postal_codeObject Also known as: zip

Returns the value of attribute postal_code


6
7
8
# File 'lib/graticule/location.rb', line 6

def postal_code
  @postal_code
end

#precisionObject

Returns the value of attribute precision


6
7
8
# File 'lib/graticule/location.rb', line 6

def precision
  @precision
end

#regionObject Also known as: state

Returns the value of attribute region


6
7
8
# File 'lib/graticule/location.rb', line 6

def region
  @region
end

#streetObject

Returns the value of attribute street


6
7
8
# File 'lib/graticule/location.rb', line 6

def street
  @street
end

#warningObject

Returns the value of attribute warning


6
7
8
# File 'lib/graticule/location.rb', line 6

def warning
  @warning
end

Instance Method Details

#==(other) ⇒ Object


38
39
40
# File 'lib/graticule/location.rb', line 38

def ==(other)
  other.respond_to?(:attributes) ? attributes == other.attributes : false
end

#antipodeObject Also known as: antipodal_location

Where would I be if I dug through the center of the earth?


50
51
52
# File 'lib/graticule/location.rb', line 50

def antipode
  Location.new :latitude => -latitude, :longitude => longitude + (longitude >= 0 ? -180 : 180)
end

#attributesObject


22
23
24
25
26
27
# File 'lib/graticule/location.rb', line 22

def attributes
  [:latitude, :longitude, :street, :locality, :region, :postal_code, :country, :precision].inject({}) do |result,attr|
    result[attr] = self.send(attr) unless self.send(attr).blank?
    result
  end
end

#blank?Boolean

Returns:

  • (Boolean)

29
30
31
# File 'lib/graticule/location.rb', line 29

def blank?
  attributes.except(:precision).empty?
end

#coordinatesObject

Returns an Array with latitude and longitude.


34
35
36
# File 'lib/graticule/location.rb', line 34

def coordinates
  [latitude, longitude]
end

#distance_to(destination, options = {}) ⇒ Object

Calculate the distance to another location. See the various Distance formulas for more information


44
45
46
47
# File 'lib/graticule/location.rb', line 44

def distance_to(destination, options = {})
  options = {:formula => :haversine, :units => :miles}.merge(options)
  "Graticule::Distance::#{options[:formula].to_s.titleize}".constantize.distance(self, destination, options[:units])
end

#to_s(options = {}) ⇒ Object


55
56
57
58
59
60
61
62
63
# File 'lib/graticule/location.rb', line 55

def to_s(options = {})
  options = {:coordinates => false, :country => true}.merge(options)
  result = ""
  result << "#{street}\n" if street
  result << [locality, [region, postal_code].compact.join(" ")].compact.join(", ")
  result << " #{country}" if options[:country] && country
  result << "\nlatitude: #{latitude}, longitude: #{longitude}" if options[:coordinates] && [latitude, longitude].any?
  result
end