glw 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/lib/glw.rb +69 -8
  5. metadata +24 -4
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1aae675ed2dc09d49a31cea98d0372c1f127a3ac
4
- data.tar.gz: 0a5884c14ea61823d18b51de0d0aaa19090b9d53
3
+ metadata.gz: 0bdbbfb78dda24bd5a804415c774b09409abad81
4
+ data.tar.gz: fb70b89fd9182a4da1d2473a353f5bd0328659c4
5
5
  SHA512:
6
- metadata.gz: a3aaa05b445ebb7cc31bf5c89c5baf35ed205fac71183e7723f3e31c6cfb9578874d9a6e33af6252fe412f4fce774e67d7516677b755b810f40d5c5348012863
7
- data.tar.gz: 1e98d7a561a5b978dbe9fbb1685d22d59e6d883cbae219e2c077382851b0cbecaf1f97dc3095bf4bb346d33460d20456f97e15e6842e2488ceade1d55ccb2815
6
+ metadata.gz: 14fcfe53fa01ef10a7975208a18df05afe9315d664987e92e27bb3510c688124d2d225e3de31e3d471635bbabc490f762a4eca8e42f1cf4af84f6881d7bb7da1
7
+ data.tar.gz: 6c51ba4c6446641d110e76a72fa1081b82975fc3295acf3b4bb9bced7dc5c872326e996d4a6cf6665c349cf2b09d1c2da4f77fe3dba2f1852e143eb205b1679b
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/glw.rb CHANGED
@@ -5,21 +5,31 @@
5
5
 
6
6
  require 'geocoder'
7
7
  require 'geodesic'
8
+ require 'recordx_sqlite'
8
9
 
9
10
 
10
11
  class Glw
11
12
 
12
- def initialize()
13
+ def initialize(dbfile='glw.db')
14
+
13
15
  Geocoder.configure(:timeout => 3)
16
+
17
+ @coordinates = RecordxSqlite.new(dbfile,
18
+ table: {coordinates: {coordinates: '', place_id: ''}})
19
+
20
+ fields = %i(place_id address city coordinates country postal_code route
21
+ street_number types)
22
+ h = fields.inject({}) {|r, x| r.merge(x => '') }
23
+ @places = RecordxSqlite.new(dbfile, table: {places: h})
24
+
14
25
  end
15
26
 
16
27
  def locate(lat, lon)
17
28
 
18
- s = "%s, %s" % [lat, lon]
19
- r = Geocoder.search s
29
+ loc = search lat, lon
20
30
 
21
31
  p1 = Geodesic::Position.new(lat, lon)
22
- place = Geodesic::Position.new(*r[0].coordinates)
32
+ place = Geodesic::Position.new(*loc.coordinates)
23
33
 
24
34
  d = Geodesic::dist_haversine(place.lat, place.lon, p1.lat, p1.lon).round(3)
25
35
  b = Geodesic::bearing(place.lat, place.lon, p1.lat, p1.lon).round
@@ -27,12 +37,63 @@ class Glw
27
37
  fields = %i(address city coordinates country postal_code route
28
38
  street_number types)
29
39
 
30
- h = fields.inject({}) do |hash, x|
31
- hash.merge(x => r[0].method(x).call)
32
- end
33
-
40
+ h = fields.inject({}) {|hash, x| hash.merge(x => loc.method(x).call) }
34
41
  h.merge({relative_distance: d, relative_bearing: b})
42
+
35
43
  end
44
+
45
+ private
46
+
47
+ def search(lat, lon)
48
+
49
+ # search the database using the coordinates
50
+
51
+ lat_lon = "%s, %s" % [lat.round(5), lon.round(5)]
52
+
53
+ sql = "SELECT *
54
+ FROM places INNER JOIN coordinates ON coordinates.place_id = places.place_id
55
+ WHERE coordinates.coordinates like '#{lat_lon}'"
56
+
57
+ found = @coordinates.query sql
58
+
59
+ if found.any? then
60
+
61
+ r = OpenStruct.new(found.first.to_h)
62
+ r.coordinates = r.coordinates.split(', ').map(&:to_f)
63
+ r.types = r.types.split
64
+ r.freeze
65
+
66
+ else
67
+
68
+ r = Geocoder.search "%s, %s" % [lat, lon]
69
+
70
+ # Add the place_id and place details in the *place* table if the
71
+ # place_id isn't already in the table.
72
+
73
+ place = @places.find r[0].place_id
74
+
75
+ if !place then
76
+
77
+ fields = %i(place_id address city country postal_code route
78
+ street_number )
79
+
80
+ h = fields.inject({}) {|hash, x| hash.merge(x => r[0].method(x).call) }
81
+ h.merge!({coordinates: r[0].coordinates.join(', ')})
82
+ h.merge!({types: r[0].types.join(' ')})
83
+
84
+ @places.create h
85
+ place = OpenStruct.new(h).freeze
86
+
87
+ end
88
+
89
+ # If the entry isn't found, add the coordinates and the place_id
90
+ # into the *coordinates* table.
91
+
92
+ @coordinates.create coordinates: lat_lon, place_id: place.place_id
93
+ r[0]
94
+
95
+ end
96
+ end
36
97
  end
37
98
 
38
99
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glw
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -31,7 +31,7 @@ cert_chain:
31
31
  VjRGNrP/e8DzppPHs0UL/CzbWYLyfrBZUJpbNIFlzu6g62M1vkvpzwoC12eXHyET
32
32
  uLwQN+MzhB3/YA==
33
33
  -----END CERTIFICATE-----
34
- date: 2017-06-23 00:00:00.000000000 Z
34
+ date: 2017-06-24 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: geocoder
@@ -73,6 +73,26 @@ dependencies:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: 1.0.1
76
+ - !ruby/object:Gem::Dependency
77
+ name: recordx_sqlite
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.2'
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 0.2.2
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - "~>"
91
+ - !ruby/object:Gem::Version
92
+ version: '0.2'
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 0.2.2
76
96
  description:
77
97
  email: james@jamesrobertson.eu
78
98
  executables: []
@@ -103,6 +123,6 @@ rubyforge_project:
103
123
  rubygems_version: 2.6.8
104
124
  signing_key:
105
125
  specification_version: 4
106
- summary: 'A wrapper for the geocoder gem to returns the place name and more for a
107
- given longitude and latitude. #gps #geolocation'
126
+ summary: 'A wrapper for the geocoder gem to return the place name and more for a given
127
+ longitude and latitude. #gps #geolocation'
108
128
  test_files: []
metadata.gz.sig CHANGED
Binary file