geocode 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2008-2009 David Brady github@shinybit.com
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,62 @@
1
+ = geocode
2
+
3
+ Yet another geoding library. Initial version geocodes using Google
4
+ only but I plan to extend it to use Yahoo and possibly Tiger/Line.
5
+
6
+ = Examples
7
+
8
+ == Command-Line
9
+
10
+ Geocode an address using google:
11
+
12
+ geocode -s google -g abcd1234_SAMPLE_GOOGLE_API_KEY_etc 1600 Amphitheatre Parkway, Mountain View, CA
13
+ # => 37.421759,-122.08437
14
+
15
+ Reverse geocode a lat/lng (with google api key set in configuration file):
16
+
17
+ geocode -r 37.421759,-122.08437
18
+ # => 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA
19
+
20
+ == Ruby
21
+
22
+ g = Geocode.new_geocoder :google, {:google_api_key => "abcd1234_SAMPLE_GOOGLE_API_KEY_etc"}
23
+ result1 = g.geocode "1600 Amphitheatre Parkway, Mountain View, CA"
24
+ result2 = g.reverse_geocode "37.421759,-122.08437"
25
+
26
+ result1 and result2 are hashes with the following extension functions added in:
27
+
28
+ result.success? # true if the geocode/reverse_geocode succeeded.
29
+ result.error? # opposite of success.
30
+ result.latlng # array of 2 floats, [latitude, longitude], returned by geocode.
31
+ result.address # string containing the address returned by reverse_geocode.
32
+
33
+ = Installation
34
+
35
+ gem install geocode
36
+
37
+ = Configuration (Command-Line)
38
+
39
+ You can create a configuration file in ~/.geocode called geocode.yml.
40
+ It's a handy place to put your Google API key, for example. You can
41
+ also place geocode.yml in the current directory if you don't want to
42
+ place it in your home folder. A sample file would look like:
43
+
44
+ ---
45
+ service: google
46
+ google_api_key: abcd1234_SAMPLE_GOOGLE_API_KEY_etc
47
+
48
+
49
+ = TOS WARNING
50
+
51
+ The Google geocoding service uses the Google Map API and thus you are
52
+ bound by their Terms of Service. Most notably, this says that you MAY
53
+ NOT "use or display the Content without a corresponding Google map".
54
+ There are specific exceptions to this rule but be aware.
55
+
56
+ = Authors
57
+
58
+ * David Brady <github@shinybit.com>
59
+
60
+ = License
61
+
62
+ MIT.
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+ require 'trollop'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'geocode'))
4
+
5
+ config_file = ["./geocode.yml", "~/.geocode/geocode.yml"].map {|p| File.expand_path(p)}.find {|p| File.exists? p}
6
+
7
+ config = config_file ? YAML::load_file(config_file) : {}
8
+ config.each { |k,v| config[k.to_sym] = v}
9
+
10
+ opts = Trollop.options do
11
+ opt :google_api_key, "API KEY to use", :default => nil
12
+ opt :service, "Geocoding Service to use", :default => nil
13
+ opt :reverse, "Reverse geocode", :type => :boolean, :default => false
14
+ opt :latlng, "Geocode to lat,lng (default)", :type => :boolean, :default => true
15
+ end
16
+
17
+ opts[:google_api_key] ||= config[:google_api_key] || ""
18
+ opts[:service] ||= config[:service] || "google"
19
+ opts[:service] = opts[:service].to_sym
20
+
21
+ # reverse geocoding is not compatible with latlng
22
+ if opts[:reverse] && opts[:latlng]
23
+ opts[:latlng] = false
24
+ end
25
+
26
+ # geocode the address or lat/lng given
27
+ g = Geocode.new_geocoder(opts[:service], opts)
28
+ if opts[:latlng]
29
+ puts g.geocode(ARGV * ' ').latlng * ','
30
+ else opts[:reverse]
31
+ puts g.reverse_geocode(ARGV * ' ').address
32
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'google_geocode'))
2
+
3
+ class Geocode
4
+ def self.new_geocoder(service, options)
5
+ case service
6
+ when :google
7
+ GoogleGeocode.new options[:google_api_key]
8
+ else
9
+ raise "Unsupported geocode service: #{service}"
10
+ end
11
+ end
12
+
13
+ def geocode()
14
+ raise "geocode(): Unimplemented method! Child class must override!"
15
+ end
16
+
17
+ def reverse_geocode()
18
+ raise "reverse_geocode(): Unimplemented method! Child class must override!"
19
+ end
20
+ end
@@ -0,0 +1,50 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ class GoogleGeocode
5
+ attr_reader :api_key
6
+ def initialize(api_key)
7
+ @api_key = api_key
8
+ end
9
+
10
+ def do_geocode(location)
11
+ url = "http://maps.google.com/maps/geo?sensor=false&key=#{@api_key}&output=json&q=#{location}"
12
+ resp = Net::HTTP.get_response(URI.parse(url))
13
+ data = resp.body
14
+
15
+ result = JSON.parse(data)
16
+ class << result
17
+ def success?
18
+ self["Status"]["code"] == 200
19
+ end
20
+
21
+ def error?
22
+ !success?
23
+ end
24
+
25
+ def dump
26
+ inspect
27
+ end
28
+
29
+ def latlng
30
+ self["Placemark"][0]["Point"]["coordinates"][0..1].reverse
31
+ end
32
+
33
+ def address
34
+ self["Placemark"][0]["address"]
35
+ end
36
+ end
37
+ result
38
+ end
39
+
40
+ def geocode(location)
41
+ # change whitespace to %20 for the URL
42
+ result = do_geocode location.gsub(/\s+/, '%20')
43
+ end
44
+
45
+
46
+ def reverse_geocode(location)
47
+ # reverse geocodes should not have any spaces in them
48
+ result = do_geocode location.gsub(/\s+/, '')
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geocode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Brady
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-17 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: trollop
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: geocode, a ruby library and command-line tool for geocoding addresses and reverse geocoding coordinates.
36
+ email: github@shinybit.com
37
+ executables:
38
+ - geocode
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ - MIT-LICENSE
44
+ files:
45
+ - bin/geocode
46
+ - lib/geocode.rb
47
+ - lib/google_geocode.rb
48
+ - README.rdoc
49
+ - MIT-LICENSE
50
+ has_rdoc: true
51
+ homepage: http://github.com/dbrady/geocode/
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --line-numbers
57
+ - --inline-source
58
+ - --main
59
+ - README.rdoc
60
+ - --title
61
+ - geocode
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.5
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Geocoding library and CLI tool
83
+ test_files: []
84
+