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.



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

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

Instance Attribute Details

#countryObject

Returns the value of attribute country.



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

def country
  @country
end

#geocoderObject

Returns the value of attribute geocoder.



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

def geocoder
  @geocoder
end

#latitudeObject

Returns the value of attribute latitude.



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

def latitude
  @latitude
end

#localityObject Also known as: city

Returns the value of attribute locality.



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

def locality
  @locality
end

#longitudeObject

Returns the value of attribute longitude.



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

def longitude
  @longitude
end

#postal_codeObject Also known as: zip

Returns the value of attribute postal_code.



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

def postal_code
  @postal_code
end

#precisionObject

Returns the value of attribute precision.



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

def precision
  @precision
end

#premiseObject

Returns the value of attribute premise.



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

def premise
  @premise
end

#regionObject Also known as: state

Returns the value of attribute region.



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

def region
  @region
end

#streetObject

Returns the value of attribute street.



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

def street
  @street
end

#warningObject

Returns the value of attribute warning.



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

def warning
  @warning
end

Instance Method Details

#==(other) ⇒ Object



64
65
66
# File 'lib/graticule/location.rb', line 64

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?



84
85
86
# File 'lib/graticule/location.rb', line 84

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

#attributesObject



19
20
21
22
23
24
# File 'lib/graticule/location.rb', line 19

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

#coordinatesObject

Returns an Array with latitude and longitude.



60
61
62
# File 'lib/graticule/location.rb', line 60

def coordinates
  [latitude, longitude]
end

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

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



78
79
80
81
# File 'lib/graticule/location.rb', line 78

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/graticule/location.rb', line 68

def eql?(other)
    self == other
end

#hashObject



72
73
74
# File 'lib/graticule/location.rb', line 72

def hash
    attributes.to_s.hash
end

#normalized_countryObject



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

def normalized_country
  normalize_string(country)
end

#normalized_localityObject



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

def normalized_locality
  normalize_string(normalize_abbreviations(locality))
end

#normalized_premiseObject



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

def normalized_premise
  normalize_string(normalize_abbreviations(premise))
end

#normalized_regionObject



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

def normalized_region
  normalize_string(region)
end

#normalized_streetObject



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

def normalized_street
  normalize_string(normalize_abbreviations(street))
end

#postal_code_baseObject



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

def postal_code_base
  base = case country
  when "CA"
      postal_code.to_s[0..2]
  else
      postal_code.to_s[0..4]
  end

  base.strip!

  base
end

#to_csv(options = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/graticule/location.rb', line 124

def to_csv(options = {})
  hash = to_hash

  FasterCSV.generate do |csv|
    if(options[:headers])
      csv << hash.keys
    end

    csv << hash.values
  end
end

#to_hash(options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/graticule/location.rb', line 99

def to_hash(options = {})
  hash = ActiveSupport::OrderedHash.new
  hash[:latitude] = latitude
  hash[:longitude] = longitude
  hash[:premise] = premise
  hash[:street] = street
  hash[:locality] = locality
  hash[:region] = region
  hash[:postal_code] = postal_code
  hash[:country] = country
  hash[:precision] = precision.order
  hash[:warning] = warning

  hash
end

#to_json(options = {}) ⇒ Object



120
121
122
# File 'lib/graticule/location.rb', line 120

def to_json(options = {})
  to_hash.to_json(options)
end

#to_s(options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/graticule/location.rb', line 89

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

#to_xml(options = {}) ⇒ Object



115
116
117
118
# File 'lib/graticule/location.rb', line 115

def to_xml(options = {})
  options.reverse_merge!(:root => "location")
  to_hash.to_xml(options)
end