timezone_lat_lon 1.0 → 1.0.1

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
  SHA256:
3
- metadata.gz: 83f1636b6cbdc2dd4e89a6de5f3388b0e5c49db784a01b34a25cbd12005a18ce
4
- data.tar.gz: 3bce6b0fe53d0e27271e10fcf0eb0b8f4c206a129a81ee996182a95f3e391121
3
+ metadata.gz: f2d1bd9290f69fa622101dc58298187b5661692211700ab4b9137f060c13492f
4
+ data.tar.gz: c6903a82828a604c3ad63ef123a96df9366cdf381be2c79b30cf9e9cddf92cf9
5
5
  SHA512:
6
- metadata.gz: fbf6ecb7b358de8b118d83288153b3f90fbbb2c45fb204e1020aad0e0bd2c55dea1da887dd3a12ed171ceb64acead6b1ac00893c2d37100ca4d017ffd8f56379
7
- data.tar.gz: f8a4cbf36ef880e1b58804702a9ccf569ae45081d2ee2c2ab5da5a097aad49c912cab218ba14e621280eaef72cd0d9a549d53523189fdea8ec359f8cf8f9bdf7
6
+ metadata.gz: 954e36f11957d0e78da312072ac999e19c003aa2aeb0dbc1619191bd2f1a5d7f1a953d694576cb8af01316f9f51921c93bfe119d6501961ba4fd7f16efaf8c67
7
+ data.tar.gz: fd7dc6ae00595de75e1c6cde22edd87d214e7f6aec6ebe7c7640540419eadc35131e2767dc23b9c6addb10c37624abf60af2d306741c53875b9119804b5f3a50
@@ -5,10 +5,8 @@ module TimezoneLatLon
5
5
  class << self
6
6
  include Search
7
7
 
8
- @@loader = TimezoneLatLon::Loader.new(geojson_filename: 'combined-compressed.json')
9
-
10
8
  def loader
11
- return @@loader
9
+ @@loader ||= TimezoneLatLon::Loader.new(geojson_filename: 'combined-compressed.json')
12
10
  end
13
11
  end
14
12
  end
@@ -6,6 +6,8 @@ module TimezoneLatLon
6
6
  class Loader
7
7
  attr_accessor :config, :timezone_data
8
8
 
9
+ CACHE_FILE_NAME = 'time_zones.cache'
10
+
9
11
  def initialize(config = {})
10
12
  @config = {
11
13
  geojson_filename: 'combined.json'
@@ -17,10 +19,10 @@ module TimezoneLatLon
17
19
  private
18
20
 
19
21
  def load_data
20
- begin
21
- timezone_data_path = File.expand_path(File.join('..', '..', 'data', 'time_zones.dump'), __dir__)
22
- @timezone_data = Marshal.load(File.open(timezone_data_path).read)
23
- rescue => exception
22
+ timezone_dump_file = File.expand_path(File.join('..', '..', 'data', CACHE_FILE_NAME), __dir__)
23
+ if File.exists?(timezone_dump_file)
24
+ @timezone_data = Marshal.load(File.open(timezone_dump_file).read)
25
+ else # timezone_dump_file does not exist!
24
26
  self.load_data_from_geojson
25
27
  end
26
28
  end
@@ -41,6 +43,11 @@ module TimezoneLatLon
41
43
  attributes: attributes,
42
44
  }
43
45
  end
46
+
47
+ timezone_dump_file = File.expand_path(File.join('..', '..', 'data', CACHE_FILE_NAME), __dir__)
48
+ File.open(timezone_dump_file, "w") do |f|
49
+ f.write(Marshal.dump(@timezone_data))
50
+ end
44
51
  end
45
52
  end
46
53
  end
@@ -1,3 +1,3 @@
1
1
  module TimezoneLatLon
2
- VERSION = '1.0'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -0,0 +1,50 @@
1
+ describe 'TimezoneLatLon' do
2
+ latlon1 = { lat: 49.886505, lon: 25.167341 } # Ukraine
3
+ latlon2 = { lat: 35.852462, lon: 14.447913 } # Malta
4
+ latlon3 = { lat: 25.303352, lon: -156.278805 } # Pacific Ocean
5
+
6
+ # -- Test storing/loading time_zones.cache after initialization
7
+ describe 'cache file' do
8
+ CACHE_FILE = './data/time_zones.cache'
9
+
10
+ before :example do
11
+ cache_exists = File.exists?(CACHE_FILE)
12
+ File.delete(CACHE_FILE) if cache_exists
13
+
14
+ require 'timezone_lat_lon'
15
+ end
16
+
17
+ context 'on first run' do
18
+ it 'is created' do
19
+ TimezoneLatLon.find_timezone(latlon3)
20
+ cache_exists_after = File.exists?(CACHE_FILE)
21
+ expect(cache_exists_after).to eql(true)
22
+ end
23
+ end
24
+ end
25
+
26
+ # -- Test the find_timezone method
27
+ describe '.find_timezone(lat:, lon:)' do
28
+ before :context do
29
+ require 'timezone_lat_lon'
30
+ end
31
+
32
+ context "given #{latlon1.inspect}" do
33
+ it "returns Europe/Ukraine" do
34
+ expect(TimezoneLatLon.find_timezone(latlon1)).to eql(TZInfo::Timezone.get('Europe/Kiev'))
35
+ end
36
+ end
37
+
38
+ context "given #{latlon2}" do
39
+ it "returns Europe/Malta" do
40
+ expect(TimezoneLatLon.find_timezone(latlon2)).to eql(TZInfo::Timezone.get('Europe/Malta'))
41
+ end
42
+ end
43
+
44
+ context "given #{latlon3}" do
45
+ it "returns nil" do
46
+ expect(TimezoneLatLon.find_timezone(latlon3)).to eql(nil)
47
+ end
48
+ end
49
+ end
50
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timezone_lat_lon
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert J. Schofield
@@ -80,21 +80,7 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '2.0'
83
- - !ruby/object:Gem::Dependency
84
- name: byebug
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '11.1'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '11.1'
97
- description: Find timezones by latitude and longitude.
83
+ description: Utilities for looking up the timezone contained by a latitude and longitude.
98
84
  email: rjschofield96@gmail.com
99
85
  executables: []
100
86
  extensions: []
@@ -107,7 +93,7 @@ files:
107
93
  - lib/timezone_lat_lon/loader.rb
108
94
  - lib/timezone_lat_lon/search.rb
109
95
  - lib/timezone_lat_lon/version.rb
110
- - scripts/process-timezones.rb
96
+ - spec/timezone_lat_lon_spec.rb
111
97
  homepage: https://github.com/rjschof/timezone_lat_lon
112
98
  licenses:
113
99
  - MIT
@@ -130,5 +116,5 @@ requirements: []
130
116
  rubygems_version: 3.1.4
131
117
  signing_key:
132
118
  specification_version: 4
133
- summary: Find timezones by latitude and longitude.
119
+ summary: Timezone lookup by latitude and longitude
134
120
  test_files: []
@@ -1,87 +0,0 @@
1
- require 'benchmark'
2
- require 'byebug'
3
- require 'kdtree'
4
- require 'oj'
5
- require 'rgeo/shapefile'
6
- require 'rgeo/geo_json'
7
- require 'ruby-progressbar'
8
-
9
- # -- Global variables
10
- $timezone_data = []
11
-
12
- # -- Helper methods
13
- def find_containing_timezone(lat:, lon:)
14
- factory = RGeo::Geographic.simple_mercator_factory
15
- point = factory.point(lon, lat)
16
- return $timezone_data.find { |sd| sd[:geometry].contains?(point) }
17
- end
18
-
19
- def load_geojson(geojson_path)
20
- load_benchmark = Benchmark.measure do
21
- file = File.open(geojson_path)
22
- content = file.read
23
- geojson_obj = Oj.load(content)
24
- features = RGeo::GeoJSON.decode(geojson_obj)
25
- features.each do |feature|
26
- attributes = feature.keys.map do |key|
27
- [key, feature[key]]
28
- end.to_h
29
-
30
- $timezone_data << {
31
- geometry: feature.geometry,
32
- attributes: attributes,
33
- }
34
- end
35
- end
36
- puts("Load Benchmark: #{load_benchmark}")
37
- end
38
-
39
- def load_shapefile(shapefile_path)
40
- load_benchmark = Benchmark.measure do
41
- RGeo::Shapefile::Reader.open(shapefile_path) do |file|
42
- file.each do |record|
43
- progress.increment
44
- $timezone_data << {
45
- geometry: record.geometry,
46
- attributes: record.attributes
47
- }
48
- end
49
- end
50
- end
51
- puts("Load Benchmark: #{load_benchmark}")
52
- end
53
-
54
- def test_timezone_check(lat:, lon:)
55
- timezone = nil
56
- benchmark = Benchmark.measure do
57
- timezone = find_containing_timezone(lat: lat, lon: lon)
58
- end
59
- timezone_attrs = timezone.nil? ? nil : timezone[:attributes]
60
- puts("Timezone for [#{lat}, #{lon}]: #{timezone_attrs || 'NOT_FOUND'} - #{benchmark}")
61
- end
62
-
63
- # -- Script processing
64
- # load_shapefile('./data/timezones/shapefile/combined-shapefile.shp')
65
- begin
66
- timezone_data = File.open(File.expand_path("../../data/time_zones.dump", __FILE__)).read
67
- $timezone_data = Marshal.load(timezone_data)
68
- puts('Loaded timezone data from from dumpfile.')
69
- rescue => exception
70
- puts("Exception: #{exception.inspect}")
71
- load_geojson('./data/timezones/geojson/combined-3.json')
72
- File.open(File.expand_path("../../data/time_zones.dump", __FILE__),"w") do |f|
73
- f.write(Marshal.dump($timezone_data))
74
- end
75
- puts('Loaded timezone data from scratch.')
76
- end
77
-
78
- puts("---------- Testing ----------")
79
- latlon_list = [
80
- { lat: 49.886505, lon: 25.167341 }, # Ukraine
81
- { lat: 35.852462, lon: 14.447913 }, # Malta
82
- { lat: 23.058549, lon: 84.893071 }, # India... Jharkhand
83
- { lat: 25.303352, lon: -156.278805 }, # Pacific Ocean .. Near Hawaii
84
- ]
85
- latlon_list.each do |latlon|
86
- test_timezone_check(latlon)
87
- end