Class: Kaupert::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
Constant Summary
Constants included from Mapcal
Mapcal::EARTH_RADIUS_IN_KMS, Mapcal::EARTH_RADIUS_IN_MILES, Mapcal::EARTH_RADIUS_IN_NMS, Mapcal::KMS_PER_LATITUDE_DEGREE, Mapcal::KMS_PER_MILE, Mapcal::LATITUDE_DEGREES, Mapcal::MILES_PER_LATITUDE_DEGREE, Mapcal::NMS_PER_LATITUDE_DEGREE, Mapcal::NMS_PER_MILE, Mapcal::PI_DIV_RAD
Instance Attribute Summary collapse
-
#accuracy ⇒ Object
accuracy is set for Yahoo and Google geocoders, it is a numeric value of the precision.
-
#all ⇒ Object
Location attributes.
-
#block_fips ⇒ Object
FCC Attributes.
-
#city ⇒ Object
Location attributes.
-
#country ⇒ Object
Location attributes.
-
#country_code ⇒ Object
Location attributes.
-
#district ⇒ Object
Location attributes.
-
#district_fips ⇒ Object
FCC Attributes.
-
#full_address ⇒ Object
full_address is provided by google but not by yahoo.
-
#precision ⇒ Object
Attributes set upon return from geocoding.
-
#provider ⇒ Object
Attributes set upon return from geocoding.
-
#province ⇒ Object
Location attributes.
-
#state ⇒ Object
Location attributes.
-
#state_fips ⇒ Object
FCC Attributes.
-
#street_address ⇒ Object
Location attributes.
-
#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.
-
#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
-
#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.
- #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?, #ll, normalize, #reverse_geocode, #to_a
Methods included from Mapcal
#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.
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/kaupert/mapcal.rb', line 361 def initialize(h={}) @all = [self] @street_address=h[:street_address] @street_number=nil @street_name=nil @city=h[:city] @state=h[:state] @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
#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
355 356 357 |
# File 'lib/kaupert/mapcal.rb', line 355 def accuracy @accuracy end |
#all ⇒ 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
348 349 350 |
# File 'lib/kaupert/mapcal.rb', line 348 def all @all end |
#block_fips ⇒ Object
FCC Attributes
357 358 359 |
# File 'lib/kaupert/mapcal.rb', line 357 def block_fips @block_fips end |
#city ⇒ 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
348 349 350 |
# File 'lib/kaupert/mapcal.rb', line 348 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
348 349 350 |
# File 'lib/kaupert/mapcal.rb', line 348 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
348 349 350 |
# File 'lib/kaupert/mapcal.rb', line 348 def country_code @country_code end |
#district ⇒ 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
348 349 350 |
# File 'lib/kaupert/mapcal.rb', line 348 def district @district end |
#district_fips ⇒ Object
FCC Attributes
357 358 359 |
# File 'lib/kaupert/mapcal.rb', line 357 def district_fips @district_fips 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.
348 349 350 |
# File 'lib/kaupert/mapcal.rb', line 348 def full_address @full_address 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.
352 353 354 |
# File 'lib/kaupert/mapcal.rb', line 352 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.
352 353 354 |
# File 'lib/kaupert/mapcal.rb', line 352 def provider @provider end |
#province ⇒ 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
348 349 350 |
# File 'lib/kaupert/mapcal.rb', line 348 def province @province end |
#state ⇒ 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
348 349 350 |
# File 'lib/kaupert/mapcal.rb', line 348 def state @state end |
#state_fips ⇒ Object
FCC Attributes
357 358 359 |
# File 'lib/kaupert/mapcal.rb', line 357 def state_fips @state_fips end |
#street_address ⇒ 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
348 349 350 |
# File 'lib/kaupert/mapcal.rb', line 348 def street_address @street_address end |
#street_name ⇒ Object
Returns the street name portion of the street address where possible
348 349 350 |
# File 'lib/kaupert/mapcal.rb', line 348 def street_name @street_name end |
#street_number ⇒ Object
Extracts the street number from the street address where possible.
348 349 350 |
# File 'lib/kaupert/mapcal.rb', line 348 def street_number @street_number 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.
352 353 354 |
# File 'lib/kaupert/mapcal.rb', line 352 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.
352 353 354 |
# File 'lib/kaupert/mapcal.rb', line 352 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
348 349 350 |
# File 'lib/kaupert/mapcal.rb', line 348 def zip @zip end |
Instance Method Details
#hash ⇒ Object Also known as: to_hash
gives you all the important fields as key-value pairs
407 408 409 410 411 |
# File 'lib/kaupert/mapcal.rb', line 407 def hash res={} [: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].each { |s| res[s] = self.send(s.to_s) } res end |
#is_us? ⇒ Boolean
Returns true if geocoded to the United States.
379 380 381 |
# File 'lib/kaupert/mapcal.rb', line 379 def is_us? country_code == 'US' end |
#success? ⇒ Boolean
383 384 385 |
# File 'lib/kaupert/mapcal.rb', line 383 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.
430 431 432 433 434 |
# File 'lib/kaupert/mapcal.rb', line 430 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_s ⇒ Object
Returns a string representation of the instance.
441 442 443 |
# File 'lib/kaupert/mapcal.rb', line 441 def to_s "Provider: #{provider}\nStreet: #{street_address}\nCity: #{city}\nState: #{state}\nZip: #{zip}\nLatitude: #{lat}\nLongitude: #{lng}\nCountry: #{country_code}\nSuccess: #{success}" end |
#to_yaml_properties ⇒ Object
436 437 438 |
# File 'lib/kaupert/mapcal.rb', line 436 def to_yaml_properties (instance_variables - ['@all']).sort end |