Parent

Methods

Geokit::Geocoders::GeonamesGeocoder

Another geocoding web service www.geonames.org

Private Class Methods

do_geocode(address, options = {}) click to toggle source

Template method which does the geocode lookup.

     # File /Users/andre/projects/rails/geokit/lib/geokit/geocoders.rb, line 343
343:       def self.do_geocode(address, options = {})
344:         address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
345:         # geonames need a space seperated search string
346:         address_str.gsub!(/,/, " ")
347:         params = "/postalCodeSearch?placename=#{Geokit::Inflector::url_escape(address_str)}&maxRows=10"
348:         
349:         if(GeoKit::Geocoders::geonames)
350:           url = "http://ws.geonames.net#{params}&username=#{GeoKit::Geocoders::geonames}"
351:         else
352:           url = "http://ws.geonames.org#{params}"
353:         end
354:         
355:         res = self.call_geocoder_service(url)
356:         
357:         return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
358:         
359:         xml=res.body
360:         logger.debug "Geonames geocoding. Address: #{address}. Result: #{xml}"
361:         doc=REXML::Document.new(xml)
362:         
363:         if(doc.elements['//geonames/totalResultsCount'].text.to_i > 0)
364:           res=GeoLoc.new
365:         
366:           # only take the first result
367:           res.lat=doc.elements['//code/lat'].text if doc.elements['//code/lat']
368:           res.lng=doc.elements['//code/lng'].text if doc.elements['//code/lng']
369:           res.country_code=doc.elements['//code/countryCode'].text if doc.elements['//code/countryCode']
370:           res.provider='genomes'  
371:           res.city=doc.elements['//code/name'].text if doc.elements['//code/name']
372:           res.state=doc.elements['//code/adminName1'].text if doc.elements['//code/adminName1']
373:           res.zip=doc.elements['//code/postalcode'].text if doc.elements['//code/postalcode']
374:           res.success=true
375:           return res
376:         else 
377:           logger.info "Geonames was unable to geocode address: "+address
378:           return GeoLoc.new
379:         end
380:         
381:         rescue
382:           logger.error "Caught an error during Geonames geocoding call: "+$!
383:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.