geocode 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,15 @@
1
+ = Version 0.2.0 - 2009-11-25
2
+ * Added --viewport (map extents)
3
+ * Added --accuracy
4
+ * Changed dump to YAML (MUCH more readable!)
5
+ * Added field labels
6
+ * Added --quiet to turn off said labels
7
+
8
+
9
+
10
+ = Version 0.1.0 - 2009-11-17
11
+ * Initial release
12
+ * geocode --latlng to get lat,lng
13
+ * geocode --reverse to get address
14
+ * google service only
15
+
data/README.rdoc CHANGED
@@ -3,6 +3,7 @@
3
3
  Yet another geoding library. Initial version geocodes using Google
4
4
  only but I plan to extend it to use Yahoo and possibly Tiger/Line.
5
5
 
6
+
6
7
  = Examples
7
8
 
8
9
  == Command-Line
@@ -30,10 +31,67 @@ result1 and result2 are hashes with the following extension functions added in:
30
31
  result.latlng # array of 2 floats, [latitude, longitude], returned by geocode.
31
32
  result.address # string containing the address returned by reverse_geocode.
32
33
 
34
+
35
+ = Options
36
+
37
+ == --help, -h
38
+ Get the most up to date help.
39
+
40
+ == --service, -s
41
+ Which service to use. Can be configured in geocode.yml. Currently only
42
+ google is supported.
43
+
44
+ == --google-api-key, -g
45
+ Your google API key. Can be configured in geocode.yml.
46
+
47
+ == --latlng, -l
48
+ Displays the lat/lng of the given address.
49
+
50
+ Default information to display, on by default if no other display mode
51
+ is selected.
52
+
53
+ == --viewport, -v
54
+ Displays the map viewport extends (bounding box) of the given address.
55
+
56
+ == --accuracy, -a
57
+ Displays the geocoding accuracy.
58
+
59
+ == --reverse, -r
60
+ Reverse geocodes a lat/lng to an address. Not compatible with
61
+ --latlng, --viewport or --accuracy; if any of those options are also
62
+ set they will be disabled.
63
+
64
+ Note: If you input a negative latitude from the command line, Trollop
65
+ tries to interpret this as an argument to the program. You should move
66
+ to the Northern Hemisphere. As a temporary workaround, however, you
67
+ can use '--' to tell Trollop to stop processing arguments, e.g.
68
+
69
+ $ geocode -r -- -23.548943,-46.638818
70
+ Address: Av. Vinte e Tr�s de Maio, 149-165 - Rep�blica, S�o Paulo - SP, 01316-060, Brazil
71
+
72
+ == --dump, -d
73
+ Geocodes (or reverse geocodes) the address and then dumps the returned
74
+ object. Use this to noodle around in the geocode result object for
75
+ other data.
76
+
77
+ == --quiet, -q
78
+ Turns off label printing. Useful when geocode is part of an external
79
+ toolchain, e.g.
80
+
81
+ $ echo The President lives at $(geocode -q 1600 Pennsylvania Ave, Charleston, WV 25302)
82
+
83
+ For example, I produced that full address with:
84
+
85
+ $ geocode -r $(geocode -q 1600 Pennsylvania Ave)
86
+
33
87
  = Installation
34
88
 
35
89
  gem install geocode
36
90
 
91
+ Ideally, then create ~/.geocode and in it, write a geocode.yml file
92
+ containing your google API key. See Configuration below for details.
93
+
94
+
37
95
  = Configuration (Command-Line)
38
96
 
39
97
  You can create a configuration file in ~/.geocode called geocode.yml.
@@ -53,10 +111,56 @@ bound by their Terms of Service. Most notably, this says that you MAY
53
111
  NOT "use or display the Content without a corresponding Google map".
54
112
  There are specific exceptions to this rule but be aware.
55
113
 
114
+
56
115
  = Authors
57
116
 
58
117
  * David Brady <github@shinybit.com>
59
118
 
119
+
60
120
  = License
61
121
 
62
- MIT.
122
+ MIT. See MIT-LICENSE file for details.
123
+
124
+
125
+ = TODO
126
+
127
+ == Caching
128
+ Google STRONGLY recommends caching geocoded data. For now, caching is
129
+ the responsibility of the client using the geocode library. Eventually
130
+ I'd like to just include a caching strategy and/or caching object to
131
+ Geocode.new_geocoder(). This hides the caching strategy from the
132
+ geocoding client.
133
+
134
+ Possible implementations might include defining internal strategies
135
+ (give me a sqlite3 file, or an ActiveRecord connection, or the address
136
+ of a memcached server, etc) or creating a cache object API and passing
137
+ that in.
138
+
139
+ UPDATE: As a command-line introspection/visualizing tool, caching is
140
+ pretty much useless. I'm burying this for now until and unless
141
+ somebody decides they want to use this as part of a toolchain or as a
142
+ library.
143
+
144
+ == Helpful Creation of geocode.yml
145
+ It is not unreasonable to expect a new geocode user to do this:
146
+
147
+ $ sudo gem install geocode
148
+ $ geocode 123 Some Address
149
+
150
+ This will fail because there's no Google API configured. It would be
151
+ nice to throw up an error message telling the user to create the
152
+ geocode.yml file. Optionally it should even offer to create ~/.geocode
153
+ and write the file there with a default API key.
154
+
155
+ == Better error handling
156
+ Remember the user from the step above? So he runs it and it creates
157
+ the default settings file with a sample API key. So he runs it again
158
+ without editing the file. The app should make it clear that the sample
159
+ API key needs to be edited. Or at the very least it should report
160
+ whatever error Google returns when an invalid API key is used.
161
+
162
+ == Open A Map
163
+ As I use geocode as a debugging and introspection tool, I keep wanting
164
+ to jump to a map to visualize the results. Consider emitting the uri
165
+ to a map using the Google static map API.
166
+
data/bin/geocode CHANGED
@@ -7,26 +7,68 @@ config_file = ["./geocode.yml", "~/.geocode/geocode.yml"].map {|p| File.expand_p
7
7
  config = config_file ? YAML::load_file(config_file) : {}
8
8
  config.each { |k,v| config[k.to_sym] = v}
9
9
 
10
+
11
+ # Notes:
12
+ # - If you specify --reverse, all of the following will be turned off: --latlng --viewport --accuracy
13
+ # - If you specify none of --reverse, --latlng, --viewport, or --accuracy, --latlng will be assumed.
10
14
  opts = Trollop.options do
11
15
  opt :google_api_key, "API KEY to use", :default => nil
12
16
  opt :service, "Geocoding Service to use", :default => nil
13
17
  opt :reverse, "Reverse geocode", :type => :boolean, :default => false
14
- opt :latlng, "Geocode to lat,lng (default)", :type => :boolean, :default => true
18
+ opt :latlng, "Geocode to lat,lng (default)", :type => :boolean, :default => false
19
+ opt :viewport, "Show viewport (map extents) of location", :type => :boolean, :default => false
20
+ opt :accuracy, "Show accuracy level of geocoded location", :type => :boolean, :default => false
21
+ opt :dump, "Dump the geocoded results", :type => :boolean, :default => false
22
+ opt :quiet, "Don't show labels", :type => :boolean, :default => false
15
23
  end
16
24
 
17
25
  opts[:google_api_key] ||= config[:google_api_key] || ""
18
26
  opts[:service] ||= config[:service] || "google"
19
27
  opts[:service] = opts[:service].to_sym
20
28
 
21
- # reverse geocoding is not compatible with latlng
29
+
30
+ # reverse geocoding is not compatible with latlng / viewport / accuracy.
22
31
  if opts[:reverse] && opts[:latlng]
23
32
  opts[:latlng] = false
33
+ opts[:viewport] = false
34
+ opts[:accuracy] = false
24
35
  end
25
36
 
37
+ # Default latlng to true if no other options have been given
38
+ opts[:latlng] = true unless [:reverse, :viewport, :accuracy].any? { |option| opts[option] }
39
+
40
+
26
41
  # geocode the address or lat/lng given
27
42
  g = Geocode.new_geocoder(opts[:service], opts)
43
+
44
+ geo = if opts[:reverse]
45
+ g.reverse_geocode(ARGV * ' ')
46
+ else
47
+ g.geocode(ARGV * ' ')
48
+ end
49
+
28
50
  if opts[:latlng]
29
- puts g.geocode(ARGV * ' ').latlng * ','
30
- else opts[:reverse]
31
- puts g.reverse_geocode(ARGV * ' ').address
51
+ print 'Location: ' unless opts[:quiet]
52
+ puts "%f,%f" % geo.latlng
53
+ end
54
+
55
+ if opts[:viewport]
56
+ print 'Viewport: ' unless opts[:quiet]
57
+ vp = geo.viewport
58
+ puts "%f,%f %f,%f" % vp
59
+ end
60
+
61
+ if opts[:accuracy]
62
+ print 'Accuracy: ' unless opts[:quiet]
63
+ puts geo.accuracy
64
+ end
65
+
66
+ if opts[:reverse]
67
+ print 'Address: ' unless opts[:quiet]
68
+ puts geo.address
69
+ end
70
+
71
+ if opts[:dump]
72
+ puts 'Dump: ' unless opts[:quiet]
73
+ puts geo.dump
32
74
  end
@@ -1,5 +1,6 @@
1
1
  require 'net/http'
2
2
  require 'json'
3
+ require 'yaml'
3
4
 
4
5
  class GoogleGeocode
5
6
  attr_reader :api_key
@@ -23,7 +24,7 @@ class GoogleGeocode
23
24
  end
24
25
 
25
26
  def dump
26
- inspect
27
+ self.to_yaml
27
28
  end
28
29
 
29
30
  def latlng
@@ -33,6 +34,15 @@ class GoogleGeocode
33
34
  def address
34
35
  self["Placemark"][0]["address"]
35
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
36
46
  end
37
47
  result
38
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geocode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Brady
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-17 00:00:00 -07:00
12
+ date: 2009-11-25 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -41,12 +41,14 @@ extensions: []
41
41
  extra_rdoc_files:
42
42
  - README.rdoc
43
43
  - MIT-LICENSE
44
+ - CHANGELOG.rdoc
44
45
  files:
46
+ - CHANGELOG.rdoc
47
+ - MIT-LICENSE
48
+ - README.rdoc
45
49
  - bin/geocode
46
50
  - lib/geocode.rb
47
51
  - lib/google_geocode.rb
48
- - README.rdoc
49
- - MIT-LICENSE
50
52
  has_rdoc: true
51
53
  homepage: http://github.com/dbrady/geocode/
52
54
  licenses: []