class Geokit::GeoLoc
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
Attributes
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
FCC Attributes
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
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
FCC Attributes
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.
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.
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.
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
FCC Attributes
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
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.
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.
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
Public Class Methods
Constructor expects a hash of symbols to correspond with attributes.
# 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
Public Instance Methods
Sets the city after capitalizing each word within the city name.
# File lib/geokit/geo_loc.rb, line 121 def city=(city) @city = Geokit::Inflector.titleize(city) if city end
# 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
Returns Google-supplied normalized address string or concatenation of address parts
# File lib/geokit/geo_loc.rb, line 105 def formatted_address @formatted_address ||= full_address end
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.
# File lib/geokit/geo_loc.rb, line 88 def full_address @full_address ? @full_address : to_geocodeable_s end
gives you all the important fields as key-value pairs
# 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
Returns true if geocoded to the United States.
# File lib/geokit/geo_loc.rb, line 77 def is_us? country_code == 'US' end
# File lib/geokit/geo_loc.rb, line 45 def province state end
# File lib/geokit/geo_loc.rb, line 72 def state @state || @state_code || @state_name end
Sets the street address after capitalizing each word within the street address.
# File lib/geokit/geo_loc.rb, line 127 def street_address=(address) @street_address = if address && provider != 'google' Geokit::Inflector.titleize(address) else address end end
Returns the street name portion of the street address where possible
# 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
Extracts the street number from the street address where possible.
# File lib/geokit/geo_loc.rb, line 93 def street_number @street_number ||= street_address[/(\d*)/] if street_address @street_number end
# File lib/geokit/geo_loc.rb, line 81 def success? success == true end
Returns a comma-delimited string consisting of the street address, city, state, zip, and country code. Only includes those attributes that are non-blank.
# 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
Returns a string representation of the instance.
# 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
# File lib/geokit/geo_loc.rb, line 144 def to_yaml_properties (instance_variables - ['@all', :@all]).sort end