Class: Geokit::GeoLoc

Inherits:
LatLng show all
Defined in:
lib/geokit/geo_loc.rb

Overview

This class encapsulates the result of a geocoding call. It’s primary purpose is to homogenize the results of multiple geocoding providers. It also provides some additional functionality, such as the “full address” method for geocoders that do not provide a full address in their results (for example, Yahoo), and the “is_us” method.

Some geocoders can return multple results. Geoloc can capture multiple results through its “all” method.

For the geocoder setting the results, it would look something like this:

geo=GeoLoc.new(first_result)
geo.all.push(second_result)
geo.all.push(third_result)

Then, for the user of the result:

puts geo.full_address     # just like usual
puts geo.all.size  => 3   # there's three results total
puts geo.all.first        # all is just an array or additional geolocs,
                            so do what you want with it

Instance Attribute Summary collapse

Attributes inherited from LatLng

#lat, #lng

Instance Method Summary collapse

Methods inherited from LatLng

#==, #eql?, from_json, from_string, #lat_dms, #latitude=, #ll, #lng_dms, #longitude=, normalize, #reverse_geocode, #to_a, #valid?

Methods included from Mappable

#distance_to, #endpoint, #heading_from, #heading_to, included, #midpoint_to, #to_lat_lng

Constructor Details

#initialize(h = {}) ⇒ GeoLoc

Constructor expects a hash of symbols to correspond with attributes.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/geokit/geo_loc.rb', line 45

def initialize(h = {})
  @all = [self]

  @street_address = h[:street_address]
  @sub_premise = nil
  @street_number = nil
  @street_name = nil
  @city = h[:city]
  @state = h[:state]
  @state_code = h[:state_code]
  @state_name = h[:state_name]
  @zip = h[:zip]
  @country_code = h[:country_code]
  @province = h[:province]
  @success = false
  @precision = 'unknown'
  @full_address = nil
  super(h[:lat], h[:lng])
end

Instance Attribute Details

#accuracyObject

accuracy is set for Yahoo and Google geocoders, it is a numeric value of the precision. see code.google.com/apis/maps/documentation/geocoding/#GeocodingAccuracy



40
41
42
# File 'lib/geokit/geo_loc.rb', line 40

def accuracy
  @accuracy
end

#allObject

Returns the value of attribute all.



30
31
32
# File 'lib/geokit/geo_loc.rb', line 30

def all
  @all
end

#block_fipsObject

FCC Attributes



42
43
44
# File 'lib/geokit/geo_loc.rb', line 42

def block_fips
  @block_fips
end

#cityObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US Street number and street name are extracted from the street address attribute if they don’t exist



28
29
30
# File 'lib/geokit/geo_loc.rb', line 28

def city
  @city
end

#countryObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US Street number and street name are extracted from the street address attribute if they don’t exist



28
29
30
# File 'lib/geokit/geo_loc.rb', line 28

def country
  @country
end

#country_codeObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US Street number and street name are extracted from the street address attribute if they don’t exist



28
29
30
# File 'lib/geokit/geo_loc.rb', line 28

def country_code
  @country_code
end

#districtObject

Returns the value of attribute district.



30
31
32
# File 'lib/geokit/geo_loc.rb', line 30

def district
  @district
end

#district_fipsObject

FCC Attributes



42
43
44
# File 'lib/geokit/geo_loc.rb', line 42

def district_fips
  @district_fips
end

#full_addressObject

full_address is provided by google but not by yahoo. It is intended that the google geocoding method will provide the full address, whereas for yahoo it will be derived from the parts of the address we do have.



81
82
83
# File 'lib/geokit/geo_loc.rb', line 81

def full_address
  @full_address
end

#neighborhoodObject

Returns the value of attribute neighborhood.



30
31
32
# File 'lib/geokit/geo_loc.rb', line 30

def neighborhood
  @neighborhood
end

#precisionObject

Attributes set upon return from geocoding. Success will be true for successful geocode lookups. The provider will be set to the name of the providing geocoder. Finally, precision is an indicator of the accuracy of the geocoding.



36
37
38
# File 'lib/geokit/geo_loc.rb', line 36

def precision
  @precision
end

#providerObject

Attributes set upon return from geocoding. Success will be true for successful geocode lookups. The provider will be set to the name of the providing geocoder. Finally, precision is an indicator of the accuracy of the geocoding.



36
37
38
# File 'lib/geokit/geo_loc.rb', line 36

def provider
  @provider
end

#provinceObject

Returns the value of attribute province.



30
31
32
# File 'lib/geokit/geo_loc.rb', line 30

def province
  @province
end

#stateObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US Street number and street name are extracted from the street address attribute if they don’t exist



28
29
30
# File 'lib/geokit/geo_loc.rb', line 28

def state
  @state
end

#state_codeObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US Street number and street name are extracted from the street address attribute if they don’t exist



28
29
30
# File 'lib/geokit/geo_loc.rb', line 28

def state_code
  @state_code
end

#state_fipsObject

FCC Attributes



42
43
44
# File 'lib/geokit/geo_loc.rb', line 42

def state_fips
  @state_fips
end

#state_nameObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US Street number and street name are extracted from the street address attribute if they don’t exist



28
29
30
# File 'lib/geokit/geo_loc.rb', line 28

def state_name
  @state_name
end

#street_addressObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US Street number and street name are extracted from the street address attribute if they don’t exist



28
29
30
# File 'lib/geokit/geo_loc.rb', line 28

def street_address
  @street_address
end

#street_nameObject

Returns the street name portion of the street address where possible



28
29
30
# File 'lib/geokit/geo_loc.rb', line 28

def street_name
  @street_name
end

#street_numberObject

Extracts the street number from the street address where possible.



28
29
30
# File 'lib/geokit/geo_loc.rb', line 28

def street_number
  @street_number
end

#sub_premiseObject

Returns the value of attribute sub_premise.



30
31
32
# File 'lib/geokit/geo_loc.rb', line 30

def sub_premise
  @sub_premise
end

#successObject

Attributes set upon return from geocoding. Success will be true for successful geocode lookups. The provider will be set to the name of the providing geocoder. Finally, precision is an indicator of the accuracy of the geocoding.



36
37
38
# File 'lib/geokit/geo_loc.rb', line 36

def success
  @success
end

#suggested_boundsObject

Attributes set upon return from geocoding. Success will be true for successful geocode lookups. The provider will be set to the name of the providing geocoder. Finally, precision is an indicator of the accuracy of the geocoding.



36
37
38
# File 'lib/geokit/geo_loc.rb', line 36

def suggested_bounds
  @suggested_bounds
end

#zipObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US Street number and street name are extracted from the street address attribute if they don’t exist



28
29
30
# File 'lib/geokit/geo_loc.rb', line 28

def zip
  @zip
end

Instance Method Details

#encode_with(coder) ⇒ Object



136
137
138
139
140
# File 'lib/geokit/geo_loc.rb', line 136

def encode_with(coder)
  to_yaml_properties.each do |name|
    coder[name[1..-1].to_s] = instance_variable_get(name.to_s)
  end
end

#hashObject Also known as: to_hash

gives you all the important fields as key-value pairs



98
99
100
101
102
103
104
105
# File 'lib/geokit/geo_loc.rb', line 98

def hash
  res = {}
  fields = [:success, :lat, :lng, :country_code, :city, :state, :zip,
   :street_address, :province, :district, :provider, :full_address, :is_us?,
   :ll, :precision, :district_fips, :state_fips, :block_fips, :sub_premise]
  fields.each { |s| res[s] = send(s.to_s) }
  res
end

#is_us?Boolean

Returns true if geocoded to the United States.

Returns:

  • (Boolean)


70
71
72
# File 'lib/geokit/geo_loc.rb', line 70

def is_us?
  country_code == 'US'
end

#success?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/geokit/geo_loc.rb', line 74

def success?
  success == true
end

#to_geocodeable_sObject

Returns a comma-delimited string consisting of the street address, city, state, zip, and country code. Only includes those attributes that are non-blank.



126
127
128
129
130
# File 'lib/geokit/geo_loc.rb', line 126

def to_geocodeable_s
  a = [street_address, district, city, province, state, zip, country_code].compact
  a.delete_if { |e| !e || e == '' }
  a.join(', ')
end

#to_sObject

Returns a string representation of the instance.



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/geokit/geo_loc.rb', line 143

def to_s
  ["Provider: #{provider}",
   "Street: #{street_address}",
   "City: #{city}",
   "State: #{state}",
   "Zip: #{zip}",
   "Latitude: #{lat}",
   "Longitude: #{lng}",
   "Country: #{country_code}",
   "Success: #{success}"
  ].join("\n")
end

#to_yaml_propertiesObject



132
133
134
# File 'lib/geokit/geo_loc.rb', line 132

def to_yaml_properties
  (instance_variables - ['@all', :@all]).sort
end