forked from geokit/geokit
/
geocoders.rb
643 lines (561 loc) · 26.6 KB
/
geocoders.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
require 'net/http'
require 'rexml/document'
require 'yaml'
require 'timeout'
require 'logger'
module Geokit
module Inflector
extend self
def titleize(word)
humanize(underscore(word)).gsub(/\b([a-z])/u) { $1.capitalize }
end
def underscore(camel_cased_word)
camel_cased_word.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/u,'\1_\2').
gsub(/([a-z\d])([A-Z])/u,'\1_\2').
tr("-", "_").
downcase
end
def humanize(lower_case_and_underscored_word)
lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
end
def snake_case(s)
return s.downcase if s =~ /^[A-Z]+$/u
s.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/u, '_\&') =~ /_*(.*)/
return $+.downcase
end
def url_escape(s)
s.gsub(/([^ a-zA-Z0-9_.-]+)/nu) do
'%' + $1.unpack('H2' * $1.size).join('%').upcase
end.tr(' ', '+')
end
def camelize(str)
str.split('_').map {|w| w.capitalize}.join
end
end
# Contains a range of geocoders:
#
# ### "regular" address geocoders
# * Yahoo Geocoder - requires an API key.
# * Geocoder.us - may require authentication if performing more than the free request limit.
# * Geocoder.ca - for Canada; may require authentication as well.
# * Geonames - a free geocoder
#
# ### address geocoders that also provide reverse geocoding
# * Google Geocoder - requires an API key.
#
# ### IP address geocoders
# * IP Geocoder - geocodes an IP address using hostip.info's web service.
# * Geoplugin.net -- another IP address geocoder
#
# ### The Multigeocoder
# * Multi Geocoder - provides failover for the physical location geocoders.
#
# Some of these geocoders require configuration. You don't have to provide it here. See the README.
module Geocoders
@@proxy_addr = nil
@@proxy_port = nil
@@proxy_user = nil
@@proxy_pass = nil
@@request_timeout = nil
@@yahoo = 'REPLACE_WITH_YOUR_YAHOO_KEY'
@@google = 'REPLACE_WITH_YOUR_GOOGLE_KEY'
@@geocoder_us = false
@@geocoder_ca = false
@@geonames = false
@@provider_order = [:google,:us]
@@ip_provider_order = [:geo_plugin,:ip]
@@logger=Logger.new(STDOUT)
@@logger.level=Logger::INFO
@@domain = nil
def self.__define_accessors
class_variables.each do |v|
sym = v.to_s.delete("@").to_sym
unless self.respond_to? sym
module_eval <<-EOS, __FILE__, __LINE__
def self.#{sym}
value = if defined?(#{sym.to_s.upcase})
#{sym.to_s.upcase}
else
@@#{sym}
end
if value.is_a?(Hash)
value = (self.domain.nil? ? nil : value[self.domain]) || value.values.first
end
value
end
def self.#{sym}=(obj)
@@#{sym} = obj
end
EOS
end
end
end
__define_accessors
# Error which is thrown in the event a geocoding error occurs.
class GeocodeError < StandardError; end
# -------------------------------------------------------------------------------------------
# Geocoder Base class -- every geocoder should inherit from this
# -------------------------------------------------------------------------------------------
# The Geocoder base class which defines the interface to be used by all
# other geocoders.
class Geocoder
# Main method which calls the do_geocode template method which subclasses
# are responsible for implementing. Returns a populated GeoLoc or an
# empty one with a failed success code.
def self.geocode(address, options = {})
res = do_geocode(address, options)
return res.nil? ? GeoLoc.new : res
end
# Main method which calls the do_reverse_geocode template method which subclasses
# are responsible for implementing. Returns a populated GeoLoc or an
# empty one with a failed success code.
def self.reverse_geocode(latlng)
res = do_reverse_geocode(latlng)
return res.success? ? res : GeoLoc.new
end
# Call the geocoder service using the timeout if configured.
def self.call_geocoder_service(url)
Timeout::timeout(Geokit::Geocoders::request_timeout) { return self.do_get(url) } if Geokit::Geocoders::request_timeout
return self.do_get(url)
rescue TimeoutError
return nil
end
# Not all geocoders can do reverse geocoding. So, unless the subclass explicitly overrides this method,
# a call to reverse_geocode will return an empty GeoLoc. If you happen to be using MultiGeocoder,
# this will cause it to failover to the next geocoder, which will hopefully be one which supports reverse geocoding.
def self.do_reverse_geocode(latlng)
return GeoLoc.new
end
protected
def self.logger()
Geokit::Geocoders::logger
end
private
# Wraps the geocoder call around a proxy if necessary.
def self.do_get(url)
uri = URI.parse(url)
req = Net::HTTP::Get.new(url)
req.basic_auth(uri.user, uri.password) if uri.userinfo
res = Net::HTTP::Proxy(GeoKit::Geocoders::proxy_addr,
GeoKit::Geocoders::proxy_port,
GeoKit::Geocoders::proxy_user,
GeoKit::Geocoders::proxy_pass).start(uri.host, uri.port) { |http| http.get(uri.path + "?" + uri.query) }
return res
end
# Adds subclass' geocode method making it conveniently available through
# the base class.
def self.inherited(clazz)
class_name = clazz.name.split('::').last
src = <<-END_SRC
def self.#{Geokit::Inflector.underscore(class_name)}(address, options = {})
#{class_name}.geocode(address, options)
end
END_SRC
class_eval(src)
end
end
# -------------------------------------------------------------------------------------------
# "Regular" Address geocoders
# -------------------------------------------------------------------------------------------
# Geocoder CA geocoder implementation. Requires the Geokit::Geocoders::GEOCODER_CA variable to
# contain true or false based upon whether authentication is to occur. Conforms to the
# interface set by the Geocoder class.
#
# Returns a response like:
# <?xml version="1.0" encoding="UTF-8" ?>
# <geodata>
# <latt>49.243086</latt>
# <longt>-123.153684</longt>
# </geodata>
class CaGeocoder < Geocoder
private
# Template method which does the geocode lookup.
def self.do_geocode(address, options = {})
raise ArgumentError('Geocoder.ca requires a GeoLoc argument') unless address.is_a?(GeoLoc)
url = construct_request(address)
res = self.call_geocoder_service(url)
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
xml = res.body
logger.debug "Geocoder.ca geocoding. Address: #{address}. Result: #{xml}"
# Parse the document.
doc = REXML::Document.new(xml)
address.lat = doc.elements['//latt'].text
address.lng = doc.elements['//longt'].text
address.success = true
return address
rescue
logger.error "Caught an error during Geocoder.ca geocoding call: "+$!
return GeoLoc.new
end
# Formats the request in the format acceptable by the CA geocoder.
def self.construct_request(location)
url = ""
url += add_ampersand(url) + "stno=#{location.street_number}" if location.street_address
url += add_ampersand(url) + "addresst=#{Geokit::Inflector::url_escape(location.street_name)}" if location.street_address
url += add_ampersand(url) + "city=#{Geokit::Inflector::url_escape(location.city)}" if location.city
url += add_ampersand(url) + "prov=#{location.state}" if location.state
url += add_ampersand(url) + "postal=#{location.zip}" if location.zip
url += add_ampersand(url) + "auth=#{Geokit::Geocoders::geocoder_ca}" if Geokit::Geocoders::geocoder_ca
url += add_ampersand(url) + "geoit=xml"
'http://geocoder.ca/?' + url
end
def self.add_ampersand(url)
url && url.length > 0 ? "&" : ""
end
end
# 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.
class UsGeocoder < Geocoder
private
def self.do_geocode(address, options = {})
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 = self.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}"
array = data.chomp.split(',')
if array.length == 5
res=GeoLoc.new
res.lat,res.lng,res.city,res.state,res.zip=array
res.country_code='US'
res.success=true
return res
elsif array.length == 6
res=GeoLoc.new
res.lat,res.lng,res.street_address,res.city,res.state,res.zip=array
res.country_code='US'
res.success=true
return res
else
logger.info "geocoder.us was unable to geocode address: "+address
return GeoLoc.new
end
rescue
logger.error "Caught an error during geocoder.us geocoding call: "+$!
return GeoLoc.new
end
end
# Yahoo geocoder implementation. Requires the Geokit::Geocoders::YAHOO variable to
# contain a Yahoo API key. Conforms to the interface set by the Geocoder class.
class YahooGeocoder < Geocoder
private
# Template method which does the geocode lookup.
def self.do_geocode(address, options = {})
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
url="http://api.local.yahoo.com/MapsService/V1/geocode?appid=#{Geokit::Geocoders::yahoo}&location=#{Geokit::Inflector::url_escape(address_str)}"
res = self.call_geocoder_service(url)
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
xml = res.body
doc = REXML::Document.new(xml)
logger.debug "Yahoo geocoding. Address: #{address}. Result: #{xml}"
if doc.elements['//ResultSet']
res=GeoLoc.new
#basic
res.lat=doc.elements['//Latitude'].text
res.lng=doc.elements['//Longitude'].text
res.country_code=doc.elements['//Country'].text
res.provider='yahoo'
#extended - false if not available
res.city=doc.elements['//City'].text if doc.elements['//City'] && doc.elements['//City'].text != nil
res.state=doc.elements['//State'].text if doc.elements['//State'] && doc.elements['//State'].text != nil
res.zip=doc.elements['//Zip'].text if doc.elements['//Zip'] && doc.elements['//Zip'].text != nil
res.street_address=doc.elements['//Address'].text if doc.elements['//Address'] && doc.elements['//Address'].text != nil
res.precision=doc.elements['//Result'].attributes['precision'] if doc.elements['//Result']
# set the accuracy as google does (added by Andruby)
res.accuracy=%w{unknown country state state city zip zip+4 street address building}.index(res.precision)
res.success=true
return res
else
logger.info "Yahoo was unable to geocode address: "+address
return GeoLoc.new
end
rescue
logger.info "Caught an error during Yahoo geocoding call: "+$!
return GeoLoc.new
end
end
# Another geocoding web service
# http://www.geonames.org
class GeonamesGeocoder < Geocoder
private
# Template method which does the geocode lookup.
def self.do_geocode(address, options = {})
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(GeoKit::Geocoders::geonames)
url = "http://ws.geonames.net#{params}&username=#{GeoKit::Geocoders::geonames}"
else
url = "http://ws.geonames.org#{params}"
end
res = self.call_geocoder_service(url)
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
xml=res.body
logger.debug "Geonames geocoding. Address: #{address}. Result: #{xml}"
doc=REXML::Document.new(xml)
if(doc.elements['//geonames/totalResultsCount'].text.to_i > 0)
res=GeoLoc.new
# only take the first result
res.lat=doc.elements['//code/lat'].text if doc.elements['//code/lat']
res.lng=doc.elements['//code/lng'].text if doc.elements['//code/lng']
res.country_code=doc.elements['//code/countryCode'].text if doc.elements['//code/countryCode']
res.provider='genomes'
res.city=doc.elements['//code/name'].text if doc.elements['//code/name']
res.state=doc.elements['//code/adminName1'].text if doc.elements['//code/adminName1']
res.zip=doc.elements['//code/postalcode'].text if doc.elements['//code/postalcode']
res.success=true
return res
else
logger.info "Geonames was unable to geocode address: "+address
return GeoLoc.new
end
rescue
logger.error "Caught an error during Geonames geocoding call: "+$!
end
end
# -------------------------------------------------------------------------------------------
# Address geocoders that also provide reverse geocoding
# -------------------------------------------------------------------------------------------
# Google geocoder implementation. Requires the Geokit::Geocoders::GOOGLE variable to
# contain a Google API key. Conforms to the interface set by the Geocoder class.
class GoogleGeocoder < Geocoder
private
# Template method which does the reverse-geocode lookup.
def self.do_reverse_geocode(latlng)
latlng=LatLng.normalize(latlng)
res = self.call_geocoder_service("http://maps.google.com/maps/geo?ll=#{Geokit::Inflector::url_escape(latlng.ll)}&output=xml&key=#{Geokit::Geocoders::google}&oe=utf-8")
# res = Net::HTTP.get_response(URI.parse("http://maps.google.com/maps/geo?ll=#{Geokit::Inflector::url_escape(address_str)}&output=xml&key=#{Geokit::Geocoders::google}&oe=utf-8"))
return GeoLoc.new unless (res.is_a?(Net::HTTPSuccess) || res.is_a?(Net::HTTPOK))
xml = res.body
logger.debug "Google reverse-geocoding. LL: #{latlng}. Result: #{xml}"
return self.xml2GeoLoc(xml)
end
# Template method which does the geocode lookup.
#
# Supports viewport/country code biasing
#
# ==== OPTIONS
# * :bias - This option makes the Google Geocoder return results biased to a particular
# country or viewport. Country code biasing is achieved by passing the ccTLD
# ('uk' for .co.uk, for example) as a :bias value. For a list of ccTLD's,
# look here: http://en.wikipedia.org/wiki/CcTLD. By default, the geocoder
# will be biased to results within the US (ccTLD .com).
#
# If you'd like the Google Geocoder to prefer results within a given viewport,
# you can pass a Geokit::Bounds object as the :bias value.
#
# ==== EXAMPLES
# # By default, the geocoder will return Syracuse, NY
# Geokit::Geocoders::GoogleGeocoder.geocode('Syracuse').country_code # => 'US'
# # With country code biasing, it returns Syracuse in Sicily, Italy
# Geokit::Geocoders::GoogleGeocoder.geocode('Syracuse', :bias => :it).country_code # => 'IT'
#
# # By default, the geocoder will return Winnetka, IL
# Geokit::Geocoders::GoogleGeocoder.geocode('Winnetka').state # => 'IL'
# # When biased to an bounding box around California, it will now return the Winnetka neighbourhood, CA
# bounds = Geokit::Bounds.normalize([34.074081, -118.694401], [34.321129, -118.399487])
# Geokit::Geocoders::GoogleGeocoder.geocode('Winnetka', :bias => bounds).state # => 'CA'
def self.do_geocode(address, options = {})
bias_str = options[:bias] ? construct_bias_string_from_options(options[:bias]) : ''
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
res = self.call_geocoder_service("http://maps.google.com/maps/geo?q=#{Geokit::Inflector::url_escape(address_str)}&output=xml#{bias_str}&key=#{Geokit::Geocoders::google}&oe=utf-8")
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
xml = res.body
logger.debug "Google geocoding. Address: #{address}. Result: #{xml}"
return self.xml2GeoLoc(xml, address)
end
def self.construct_bias_string_from_options(bias)
if bias.is_a?(String) or bias.is_a?(Symbol)
# country code biasing
"&gl=#{bias.to_s.downcase}"
elsif bias.is_a?(Bounds)
# viewport biasing
"&ll=#{bias.center.ll}&spn=#{bias.to_span.ll}"
end
end
def self.xml2GeoLoc(xml, address="")
doc=REXML::Document.new(xml)
if doc.elements['//kml/Response/Status/code'].text == '200'
geoloc = nil
# Google can return multiple results as //Placemark elements.
# iterate through each and extract each placemark as a geoloc
doc.each_element('//Placemark') do |e|
extracted_geoloc = extract_placemark(e) # g is now an instance of GeoLoc
if geoloc.nil?
# first time through, geoloc is still nil, so we make it the geoloc we just extracted
geoloc = extracted_geoloc
else
# second (and subsequent) iterations, we push additional
# geolocs onto "geoloc.all"
geoloc.all.push(extracted_geoloc)
end
end
return geoloc
else
logger.info "Google was unable to geocode address: "+address
return GeoLoc.new
end
rescue
logger.error "Caught an error during Google geocoding call: "+$!
return GeoLoc.new
end
# extracts a single geoloc from a //placemark element in the google results xml
def self.extract_placemark(doc)
res = GeoLoc.new
coordinates=doc.elements['.//coordinates'].text.to_s.split(',')
#basics
res.lat=coordinates[1]
res.lng=coordinates[0]
res.country_code=doc.elements['.//CountryNameCode'].text if doc.elements['.//CountryNameCode']
res.provider='google'
#extended -- false if not not available
res.city = doc.elements['.//LocalityName'].text if doc.elements['.//LocalityName']
res.state = doc.elements['.//AdministrativeAreaName'].text if doc.elements['.//AdministrativeAreaName']
res.full_address = doc.elements['.//address'].text if doc.elements['.//address'] # google provides it
res.zip = doc.elements['.//PostalCodeNumber'].text if doc.elements['.//PostalCodeNumber']
res.street_address = doc.elements['.//ThoroughfareName'].text if doc.elements['.//ThoroughfareName']
# Translate accuracy into Yahoo-style token address, street, zip, zip+4, city, state, country
# For Google, 1=low accuracy, 8=high accuracy
address_details=doc.elements['.//*[local-name() = "AddressDetails"]']
res.accuracy = address_details ? address_details.attributes['Accuracy'].to_i : 0
res.precision=%w{unknown country state state city zip zip+4 street address building}[res.accuracy]
# google returns a set of suggested boundaries for the geocoded result
if suggested_bounds = doc.elements['//LatLonBox']
res.suggested_bounds = Bounds.normalize(
[suggested_bounds.attributes['south'], suggested_bounds.attributes['west']],
[suggested_bounds.attributes['north'], suggested_bounds.attributes['east']])
end
res.success=true
return res
end
end
# -------------------------------------------------------------------------------------------
# IP Geocoders
# -------------------------------------------------------------------------------------------
# Provides geocoding based upon an IP address. The underlying web service is geoplugin.net
class GeoPluginGeocoder < Geocoder
private
def self.do_geocode(ip, options = {})
return GeoLoc.new unless /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(ip)
response = self.call_geocoder_service("http://www.geoplugin.net/xml.gp?ip=#{ip}")
return response.is_a?(Net::HTTPSuccess) ? parse_xml(response.body) : GeoLoc.new
rescue
logger.error "Caught an error during GeoPluginGeocoder geocoding call: "+$!
return GeoLoc.new
end
def self.parse_xml(xml)
xml = REXML::Document.new(xml)
geo = GeoLoc.new
geo.provider='geoPlugin'
geo.city = xml.elements['//geoplugin_city'].text
geo.state = xml.elements['//geoplugin_region'].text
geo.country_code = xml.elements['//geoplugin_countryCode'].text
geo.lat = xml.elements['//geoplugin_latitude'].text.to_f
geo.lng = xml.elements['//geoplugin_longitude'].text.to_f
geo.success = !!geo.city && !geo.city.empty?
return geo
end
end
# Provides geocoding based upon an IP address. The underlying web service is a hostip.info
# which sources their data through a combination of publicly available information as well
# as community contributions.
class IpGeocoder < Geocoder
private
# Given an IP address, returns a GeoLoc instance which contains latitude,
# longitude, city, and country code. Sets the success attribute to false if the ip
# parameter does not match an ip address.
def self.do_geocode(ip, options = {})
return GeoLoc.new if '0.0.0.0' == ip
return GeoLoc.new unless /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(ip)
url = "http://api.hostip.info/get_html.php?ip=#{ip}&position=true"
response = self.call_geocoder_service(url)
response.is_a?(Net::HTTPSuccess) ? parse_body(response.body) : GeoLoc.new
rescue
logger.error "Caught an error during HostIp geocoding call: "+$!
return GeoLoc.new
end
# Converts the body to YAML since its in the form of:
#
# Country: UNITED STATES (US)
# City: Sugar Grove, IL
# Latitude: 41.7696
# Longitude: -88.4588
#
# then instantiates a GeoLoc instance to populate with location data.
def self.parse_body(body) # :nodoc:
yaml = YAML.load(body)
res = GeoLoc.new
res.provider = 'hostip'
res.city, res.state = yaml['City'].split(', ')
country, res.country_code = yaml['Country'].split(' (')
res.lat = yaml['Latitude']
res.lng = yaml['Longitude']
res.country_code.chop!
res.success = !(res.city =~ /\(.+\)/)
res
end
end
# -------------------------------------------------------------------------------------------
# The Multi Geocoder
# -------------------------------------------------------------------------------------------
# Provides methods to geocode with a variety of geocoding service providers, plus failover
# among providers in the order you configure. When 2nd parameter is set 'true', perform
# ip location lookup with 'address' as the ip address.
#
# Goal:
# - homogenize the results of multiple geocoders
#
# Limitations:
# - currently only provides the first result. Sometimes geocoders will return multiple results.
# - currently discards the "accuracy" component of the geocoding calls
class MultiGeocoder < Geocoder
private
# This method will call one or more geocoders in the order specified in the
# configuration until one of the geocoders work.
#
# The failover approach is crucial for production-grade apps, but is rarely used.
# 98% of your geocoding calls will be successful with the first call
def self.do_geocode(address, options = {})
geocode_ip = /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/.match(address)
provider_order = geocode_ip ? Geokit::Geocoders::ip_provider_order : Geokit::Geocoders::provider_order
provider_order.each do |provider|
begin
klass = Geokit::Geocoders.const_get "#{Geokit::Inflector::camelize(provider.to_s)}Geocoder"
res = klass.send :geocode, address, options
return res if res.success?
rescue
logger.error("Something has gone very wrong during geocoding, OR you have configured an invalid class name in Geokit::Geocoders::provider_order. Address: #{address}. Provider: #{provider}")
end
end
# If we get here, we failed completely.
GeoLoc.new
end
# This method will call one or more geocoders in the order specified in the
# configuration until one of the geocoders work, only this time it's going
# to try to reverse geocode a geographical point.
def self.do_reverse_geocode(latlng)
Geokit::Geocoders::provider_order.each do |provider|
begin
klass = Geokit::Geocoders.const_get "#{Geokit::Inflector::camelize(provider.to_s)}Geocoder"
res = klass.send :reverse_geocode, latlng
return res if res.success?
rescue
logger.error("Something has gone very wrong during reverse geocoding, OR you have configured an invalid class name in Geokit::Geocoders::provider_order. LatLng: #{latlng}. Provider: #{provider}")
end
end
# If we get here, we failed completely.
GeoLoc.new
end
end
end
end