Sha256: d94ef6a8a994d349e662339f317a48cbe5d18568d9891e1e4f39bda3d8464ec6

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# 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.
module Geokit
 module Geocoders
    class UsGeocoder < Geocoder

      private
      def self.do_geocode(address)
        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 = 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}"
        parse_csv data
      end

      def self.parse_csv(data)
        array = data.chomp.split(',')

        loc = GeoLoc.new
        if array.length == 5
          loc.lat,loc.lng,loc.city,loc.state,loc.zip=array
          loc.country_code='US'
          loc.success=true
        elsif array.length == 6
          loc.lat,loc.lng,loc.street_address,loc.city,loc.state,loc.zip=array
          loc.country_code='US'
          loc.success=true
        end
        loc
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geokit-1.8.0 lib/geokit/geocoders/us_geocoder.rb