class Geokit::Geocoders::GeonamesGeocoder

Another geocoding web service www.geonames.org

Constants

XML_MAPPINGS

Private Class Methods

do_geocode(address) click to toggle source

Template method which does the geocode lookup.

# File lib/geokit/geocoders/geonames.rb, line 11
def self.do_geocode(address)
  process :xml, submit_url(address)
end
parse_xml(xml) click to toggle source
# File lib/geokit/geocoders/geonames.rb, line 42
def self.parse_xml(xml)
  count = xml.elements["geonames/totalResultsCount"]
  return GeoLoc.new unless !count.nil? && count.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
submit_url(address) click to toggle source
# File lib/geokit/geocoders/geonames.rb, line 15
def self.submit_url(address)
  if key.nil? || key.empty?
    raise(Geokit::Geocoders::GeocodeError, "Geonames requires a key to use their service.")
  end

  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 premium
    "http://ws.geonames.net#{params}&username=#{key}"
  else
    "http://api.geonames.org#{params}&username=#{key}"
  end
end