geokit 1.8.5 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +0 -1
- data/CHANGELOG.md +7 -0
- data/README.markdown +31 -14
- data/fixtures/vcr_cassettes/geonames_geocode.yml +1 -1
- data/fixtures/vcr_cassettes/geonames_geocode_premium.yml +42 -0
- data/fixtures/vcr_cassettes/google_country_code_biased_result_orly.yml +160 -0
- data/fixtures/vcr_cassettes/google_country_code_biased_result_toledo.yml +111 -0
- data/fixtures/vcr_cassettes/google_result_toledo_default_bias.yml +275 -0
- data/fixtures/vcr_cassettes/google_sublocality.yml +126 -0
- data/fixtures/vcr_cassettes/mapbox_forward_geocode.yml +59 -0
- data/fixtures/vcr_cassettes/mapbox_reverse_geocode.yml +63 -0
- data/fixtures/vcr_cassettes/opencage_city.yml +51 -0
- data/fixtures/vcr_cassettes/opencage_full.yml +65 -0
- data/fixtures/vcr_cassettes/opencage_language_response_es.yml +66 -0
- data/fixtures/vcr_cassettes/opencage_reverse_madrid.yml +51 -0
- data/fixtures/vcr_cassettes/opencage_reverse_prilep.yml +53 -0
- data/geokit.gemspec +5 -3
- data/lib/geokit/bounds.rb +40 -31
- data/lib/geokit/core_ext.rb +1 -1
- data/lib/geokit/geo_loc.rb +63 -41
- data/lib/geokit/geocoders.rb +13 -13
- data/lib/geokit/geocoders/bing.rb +9 -9
- data/lib/geokit/geocoders/ca_geocoder.rb +29 -29
- data/lib/geokit/geocoders/fcc.rb +4 -4
- data/lib/geokit/geocoders/free_geo_ip.rb +6 -7
- data/lib/geokit/geocoders/geo_plugin.rb +5 -6
- data/lib/geokit/geocoders/geocodio.rb +2 -2
- data/lib/geokit/geocoders/geonames.rb +18 -12
- data/lib/geokit/geocoders/google.rb +31 -30
- data/lib/geokit/geocoders/ip.rb +3 -4
- data/lib/geokit/geocoders/mapbox.rb +94 -0
- data/lib/geokit/geocoders/mapquest.rb +5 -5
- data/lib/geokit/geocoders/opencage.rb +93 -0
- data/lib/geokit/geocoders/openstreetmap.rb +9 -6
- data/lib/geokit/geocoders/ripe.rb +3 -3
- data/lib/geokit/geocoders/us_geocoder.rb +10 -9
- data/lib/geokit/geocoders/yahoo.rb +33 -34
- data/lib/geokit/geocoders/yandex.rb +17 -17
- data/lib/geokit/inflectors.rb +4 -10
- data/lib/geokit/lat_lng.rb +50 -26
- data/lib/geokit/mappable.rb +83 -83
- data/lib/geokit/multi_geocoder.rb +25 -20
- data/lib/geokit/net_adapter/net_http.rb +7 -4
- data/lib/geokit/polygon.rb +36 -4
- data/lib/geokit/version.rb +1 -1
- data/test/helper.rb +15 -13
- data/test/test_base_geocoder.rb +6 -7
- data/test/test_bing_geocoder.rb +20 -21
- data/test/test_bounds.rb +26 -28
- data/test/test_ca_geocoder.rb +9 -10
- data/test/test_fcc_geocoder.rb +1 -1
- data/test/test_free_geo_ip_geocoder.rb +1 -1
- data/test/test_geo_plugin_geocoder.rb +9 -9
- data/test/test_geoloc.rb +7 -6
- data/test/test_geonames_geocoder.rb +28 -6
- data/test/test_google_geocoder.rb +210 -176
- data/test/test_inflector.rb +0 -1
- data/test/test_ipgeocoder.rb +17 -18
- data/test/test_latlng.rb +105 -85
- data/test/test_map_quest.rb +18 -21
- data/test/test_mapbox_geocoder.rb +31 -0
- data/test/test_mappable.rb +46 -0
- data/test/test_maxmind_geocoder.rb +1 -3
- data/test/test_multi_geocoder.rb +8 -9
- data/test/test_multi_ip_geocoder.rb +3 -5
- data/test/test_net_adapter.rb +4 -4
- data/test/test_opencage_geocoder.rb +108 -0
- data/test/test_openstreetmap_geocoder.rb +62 -44
- data/test/{test_polygon_contains.rb → test_polygon.rb} +30 -20
- data/test/test_ripe_geocoder.rb +2 -0
- data/test/test_us_geocoder.rb +7 -8
- data/test/test_yahoo_geocoder.rb +20 -21
- data/test/test_yandex_geocoder.rb +34 -35
- metadata +79 -56
- data/fixtures/vcr_cassettes/google_country_code_biased_result.yml +0 -401
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'helper')
|
2
|
+
|
3
|
+
Geokit::Geocoders::MapboxGeocoder.key = ''
|
4
|
+
|
5
|
+
class MapboxGeocoderTest < BaseGeocoderTest #:nodoc: all
|
6
|
+
def setup
|
7
|
+
super
|
8
|
+
@address = '1714 14th Street NW, Washington, DC'
|
9
|
+
@latlng = Geokit::LatLng.new(38.913175, -77.032458)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_forward_geocode
|
13
|
+
VCR.use_cassette('mapbox_forward_geocode') do
|
14
|
+
res = Geokit::Geocoders::MapboxGeocoder.geocode(@address)
|
15
|
+
assert_equal res.lat, 38.899393
|
16
|
+
assert_equal res.lng, -76.992695
|
17
|
+
assert_equal res.country_code, 'US'
|
18
|
+
assert_equal res.state, 'District of Columbia'
|
19
|
+
assert_equal res.zip, '20002'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_reverse_geocode
|
24
|
+
VCR.use_cassette('mapbox_reverse_geocode') do
|
25
|
+
res = Geokit::Geocoders::MapboxGeocoder.reverse_geocode(@latlng)
|
26
|
+
assert_equal res.country_code, 'US'
|
27
|
+
assert_equal res.state, 'District of Columbia'
|
28
|
+
assert_equal res.zip, '20009'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/test/test_mappable.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'helper')
|
2
2
|
|
3
|
+
# Test distances from http://www.csgnetwork.com/degreelenllavcalc.html
|
3
4
|
class MappableTest < Test::Unit::TestCase #:nodoc: all
|
5
|
+
class TestMappable
|
6
|
+
include Geokit::Mappable
|
7
|
+
end
|
4
8
|
|
5
9
|
def test_math_error_classes
|
6
10
|
error_case = 0.49 * 180
|
@@ -9,4 +13,46 @@ class MappableTest < Test::Unit::TestCase #:nodoc: all
|
|
9
13
|
assert_equal 0.0, Geokit::LatLng.distance_between_sphere(from, to, :kms)
|
10
14
|
end
|
11
15
|
|
16
|
+
def test_units_sphere_multiplier
|
17
|
+
delta = 0.001
|
18
|
+
assert_in_delta 3963.190, TestMappable.units_sphere_multiplier(:miles), delta
|
19
|
+
assert_in_delta 6376.773, TestMappable.units_sphere_multiplier(:kms), delta
|
20
|
+
assert_in_delta 3443.918, TestMappable.units_sphere_multiplier(:nms), delta
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_units_per_latitude_degree
|
24
|
+
delta = 0.00001
|
25
|
+
assert_in_delta 69.10000, TestMappable.units_per_latitude_degree(:miles), delta
|
26
|
+
assert_in_delta 111.18190, TestMappable.units_per_latitude_degree(:kms), delta
|
27
|
+
assert_in_delta 60.04625, TestMappable.units_per_latitude_degree(:nms), delta
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_units_per_longitude_degree_miles
|
31
|
+
delta = 0.2
|
32
|
+
assert_in_delta 69.170565, TestMappable.units_per_longitude_degree( 0, :miles), delta
|
33
|
+
assert_in_delta 66.828621, TestMappable.units_per_longitude_degree( 15, :miles), delta
|
34
|
+
assert_in_delta 59.953655, TestMappable.units_per_longitude_degree( 30, :miles), delta
|
35
|
+
assert_in_delta 48.993036, TestMappable.units_per_longitude_degree( 45, :miles), delta
|
36
|
+
assert_in_delta 34.672430, TestMappable.units_per_longitude_degree( 60, :miles), delta
|
37
|
+
assert_in_delta 17.958830, TestMappable.units_per_longitude_degree( 75, :miles), delta
|
38
|
+
assert_in_delta 0.000000, TestMappable.units_per_longitude_degree( 90, :miles), delta
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_units_per_longitude_degree_kms
|
42
|
+
delta = 0.2
|
43
|
+
assert_in_delta 111.319458, TestMappable.units_per_longitude_degree( 0, :kms), delta
|
44
|
+
assert_in_delta 107.550455, TestMappable.units_per_longitude_degree( 15, :kms), delta
|
45
|
+
assert_in_delta 96.486248, TestMappable.units_per_longitude_degree( 30, :kms), delta
|
46
|
+
assert_in_delta 78.846806, TestMappable.units_per_longitude_degree( 45, :kms), delta
|
47
|
+
assert_in_delta 55.799979, TestMappable.units_per_longitude_degree( 60, :kms), delta
|
48
|
+
assert_in_delta 28.901993, TestMappable.units_per_longitude_degree( 75, :kms), delta
|
49
|
+
assert_in_delta 0.000000, TestMappable.units_per_longitude_degree( 90, :kms), delta
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_units_per_longitude_degree_nms
|
53
|
+
delta = 0.2
|
54
|
+
assert_in_delta 60.107578, TestMappable.units_per_longitude_degree( 0, :nms), delta
|
55
|
+
assert_in_delta 42.573784, TestMappable.units_per_longitude_degree( 45, :nms), delta
|
56
|
+
assert_in_delta 0.000000, TestMappable.units_per_longitude_degree( 90, :nms), delta
|
57
|
+
end
|
12
58
|
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'helper')
|
2
2
|
|
3
3
|
class MaxmindGeocoderTest < BaseGeocoderTest #:nodoc: all
|
4
|
-
|
5
4
|
def setup
|
6
5
|
super
|
7
6
|
@ip = '118.210.47.142'
|
@@ -44,5 +43,4 @@ class MaxmindGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
44
43
|
assert_equal true, res.success
|
45
44
|
assert res.city
|
46
45
|
end
|
47
|
-
|
48
|
-
end
|
46
|
+
end
|
data/test/test_multi_geocoder.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'helper')
|
2
2
|
|
3
|
-
Geokit::Geocoders
|
3
|
+
Geokit::Geocoders.provider_order = [:google, :bing, :us]
|
4
4
|
|
5
5
|
class MultiGeocoderTest < BaseGeocoderTest #:nodoc: all
|
6
|
-
|
7
6
|
def setup
|
8
7
|
super
|
9
8
|
@failure = Geokit::GeoLoc.new
|
@@ -35,7 +34,7 @@ class MultiGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
35
34
|
end
|
36
35
|
|
37
36
|
def test_invalid_provider
|
38
|
-
temp = Geokit::Geocoders
|
37
|
+
temp = Geokit::Geocoders.provider_order
|
39
38
|
Geokit::Geocoders.provider_order = [:bogus]
|
40
39
|
assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@address)
|
41
40
|
Geokit::Geocoders.provider_order = temp
|
@@ -45,9 +44,9 @@ class MultiGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
45
44
|
t1, t2 = Geokit::Geocoders.provider_order, Geokit::Geocoders.ip_provider_order # will need to reset after
|
46
45
|
Geokit::Geocoders.provider_order = [:google]
|
47
46
|
Geokit::Geocoders.ip_provider_order = [:geo_plugin]
|
48
|
-
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(
|
47
|
+
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with('').returns(@failure)
|
49
48
|
Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).never
|
50
|
-
assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(
|
49
|
+
assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode('')
|
51
50
|
Geokit::Geocoders.provider_order, Geokit::Geocoders.ip_provider_order = t1, t2 # reset to orig values
|
52
51
|
end
|
53
52
|
|
@@ -77,7 +76,7 @@ class MultiGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
77
76
|
end
|
78
77
|
|
79
78
|
def test_reverse_geocode_with_invalid_provider
|
80
|
-
temp = Geokit::Geocoders
|
79
|
+
temp = Geokit::Geocoders.provider_order
|
81
80
|
Geokit::Geocoders.provider_order = [:bogus]
|
82
81
|
assert_raise NameError do
|
83
82
|
Geokit::Geocoders::MultiGeocoder.reverse_geocode(@latlng)
|
@@ -88,8 +87,8 @@ class MultiGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
88
87
|
def test_reverse_geocode_with_blank_latlng
|
89
88
|
t1 = Geokit::Geocoders.provider_order # will need to reset after
|
90
89
|
Geokit::Geocoders.provider_order = [:google]
|
91
|
-
Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(
|
92
|
-
assert_equal @failure, Geokit::Geocoders::MultiGeocoder.reverse_geocode(
|
90
|
+
Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with('').returns(@failure)
|
91
|
+
assert_equal @failure, Geokit::Geocoders::MultiGeocoder.reverse_geocode('')
|
93
92
|
Geokit::Geocoders.provider_order = t1 # reset to orig values
|
94
93
|
end
|
95
94
|
|
@@ -97,6 +96,6 @@ class MultiGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
97
96
|
Geokit::Geocoders::YahooGeocoder.expects(:geocode).with(@address, {}).returns(@success)
|
98
97
|
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).never
|
99
98
|
Geokit::Geocoders::UsGeocoder.expects(:geocode).never
|
100
|
-
assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@address, :
|
99
|
+
assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@address, provider_order: [:yahoo, :google, :us])
|
101
100
|
end
|
102
101
|
end
|
@@ -1,12 +1,11 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'helper')
|
2
2
|
|
3
|
-
Geokit::Geocoders
|
3
|
+
Geokit::Geocoders.ip_provider_order = [:geo_plugin, :ip]
|
4
4
|
|
5
5
|
class MultiIpGeocoderTest < BaseGeocoderTest #:nodoc: all
|
6
|
-
|
7
6
|
def setup
|
8
7
|
@ip_address = '10.10.10.10'
|
9
|
-
@success = Geokit::GeoLoc.new({:
|
8
|
+
@success = Geokit::GeoLoc.new({city: 'SAN FRANCISCO', state: 'CA', country_code: 'US', lat: 37.7742, lng: -122.417068})
|
10
9
|
@success.success = true
|
11
10
|
@failure = Geokit::GeoLoc.new
|
12
11
|
end
|
@@ -29,10 +28,9 @@ class MultiIpGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
29
28
|
end
|
30
29
|
|
31
30
|
def test_invalid_provider
|
32
|
-
temp = Geokit::Geocoders
|
31
|
+
temp = Geokit::Geocoders.ip_provider_order
|
33
32
|
Geokit::Geocoders.ip_provider_order = [:bogus]
|
34
33
|
assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
|
35
34
|
Geokit::Geocoders.ip_provider_order = temp
|
36
35
|
end
|
37
|
-
|
38
36
|
end
|
data/test/test_net_adapter.rb
CHANGED
@@ -23,7 +23,7 @@ class NetAdapterTest < Test::Unit::TestCase #:nodoc: all
|
|
23
23
|
end
|
24
24
|
|
25
25
|
RESULT = '{"name":"json"}'
|
26
|
-
RESULT_HASH = {
|
26
|
+
RESULT_HASH = {'name' => 'json'}
|
27
27
|
|
28
28
|
# Defines common test fixtures.
|
29
29
|
def setup
|
@@ -32,8 +32,8 @@ class NetAdapterTest < Test::Unit::TestCase #:nodoc: all
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def test_cache
|
35
|
-
old_adapter = Geokit::Geocoders
|
36
|
-
Geokit::Geocoders
|
35
|
+
old_adapter = Geokit::Geocoders.net_adapter
|
36
|
+
Geokit::Geocoders.net_adapter = Geokit::NetAdapter::Typhoeus
|
37
37
|
Typhoeus::Config.cache = SuperSimpleCache
|
38
38
|
success = MockSuccess.new
|
39
39
|
success.expects(:body).returns(RESULT)
|
@@ -41,6 +41,6 @@ class NetAdapterTest < Test::Unit::TestCase #:nodoc: all
|
|
41
41
|
Geokit::NetAdapter::Typhoeus.expects(:do_get).with(@url).returns(success)
|
42
42
|
assert_equal RESULT_HASH, Geokit::Geocoders::CachedGeocoder.process(:json, @url)
|
43
43
|
Typhoeus::Config.cache = nil
|
44
|
-
Geokit::Geocoders
|
44
|
+
Geokit::Geocoders.net_adapter = old_adapter
|
45
45
|
end
|
46
46
|
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), 'helper')
|
3
|
+
|
4
|
+
class OpencageGeocoderTest < BaseGeocoderTest #:nodoc: all
|
5
|
+
def setup
|
6
|
+
super
|
7
|
+
@opencage_full_hash = {street_address: '100 Spear St', city: 'San Francisco', state: 'CA', zip: '94105', country_code: 'US'}
|
8
|
+
@opencage_city_hash = {city: 'San Francisco', state: 'CA'}
|
9
|
+
@opencage_full_loc = Geokit::GeoLoc.new(@opencage_full_hash)
|
10
|
+
@opencage_city_loc = Geokit::GeoLoc.new(@opencage_city_hash)
|
11
|
+
|
12
|
+
# Note: This is not a real key because having real keys on github
|
13
|
+
# is not advised
|
14
|
+
key = 'someopencageapikey'
|
15
|
+
Geokit::Geocoders::OpencageGeocoder.key = key
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_opencage_full_address
|
19
|
+
VCR.use_cassette('opencage_full') do
|
20
|
+
url = 'https://api.opencagedata.com/geocode/v1/json?key=someopencageapikey&query=100+Spear+St%2C+San+Francisco%2C+CA%2C+94105%2C+US&no_annotations=1'
|
21
|
+
TestHelper.expects(:last_url).with(url)
|
22
|
+
res = Geokit::Geocoders::OpencageGeocoder.geocode(@opencage_full_loc)
|
23
|
+
|
24
|
+
assert_equal 'California', res.state
|
25
|
+
assert_equal 'San Francisco', res.city
|
26
|
+
assert_array_in_delta [37.7921509, -122.394], res.to_a
|
27
|
+
assert res.is_us?
|
28
|
+
assert_equal 'Spear Street 100, San Francisco, California, 94103, US', res.full_address
|
29
|
+
assert_equal 'opencage', res.provider
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_opencage_city
|
34
|
+
VCR.use_cassette('opencage_city') do
|
35
|
+
url = 'https://api.opencagedata.com/geocode/v1/json?key=someopencageapikey&query=San+Francisco%2C+CA&no_annotations=1'
|
36
|
+
TestHelper.expects(:last_url).with(url)
|
37
|
+
res = Geokit::Geocoders::OpencageGeocoder.geocode(@opencage_city_loc)
|
38
|
+
|
39
|
+
assert_equal 'California', res.state
|
40
|
+
assert_equal 'San Francisco', res.city
|
41
|
+
assert_array_in_delta [37.7792768, -122.4192704], res.to_a
|
42
|
+
assert res.is_us?
|
43
|
+
assert_equal 'San Francisco, California, US', res.full_address
|
44
|
+
assert_equal 'opencage', res.provider
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_opencage_reverse
|
49
|
+
VCR.use_cassette('opencage_reverse_madrid') do
|
50
|
+
|
51
|
+
location = Geokit::GeoLoc.new
|
52
|
+
location.lat, location.lng = '40.4167413', '-3.7032498' # Madrid
|
53
|
+
|
54
|
+
url = 'https://api.opencagedata.com/geocode/v1/json?key=someopencageapikey&query=40.4167413%2C-3.7032498&no_annotations=1'
|
55
|
+
TestHelper.expects(:last_url).with(url)
|
56
|
+
res = Geokit::Geocoders::OpencageGeocoder.geocode(location.ll)
|
57
|
+
|
58
|
+
assert_equal 'ES', res.country_code
|
59
|
+
assert_equal 'opencage', res.provider
|
60
|
+
|
61
|
+
assert_equal 'Madrid', res.city
|
62
|
+
assert_equal 'Community of Madrid', res.state
|
63
|
+
|
64
|
+
assert_equal 'Spain', res.country
|
65
|
+
assert_equal true, res.success
|
66
|
+
|
67
|
+
assert_equal 'Calle De Zurbano, Chamberí, Madrid, Community of Madrid, 28036, ES', res.full_address
|
68
|
+
assert_equal 28036, res.zip
|
69
|
+
assert_equal 'Calle De Zurbano', res.street_address
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_opencage_reverse2
|
74
|
+
VCR.use_cassette('opencage_reverse_prilep') do
|
75
|
+
|
76
|
+
location = Geokit::GeoLoc.new
|
77
|
+
location.lat, location.lng = '41.3527177', '21.5497808'
|
78
|
+
|
79
|
+
url = 'https://api.opencagedata.com/geocode/v1/json?key=someopencageapikey&query=41.3527177%2C21.5497808&no_annotations=1'
|
80
|
+
TestHelper.expects(:last_url).with(url)
|
81
|
+
res = Geokit::Geocoders::OpencageGeocoder.geocode(location.ll)
|
82
|
+
|
83
|
+
assert_equal 'MK', res.country_code
|
84
|
+
assert_equal 'opencage', res.provider
|
85
|
+
|
86
|
+
assert_equal 'Prilep', res.city
|
87
|
+
assert_equal 'Pelagonia Region', res.state
|
88
|
+
|
89
|
+
assert_equal 'Macedonia', res.country
|
90
|
+
assert_equal 10, res.precision
|
91
|
+
assert_equal true, res.success
|
92
|
+
|
93
|
+
assert_equal 'Прилепски Бранители, Prilep, Pelagonia Region, MK', res.full_address
|
94
|
+
assert_equal 'Прилепски Бранители', res.street_address
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# check if the results are in Spanish if &language=es
|
99
|
+
def test_language_response
|
100
|
+
VCR.use_cassette('opencage_language_response_es') do
|
101
|
+
url = 'https://api.opencagedata.com/geocode/v1/json?key=someopencageapikey&language=es&query=London&no_annotations=1'
|
102
|
+
TestHelper.expects(:last_url).with(url)
|
103
|
+
language_result = Geokit::Geocoders::OpencageGeocoder.geocode('London', language: 'es')
|
104
|
+
|
105
|
+
assert_equal 'Londres', language_result.city
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -2,27 +2,26 @@
|
|
2
2
|
require File.join(File.dirname(__FILE__), 'helper')
|
3
3
|
|
4
4
|
class OSMGeocoderTest < BaseGeocoderTest #:nodoc: all
|
5
|
-
OSM_FULL
|
5
|
+
OSM_FULL = <<-EOF.strip
|
6
6
|
[{"place_id":"425554497","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","boundingbox":["37.792341","37.792441","-122.394074","-122.393974"],"lat":"37.792391","lon":"-122.394024","display_name":"100, Spear Street, Financial District, San Francisco, California, 94105, United States of America","class":"place","type":"house","address":{"house_number":"100","road":"Spear Street","place":"Financial District","city":"San Francisco","county":"San Francisco","state":"California","postcode":"94105","country":"United States of America","country_code":"us"}}]
|
7
7
|
EOF
|
8
8
|
|
9
|
-
|
10
|
-
OSM_CITY=<<-EOF.strip
|
9
|
+
OSM_CITY = <<-EOF.strip
|
11
10
|
[{"place_id":"1586484","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"node","osm_id":"316944780","boundingbox":["37.7240965271","37.744100341797","-122.40126586914","-122.38125823975"],"lat":"37.7340974","lon":"-122.3912596","display_name":"San Francisco, California, United States of America","class":"place","type":"county","icon":"http://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png","address":{"county":"San Francisco","state":"California","country":"United States of America","country_code":"us"}},{"place_id":"42109083","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"way","osm_id":"33090874","boundingbox":["37.6398277282715","37.8230590820312","-123.173828125","-122.935707092285"],"lat":"37.7333682068208","lon":"-123.051926367593","display_name":"San Francisco, Marin, California, United States of America","class":"place","type":"city","icon":"http://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png","address":{"city":"San Francisco","county":"Marin","state":"California","country":"United States of America","country_code":"us"}},{"place_id":"145970","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"node","osm_id":"26819236","boundingbox":["37.768957366943","37.788961181641","-122.42920471191","-122.40919708252"],"lat":"37.7789601","lon":"-122.419199","display_name":"San Francisco, San Francisco County, California, United States of America, North America","class":"place","type":"city","icon":"http://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png","address":{"city":"San Francisco","county":"San Francisco County","state":"California","country":"United States of America","country_code":"us","place":"North America"}},{"place_id":"42108900","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"way","osm_id":"33090814","boundingbox":["37.7067184448242","37.9298248291016","-122.612289428711","-122.281776428223"],"lat":"37.7782304646168","lon":"-122.442503042395","display_name":"San Francisco, San Francisco County, California, United States of America","class":"place","type":"city","icon":"http://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png","address":{"city":"San Francisco","county":"San Francisco County","state":"California","country":"United States of America","country_code":"us"}}]
|
12
11
|
EOF
|
13
12
|
|
14
|
-
OSM_REVERSE_MADRID
|
13
|
+
OSM_REVERSE_MADRID = <<-EOF.strip
|
15
14
|
{"place_id":"41067113","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"way","osm_id":"31822457","lat":"40.3787537310091","lon":"-3.70187699287946","display_name":"Línea 3, Calle del Doctor Tolosa Latour, Usera, Madrid, 28026, Spain","address":{"subway":"Línea 3","road":"Calle del Doctor Tolosa Latour","suburb":"Usera","city_district":"Usera","city":"Madrid","county":"Madrid","state":"Madrid","postcode":"28026","country":"Spain","country_code":"es"}}
|
16
15
|
EOF
|
17
16
|
|
18
|
-
OSM_REVERSE_PRILEP
|
17
|
+
OSM_REVERSE_PRILEP = <<-EOF.strip
|
19
18
|
{"place_id":"46960069","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"way","osm_id":"39757255","lat":"41.3508581896005","lon":"21.549896984439","display_name":"Gimnasium Mirche Acev, Marksova, Prilep, Macedonia","address":{"school":"Gimnasium Mirche Acev","road":"Marksova","city":"Prilep","country":"Macedonia","country_code":"mk"}}
|
20
19
|
EOF
|
21
20
|
|
22
21
|
def setup
|
23
22
|
super
|
24
|
-
@osm_full_hash = {:
|
25
|
-
@osm_city_hash = {:
|
23
|
+
@osm_full_hash = {street_address: '100 Spear St', city: 'San Francisco', state: 'CA', zip: '94105', country_code: 'US'}
|
24
|
+
@osm_city_hash = {city: 'San Francisco', state: 'CA'}
|
26
25
|
@osm_full_loc = Geokit::GeoLoc.new(@osm_full_hash)
|
27
26
|
@osm_city_loc = Geokit::GeoLoc.new(@osm_city_hash)
|
28
27
|
end
|
@@ -31,7 +30,7 @@ class OSMGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
31
30
|
def test_osm_full_address
|
32
31
|
response = MockSuccess.new
|
33
32
|
response.expects(:body).returns(OSM_FULL)
|
34
|
-
url="http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}"
|
33
|
+
url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}"
|
35
34
|
Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response)
|
36
35
|
do_full_address_assertions(Geokit::Geocoders::OSMGeocoder.geocode(@full_address_short_zip))
|
37
36
|
end
|
@@ -39,7 +38,7 @@ class OSMGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
39
38
|
def test_osm_full_address_accuracy
|
40
39
|
response = MockSuccess.new
|
41
40
|
response.expects(:body).returns(OSM_FULL)
|
42
|
-
url="http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}"
|
41
|
+
url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}"
|
43
42
|
Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response)
|
44
43
|
res = Geokit::Geocoders::OSMGeocoder.geocode(@full_address_short_zip)
|
45
44
|
assert_equal 'house', res.accuracy
|
@@ -48,7 +47,7 @@ class OSMGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
48
47
|
def test_osm_full_address_with_geo_loc
|
49
48
|
response = MockSuccess.new
|
50
49
|
response.expects(:body).returns(OSM_FULL)
|
51
|
-
url="http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}"
|
50
|
+
url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}"
|
52
51
|
Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response)
|
53
52
|
do_full_address_assertions(Geokit::Geocoders::OSMGeocoder.geocode(@osm_full_loc))
|
54
53
|
end
|
@@ -56,24 +55,32 @@ class OSMGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
56
55
|
def test_osm_city
|
57
56
|
response = MockSuccess.new
|
58
57
|
response.expects(:body).returns(OSM_CITY)
|
59
|
-
url="http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}"
|
58
|
+
url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}"
|
60
59
|
Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response)
|
61
60
|
do_city_assertions(Geokit::Geocoders::OSMGeocoder.geocode(@address))
|
62
61
|
end
|
63
62
|
|
63
|
+
def test_osm_city_with_accept_language
|
64
|
+
response = MockSuccess.new
|
65
|
+
response.expects(:body).returns(OSM_CITY)
|
66
|
+
url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&accept-language=pt-br&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}"
|
67
|
+
Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response)
|
68
|
+
do_city_assertions(Geokit::Geocoders::OSMGeocoder.geocode(@address, {:'accept-language' => 'pt-br'}))
|
69
|
+
end
|
70
|
+
|
64
71
|
def test_osm_city_accuracy
|
65
72
|
response = MockSuccess.new
|
66
73
|
response.expects(:body).returns(OSM_CITY)
|
67
|
-
url="http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}"
|
74
|
+
url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}"
|
68
75
|
Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response)
|
69
76
|
res = Geokit::Geocoders::OSMGeocoder.geocode(@address)
|
70
|
-
assert_equal
|
77
|
+
assert_equal 'county', res.accuracy
|
71
78
|
end
|
72
79
|
|
73
80
|
def test_osm_city_with_geo_loc
|
74
81
|
response = MockSuccess.new
|
75
82
|
response.expects(:body).returns(OSM_CITY)
|
76
|
-
url="http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}"
|
83
|
+
url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}"
|
77
84
|
Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response)
|
78
85
|
do_city_assertions(Geokit::Geocoders::OSMGeocoder.geocode(@osm_city_loc))
|
79
86
|
end
|
@@ -82,26 +89,26 @@ class OSMGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
82
89
|
response = MockSuccess.new
|
83
90
|
response.expects(:body).returns(OSM_REVERSE_PRILEP)
|
84
91
|
prilep = Geokit::GeoLoc.new
|
85
|
-
prilep.lat, prilep.lng =
|
92
|
+
prilep.lat, prilep.lng = '41.3527177', '21.5497808'
|
86
93
|
url = "http://nominatim.openstreetmap.org/reverse?format=json&addressdetails=1&lat=#{prilep.lat}&lon=#{prilep.lng}"
|
87
94
|
Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response)
|
88
95
|
res = Geokit::Geocoders::OSMGeocoder.do_reverse_geocode(prilep.ll)
|
89
96
|
|
90
|
-
#OSM does not return the exast lat lng in response
|
91
|
-
#assert_equal prilep.lat.to_s.slice(1..5), res.lat.to_s.slice(1..5)
|
92
|
-
#assert_equal prilep.lng.to_s.slice(1..5), res.lng.to_s.slice(1..5)
|
93
|
-
assert_equal
|
94
|
-
assert_equal
|
97
|
+
# OSM does not return the exast lat lng in response
|
98
|
+
# assert_equal prilep.lat.to_s.slice(1..5), res.lat.to_s.slice(1..5)
|
99
|
+
# assert_equal prilep.lng.to_s.slice(1..5), res.lng.to_s.slice(1..5)
|
100
|
+
assert_equal 'MK', res.country_code
|
101
|
+
assert_equal 'osm', res.provider
|
95
102
|
|
96
|
-
assert_equal
|
103
|
+
assert_equal 'Prilep', res.city
|
97
104
|
assert_nil res.state
|
98
105
|
|
99
|
-
assert_equal
|
106
|
+
assert_equal 'Macedonia', res.country
|
100
107
|
assert_nil res.precision
|
101
108
|
assert_equal true, res.success
|
102
109
|
|
103
|
-
assert_equal
|
104
|
-
assert_equal
|
110
|
+
assert_equal 'Marksova, Prilep, MK', res.full_address
|
111
|
+
assert_equal 'Marksova', res.street_address
|
105
112
|
end
|
106
113
|
|
107
114
|
def test_reverse_geo_code
|
@@ -109,28 +116,39 @@ class OSMGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
109
116
|
response.expects(:body).returns(OSM_REVERSE_MADRID)
|
110
117
|
location = Geokit::GeoLoc.new
|
111
118
|
# Madrid
|
112
|
-
location.lat, location.lng =
|
119
|
+
location.lat, location.lng = '40.4167413', '-3.7032498'
|
113
120
|
url = "http://nominatim.openstreetmap.org/reverse?format=json&addressdetails=1&lat=#{location.lat}&lon=#{location.lng}"
|
114
121
|
Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response)
|
115
122
|
res = Geokit::Geocoders::OSMGeocoder.do_reverse_geocode(location.ll)
|
116
123
|
|
117
|
-
assert_equal
|
118
|
-
assert_equal
|
124
|
+
assert_equal 'ES', res.country_code
|
125
|
+
assert_equal 'osm', res.provider
|
119
126
|
|
120
|
-
assert_equal
|
121
|
-
assert_equal
|
127
|
+
assert_equal 'Madrid', res.city
|
128
|
+
assert_equal 'Madrid', res.state
|
122
129
|
|
123
|
-
assert_equal
|
130
|
+
assert_equal 'Spain', res.country
|
124
131
|
assert_equal true, res.success
|
125
132
|
|
126
|
-
assert_equal
|
127
|
-
assert_equal
|
128
|
-
assert_equal
|
133
|
+
assert_equal 'Calle Del Doctor Tolosa Latour, Usera, Madrid, Madrid, 28026, ES', res.full_address
|
134
|
+
assert_equal '28026', res.zip
|
135
|
+
assert_equal 'Calle Del Doctor Tolosa Latour', res.street_address
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_reverse_geo_code_with_accept_language
|
139
|
+
response = MockSuccess.new
|
140
|
+
response.expects(:body).returns(OSM_REVERSE_MADRID)
|
141
|
+
location = Geokit::GeoLoc.new
|
142
|
+
# Madrid
|
143
|
+
location.lat, location.lng = '40.4167413', '-3.7032498'
|
144
|
+
url = "http://nominatim.openstreetmap.org/reverse?format=json&addressdetails=1&lat=#{location.lat}&lon=#{location.lng}&accept-language=pt-br"
|
145
|
+
Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response)
|
146
|
+
Geokit::Geocoders::OSMGeocoder.do_reverse_geocode(location.ll, {:'accept-language' => 'pt-br'})
|
129
147
|
end
|
130
148
|
|
131
149
|
def test_service_unavailable
|
132
150
|
response = MockFailure.new
|
133
|
-
url = url="http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}"
|
151
|
+
url = url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}"
|
134
152
|
Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response)
|
135
153
|
assert !Geokit::Geocoders::OSMGeocoder.geocode(@osm_city_loc).success
|
136
154
|
end
|
@@ -139,21 +157,21 @@ class OSMGeocoderTest < BaseGeocoderTest #:nodoc: all
|
|
139
157
|
|
140
158
|
# next two methods do the assertions for both address-level and city-level lookups
|
141
159
|
def do_full_address_assertions(res)
|
142
|
-
assert_equal
|
143
|
-
assert_equal
|
144
|
-
assert_equal
|
160
|
+
assert_equal 'California', res.state
|
161
|
+
assert_equal 'San Francisco', res.city
|
162
|
+
assert_equal '37.792391,-122.394024', res.ll
|
145
163
|
assert res.is_us?
|
146
|
-
assert_equal
|
147
|
-
assert_equal
|
164
|
+
assert_equal 'Spear Street 100, San Francisco, California, 94105, US', res.full_address
|
165
|
+
assert_equal 'osm', res.provider
|
148
166
|
end
|
149
167
|
|
150
168
|
def do_city_assertions(res)
|
151
|
-
assert_equal
|
152
|
-
assert_equal
|
153
|
-
assert_equal
|
169
|
+
assert_equal 'California', res.state
|
170
|
+
assert_equal 'San Francisco', res.city
|
171
|
+
assert_equal '37.7340974,-122.3912596', res.ll
|
154
172
|
assert res.is_us?
|
155
|
-
assert_equal
|
173
|
+
assert_equal 'San Francisco, California, US', res.full_address
|
156
174
|
assert_nil res.street_address
|
157
|
-
assert_equal
|
175
|
+
assert_equal 'osm', res.provider
|
158
176
|
end
|
159
177
|
end
|