Sha256: 2505f4e09e4f05af9193de407b18d6424d5e2328ad1c99326168e75647286e58

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

module Geokit
  module Geocoders
    # Another geocoding web service
    # http://www.geonames.org
    class GeonamesGeocoder < Geocoder
      config :key

      private

      # Template method which does the geocode lookup.
      def self.do_geocode(address)
        process :xml, submit_url(address)
      end

      def self.submit_url(address)
        address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
        # geonames need a space seperated search string
        address_str.gsub!(/,/, " ")
        params = "/postalCodeSearch?placename=#{Geokit::Inflector::url_escape(address_str)}&maxRows=10"

        if key
          "http://ws.geonames.net#{params}&username=#{key}"
        else
          "http://ws.geonames.org#{params}"
        end
      end

      XML_MAPPINGS = {
        :city         => 'name',
        :state        => 'adminName1',
        :zip          => 'postalcode',
        :country_code => 'countryCode',
        :lat          => 'lat',
        :lng          => 'lng'
      }

      def self.parse_xml(xml)
        return GeoLoc.new unless xml.elements['geonames/totalResultsCount'].text.to_i > 0
        loc = new_loc
        # only take the first result
        set_mappings(loc, xml.elements['geonames/code'], XML_MAPPINGS)
        loc.success = true
        loc
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
geokit-1.8.5 lib/geokit/geocoders/geonames.rb
geokit-1.8.4 lib/geokit/geocoders/geonames.rb
geokit-1.8.3 lib/geokit/geocoders/geonames.rb
geokit-1.8.2 lib/geokit/geocoders/geonames.rb