Class: Geokit::Geocoders::GeonamesGeocoder

Inherits:
Geocoder
  • Object
show all
Defined in:
lib/geokit/geocoders.rb

Overview

Another geocoding web service www.geonames.org

Class Method Summary collapse

Methods inherited from Geocoder

call_geocoder_service, do_reverse_geocode, geocode, reverse_geocode

Class Method Details

.cities_in_bounds(bounds) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/geokit/geocoders.rb', line 374

def self.cities_in_bounds(bounds)
  params = "/cities?north=#{bounds.ne.lat}&south=#{bounds.sw.lat}&east=#{bounds.ne.lng}&west=#{bounds.sw.lng}"

  self.do_call_geocoder_service(bounds.to_s, params) do |res, doc|
    if doc.elements['//geonames/geoname']
      # only take the first result
      first = doc.elements['//geonames/geoname']
      res.lat = first.elements['lat'].text
      res.lng = first.elements['lng'].text
      res.city = res.name = first.elements['name'].text
      res.country_code = first.elements['countryCode'].text if first.elements['countryCode']
      res.provider='geonames'
      res.success = true
    end
  end
end

.search(address, options = {}) ⇒ Object

Returns result from geonames’ search webservice



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/geokit/geocoders.rb', line 339

def self.search(address, options = {})
  params = "/search?q=#{Geokit::Inflector::url_escape(address_string(address))}"
  params << "&continentCode=#{options[:continent]}" if options[:continent]
  if options[:feature_class]
    Array(options[:feature_class]).each do |fc|
      params << "&featureClass=#{fc}"
    end
  end
  if options[:feature_code]
    Array(options[:feature_code]).each do |fc|
      params << "&featureCode=#{fc}"
    end
  end
  params << "&style=FULL"
  params << "&maxRows=10"
  
  self.do_call_geocoder_service(address, params) do |res, doc|
    if(doc.elements['//geonames/totalResultsCount'].text.to_i > 0)
      # only take the first result
      res.lat=doc.elements['//geoname/lat'].text if doc.elements['//geoname/lat']
      res.lng=doc.elements['//geoname/lng'].text if doc.elements['//geoname/lng']
      res.name = doc.elements['//geoname/name'].text if doc.elements['//geoname/name']
      res.country_code=doc.elements['//geoname/countryCode'].text if doc.elements['//geoname/countryCode']
      res.provider='geonames'
      # if the location is a city or village
      if doc.elements['//geoname/fcl'].text == 'P'
        res.city=res.name
      end
      res.state=doc.elements['//geoname/adminCode1'].text if doc.elements['//geoname/adminCode1']
      res.timezone = doc.elements['//geoname/timezone'].text if doc.elements['//geoname/timezone']
      res.success=true
    end
  end
end