class Geokit::Geocoders::YahooPlaceFinderGeocoder

Private Class Methods

do_geocode(address, options={}) click to toggle source
# File lib/geocoders/yahoo_place_finder_geocoder.rb, line 33
def self.do_geocode(address, options={})
  address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
  res = self.call_geocoder_service(self.get_url(normalize_address(address), options))
  return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
  logger.debug "Yahoo PlaceFinder geocoding. Address: #{address}. Result: #{res}"
  
  # if success_response?(res)
  #   return self.parse_body(res)
  # else
  #   return GeoLoc.new
  # end
  return self.parse_body(res)
rescue 
  logger.info "Caught an error during Yahoo PlaceFinder geocoding call: "+$!
  return GeoLoc.new
end
do_reverse_geocode(latlng) click to toggle source
# File lib/geocoders/yahoo_place_finder_geocoder.rb, line 22
def self.do_reverse_geocode(latlng) 
  res = self.call_geocoder_service(self.get_url(latlng, :reverse => true))
  logger.debug "Yahoo PlaceFinder reverse-geocoding. LL: #{latlng}. Result: #{res}"
  
  if success_response?(res)
    return self.parse_body(Yajl::Parser.parse(res))
  else
    return GeoLoc.new
  end
end
extract_place(result) click to toggle source
# File lib/geocoders/yahoo_place_finder_geocoder.rb, line 80
def self.extract_place(result)
  geoloc = GeoLoc.new

  # basics
  geoloc.lat            = result['latitude']
  geoloc.lng            = result['longitude']
  geoloc.country_code   = result['countrycode']
  geoloc.provider       = 'yahoo_place_finder'

  # extended -- false if not not available
  geoloc.street_address = result['line1']
  geoloc.city           = result['city']
  # geoloc.neighborhood   = result['neighborhood']
  # geoloc.county         = result['county']
  geoloc.zip            = result['postal']
  geoloc.country        = result['country']    
  # geoloc.quality        = result['quality']
  # geoloc.woeid          = result['woeid']
  # geoloc.woetype        = result['woetype']
  if geoloc.is_us?
    geoloc.state = result['statecode']
  else
    geoloc.state = result['state']
  end
  case result['quality'].to_i
    when 9,10        then geoloc.precision = 'country'
    when 19..30      then geoloc.precision = 'state'
    when 39,40       then geoloc.precision = 'city'
    when 49,50       then geoloc.precision = 'neighborhood'
    when 59,60,64    then geoloc.precision = 'zip'
    when 74,75       then geoloc.precision = 'zip+4'
    when 70..72      then geoloc.precision = 'street'
    when 80..87      then geoloc.precision = 'address'
    when 62,63,90,99 then geoloc.precision = 'building'
    else 'unknown'
  end
  
  # Set the bounds from the viewport
  bounds = result['boundingbox']
  geoloc.suggested_bounds = Bounds.normalize(
    [bounds['north'], bounds['east']],
    [bounds['south'], bounds['west']]
  )        
          
  geoloc.accuracy = %w{unknown country state state city zip zip+4 street address building}.index(geoloc.precision)

  geoloc.success = true

  return geoloc
end
get_url(address, options={}) click to toggle source
# File lib/geocoders/yahoo_place_finder_geocoder.rb, line 11
def self.get_url(address, options={})        
  if (options[:reverse])
    latlng=LatLng.normalize(address)
    yql = "select * from geo.placefinder where text='#{latlng}' and gflags='R'"
  else
    address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
    yql = "select * from geo.placefinder where text='#{address}' and flags='X'"
  end
  return "http://query.yahooapis.com/v1/public/yql?q=#{Geokit::Inflector::url_escape(yql)}&format=json"
end
parse_body(res) click to toggle source
# File lib/geocoders/yahoo_place_finder_geocoder.rb, line 50
def self.parse_body(res)
  # if res.is_a?(String)
  #   body = Yajl::Parser.parse(res)
  # else
    body = Yajl::Parser.parse(res.body)
  # end

  count = body['query']['count']
  if (count == 1)
    return extract_place(body['query']['results']['Result'])
  elsif (count > 1)
    results = body['query']['results']['Result']
    geoloc = nil
    results.each do |r|
      extracted_geoloc = extract_place(r)
      if geoloc.nil? 
        # first time through, geoloc is still nil, so we make it the geoloc we just extracted
        geoloc = extracted_geoloc 
      else
        # second (and subsequent) iterations, we push additional
        # geoloc onto "geoloc.all"
        geoloc.all.push(extracted_geoloc) 
      end  
    end
    return geoloc
  else
    return GeoLoc.new
  end
end
success_response?(res) click to toggle source
# File lib/geocoders/yahoo_place_finder_geocoder.rb, line 7
def self.success_response?(res)
  res.is_a?(Net::HTTPSuccess) || res.is_a?(String)
end