Class: Geokit::GeoLoc

Inherits:
LatLng
  • Object
show all
Defined in:
lib/geokit/mappable.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.

Constant Summary

Constants included from Mappable

Mappable::EARTH_RADIUS_IN_KMS, Mappable::EARTH_RADIUS_IN_MILES, Mappable::EARTH_RADIUS_IN_NMS, Mappable::KMS_PER_LATITUDE_DEGREE, Mappable::KMS_PER_MILE, Mappable::LATITUDE_DEGREES, Mappable::MILES_PER_LATITUDE_DEGREE, Mappable::NMS_PER_LATITUDE_DEGREE, Mappable::NMS_PER_MILE, Mappable::PI_DIV_RAD

Instance Attribute Summary collapse

Attributes inherited from LatLng

#lat, #lng

Instance Method Summary collapse

Methods inherited from LatLng

#==, #ll, normalize, #to_a

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.



305
306
307
308
309
310
311
312
313
314
315
# File 'lib/geokit/mappable.rb', line 305

def initialize(h={})
  @street_address=h[:street_address] 
  @city=h[:city] 
  @state=h[:state] 
  @zip=h[:zip] 
  @country_code=h[:country_code] 
  @success=false
  @precision='unknown'
  @full_address=nil
  super(h[:lat],h[:lng])
end

Instance Attribute Details

#boundsObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US



296
297
298
# File 'lib/geokit/mappable.rb', line 296

def bounds
  @bounds
end

#cityObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US



296
297
298
# File 'lib/geokit/mappable.rb', line 296

def city
  @city
end

#country_codeObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US



296
297
298
# File 'lib/geokit/mappable.rb', line 296

def country_code
  @country_code
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.



296
297
298
# File 'lib/geokit/mappable.rb', line 296

def full_address
  @full_address
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.



300
301
302
# File 'lib/geokit/mappable.rb', line 300

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.



300
301
302
# File 'lib/geokit/mappable.rb', line 300

def provider
  @provider
end

#stateObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US



296
297
298
# File 'lib/geokit/mappable.rb', line 296

def state
  @state
end

#street_addressObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US



296
297
298
# File 'lib/geokit/mappable.rb', line 296

def street_address
  @street_address
end

#street_nameObject (readonly)

Returns the street name portion of the street address.



302
303
304
# File 'lib/geokit/mappable.rb', line 302

def street_name
  @street_name
end

#street_numberObject (readonly)

Extracts the street number from the street address if the street address has a value.



302
303
304
# File 'lib/geokit/mappable.rb', line 302

def street_number
  @street_number
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.



300
301
302
# File 'lib/geokit/mappable.rb', line 300

def success
  @success
end

#zipObject

Location attributes. Full address is a concatenation of all values. For example: 100 Spear St, San Francisco, CA, 94101, US



296
297
298
# File 'lib/geokit/mappable.rb', line 296

def zip
  @zip
end

Instance Method Details

#hashObject Also known as: to_hash

gives you all the important fields as key-value pairs



341
342
343
344
345
# File 'lib/geokit/mappable.rb', line 341

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

#is_us?Boolean

Returns true if geocoded to the United States.

Returns:

  • (Boolean)


318
319
320
# File 'lib/geokit/mappable.rb', line 318

def is_us?
  country_code == 'US'
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.



364
365
366
367
368
# File 'lib/geokit/mappable.rb', line 364

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

#to_sObject

Returns a string representation of the instance.



371
372
373
# File 'lib/geokit/mappable.rb', line 371

def to_s
  "Provider: #{provider}\n Street: #{street_address}\nCity: #{city}\nState: #{state}\nZip: #{zip}\nLatitude: #{lat}\nLongitude: #{lng}\nCountry: #{country_code}\nBounds: #{bounds}\nSuccess: #{success}"
end