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.



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

def initialize(attrs = {})
  attrs.each do |key,value|
    instance_variable_set "@#{key}", value
  end
  self.precision ||= :unknown
end

Instance Attribute Details

#countryObject

Returns the value of attribute country.



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

def country
  @country
end

#latitudeObject

Returns the value of attribute latitude.



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

def latitude
  @latitude
end

#localityObject Also known as: city

Returns the value of attribute locality.



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

def locality
  @locality
end

#longitudeObject

Returns the value of attribute longitude.



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

def longitude
  @longitude
end

#postal_codeObject Also known as: zip

Returns the value of attribute postal_code.



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

def postal_code
  @postal_code
end

#precisionObject

Returns the value of attribute precision.



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

def precision
  @precision
end

#regionObject Also known as: state

Returns the value of attribute region.



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

def region
  @region
end

#streetObject

Returns the value of attribute street.



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

def street
  @street
end

#warningObject

Returns the value of attribute warning.



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

def warning
  @warning
end

Instance Method Details

#==(object) ⇒ Object



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

def ==(object)
  super(object) || [:latitude, :longitude, :street, :locality, :region, :postal_code, :country, :precision].all? do |m|
    object.respond_to?(m) && self.send(m) == object.send(m)
  end
end

#antipodeObject Also known as: antipodal_location

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



42
43
44
# File 'lib/graticule/location.rb', line 42

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

#attributesObject



17
18
19
20
21
22
# File 'lib/graticule/location.rb', line 17

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

#coordinatesObject

Returns an Array with latitude and longitude.



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

def coordinates
  [latitude, longitude]
end

#distance_to(destination, units = :miles, formula = :haversine) ⇒ Object

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



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

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

#to_s(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/graticule/location.rb', line 47

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