geokit 1.7.1 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/.travis.yml +1 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +2 -1
- data/MIT-LICENSE +20 -0
- data/README.markdown +44 -39
- data/Rakefile +15 -0
- data/fixtures/vcr_cassettes/bing_full.yml +102 -0
- data/fixtures/vcr_cassettes/bing_full_au.yml +91 -0
- data/fixtures/vcr_cassettes/bing_full_de.yml +91 -0
- data/fixtures/vcr_cassettes/fcc_reverse_geocode.yml +37 -0
- data/fixtures/vcr_cassettes/free_geo_ip_geocode.yml +36 -0
- data/fixtures/vcr_cassettes/geo_plugin_geocode.yml +38 -0
- data/fixtures/vcr_cassettes/geonames_geocode.yml +304 -0
- data/fixtures/vcr_cassettes/{google3_city.yml → google_city.yml} +0 -0
- data/fixtures/vcr_cassettes/{google3_country_code_biased_result.yml → google_country_code_biased_result.yml} +0 -0
- data/fixtures/vcr_cassettes/{google3_full.yml → google_full.yml} +0 -0
- data/fixtures/vcr_cassettes/{google3_full_short.yml → google_full_short.yml} +0 -0
- data/fixtures/vcr_cassettes/{google3_language_response_fr.yml → google_language_response_fr.yml} +0 -0
- data/fixtures/vcr_cassettes/{google3_multi.yml → google_multi.yml} +0 -0
- data/fixtures/vcr_cassettes/{google3_reverse_madrid.yml → google_reverse_madrid.yml} +0 -0
- data/fixtures/vcr_cassettes/ripe_geocode.yml +66 -0
- data/fixtures/vcr_cassettes/ripe_geocode_au.yml +66 -0
- data/geokit.gemspec +1 -1
- data/lib/geokit.rb +5 -0
- data/lib/geokit/bounds.rb +96 -0
- data/lib/geokit/core_ext.rb +17 -0
- data/lib/geokit/geo_loc.rb +134 -0
- data/lib/geokit/geocoders.rb +48 -35
- data/lib/geokit/geocoders/base_ip.rb +43 -0
- data/lib/geokit/geocoders/bing.rb +101 -0
- data/lib/geokit/geocoders/ca_geocoder.rb +50 -0
- data/lib/geokit/{services → geocoders}/fcc.rb +17 -20
- data/lib/geokit/geocoders/free_geo_ip.rb +34 -0
- data/lib/geokit/geocoders/geo_plugin.rb +33 -0
- data/lib/geokit/geocoders/geonames.rb +53 -0
- data/lib/geokit/{services/google3.rb → geocoders/google.rb} +59 -57
- data/lib/geokit/geocoders/ip.rb +69 -0
- data/lib/geokit/geocoders/mapquest.rb +72 -0
- data/lib/geokit/geocoders/maxmind.rb +29 -0
- data/lib/geokit/geocoders/openstreetmap.rb +119 -0
- data/lib/geokit/geocoders/ripe.rb +41 -0
- data/lib/geokit/{services → geocoders}/us_geocoder.rb +15 -20
- data/lib/geokit/{services → geocoders}/yahoo.rb +52 -55
- data/lib/geokit/geocoders/yandex.rb +61 -0
- data/lib/geokit/inflectors.rb +1 -2
- data/lib/geokit/lat_lng.rb +129 -0
- data/lib/geokit/mappable.rb +41 -424
- data/lib/geokit/multi_geocoder.rb +6 -2
- data/lib/geokit/polygon.rb +46 -0
- data/lib/geokit/version.rb +1 -1
- data/test/helper.rb +2 -12
- data/test/test_base_geocoder.rb +0 -10
- data/test/test_bing_geocoder.rb +60 -0
- data/test/test_fcc_geocoder.rb +23 -0
- data/test/test_free_geo_ip_geocoder.rb +23 -0
- data/test/test_geo_plugin_geocoder.rb +23 -0
- data/test/test_geonames_geocoder.rb +23 -0
- data/test/test_google_geocoder.rb +208 -235
- data/test/test_maxmind_geocoder.rb +35 -4
- data/test/test_multi_geocoder.rb +3 -1
- data/test/test_ripe_geocoder.rb +35 -0
- data/test/test_yahoo_geocoder.rb +0 -12
- metadata +78 -52
- data/LICENSE +0 -25
- data/Manifest.txt +0 -21
- data/data/GeoLiteCity.dat +0 -0
- data/lib/geokit/services/ca_geocoder.rb +0 -55
- data/lib/geokit/services/geo_plugin.rb +0 -31
- data/lib/geokit/services/geonames.rb +0 -53
- data/lib/geokit/services/google.rb +0 -158
- data/lib/geokit/services/ip.rb +0 -103
- data/lib/geokit/services/maxmind.rb +0 -39
- data/lib/geokit/services/openstreetmap.rb +0 -119
- data/lib/geokit/services/ripe.rb +0 -32
- data/lib/geokit/services/yandex.rb +0 -51
- data/test/test_google_geocoder3.rb +0 -238
- data/test/test_google_reverse_geocoder.rb +0 -49
@@ -1,119 +0,0 @@
|
|
1
|
-
module Geokit
|
2
|
-
module Geocoders
|
3
|
-
# Open Street Map geocoder implementation.
|
4
|
-
class OSMGeocoder < Geocoder
|
5
|
-
|
6
|
-
private
|
7
|
-
|
8
|
-
# Template method which does the geocode lookup.
|
9
|
-
def self.do_geocode(address, options = {})
|
10
|
-
options_str = generate_bool_param_for_option(:polygon, options)
|
11
|
-
options_str << generate_param_for_option(:json_callback, options)
|
12
|
-
options_str << generate_param_for_option(:countrycodes, options)
|
13
|
-
options_str << generate_param_for_option(:viewbox, options)
|
14
|
-
|
15
|
-
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
|
16
|
-
|
17
|
-
#url="http://where.yahooapis.com/geocode?flags=J&appid=#{Geokit::Geocoders::yahoo}&q=#{Geokit::Inflector::url_escape(address_str)}"
|
18
|
-
url="http://nominatim.openstreetmap.org/search?format=json#{options_str}&addressdetails=1&q=#{Geokit::Inflector::url_escape(address_str)}"
|
19
|
-
res = self.call_geocoder_service(url)
|
20
|
-
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
|
21
|
-
json = res.body
|
22
|
-
logger.debug "OSM geocoding. Address: #{address}. Result: #{json}"
|
23
|
-
return self.json2GeoLoc(json, address)
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.do_reverse_geocode(latlng, options = {})
|
27
|
-
latlng = LatLng.normalize(latlng)
|
28
|
-
options_str = generate_param_for(:lat, latlng.lat)
|
29
|
-
options_str << generate_param_for(:lon, latlng.lng)
|
30
|
-
options_str << generate_param_for_option(:zoom, options)
|
31
|
-
options_str << generate_param_for_option(:osm_type, options)
|
32
|
-
options_str << generate_param_for_option(:osm_id, options)
|
33
|
-
options_str << generate_param_for_option(:json_callback, options)
|
34
|
-
url = "http://nominatim.openstreetmap.org/reverse?format=json&addressdetails=1#{options_str}"
|
35
|
-
res = self.call_geocoder_service(url)
|
36
|
-
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
|
37
|
-
json = res.body
|
38
|
-
logger.debug "OSM reverse geocoding: Lat: #{latlng.lat}, Lng: #{latlng.lng}. Result: #{json}"
|
39
|
-
return self.json2GeoLoc(json, latlng)
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.generate_param_for(param, value)
|
43
|
-
"&#{param}=#{Geokit::Inflector::url_escape(value.to_s)}"
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.generate_param_for_option(param, options)
|
47
|
-
options[param] ? "&#{param}=#{Geokit::Inflector::url_escape(options[param])}" : ''
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.generate_bool_param_for_option(param, options)
|
51
|
-
options[param] ? "&#{param}=1" : "&#{param}=0"
|
52
|
-
end
|
53
|
-
|
54
|
-
def self.json2GeoLoc(json, obj)
|
55
|
-
results = MultiJson.load(json)
|
56
|
-
if results.is_a?(Hash)
|
57
|
-
return GeoLoc.new if results['error']
|
58
|
-
results = [results]
|
59
|
-
end
|
60
|
-
unless results.empty?
|
61
|
-
geoloc = nil
|
62
|
-
results.each do |result|
|
63
|
-
extract_geoloc = extract_geoloc(result)
|
64
|
-
if geoloc.nil?
|
65
|
-
geoloc = extract_geoloc
|
66
|
-
else
|
67
|
-
geoloc.all.push(extract_geoloc)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
return geoloc
|
71
|
-
else
|
72
|
-
logger.info "OSM was unable to geocode #{obj}"
|
73
|
-
return GeoLoc.new
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def self.extract_geoloc(result_json)
|
78
|
-
geoloc = GeoLoc.new
|
79
|
-
|
80
|
-
# basic
|
81
|
-
geoloc.lat = result_json['lat']
|
82
|
-
geoloc.lng = result_json['lon']
|
83
|
-
|
84
|
-
geoloc.provider = 'osm'
|
85
|
-
geoloc.precision = result_json['class']
|
86
|
-
geoloc.accuracy = result_json['type']
|
87
|
-
|
88
|
-
# Todo accuracy does not work as Yahoo and Google maps on OSM
|
89
|
-
#geoloc.accuracy = %w{unknown amenity building highway historic landuse leisure natural place railway shop tourism waterway man_made}.index(geoloc.precision)
|
90
|
-
#geoloc.full_address = result_json['display_name']
|
91
|
-
if result_json['address']
|
92
|
-
address_data = result_json['address']
|
93
|
-
|
94
|
-
geoloc.country = address_data['country']
|
95
|
-
geoloc.country_code = address_data['country_code'].upcase if address_data['country_code']
|
96
|
-
geoloc.state = address_data['state']
|
97
|
-
geoloc.city = address_data['city']
|
98
|
-
geoloc.city = address_data['county'] if geoloc.city.nil? && address_data['county']
|
99
|
-
geoloc.zip = address_data['postcode']
|
100
|
-
geoloc.district = address_data['city_district']
|
101
|
-
geoloc.district = address_data['state_district'] if geoloc.district.nil? && address_data['state_district']
|
102
|
-
geoloc.street_address = "#{address_data['road']} #{address_data['house_number']}".strip if address_data['road']
|
103
|
-
geoloc.street_name = address_data['road']
|
104
|
-
geoloc.street_number = address_data['house_number']
|
105
|
-
end
|
106
|
-
|
107
|
-
if result_json['boundingbox']
|
108
|
-
geoloc.suggested_bounds = Bounds.normalize(
|
109
|
-
[result_json['boundingbox'][0], result_json['boundingbox'][1]],
|
110
|
-
[result_json['boundingbox'][2], result_json['boundingbox'][3]])
|
111
|
-
end
|
112
|
-
|
113
|
-
geoloc.success = true
|
114
|
-
|
115
|
-
return geoloc
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
data/lib/geokit/services/ripe.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
module Geokit
|
2
|
-
module Geocoders
|
3
|
-
# Provides geocoding based upon an IP address. The underlying web service is geoplugin.net
|
4
|
-
class RipeGeocoder < Geocoder
|
5
|
-
private
|
6
|
-
|
7
|
-
def self.do_geocode(ip, options = {})
|
8
|
-
return GeoLoc.new unless /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(ip)
|
9
|
-
response = self.call_geocoder_service("http://stat.ripe.net/data/geoloc/data.json?resource=#{ip}")
|
10
|
-
return response.is_a?(Net::HTTPSuccess) ? parse_json(response.body) : GeoLoc.new
|
11
|
-
rescue
|
12
|
-
logger.error "Caught an error during GeoPluginGeocoder geocoding call: #{$!}"
|
13
|
-
return GeoLoc.new
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.parse_json(json)
|
17
|
-
json = MultiJson.load(json)
|
18
|
-
geo = GeoLoc.new
|
19
|
-
data = json['data']['locations'][0]
|
20
|
-
|
21
|
-
geo.provider='RIPE'
|
22
|
-
geo.city = data['city']
|
23
|
-
geo.country_code = data['country']
|
24
|
-
geo.lat = data['latitude']
|
25
|
-
geo.lng = data['longitude']
|
26
|
-
geo.success = (data['status_code'] == 200)
|
27
|
-
return geo
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
module Geokit
|
2
|
-
module Geocoders
|
3
|
-
# Yandex geocoder implementation. Expects the Geokit::Geocoders::YANDEX variable to
|
4
|
-
# contain a Yandex API key (optional). Conforms to the interface set by the Geocoder class.
|
5
|
-
class YandexGeocoder < Geocoder
|
6
|
-
private
|
7
|
-
|
8
|
-
# Template method which does the geocode lookup.
|
9
|
-
def self.do_geocode(address, options = {})
|
10
|
-
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
|
11
|
-
url = "http://geocode-maps.yandex.ru/1.x/?geocode=#{Geokit::Inflector::url_escape(address_str)}&format=json"
|
12
|
-
url += "&key=#{Geokit::Geocoders::yandex}" if Geokit::Geocoders::yandex != 'REPLACE_WITH_YOUR_YANDEX_KEY'
|
13
|
-
res = self.call_geocoder_service(url)
|
14
|
-
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
|
15
|
-
json = res.body
|
16
|
-
# logger.debug "Yandex geocoding. Address: #{address}. Result: #{json}"
|
17
|
-
return self.json2GeoLoc(json, address)
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.json2GeoLoc(json, address)
|
21
|
-
geoloc = GeoLoc.new
|
22
|
-
|
23
|
-
result = MultiJson.load(json)
|
24
|
-
if result["response"]["GeoObjectCollection"]["metaDataProperty"]["GeocoderResponseMetaData"]["found"].to_i > 0
|
25
|
-
loc = result["response"]["GeoObjectCollection"]["featureMember"][0]["GeoObject"]
|
26
|
-
|
27
|
-
geoloc.success = true
|
28
|
-
geoloc.provider = "yandex"
|
29
|
-
geoloc.lng = loc["Point"]["pos"].split(" ").first
|
30
|
-
geoloc.lat = loc["Point"]["pos"].split(" ").last
|
31
|
-
geoloc.country_code = loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["CountryNameCode"]
|
32
|
-
geoloc.full_address = loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["AddressLine"]
|
33
|
-
geoloc.street_address = loc["name"]
|
34
|
-
|
35
|
-
locality = loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["Locality"] || loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["AdministrativeArea"]["Locality"] || loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["AdministrativeArea"]["SubAdministrativeArea"]["Locality"] rescue nil
|
36
|
-
geoloc.street_number = locality["Thoroughfare"]["Premise"]["PremiseNumber"] rescue nil
|
37
|
-
geoloc.street_name = locality["Thoroughfare"]["ThoroughfareName"] rescue nil
|
38
|
-
geoloc.city = locality["LocalityName"] rescue nil
|
39
|
-
geoloc.state = loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["AdministrativeArea"]["AdministrativeAreaName"] rescue nil
|
40
|
-
geoloc.state ||= loc["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]["Locality"]["LocalityName"] rescue nil
|
41
|
-
geoloc.precision = loc["metaDataProperty"]["GeocoderMetaData"]["precision"].sub(/exact/, "building").sub(/number|near/, "address").sub(/other/, "city")
|
42
|
-
geoloc.precision = "country" unless locality
|
43
|
-
else
|
44
|
-
logger.info "Yandex was unable to geocode address: " + address
|
45
|
-
end
|
46
|
-
|
47
|
-
return geoloc
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
@@ -1,238 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
require File.join(File.dirname(__FILE__), 'helper')
|
3
|
-
|
4
|
-
Geokit::Geocoders::google = 'Google'
|
5
|
-
Geokit::Geocoders::google_client_id = nil
|
6
|
-
Geokit::Geocoders::google_cryptographic_key = nil
|
7
|
-
Geokit::Geocoders::google_channel = nil
|
8
|
-
|
9
|
-
class GoogleGeocoder3Test < BaseGeocoderTest #:nodoc: all
|
10
|
-
|
11
|
-
def setup
|
12
|
-
super
|
13
|
-
@full_address = '100 Spear St Apt. 5, San Francisco, CA, 94105-1522, US'
|
14
|
-
@full_address_short_zip = '100 Spear St Apt. 5, San Francisco, CA, 94105, US'
|
15
|
-
@google_full_hash = {:street_address=>"100 Spear St Apt. 5", :city=>"San Francisco", :state=>"CA", :zip=>"94105", :country_code=>"US"}
|
16
|
-
@google_city_hash = {:city=>"San Francisco", :state=>"CA"}
|
17
|
-
|
18
|
-
@google_full_loc = Geokit::GeoLoc.new(@google_full_hash)
|
19
|
-
@google_city_loc = Geokit::GeoLoc.new(@google_city_hash)
|
20
|
-
end
|
21
|
-
|
22
|
-
|
23
|
-
# Example from:
|
24
|
-
# https://developers.google.com/maps/documentation/business/webservices#signature_examples
|
25
|
-
def test_google3_signature
|
26
|
-
cryptographic_key = 'vNIXE0xscrmjlyV-12Nj_BvUPaw='
|
27
|
-
query_string = '/maps/api/geocode/json?address=New+York&sensor=false&client=clientID'
|
28
|
-
signature = Geokit::Geocoders::GoogleGeocoder3.send(:sign_gmap_bus_api_url, query_string, cryptographic_key)
|
29
|
-
assert_equal 'KrU1TzVQM7Ur0i8i7K3huiw3MsA=', signature
|
30
|
-
end
|
31
|
-
|
32
|
-
|
33
|
-
# Example from:
|
34
|
-
# https://developers.google.com/maps/documentation/business/webservices#signature_examples
|
35
|
-
def test_google3_signature_and_url
|
36
|
-
Geokit::Geocoders::google_client_id = 'clientID'
|
37
|
-
Geokit::Geocoders::google_cryptographic_key = 'vNIXE0xscrmjlyV-12Nj_BvUPaw='
|
38
|
-
url = Geokit::Geocoders::GoogleGeocoder3.send(:submit_url, '/maps/api/geocode/json?address=New+York&sensor=false')
|
39
|
-
Geokit::Geocoders::google_client_id = nil
|
40
|
-
Geokit::Geocoders::google_cryptographic_key = nil
|
41
|
-
assert_equal 'http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA=', url
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_google3_full_address
|
45
|
-
VCR.use_cassette('google3_full_short') do
|
46
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
|
47
|
-
TestHelper.expects(:last_url).with(url)
|
48
|
-
res=Geokit::Geocoders::GoogleGeocoder3.geocode(@address)
|
49
|
-
assert_equal "CA", res.state
|
50
|
-
assert_equal "San Francisco", res.city
|
51
|
-
assert_array_in_delta [37.7749295, -122.4194155], res.to_a # slightly dif from yahoo
|
52
|
-
assert res.is_us?
|
53
|
-
assert_equal "San Francisco, CA, USA", res.full_address #slightly different from yahoo
|
54
|
-
assert_equal "google3", res.provider
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_google3_full_address_with_geo_loc
|
59
|
-
VCR.use_cassette('google3_full') do
|
60
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@full_address_short_zip)}"
|
61
|
-
TestHelper.expects(:last_url).with(url)
|
62
|
-
res=Geokit::Geocoders::GoogleGeocoder3.geocode(@google_full_loc)
|
63
|
-
assert_equal "CA", res.state
|
64
|
-
assert_equal "San Francisco", res.city
|
65
|
-
assert_array_in_delta [37.7921509, -122.394], res.to_a # slightly dif from yahoo
|
66
|
-
assert res.is_us?
|
67
|
-
assert_equal "100 Spear Street #5, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
|
68
|
-
assert_equal "google3", res.provider
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_google3_full_address_accuracy
|
73
|
-
VCR.use_cassette('google3_full') do
|
74
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@full_address_short_zip)}"
|
75
|
-
TestHelper.expects(:last_url).with(url)
|
76
|
-
res=Geokit::Geocoders::GoogleGeocoder3.geocode(@google_full_loc)
|
77
|
-
|
78
|
-
assert_equal 9, res.accuracy
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_google3_city
|
83
|
-
VCR.use_cassette('google3_city') do
|
84
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
|
85
|
-
TestHelper.expects(:last_url).with(url)
|
86
|
-
res=Geokit::Geocoders::GoogleGeocoder3.do_geocode(@address)
|
87
|
-
assert_nil res.street_address
|
88
|
-
assert_equal "CA", res.state
|
89
|
-
assert_equal "San Francisco", res.city
|
90
|
-
assert_equal "37.7749295,-122.4194155", res.ll
|
91
|
-
assert res.is_us?
|
92
|
-
assert_equal "San Francisco, CA, USA", res.full_address
|
93
|
-
assert_equal "google3", res.provider
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
def test_google3_city_accuracy
|
98
|
-
VCR.use_cassette('google3_city') do
|
99
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
|
100
|
-
TestHelper.expects(:last_url).with(url)
|
101
|
-
res=Geokit::Geocoders::GoogleGeocoder3.geocode(@address)
|
102
|
-
assert_equal 4, res.accuracy
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_google3_city_with_geo_loc
|
107
|
-
VCR.use_cassette('google3_city') do
|
108
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
|
109
|
-
TestHelper.expects(:last_url).with(url)
|
110
|
-
res=Geokit::Geocoders::GoogleGeocoder3.geocode(@google_city_loc)
|
111
|
-
assert_equal "CA", res.state
|
112
|
-
assert_equal "San Francisco", res.city
|
113
|
-
assert_equal "37.7749295,-122.4194155", res.ll
|
114
|
-
assert res.is_us?
|
115
|
-
assert_equal "San Francisco, CA, USA", res.full_address
|
116
|
-
assert_nil res.street_address
|
117
|
-
assert_equal "google3", res.provider
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
def test_google3_suggested_bounds
|
122
|
-
VCR.use_cassette('google3_full') do
|
123
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@full_address_short_zip)}"
|
124
|
-
TestHelper.expects(:last_url).with(url)
|
125
|
-
res = Geokit::Geocoders::GoogleGeocoder3.geocode(@google_full_loc)
|
126
|
-
assert_instance_of Geokit::Bounds, res.suggested_bounds
|
127
|
-
assert_array_in_delta [37.7908019197085, -122.3953489802915], res.suggested_bounds.sw.to_a
|
128
|
-
assert_array_in_delta [37.7934998802915, -122.3926510197085], res.suggested_bounds.ne.to_a
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def test_google3_suggested_bounds_url
|
133
|
-
bounds = Geokit::Bounds.new(
|
134
|
-
Geokit::LatLng.new(33.7036917, -118.6681759),
|
135
|
-
Geokit::LatLng.new(34.3373061, -118.1552891)
|
136
|
-
)
|
137
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=Winnetka&bounds=33.7036917%2C-118.6681759%7C34.3373061%2C-118.1552891"
|
138
|
-
Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url)
|
139
|
-
Geokit::Geocoders::GoogleGeocoder3.geocode('Winnetka', :bias => bounds)
|
140
|
-
end
|
141
|
-
|
142
|
-
def test_service_unavailable
|
143
|
-
response = MockFailure.new
|
144
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector::url_escape(@address)}"
|
145
|
-
Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
|
146
|
-
assert !Geokit::Geocoders::GoogleGeocoder3.geocode(@google_city_loc).success
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_multiple_results
|
150
|
-
VCR.use_cassette('google3_multi') do
|
151
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape('via Sandro Pertini 8, Ossona, MI')}"
|
152
|
-
TestHelper.expects(:last_url).with(url)
|
153
|
-
res=Geokit::Geocoders::GoogleGeocoder3.geocode('via Sandro Pertini 8, Ossona, MI')
|
154
|
-
assert_equal 5, res.all.size
|
155
|
-
res = res.all[0]
|
156
|
-
assert_equal "Lombardy", res.state
|
157
|
-
assert_equal "Mesero", res.city
|
158
|
-
assert_array_in_delta [45.4966218, 8.852694], res.to_a
|
159
|
-
assert !res.is_us?
|
160
|
-
assert_equal "Via Sandro Pertini, 8, 20010 Mesero Milan, Italy", res.full_address
|
161
|
-
assert_equal "8 Via Sandro Pertini", res.street_address
|
162
|
-
assert_equal "google3", res.provider
|
163
|
-
|
164
|
-
res = res.all[4]
|
165
|
-
assert_equal "Lombardy", res.state
|
166
|
-
assert_equal "Ossona", res.city
|
167
|
-
assert_array_in_delta [45.5074444, 8.90232], res.to_a
|
168
|
-
assert !res.is_us?
|
169
|
-
assert_equal "Via S. Pertini, 20010 Ossona Milan, Italy", res.full_address
|
170
|
-
assert_equal "Via S. Pertini", res.street_address
|
171
|
-
assert_equal "google3", res.provider
|
172
|
-
end
|
173
|
-
end
|
174
|
-
#
|
175
|
-
def test_reverse_geocode
|
176
|
-
VCR.use_cassette('google3_reverse_madrid') do
|
177
|
-
madrid = Geokit::GeoLoc.new
|
178
|
-
madrid.lat, madrid.lng = "40.4167413", "-3.7032498"
|
179
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&latlng=#{Geokit::Inflector::url_escape(madrid.ll)}"
|
180
|
-
TestHelper.expects(:last_url).with(url)
|
181
|
-
res=Geokit::Geocoders::GoogleGeocoder3.do_reverse_geocode(madrid.ll)
|
182
|
-
|
183
|
-
assert_equal madrid.lat.to_s.slice(1..5), res.lat.to_s.slice(1..5)
|
184
|
-
assert_equal madrid.lng.to_s.slice(1..5), res.lng.to_s.slice(1..5)
|
185
|
-
assert_equal "ES", res.country_code
|
186
|
-
assert_equal "google3", res.provider
|
187
|
-
|
188
|
-
assert_equal "Madrid", res.city
|
189
|
-
assert_equal "Community of Madrid", res.state
|
190
|
-
|
191
|
-
assert_equal "Spain", res.country
|
192
|
-
assert_equal "28013", res.zip
|
193
|
-
assert_equal true, res.success
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
def test_country_code_biasing
|
198
|
-
VCR.use_cassette('google3_country_code_biased_result') do
|
199
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=Syracuse®ion=it"
|
200
|
-
TestHelper.expects(:last_url).with(url)
|
201
|
-
biased_result = Geokit::Geocoders::GoogleGeocoder3.geocode('Syracuse', :bias => 'it')
|
202
|
-
|
203
|
-
assert_equal 'IT', biased_result.country_code
|
204
|
-
assert_equal 'Sicilia', biased_result.state
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
def test_language_response
|
209
|
-
VCR.use_cassette('google3_language_response_fr') do
|
210
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=Hanoi&language=FR"
|
211
|
-
TestHelper.expects(:last_url).with(url)
|
212
|
-
language_result = Geokit::Geocoders::GoogleGeocoder3.geocode('Hanoi', :language => 'FR')
|
213
|
-
|
214
|
-
assert_equal 'VN', language_result.country_code
|
215
|
-
assert_equal 'Hanoï', language_result.city
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|
219
|
-
def test_too_many_queries
|
220
|
-
response = MockSuccess.new
|
221
|
-
response.expects(:body).returns %q/{"status": "OVER_QUERY_LIMIT"}/
|
222
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape(@address)}"
|
223
|
-
Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
|
224
|
-
assert_raise Geokit::TooManyQueriesError do
|
225
|
-
res=Geokit::Geocoders::GoogleGeocoder3.geocode(@address)
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
def test_invalid_request
|
230
|
-
response = MockSuccess.new
|
231
|
-
response.expects(:body).returns %q/{"results" : [], "status" : "INVALID_REQUEST"}/
|
232
|
-
url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=#{Geokit::Inflector.url_escape("3961 V\u00EDa Marisol")}"
|
233
|
-
Geokit::Geocoders::GoogleGeocoder3.expects(:call_geocoder_service).with(url).returns(response)
|
234
|
-
assert_raise Geokit::Geocoders::GeocodeError do
|
235
|
-
Geokit::Geocoders::GoogleGeocoder3.geocode("3961 V\u00EDa Marisol")
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'helper')
|
2
|
-
|
3
|
-
Geokit::Geocoders::google = 'Google'
|
4
|
-
|
5
|
-
class GoogleReverseGeocoderTest < BaseGeocoderTest #:nodoc: all
|
6
|
-
|
7
|
-
GOOGLE_REVERSE_FULL=<<-EOF.strip
|
8
|
-
<?xml version="1.0" encoding="UTF-8" ?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>51.457833,7.016685</name><Status><code>200</code><request>geocode</request></Status><Placemark id="p1"><address>Porscheplatz 1, 45127 Essen, Deutschland</address><AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName><DependentLocality><DependentLocalityName>Stadtkern</DependentLocalityName><Thoroughfare><ThoroughfareName>Porscheplatz 1</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>45127</PostalCodeNumber></PostalCode></DependentLocality></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.4609805" south="51.4546853" east="7.0198324" west="7.0135372" /></ExtendedData><Point><coordinates>7.0166848,51.4578329,0</coordinates></Point></Placemark><Placemark id="p2"><address>Stadtkern, Essen, Deutschland</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName><DependentLocality><DependentLocalityName>Stadtkern</DependentLocalityName></DependentLocality></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.4630710" south="51.4506320" east="7.0193200" west="7.0026170" /></ExtendedData><Point><coordinates>7.0124328,51.4568201,0</coordinates></Point></Placemark><Placemark id="p3"><address>45127 Essen, Deutschland</address><AddressDetails Accuracy="5" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName><PostalCode><PostalCodeNumber>45127</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.4637808" south="51.4503125" east="7.0231080" west="6.9965454" /></ExtendedData><Point><coordinates>7.0104543,51.4556194,0</coordinates></Point></Placemark><Placemark id="p4"><address>Essen, Deutschland</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.5342070" south="51.3475730" east="7.1376530" west="6.8943470" /></ExtendedData><Point><coordinates>7.0147614,51.4580686,0</coordinates></Point></Placemark><Placemark id="p5"><address>Essen, Deutschland</address><AddressDetails Accuracy="3" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.5342070" south="51.3475730" east="7.1376530" west="6.8943470" /></ExtendedData><Point><coordinates>7.0461136,51.4508381,0</coordinates></Point></Placemark><Placemark id="p6"><address>Nordrhein-Westfalen, Deutschland</address><AddressDetails Accuracy="2" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="52.5314170" south="50.3225720" east="9.4615950" west="5.8663566" /></ExtendedData><Point><coordinates>7.6615938,51.4332367,0</coordinates></Point></Placemark><Placemark id="p7"><address>Deutschland</address><AddressDetails Accuracy="1" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName></Country></AddressDetails><ExtendedData><LatLonBox north="55.0568230" south="47.2701270" east="15.0418536" west="5.8663566" /></ExtendedData><Point><coordinates>10.4515260,51.1656910,0</coordinates></Point></Placemark></Response></kml>
|
9
|
-
EOF
|
10
|
-
|
11
|
-
|
12
|
-
def test_google_full_address
|
13
|
-
response = MockSuccess.new
|
14
|
-
response.expects(:body).returns(GOOGLE_REVERSE_FULL)
|
15
|
-
|
16
|
-
|
17
|
-
# http://maps.google.com/maps/geo?output=xml&oe=utf-8&ll=51.4578329,7.0166848&key=asdad
|
18
|
-
|
19
|
-
# #<Geokit::GeoLoc:0x10ec7ec
|
20
|
-
# @city="Essen",
|
21
|
-
# @country_code="DE",
|
22
|
-
# @full_address="Porscheplatz 1, 45127 Essen, Germany",
|
23
|
-
# @lat=51.4578329,
|
24
|
-
# @lng=7.0166848,
|
25
|
-
# @precision="address",
|
26
|
-
# @provider="google",
|
27
|
-
# @state="Nordrhein-Westfalen",
|
28
|
-
# @street_address="Porscheplatz 1",
|
29
|
-
# @success=true,
|
30
|
-
# @zip="45127">
|
31
|
-
#
|
32
|
-
|
33
|
-
|
34
|
-
@latlng = "51.4578329,7.0166848"
|
35
|
-
|
36
|
-
url = "http://maps.google.com/maps/geo?ll=#{Geokit::Inflector.url_escape(@latlng)}&output=xml&key=Google&oe=utf-8"
|
37
|
-
Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
|
38
|
-
res=Geokit::Geocoders::GoogleGeocoder.reverse_geocode(@latlng)
|
39
|
-
assert_equal "Nordrhein-Westfalen", res.state
|
40
|
-
assert_equal "Essen", res.city
|
41
|
-
assert_equal "45127", res.zip
|
42
|
-
assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
|
43
|
-
assert res.is_us? == false
|
44
|
-
assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
|
45
|
-
assert_equal "google", res.provider
|
46
|
-
end
|
47
|
-
|
48
|
-
|
49
|
-
end
|