Class: Geokit::GeoLoc
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
-
#accuracy ⇒ Object
accuracy is set for Yahoo and Google geocoders, it is a numeric value of the precision.
-
#all ⇒ Object
Returns the value of attribute all.
-
#block_fips ⇒ Object
FCC Attributes.
-
#city ⇒ Object
Returns the value of attribute city.
-
#country ⇒ Object
Location attributes.
-
#country_code ⇒ Object
Location attributes.
-
#district ⇒ Object
Returns the value of attribute district.
-
#district_fips ⇒ Object
FCC Attributes.
-
#formatted_address ⇒ Object
Returns Google-supplied normalized address string or concatenation of address parts.
-
#full_address ⇒ Object
full_address is provided by google but not by yahoo.
-
#neighborhood ⇒ Object
Returns the value of attribute neighborhood.
-
#place_id ⇒ Object
Attributes set upon return from geocoding.
-
#precision ⇒ Object
Attributes set upon return from geocoding.
-
#provider ⇒ Object
Attributes set upon return from geocoding.
- #state ⇒ Object
-
#state_code ⇒ Object
Location attributes.
-
#state_fips ⇒ Object
FCC Attributes.
-
#state_name ⇒ Object
Location attributes.
-
#street_address ⇒ Object
Returns the value of attribute street_address.
-
#street_name ⇒ Object
Returns the street name portion of the street address where possible.
-
#street_number ⇒ Object
Extracts the street number from the street address where possible.
-
#sub_premise ⇒ Object
Returns the value of attribute sub_premise.
-
#success ⇒ Object
Attributes set upon return from geocoding.
-
#suggested_bounds ⇒ Object
Attributes set upon return from geocoding.
-
#zip ⇒ Object
Location attributes.
Attributes inherited from LatLng
Instance Method Summary collapse
- #encode_with(coder) ⇒ Object
-
#hash ⇒ Object
(also: #to_hash)
gives you all the important fields as key-value pairs.
-
#initialize(h = {}) ⇒ GeoLoc
constructor
Constructor expects a hash of symbols to correspond with attributes.
-
#is_us? ⇒ Boolean
Returns true if geocoded to the United States.
- #province ⇒ Object
- #success? ⇒ Boolean
-
#to_geocodeable_s ⇒ Object
Returns a comma-delimited string consisting of the street address, city, state, zip, and country code.
-
#to_s ⇒ Object
Returns a string representation of the instance.
- #to_yaml_properties ⇒ Object
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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/geokit/geo_loc.rb', line 50 def initialize(h = {}) @all = [self] # sanatises the GeoLoc object so that it conforms to [] h = h.to_hash @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] @success = false @precision = 'unknown' @full_address = nil super(h[:lat], h[:lng]) end |
Instance Attribute Details
#accuracy ⇒ Object
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
41 42 43 |
# File 'lib/geokit/geo_loc.rb', line 41 def accuracy @accuracy end |
#all ⇒ Object
Returns the value of attribute all.
29 30 31 |
# File 'lib/geokit/geo_loc.rb', line 29 def all @all end |
#block_fips ⇒ Object
FCC Attributes
43 44 45 |
# File 'lib/geokit/geo_loc.rb', line 43 def block_fips @block_fips end |
#city ⇒ Object
Returns the value of attribute city.
32 33 34 |
# File 'lib/geokit/geo_loc.rb', line 32 def city @city end |
#country ⇒ Object
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_code ⇒ Object
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 |
#district ⇒ Object
Returns the value of attribute district.
29 30 31 |
# File 'lib/geokit/geo_loc.rb', line 29 def district @district end |
#district_fips ⇒ Object
FCC Attributes
43 44 45 |
# File 'lib/geokit/geo_loc.rb', line 43 def district_fips @district_fips end |
#formatted_address ⇒ Object
Returns Google-supplied normalized address string or concatenation of address parts
105 106 107 |
# File 'lib/geokit/geo_loc.rb', line 105 def formatted_address @formatted_address ||= full_address end |
#full_address ⇒ Object
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.
88 89 90 |
# File 'lib/geokit/geo_loc.rb', line 88 def full_address @full_address ? @full_address : to_geocodeable_s end |
#neighborhood ⇒ Object
Returns the value of attribute neighborhood.
29 30 31 |
# File 'lib/geokit/geo_loc.rb', line 29 def neighborhood @neighborhood end |
#place_id ⇒ Object
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.
37 38 39 |
# File 'lib/geokit/geo_loc.rb', line 37 def place_id @place_id end |
#precision ⇒ Object
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.
37 38 39 |
# File 'lib/geokit/geo_loc.rb', line 37 def precision @precision end |
#provider ⇒ Object
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.
37 38 39 |
# File 'lib/geokit/geo_loc.rb', line 37 def provider @provider end |
#state ⇒ Object
72 73 74 |
# File 'lib/geokit/geo_loc.rb', line 72 def state @state || @state_code || @state_name end |
#state_code ⇒ Object
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_fips ⇒ Object
FCC Attributes
43 44 45 |
# File 'lib/geokit/geo_loc.rb', line 43 def state_fips @state_fips end |
#state_name ⇒ Object
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_address ⇒ Object
Returns the value of attribute street_address.
32 33 34 |
# File 'lib/geokit/geo_loc.rb', line 32 def street_address @street_address end |
#street_name ⇒ Object
Returns the street name portion of the street address where possible
99 100 101 102 |
# File 'lib/geokit/geo_loc.rb', line 99 def street_name @street_name ||= street_address[street_number.length, street_address.length].strip if street_address @street_name end |
#street_number ⇒ Object
Extracts the street number from the street address where possible.
93 94 95 96 |
# File 'lib/geokit/geo_loc.rb', line 93 def street_number @street_number ||= street_address[/(\d*)/] if street_address @street_number end |
#sub_premise ⇒ Object
Returns the value of attribute sub_premise.
29 30 31 |
# File 'lib/geokit/geo_loc.rb', line 29 def sub_premise @sub_premise end |
#success ⇒ Object
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.
37 38 39 |
# File 'lib/geokit/geo_loc.rb', line 37 def success @success end |
#suggested_bounds ⇒ Object
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.
37 38 39 |
# File 'lib/geokit/geo_loc.rb', line 37 def suggested_bounds @suggested_bounds end |
#zip ⇒ Object
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
148 149 150 151 152 |
# File 'lib/geokit/geo_loc.rb', line 148 def encode_with(coder) to_yaml_properties.each do |name| coder[name[1..-1].to_s] = instance_variable_get(name.to_s) end end |
#hash ⇒ Object Also known as: to_hash
gives you all the important fields as key-value pairs
110 111 112 113 114 115 116 117 |
# File 'lib/geokit/geo_loc.rb', line 110 def hash res = {} fields = [:success, :lat, :lng, :country_code, :city, :state, :zip, :street_address, :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.
77 78 79 |
# File 'lib/geokit/geo_loc.rb', line 77 def is_us? country_code == 'US' end |
#province ⇒ Object
45 46 47 |
# File 'lib/geokit/geo_loc.rb', line 45 def province state end |
#success? ⇒ Boolean
81 82 83 |
# File 'lib/geokit/geo_loc.rb', line 81 def success? success == true end |
#to_geocodeable_s ⇒ Object
Returns a comma-delimited string consisting of the street address, city, state, zip, and country code. Only includes those attributes that are non-blank.
138 139 140 141 142 |
# File 'lib/geokit/geo_loc.rb', line 138 def to_geocodeable_s a = [street_address, district, city, state, zip, country_code].compact a.delete_if { |e| !e || e == '' } a.join(', ') end |
#to_s ⇒ Object
Returns a string representation of the instance.
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/geokit/geo_loc.rb', line 155 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_properties ⇒ Object
144 145 146 |
# File 'lib/geokit/geo_loc.rb', line 144 def to_yaml_properties (instance_variables - ['@all', :@all]).sort end |