timezone_lat_lon 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ require 'timezone_lat_lon/loader'
2
+ require 'timezone_lat_lon/search'
3
+
4
+ module TimezoneLatLon
5
+ class << self
6
+ include Search
7
+
8
+ @@loader = TimezoneLatLon::Loader.new(geojson_filename: 'combined-compressed.json')
9
+
10
+ def loader
11
+ return @@loader
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,46 @@
1
+ require 'oj'
2
+ require 'rgeo/shapefile'
3
+ require 'rgeo/geo_json'
4
+
5
+ module TimezoneLatLon
6
+ class Loader
7
+ attr_accessor :config, :timezone_data
8
+
9
+ def initialize(config = {})
10
+ @config = {
11
+ geojson_filename: 'combined.json'
12
+ }.merge(config)
13
+
14
+ load_data
15
+ end
16
+
17
+ private
18
+
19
+ 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
24
+ self.load_data_from_geojson
25
+ end
26
+ end
27
+
28
+ def load_data_from_geojson
29
+ geojson_path = File.expand_path(File.join('..', '..', 'data', 'timezones', 'geojson', @config[:geojson_filename]), __dir__)
30
+ content = File.open(geojson_path).read
31
+ geojson_obj = Oj.load(content)
32
+ features = RGeo::GeoJSON.decode(geojson_obj)
33
+ @timezone_data = []
34
+ features.each_with_index do |feature, index|
35
+ attributes = feature.keys.map do |key|
36
+ [key, feature[key]]
37
+ end.to_h
38
+
39
+ @timezone_data << {
40
+ geometry: feature.geometry,
41
+ attributes: attributes,
42
+ }
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,12 @@
1
+ require 'tzinfo'
2
+
3
+ module TimezoneLatLon
4
+ module Search
5
+ def find_timezone(lat: , lon:)
6
+ factory = RGeo::Geographic.simple_mercator_factory
7
+ point = factory.point(lon, lat)
8
+ raw_timezone = TimezoneLatLon.loader.timezone_data.find { |sd| sd[:geometry].contains?(point) }
9
+ return raw_timezone.nil? ? nil : TZInfo::Timezone.get(raw_timezone[:attributes]['tzid'])
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module TimezoneLatLon
2
+ VERSION = '1.0'
3
+ end
@@ -0,0 +1,87 @@
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
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timezone_lat_lon
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Robert J. Schofield
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oj
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rgeo
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rgeo-geojson
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rgeo-shapefile
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: tzinfo
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
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.
98
+ email: rjschofield96@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - LICENSE.md
104
+ - README.md
105
+ - data/timezones/geojson/combined-compressed.json
106
+ - lib/timezone_lat_lon.rb
107
+ - lib/timezone_lat_lon/loader.rb
108
+ - lib/timezone_lat_lon/search.rb
109
+ - lib/timezone_lat_lon/version.rb
110
+ - scripts/process-timezones.rb
111
+ homepage: https://github.com/rjschof/timezone_lat_lon
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.1.4
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Find timezones by latitude and longitude.
134
+ test_files: []