geocoder 1.6.1 → 1.6.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/LICENSE +1 -1
  4. data/README.md +7 -28
  5. data/bin/console +13 -0
  6. data/lib/easting_northing.rb +171 -0
  7. data/lib/geocoder/cache.rb +4 -0
  8. data/lib/geocoder/configuration.rb +2 -1
  9. data/lib/geocoder/configuration_hash.rb +4 -4
  10. data/lib/geocoder/ip_address.rb +2 -1
  11. data/lib/geocoder/lookup.rb +2 -0
  12. data/lib/geocoder/lookups/ban_data_gouv_fr.rb +1 -1
  13. data/lib/geocoder/lookups/esri.rb +6 -0
  14. data/lib/geocoder/lookups/geocodio.rb +1 -1
  15. data/lib/geocoder/lookups/google.rb +7 -2
  16. data/lib/geocoder/lookups/google_places_details.rb +8 -14
  17. data/lib/geocoder/lookups/google_places_search.rb +28 -2
  18. data/lib/geocoder/lookups/google_premier.rb +4 -0
  19. data/lib/geocoder/lookups/latlon.rb +1 -2
  20. data/lib/geocoder/lookups/maxmind_local.rb +7 -1
  21. data/lib/geocoder/lookups/nationaal_georegister_nl.rb +38 -0
  22. data/lib/geocoder/lookups/smarty_streets.rb +6 -1
  23. data/lib/geocoder/lookups/telize.rb +1 -1
  24. data/lib/geocoder/lookups/test.rb +4 -0
  25. data/lib/geocoder/lookups/uk_ordnance_survey_names.rb +59 -0
  26. data/lib/geocoder/lookups/yandex.rb +1 -2
  27. data/lib/geocoder/results/ban_data_gouv_fr.rb +26 -1
  28. data/lib/geocoder/results/db_ip_com.rb +1 -1
  29. data/lib/geocoder/results/ipregistry.rb +4 -8
  30. data/lib/geocoder/results/nationaal_georegister_nl.rb +62 -0
  31. data/lib/geocoder/results/nominatim.rb +4 -0
  32. data/lib/geocoder/results/uk_ordnance_survey_names.rb +59 -0
  33. data/lib/geocoder/results/yandex.rb +217 -59
  34. data/lib/geocoder/util.rb +29 -0
  35. data/lib/geocoder/version.rb +1 -1
  36. metadata +13 -8
  37. data/lib/hash_recursive_merge.rb +0 -73
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 25d03f1aeb92b80a0ec269a1f42093bb641e468d0824d324ad5157ed9cfcb559
4
- data.tar.gz: 1a23a47bfb3d4af0de14421f8b1b76e03234aee309a8be7ddb2acd91ce04c30d
3
+ metadata.gz: 23cb694f657cd4b5921d4480c97abd7271ae157ead4e09bb1d614bf90976d567
4
+ data.tar.gz: ba36330f7d2fe99a6c5b2f445f1185b421ac932a633ffab2b896d926b3a74ce2
5
5
  SHA512:
6
- metadata.gz: e8f4fd2a60fa6ccb91f7a04a737b5720aa4e276181c202b215b5dd8c50b1746d833ace8c2a034aae1282132e62f96be518b3b366c34f03c73ffcb4cf1dd3c6ab
7
- data.tar.gz: 848c835773fd172b8469eefc28646c4c5cd3176d10bfb175f5d447bd24b51982555644b7ff5d45eddb2bc04cf7820fadb2363a23cf907ad13aa930f33b5dad09
6
+ metadata.gz: 87d72900f31678781bf09cb6706487b4e0528defe0a98601b2a718fa9c31c89c9876780f933d8e4364bd1459285cb0e9a71cc7a6c76d17cfce2959644d974ee1
7
+ data.tar.gz: '083c991cf76381add5b4b8aa3312fa1bd5836990370506351d47d17d3e857f2a0d855df901702c9fabc27589491b458a4fbcb378731a3ea3d079c2f4d7d274d7'
data/CHANGELOG.md CHANGED
@@ -3,6 +3,32 @@ Changelog
3
3
 
4
4
  Major changes to Geocoder for each release. Please see the Git log for complete list of changes.
5
5
 
6
+ 1.6.6 (2020 Mar 4)
7
+ -------------------
8
+ * Rescue from exception on cache read/write error. Issue warning instead.
9
+
10
+ 1.6.5 (2020 Feb 10)
11
+ -------------------
12
+ * Fix backward coordinates bug in NationaalregisterNl lookup (thanks github.com/Marthyn).
13
+ * Allow removal of single stubs in test mode (thanks github.com/jmmastey).
14
+ * Improve results for :ban_data_gouv_fr lookup (thanks github.com/Intrepidd).
15
+
16
+ 1.6.4 (2020 Oct 6)
17
+ -------------------
18
+ * Various updates in response to geocoding API changes.
19
+ * Refactor of Google Places Search lookup (thanks github.com/maximilientyc).
20
+
21
+ 1.6.3 (2020 Apr 30)
22
+ -------------------
23
+ * Update URL for :telize lookup (thanks github.com/alexwalling).
24
+ * Fix bug parsing IPv6 with port (thanks github.com/gdomingu).
25
+
26
+ 1.6.2 (2020 Mar 16)
27
+ -------------------
28
+ * Add support for :nationaal_georegister_nl lookup (thanks github.com/opensourceame).
29
+ * Add support for :uk_ordnance_survey_names lookup (thanks github.com/pezholio).
30
+ * Refactor and fix bugs in Yandex lookup (thanks github.com/iarie and stereodenis).
31
+
6
32
  1.6.1 (2020 Jan 23)
7
33
  -------------------
8
34
  * Sanitize lat/lon values passed to within_bounding_box to prevent SQL injection.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-2020 Alex Reisner
1
+ Copyright (c) 2009-2021 Alex Reisner
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -213,7 +213,7 @@ Some common options are:
213
213
  # set default units to kilometers:
214
214
  units: :km,
215
215
 
216
- # caching (see [below](#caching) for details):
216
+ # caching (see Caching section below for details):
217
217
  cache: Redis.new,
218
218
  cache_prefix: "..."
219
219
 
@@ -532,10 +532,14 @@ With the above stub defined, any query for "New York, NY" will return the result
532
532
  ]
533
533
  )
534
534
 
535
+ You may also delete a single stub, or reset all stubs _including the default stub_:
536
+
537
+ Geocoder::Lookup::Test.delete_stub('New York, NY')
538
+ Geocoder::Lookup::Test.reset
539
+
535
540
  Notes:
536
541
 
537
542
  - Keys must be strings (not symbols) when calling `add_stub` or `set_default_stub`. For example `'country' =>` not `:country =>`.
538
- - To clear stubs (e.g. prior to another spec), use `Geocoder::Lookup::Test.reset`. This will clear all stubs _including the default stub_.
539
543
  - The stubbed result objects returned by the Test lookup do not support all the methods real result objects do. If you need to test interaction with real results it may be better to use an external stubbing tool and something like WebMock or VCR to prevent network calls.
540
544
 
541
545
 
@@ -676,29 +680,4 @@ If anyone has a more elegant solution to this problem I am very interested in se
676
680
  The `near` method will not look across the 180th meridian to find objects close to a given point. In practice this is rarely an issue outside of New Zealand and certain surrounding islands. This problem does not exist with the zero-meridian. The problem is due to a shortcoming of the Haversine formula which Geocoder uses to calculate distances.
677
681
 
678
682
 
679
- Reporting Issues
680
- ----------------
681
-
682
- When reporting an issue, please list the version of Geocoder you are using and any relevant information about your application (Rails version, database type and version, etc). Please describe as specifically as you can what behavior you are seeing (eg: an error message? a nil return value?).
683
-
684
- Please DO NOT use GitHub issues to ask questions about how to use Geocoder. Sites like [StackOverflow](http://www.stackoverflow.com/) are a better forum for such discussions.
685
-
686
-
687
- Contributing
688
- ------------
689
-
690
- Contributions are welcome via Github pull requests. If you are new to the project and looking for a way to get involved, try picking up an issue with a "beginner-task" label. Hints about what needs to be done are usually provided.
691
-
692
- For all contributions, please respect the following guidelines:
693
-
694
- * Each pull request should implement ONE feature or bugfix. If you want to add or fix more than one thing, submit more than one pull request.
695
- * Do not commit changes to files that are irrelevant to your feature or bugfix (eg: `.gitignore`).
696
- * Do not add dependencies on other gems.
697
- * Do not add unnecessary `require` statements which could cause LoadErrors on certain systems.
698
- * Remember: Geocoder needs to run outside of Rails. Don't assume things like ActiveSupport are available.
699
- * Be willing to accept criticism and work on improving your code; Geocoder is used by thousands of developers and care must be taken not to introduce bugs.
700
- * Be aware that the pull request review process is not immediate, and is generally proportional to the size of the pull request.
701
- * If your pull request is merged, please do not ask for an immediate release of the gem. There are many factors contributing to when releases occur (remember that they affect thousands of apps with Geocoder in their Gemfiles). If necessary, please install from the Github source until the next official release.
702
-
703
-
704
- Copyright :copyright: 2009-2020 Alex Reisner, released under the MIT license.
683
+ Copyright :copyright: 2009-2021 Alex Reisner, released under the MIT license.
data/bin/console ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'geocoder'
5
+
6
+ if File.exist?("api_keys.yml")
7
+ require 'yaml'
8
+ @api_keys = YAML.load(File.read("api_keys.yml"), symbolize_names: true)
9
+ Geocoder.configure(@api_keys)
10
+ end
11
+
12
+ require 'irb'
13
+ IRB.start
@@ -0,0 +1,171 @@
1
+ module Geocoder
2
+ class EastingNorthing
3
+ attr_reader :easting, :northing, :lat_lng
4
+
5
+ def initialize(opts)
6
+ @easting = opts[:easting]
7
+ @northing = opts[:northing]
8
+
9
+ @lat_lng = to_WGS84(to_osgb_36)
10
+ end
11
+
12
+ private
13
+
14
+ def to_osgb_36
15
+ osgb_fo = 0.9996012717
16
+ northing0 = -100_000.0
17
+ easting0 = 400_000.0
18
+ phi0 = deg_to_rad(49.0)
19
+ lambda0 = deg_to_rad(-2.0)
20
+ a = 6_377_563.396
21
+ b = 6_356_256.909
22
+ eSquared = ((a * a) - (b * b)) / (a * a)
23
+ phi = 0.0
24
+ lambda = 0.0
25
+ n = (a - b) / (a + b)
26
+ m = 0.0
27
+ phiPrime = ((northing - northing0) / (a * osgb_fo)) + phi0
28
+
29
+ while (northing - northing0 - m) >= 0.001
30
+ m =
31
+ (b * osgb_fo)\
32
+ * (((1 + n + ((5.0 / 4.0) * n * n) + ((5.0 / 4.0) * n * n * n))\
33
+ * (phiPrime - phi0))\
34
+ - (((3 * n) + (3 * n * n) + ((21.0 / 8.0) * n * n * n))\
35
+ * Math.sin(phiPrime - phi0)\
36
+ * Math.cos(phiPrime + phi0))\
37
+ + ((((15.0 / 8.0) * n * n) + ((15.0 / 8.0) * n * n * n))\
38
+ * Math.sin(2.0 * (phiPrime - phi0))\
39
+ * Math.cos(2.0 * (phiPrime + phi0)))\
40
+ - (((35.0 / 24.0) * n * n * n)\
41
+ * Math.sin(3.0 * (phiPrime - phi0))\
42
+ * Math.cos(3.0 * (phiPrime + phi0))))
43
+
44
+ phiPrime += (northing - northing0 - m) / (a * osgb_fo)
45
+ end
46
+
47
+ v = a * osgb_fo * ((1.0 - eSquared * sin_pow_2(phiPrime))**-0.5)
48
+ rho =
49
+ a\
50
+ * osgb_fo\
51
+ * (1.0 - eSquared)\
52
+ * ((1.0 - eSquared * sin_pow_2(phiPrime))**-1.5)
53
+ etaSquared = (v / rho) - 1.0
54
+ vii = Math.tan(phiPrime) / (2 * rho * v)
55
+ viii =
56
+ (Math.tan(phiPrime) / (24.0 * rho * (v**3.0)))\
57
+ * (5.0\
58
+ + (3.0 * tan_pow_2(phiPrime))\
59
+ + etaSquared\
60
+ - (9.0 * tan_pow_2(phiPrime) * etaSquared))
61
+ ix =
62
+ (Math.tan(phiPrime) / (720.0 * rho * (v**5.0)))\
63
+ * (61.0\
64
+ + (90.0 * tan_pow_2(phiPrime))\
65
+ + (45.0 * tan_pow_2(phiPrime) * tan_pow_2(phiPrime)))
66
+ x = sec(phiPrime) / v
67
+ xi =
68
+ (sec(phiPrime) / (6.0 * v * v * v))\
69
+ * ((v / rho) + (2 * tan_pow_2(phiPrime)))
70
+ xiii =
71
+ (sec(phiPrime) / (120.0 * (v**5.0)))\
72
+ * (5.0\
73
+ + (28.0 * tan_pow_2(phiPrime))\
74
+ + (24.0 * tan_pow_2(phiPrime) * tan_pow_2(phiPrime)))
75
+ xiia =
76
+ (sec(phiPrime) / (5040.0 * (v**7.0)))\
77
+ * (61.0\
78
+ + (662.0 * tan_pow_2(phiPrime))\
79
+ + (1320.0 * tan_pow_2(phiPrime) * tan_pow_2(phiPrime))\
80
+ + (720.0\
81
+ * tan_pow_2(phiPrime)\
82
+ * tan_pow_2(phiPrime)\
83
+ * tan_pow_2(phiPrime)))
84
+ phi =
85
+ phiPrime\
86
+ - (vii * ((easting - easting0)**2.0))\
87
+ + (viii * ((easting - easting0)**4.0))\
88
+ - (ix * ((easting - easting0)**6.0))
89
+ lambda =
90
+ lambda0\
91
+ + (x * (easting - easting0))\
92
+ - (xi * ((easting - easting0)**3.0))\
93
+ + (xiii * ((easting - easting0)**5.0))\
94
+ - (xiia * ((easting - easting0)**7.0))
95
+
96
+ [rad_to_deg(phi), rad_to_deg(lambda)]
97
+ end
98
+
99
+ def to_WGS84(latlng)
100
+ latitude = latlng[0]
101
+ longitude = latlng[1]
102
+
103
+ a = 6_377_563.396
104
+ b = 6_356_256.909
105
+ eSquared = ((a * a) - (b * b)) / (a * a)
106
+
107
+ phi = deg_to_rad(latitude)
108
+ lambda = deg_to_rad(longitude)
109
+ v = a / Math.sqrt(1 - eSquared * sin_pow_2(phi))
110
+ h = 0
111
+ x = (v + h) * Math.cos(phi) * Math.cos(lambda)
112
+ y = (v + h) * Math.cos(phi) * Math.sin(lambda)
113
+ z = ((1 - eSquared) * v + h) * Math.sin(phi)
114
+
115
+ tx = 446.448
116
+ ty = -124.157
117
+ tz = 542.060
118
+
119
+ s = -0.0000204894
120
+ rx = deg_to_rad(0.00004172222)
121
+ ry = deg_to_rad(0.00006861111)
122
+ rz = deg_to_rad(0.00023391666)
123
+
124
+ xB = tx + (x * (1 + s)) + (-rx * y) + (ry * z)
125
+ yB = ty + (rz * x) + (y * (1 + s)) + (-rx * z)
126
+ zB = tz + (-ry * x) + (rx * y) + (z * (1 + s))
127
+
128
+ a = 6_378_137.000
129
+ b = 6_356_752.3141
130
+ eSquared = ((a * a) - (b * b)) / (a * a)
131
+
132
+ lambdaB = rad_to_deg(Math.atan(yB / xB))
133
+ p = Math.sqrt((xB * xB) + (yB * yB))
134
+ phiN = Math.atan(zB / (p * (1 - eSquared)))
135
+
136
+ (1..10).each do |_i|
137
+ v = a / Math.sqrt(1 - eSquared * sin_pow_2(phiN))
138
+ phiN1 = Math.atan((zB + (eSquared * v * Math.sin(phiN))) / p)
139
+ phiN = phiN1
140
+ end
141
+
142
+ phiB = rad_to_deg(phiN)
143
+
144
+ [phiB, lambdaB]
145
+ end
146
+
147
+ def deg_to_rad(degrees)
148
+ degrees / 180.0 * Math::PI
149
+ end
150
+
151
+ def rad_to_deg(r)
152
+ (r / Math::PI) * 180
153
+ end
154
+
155
+ def sin_pow_2(x)
156
+ Math.sin(x) * Math.sin(x)
157
+ end
158
+
159
+ def cos_pow_2(x)
160
+ Math.cos(x) * Math.cos(x)
161
+ end
162
+
163
+ def tan_pow_2(x)
164
+ Math.tan(x) * Math.tan(x)
165
+ end
166
+
167
+ def sec(x)
168
+ 1.0 / Math.cos(x)
169
+ end
170
+ end
171
+ end
@@ -18,6 +18,8 @@ module Geocoder
18
18
  when store.respond_to?(:read)
19
19
  store.read key_for(url)
20
20
  end
21
+ rescue => e
22
+ warn "Geocoder cache read error: #{e}"
21
23
  end
22
24
 
23
25
  ##
@@ -32,6 +34,8 @@ module Geocoder
32
34
  when store.respond_to?(:write)
33
35
  store.write key_for(url), value
34
36
  end
37
+ rescue => e
38
+ warn "Geocoder cache write error: #{e}"
35
39
  end
36
40
 
37
41
  ##
@@ -1,5 +1,6 @@
1
1
  require 'singleton'
2
2
  require 'geocoder/configuration_hash'
3
+ require 'geocoder/util'
3
4
 
4
5
  module Geocoder
5
6
 
@@ -85,7 +86,7 @@ module Geocoder
85
86
  end
86
87
 
87
88
  def configure(options)
88
- @data.rmerge!(options)
89
+ Util.recursive_hash_merge(@data, options)
89
90
  end
90
91
 
91
92
  def initialize # :nodoc
@@ -1,11 +1,11 @@
1
- require 'hash_recursive_merge'
2
-
3
1
  module Geocoder
4
2
  class ConfigurationHash < Hash
5
- include HashRecursiveMerge
6
-
7
3
  def method_missing(meth, *args, &block)
8
4
  has_key?(meth) ? self[meth] : super
9
5
  end
6
+
7
+ def respond_to_missing?(meth, include_private = false)
8
+ has_key?(meth) || super
9
+ end
10
10
  end
11
11
  end
@@ -20,7 +20,8 @@ module Geocoder
20
20
  end
21
21
 
22
22
  def valid?
23
- !!((self =~ Resolv::IPv4::Regex) || (self =~ Resolv::IPv6::Regex))
23
+ ip = self[/(?<=\[)(.*?)(?=\])/] || self
24
+ !!((ip =~ Resolv::IPv4::Regex) || (ip =~ Resolv::IPv6::Regex))
24
25
  end
25
26
  end
26
27
  end
@@ -33,9 +33,11 @@ module Geocoder
33
33
  :bing,
34
34
  :geocoder_ca,
35
35
  :yandex,
36
+ :nationaal_georegister_nl,
36
37
  :nominatim,
37
38
  :mapbox,
38
39
  :mapquest,
40
+ :uk_ordnance_survey_names,
39
41
  :opencagedata,
40
42
  :pelias,
41
43
  :pickpoint,
@@ -22,7 +22,7 @@ module Geocoder::Lookup
22
22
  end
23
23
 
24
24
  def any_result?(doc)
25
- doc['features'].any?
25
+ doc['features'] and doc['features'].any?
26
26
  end
27
27
 
28
28
  def results(query)
@@ -9,6 +9,10 @@ module Geocoder::Lookup
9
9
  "Esri"
10
10
  end
11
11
 
12
+ def supported_protocols
13
+ [:https]
14
+ end
15
+
12
16
  private # ---------------------------------------------------------------
13
17
 
14
18
  def base_query_url(query)
@@ -47,6 +51,8 @@ module Geocoder::Lookup
47
51
  params[:forStorage] = for_storage_value
48
52
  end
49
53
  params[:sourceCountry] = configuration[:source_country] if configuration[:source_country]
54
+ params[:preferredLabelValues] = configuration[:preferred_label_values] if configuration[:preferred_label_values]
55
+
50
56
  params.merge(super)
51
57
  end
52
58
 
@@ -29,7 +29,7 @@ module Geocoder::Lookup
29
29
 
30
30
  def base_query_url(query)
31
31
  path = query.reverse_geocode? ? "reverse" : "geocode"
32
- "#{protocol}://api.geocod.io/v1.3/#{path}?"
32
+ "#{protocol}://api.geocod.io/v1.6/#{path}?"
33
33
  end
34
34
 
35
35
  def query_url_params(query)
@@ -44,10 +44,15 @@ module Geocoder::Lookup
44
44
  super(response) and ['OK', 'ZERO_RESULTS'].include?(status)
45
45
  end
46
46
 
47
+ def result_root_attr
48
+ 'results'
49
+ end
50
+
47
51
  def results(query)
48
52
  return [] unless doc = fetch_data(query)
49
- case doc['status']; when "OK" # OK status implies >0 results
50
- return doc['results']
53
+ case doc['status']
54
+ when "OK" # OK status implies >0 results
55
+ return doc[result_root_attr]
51
56
  when "OVER_QUERY_LIMIT"
52
57
  raise_error(Geocoder::OverQueryLimitError) ||
53
58
  Geocoder.log(:warn, "#{name} API error: over query limit.")
@@ -22,21 +22,15 @@ module Geocoder
22
22
  "#{protocol}://maps.googleapis.com/maps/api/place/details/json?"
23
23
  end
24
24
 
25
+ def result_root_attr
26
+ 'result'
27
+ end
28
+
25
29
  def results(query)
26
- return [] unless doc = fetch_data(query)
27
-
28
- case doc["status"]
29
- when "OK"
30
- return [doc["result"]]
31
- when "OVER_QUERY_LIMIT"
32
- raise_error(Geocoder::OverQueryLimitError) || Geocoder.log(:warn, "Google Places Details API error: over query limit.")
33
- when "REQUEST_DENIED"
34
- raise_error(Geocoder::RequestDenied) || Geocoder.log(:warn, "Google Places Details API error: request denied.")
35
- when "INVALID_REQUEST"
36
- raise_error(Geocoder::InvalidRequest) || Geocoder.log(:warn, "Google Places Details API error: invalid request.")
37
- end
38
-
39
- []
30
+ result = super(query)
31
+ return [result] unless result.is_a? Array
32
+
33
+ result
40
34
  end
41
35
 
42
36
  def query_url_google_params(query)