geocode 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/geocode +9 -1
- data/lib/geocode.rb +4 -3
- data/lib/google/geocode.rb +31 -0
- data/lib/google/result.rb +46 -0
- metadata +26 -14
- data/lib/google_geocode.rb +0 -60
data/bin/geocode
CHANGED
@@ -19,6 +19,8 @@ opts = Trollop.options do
|
|
19
19
|
opt :viewport, "Show viewport (map extents) of location", :type => :boolean, :default => false
|
20
20
|
opt :accuracy, "Show accuracy level of geocoded location", :type => :boolean, :default => false
|
21
21
|
opt :dump, "Dump the geocoded results", :type => :boolean, :default => false
|
22
|
+
# TODO: Show this in metric as well as Burmese Units...
|
23
|
+
opt :miles, "Show the viewport size in miles", :type => :boolean, :default => false
|
22
24
|
opt :quiet, "Don't show labels", :type => :boolean, :default => false
|
23
25
|
end
|
24
26
|
|
@@ -32,10 +34,11 @@ if opts[:reverse] && opts[:latlng]
|
|
32
34
|
opts[:latlng] = false
|
33
35
|
opts[:viewport] = false
|
34
36
|
opts[:accuracy] = false
|
37
|
+
opts[:miles] = false
|
35
38
|
end
|
36
39
|
|
37
40
|
# Default latlng to true if no other options have been given
|
38
|
-
opts[:latlng] = true unless [:reverse, :viewport, :accuracy].any? { |option| opts[option] }
|
41
|
+
opts[:latlng] = true unless [:miles, :reverse, :viewport, :accuracy].any? { |option| opts[option] }
|
39
42
|
|
40
43
|
|
41
44
|
# geocode the address or lat/lng given
|
@@ -57,6 +60,11 @@ if opts[:viewport]
|
|
57
60
|
vp = geo.viewport
|
58
61
|
puts "%f,%f %f,%f" % vp
|
59
62
|
end
|
63
|
+
|
64
|
+
if opts[:miles]
|
65
|
+
print 'Viewport Height (miles): ' unless opts[:quiet]
|
66
|
+
puts "%f" % geo.viewport_height
|
67
|
+
end
|
60
68
|
|
61
69
|
if opts[:accuracy]
|
62
70
|
print 'Accuracy: ' unless opts[:quiet]
|
data/lib/geocode.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'google', 'geocode'))
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'google', 'result'))
|
2
3
|
|
3
4
|
class Geocode
|
4
5
|
def self.new_geocoder(service, options)
|
5
6
|
case service
|
6
7
|
when :google
|
7
|
-
|
8
|
+
Google::Geocode.new options[:google_api_key], options[:client]
|
8
9
|
else
|
9
10
|
raise "Unsupported geocode service: #{service}"
|
10
11
|
end
|
11
12
|
end
|
12
|
-
|
13
|
+
|
13
14
|
def geocode()
|
14
15
|
raise "geocode(): Unimplemented method! Child class must override!"
|
15
16
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module Google
|
5
|
+
class Geocode
|
6
|
+
attr_reader :api_key
|
7
|
+
def initialize(api_key, client)
|
8
|
+
@api_key = api_key
|
9
|
+
@client = client
|
10
|
+
end
|
11
|
+
|
12
|
+
def do_geocode(location)
|
13
|
+
url = "http://maps.google.com/maps/geo?sensor=false&key=#{@api_key}&output=json&q=#{location}&client=#{@client}"
|
14
|
+
resp = Net::HTTP.get_response(URI.parse(url))
|
15
|
+
data = resp.body
|
16
|
+
|
17
|
+
Google::Result.new(data)
|
18
|
+
end
|
19
|
+
|
20
|
+
def geocode(location)
|
21
|
+
# change whitespace to %20 for the URL
|
22
|
+
result = do_geocode location.gsub(/\s+/, '%20')
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
def reverse_geocode(location)
|
27
|
+
# reverse geocodes should not have any spaces in them
|
28
|
+
result = do_geocode location.gsub(/\s+/, '')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Google
|
4
|
+
class Result
|
5
|
+
attr_accessor :data
|
6
|
+
|
7
|
+
def initialize(data)
|
8
|
+
@data = JSON.parse data
|
9
|
+
end
|
10
|
+
|
11
|
+
def success?
|
12
|
+
data["Status"]["code"] == 200
|
13
|
+
end
|
14
|
+
|
15
|
+
def error?
|
16
|
+
!success?
|
17
|
+
end
|
18
|
+
|
19
|
+
def dump
|
20
|
+
data.to_yaml
|
21
|
+
end
|
22
|
+
|
23
|
+
def latlng
|
24
|
+
data["Placemark"][0]["Point"]["coordinates"][0..1].reverse
|
25
|
+
end
|
26
|
+
|
27
|
+
def address
|
28
|
+
data["Placemark"][0]["address"]
|
29
|
+
end
|
30
|
+
|
31
|
+
def accuracy
|
32
|
+
data["Placemark"][0]["AddressDetails"]["Accuracy"]
|
33
|
+
end
|
34
|
+
|
35
|
+
# returns viewport in tlbr format
|
36
|
+
def viewport
|
37
|
+
@vp ||= [:north, :west, :south, :east].map {|dir| data["Placemark"][0]["ExtendedData"]["LatLonBox"][dir.to_s] }
|
38
|
+
end
|
39
|
+
|
40
|
+
def viewport_height
|
41
|
+
vp = viewport
|
42
|
+
vs = (vp[0] - vp[2]) * 67.0
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geocode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- David Brady
|
@@ -9,29 +14,33 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-09-14 00:00:00 -06:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: trollop
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
23
29
|
version: "0"
|
24
|
-
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
25
32
|
- !ruby/object:Gem::Dependency
|
26
33
|
name: json
|
27
|
-
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
36
|
requirements:
|
31
37
|
- - ">="
|
32
38
|
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
33
41
|
version: "0"
|
34
|
-
|
42
|
+
type: :runtime
|
43
|
+
version_requirements: *id002
|
35
44
|
description: geocode, a ruby library and command-line tool for geocoding addresses and reverse geocoding coordinates.
|
36
45
|
email: github@shinybit.com
|
37
46
|
executables:
|
@@ -48,7 +57,8 @@ files:
|
|
48
57
|
- README.rdoc
|
49
58
|
- bin/geocode
|
50
59
|
- lib/geocode.rb
|
51
|
-
- lib/
|
60
|
+
- lib/google/geocode.rb
|
61
|
+
- lib/google/result.rb
|
52
62
|
has_rdoc: true
|
53
63
|
homepage: http://github.com/dbrady/geocode/
|
54
64
|
licenses: []
|
@@ -67,18 +77,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
67
77
|
requirements:
|
68
78
|
- - ">="
|
69
79
|
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
70
82
|
version: "0"
|
71
|
-
version:
|
72
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
84
|
requirements:
|
74
85
|
- - ">="
|
75
86
|
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 0
|
76
89
|
version: "0"
|
77
|
-
version:
|
78
90
|
requirements: []
|
79
91
|
|
80
92
|
rubyforge_project:
|
81
|
-
rubygems_version: 1.3.
|
93
|
+
rubygems_version: 1.3.6
|
82
94
|
signing_key:
|
83
95
|
specification_version: 3
|
84
96
|
summary: Geocoding library and CLI tool
|
data/lib/google_geocode.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'json'
|
3
|
-
require 'yaml'
|
4
|
-
|
5
|
-
class GoogleGeocode
|
6
|
-
attr_reader :api_key
|
7
|
-
def initialize(api_key)
|
8
|
-
@api_key = api_key
|
9
|
-
end
|
10
|
-
|
11
|
-
def do_geocode(location)
|
12
|
-
url = "http://maps.google.com/maps/geo?sensor=false&key=#{@api_key}&output=json&q=#{location}"
|
13
|
-
resp = Net::HTTP.get_response(URI.parse(url))
|
14
|
-
data = resp.body
|
15
|
-
|
16
|
-
result = JSON.parse(data)
|
17
|
-
class << result
|
18
|
-
def success?
|
19
|
-
self["Status"]["code"] == 200
|
20
|
-
end
|
21
|
-
|
22
|
-
def error?
|
23
|
-
!success?
|
24
|
-
end
|
25
|
-
|
26
|
-
def dump
|
27
|
-
self.to_yaml
|
28
|
-
end
|
29
|
-
|
30
|
-
def latlng
|
31
|
-
self["Placemark"][0]["Point"]["coordinates"][0..1].reverse
|
32
|
-
end
|
33
|
-
|
34
|
-
def address
|
35
|
-
self["Placemark"][0]["address"]
|
36
|
-
end
|
37
|
-
|
38
|
-
def accuracy
|
39
|
-
self["Placemark"][0]["AddressDetails"]["Accuracy"]
|
40
|
-
end
|
41
|
-
|
42
|
-
# returns viewport in tlbr format
|
43
|
-
def viewport
|
44
|
-
vp = [:north, :west, :south, :east].map {|dir| self["Placemark"][0]["ExtendedData"]["LatLonBox"][dir.to_s] }
|
45
|
-
end
|
46
|
-
end
|
47
|
-
result
|
48
|
-
end
|
49
|
-
|
50
|
-
def geocode(location)
|
51
|
-
# change whitespace to %20 for the URL
|
52
|
-
result = do_geocode location.gsub(/\s+/, '%20')
|
53
|
-
end
|
54
|
-
|
55
|
-
|
56
|
-
def reverse_geocode(location)
|
57
|
-
# reverse geocodes should not have any spaces in them
|
58
|
-
result = do_geocode location.gsub(/\s+/, '')
|
59
|
-
end
|
60
|
-
end
|