wheretz 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e41cda2d85607a26ed55eca8587c6014611a39a3
4
- data.tar.gz: 242f0817239a37c7c1ae41f85dc8389d2d5eb6f0
3
+ metadata.gz: 68585df38909578b7e89a731f3ee4f89c0600787
4
+ data.tar.gz: 5ef6f73de30fe6f954b132ec74f8407b33a619af
5
5
  SHA512:
6
- metadata.gz: 28fc8de7c3308c7f5212c64ca9237392c06f0af8b394e477c7d95804af9a05292de6380578573ad98b8adedb85793faee45d0a436ce84f093d48dd78fcc00eec
7
- data.tar.gz: d1b239c68d11b09f09d4476c18d83b5befa180aa80cf173783db43bc9f9c6cd1ffde224992ba3b7b00c944d43716cc973587907ce39144514f110348b5333165
6
+ metadata.gz: e84235f01b2c82c75a9af7eec670b598641071f7f1262bce3d0f6980daa6b9635f7a630880ba6ca26d6004645c684d4b250f9abe051372a345c62a77616edb72
7
+ data.tar.gz: 74bc547aa0bd00c26a4e9cdcbb0036e49ca68a8f7c4c32293f1eee6830358be22c349cb636c96a728f1b2f80faac05e206317a69bfc85410d689e7f579c94c1f
data/Changelog.md ADDED
@@ -0,0 +1,9 @@
1
+ # WhereTZ changelog
2
+
3
+ ## 0.0.2 (2016-03-04)
4
+
5
+ Fixed edge case (point slightly outside any of polygon).
6
+
7
+ ## 0.0.1 (2016-01-28)
8
+
9
+ Initial.
data/README.md CHANGED
@@ -32,7 +32,7 @@ WhereTZ.get(50.004444, 36.231389)
32
32
  # you should have tzinfo gem installed, wheretz doesn't list it as dependency
33
33
  ```
34
34
 
35
- From commandline, after gem installed:
35
+ From command-line, after gem installed:
36
36
 
37
37
  ```bash
38
38
  wheretz 50.004444,36.231389
@@ -51,11 +51,14 @@ wheretz 50.004444,36.231389
51
51
  4. If there's several intersecting bounding boxes, `WhereTZ` reads only
52
52
  relevant timezone files (which are not very large) and checks which
53
53
  polygon actually contains the point.
54
+ 5. **Added in 0.0.2** If point occures outside any of polygons, find
55
+ the closest one (it's last resort for really rare cases and slower
56
+ than other approaches).
54
57
 
55
58
  ## Known problems
56
59
 
57
- * On "bounding box only" check, some points deeply in sea (and actally
58
- belonging to no timezone polygon) can be wrongfly guessed as belonging
60
+ * On "bounding box only" check, some points deeply in sea (and actually
61
+ belonging to no timezone polygon) can be wrongly guessed as belonging
59
62
  to some timezone;
60
63
  * Loading/unloading `.geojson` files can be uneffective when called
61
64
  multiple times; future releases will provide option for preserve
data/lib/wheretz.rb CHANGED
@@ -8,7 +8,7 @@ require 'geo_ruby/geojson'
8
8
  # ```ruby
9
9
  # WhereTZ.lookup(50.004444, 36.231389)
10
10
  # # => 'Europe/Kiev'
11
- #
11
+ #
12
12
  # WhereTZ.get(50.004444, 36.231389)
13
13
  # # => #<TZInfo::DataTimezone: Europe/Kiev>
14
14
  # ```
@@ -80,17 +80,36 @@ module WhereTZ
80
80
  def lookup_geo(lat, lng, candidates)
81
81
  point = GeoRuby::SimpleFeatures::Point.from_coordinates([lng, lat])
82
82
 
83
- candidates = candidates.map{|fname, zone, *|
83
+ polygons = candidates.map{|fname, zone, *|
84
84
  [zone, PARSER.parse(File.read(fname)).features.first.geometry]
85
- }.select{|_, multipolygon|
85
+ }
86
+ candidates = polygons.select{|_, multipolygon|
86
87
  multipolygon.geometries.any?{|polygon| polygon.contains_point?(point)}
87
88
  }
88
89
 
89
90
  case candidates.size
90
- when 0 then nil
91
+ when 0 then guess_outside(point, polygons)
91
92
  when 1 then candidates.first.first
92
93
  else
93
94
  fail(AmbigousTimezone, "Ambigous timezone: #{candidates.map(&:first)}")
94
95
  end
95
96
  end
97
+
98
+ # Last resort: pretty slow check for the cases when the point
99
+ # is slightly outside polygons.
100
+ # See https://github.com/zverok/wheretz/issues/4
101
+ def guess_outside(point, polygons)
102
+ # create pairs [timezone, distance to closest point of its polygon]
103
+ distances = polygons.map{|zone, multipolygon|
104
+ [
105
+ zone,
106
+ multipolygon.geometries.map(&:rings).flatten.
107
+ map{|p| p.ellipsoidal_distance(point)}.min
108
+ ]
109
+ }
110
+
111
+ # FIXME: maybe need some tolerance range for maximal reasonable distance?
112
+
113
+ distances.sort_by(&:last).first.first
114
+ end
96
115
  end
data/wheretz.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'wheretz'
3
- s.version = '0.0.1'
3
+ s.version = '0.0.2'
4
4
  s.authors = ['Victor Shepelev']
5
5
  s.email = 'zverok.offline@gmail.com'
6
6
  s.homepage = 'https://github.com/zverok/wheretz'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wheretz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Shepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-28 00:00:00.000000000 Z
11
+ date: 2016-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: georuby
@@ -116,6 +116,7 @@ extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
118
  - ".yardopts"
119
+ - Changelog.md
119
120
  - LICENSE.txt
120
121
  - README.md
121
122
  - bin/wheretz