Sha256: 682267e5ad1b4ed469687bdec0c505f6b3a24a7beb7137d29224beb8d5430624

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

module Geokit
  module Geocoders
    # Geocoder Us geocoder implementation.  Requires the Geokit::Geocoders::GEOCODER_US variable to
    # contain true or false based upon whether authentication is to occur.  Conforms to the
    # interface set by the Geocoder class.
    class UsGeocoder < Geocoder

      private
      def self.do_geocode(address, options = {})
        address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address

        query = (address_str =~ /^\d{5}(?:-\d{4})?$/ ? "zip" : "address") + "=#{Geokit::Inflector::url_escape(address_str)}"
        url = if Geokit::Geocoders::geocoder_us
          "http://#{Geokit::Geocoders::geocoder_us}@geocoder.us/member/service/csv/geocode"
        else
          "http://geocoder.us/service/csv/geocode"
        end

        url = "#{url}?#{query}"
        res = self.call_geocoder_service(url)

        return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
        data = res.body
        logger.debug "Geocoder.us geocoding. Address: #{address}. Result: #{data}"
        array = data.chomp.split(',')

        if array.length == 5
          res=GeoLoc.new
          res.lat,res.lng,res.city,res.state,res.zip=array
          res.country_code='US'
          res.success=true
          return res
        elsif array.length == 6
          res=GeoLoc.new
          res.lat,res.lng,res.street_address,res.city,res.state,res.zip=array
          res.country_code='US'
          res.success=true
          return res
        else
          logger.info "geocoder.us was unable to geocode address: "+address
          return GeoLoc.new
        end
        rescue
          logger.error "Caught an error during geocoder.us geocoding call: "+$!
          return GeoLoc.new

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
abuiles-geokit-1.6.1 lib/geokit/geocoders/us_geocoder.rb
steveh-geokit-1.6.1 lib/geokit/geocoders/us_geocoder.rb
steveh-geokit-1.6.0 lib/geokit/geocoders/us_geocoder.rb