ym4r 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ym4r/google_maps/geocoding.rb +116 -62
- data/rakefile.rb +3 -3
- metadata +44 -37
@@ -4,73 +4,127 @@ require 'rexml/document'
|
|
4
4
|
|
5
5
|
module Ym4r
|
6
6
|
module GoogleMaps
|
7
|
-
|
7
|
+
module Geocoding
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
xml = open(URI.encode(url)).read
|
23
|
-
rescue
|
24
|
-
raise ConnectionException.new("Unable to connect to Google Maps Geocoding service")
|
25
|
-
end
|
26
|
-
|
27
|
-
doc = REXML::Document.new(xml)
|
28
|
-
|
29
|
-
response = doc.elements['//Response']
|
30
|
-
placemarks = Placemarks.new(response.elements['name'].text,response.elements['Status/code'].text.to_i)
|
31
|
-
response.elements.each("Placemark") do |placemark|
|
32
|
-
data = placemark.elements
|
33
|
-
data_country = data['descendant::CountryNameCode']
|
34
|
-
data_administrative = data['descendant::AdministrativeAreaName']
|
35
|
-
data_sub_administrative = data['descendant::SubAdministrativeAreaName']
|
36
|
-
data_locality = data['descendant::LocalityName']
|
37
|
-
data_dependent_locality = data['descendant::DependentLocalityName']
|
38
|
-
data_thoroughfare = data['descendant::ThoroughfareName']
|
39
|
-
data_postal_code = data['descendant::PostalCodeNumber']
|
40
|
-
placemarks << Geocoding::Placemark.new(data['address'].text,
|
41
|
-
data_country.nil? ? "" : data_country.text,
|
42
|
-
data_administrative.nil? ? "" : data_administrative.text,
|
43
|
-
data_sub_administrative.nil? ? "" : data_sub_administrative.text,
|
44
|
-
data_locality.nil? ? "" : data_locality.text,
|
45
|
-
data_dependent_locality.nil? ? "" : data_dependent_locality.text,
|
46
|
-
data_thoroughfare.nil? ? "" : data_thoroughfare.text,
|
47
|
-
data_postal_code.nil? ? "" : data_postal_code.text,
|
48
|
-
*(data['descendant::coordinates'].text.split(",")[0..1].collect {|l| l.to_f }))
|
49
|
-
end
|
50
|
-
placemarks
|
51
|
-
end
|
9
|
+
GEO_SUCCESS = 200
|
10
|
+
GEO_MISSING_ADDRESS = 601
|
11
|
+
GEO_UNKNOWN_ADDRESS = 602
|
12
|
+
GEO_UNAVAILABLE_ADDRESS = 603
|
13
|
+
GEO_BAD_KEY = 610
|
14
|
+
GEO_TOO_MANY_QUERIES = 620
|
15
|
+
GEO_SERVER_ERROR = 500
|
16
|
+
|
17
|
+
#Gets placemarks by querying the Google Maps Geocoding service with the +request+ string. Options can either an explicity GMaps API key (<tt>:key</tt>) or a host, (<tt>:host</tt>).
|
18
|
+
def self.get(request,options = {})
|
19
|
+
api_key = options[:key]
|
20
|
+
output = options[:output] || "kml"
|
21
|
+
url = "http://maps.google.com/maps/geo?q=#{URI.encode(request)}&key=#{api_key}&output=#{output}"
|
52
22
|
|
53
|
-
|
54
|
-
class Placemarks < Array
|
55
|
-
attr_accessor :name,:status
|
23
|
+
res = open(url).read
|
56
24
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
25
|
+
case output.to_sym
|
26
|
+
when :json
|
27
|
+
res = eval(res.gsub(":","=>")) #!!!EVAL EVAL EVAL EVAL!!! hopefully we can trust google...
|
28
|
+
placemarks = Placemarks.new(res['name'],res['Status']['code'])
|
29
|
+
if res['Placemark']
|
30
|
+
placemark = res['Placemark']
|
31
|
+
|
32
|
+
placemark.each do |data|
|
33
|
+
|
34
|
+
data_country = data['Country']['CountryNameCode'] rescue ""
|
35
|
+
data_administrative = data['Country']['AdministrativeArea']['AdministrativeAreaName'] rescue ""
|
36
|
+
data_sub_administrative = data['Country']['AdministrativeArea']['SubAdministrativeArea']['SubAdministrativeAreaName'] rescue ""
|
37
|
+
data_locality = data['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['LocalityName'] rescue ""
|
38
|
+
data_dependent_locality = data['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['DependentLocality']['DependentLocalityName'] rescue ""
|
39
|
+
data_thoroughfare = data['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['DependentLocality']['Thoroughfare']['ThoroughfareName'] rescue ""
|
40
|
+
data_postal_code = data['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['DependentLocality']['Thoroughfare']['PostalCode']['PostalCodeNumber'] rescue ""
|
41
|
+
lon, lat = data['Point']['coordinates'][0,2]
|
42
|
+
data_accuracy = data['Accuracy']
|
43
|
+
unless data_accuracy.nil?
|
44
|
+
data_accuracy = data_accuracy.to_i
|
45
|
+
end
|
46
|
+
|
47
|
+
placemarks << Geocoding::Placemark.new(data['address'],
|
48
|
+
data_country,
|
49
|
+
data_administrative,
|
50
|
+
data_sub_administrative,
|
51
|
+
data_locality,
|
52
|
+
data_dependent_locality,
|
53
|
+
data_thoroughfare,
|
54
|
+
data_postal_code,
|
55
|
+
lon, lat, data_accuracy)
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
when :kml, :xml
|
60
|
+
|
61
|
+
doc = REXML::Document.new(res)
|
63
62
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
63
|
+
response = doc.elements['//Response']
|
64
|
+
placemarks = Placemarks.new(response.elements['name'].text,response.elements['Status/code'].text.to_i)
|
65
|
+
response.elements.each(".//Placemark") do |placemark|
|
66
|
+
data = placemark.elements
|
67
|
+
data_country = data['.//CountryNameCode']
|
68
|
+
data_administrative = data['.//AdministrativeAreaName']
|
69
|
+
data_sub_administrative = data['.//SubAdministrativeAreaName']
|
70
|
+
data_locality = data['.//LocalityName']
|
71
|
+
data_dependent_locality = data['.//DependentLocalityName']
|
72
|
+
data_thoroughfare = data['.//ThoroughfareName']
|
73
|
+
data_postal_code = data['.//PostalCodeNumber']
|
74
|
+
lon, lat = data['.//coordinates'].text.split(",")[0..1].collect {|l| l.to_f }
|
75
|
+
data_accuracy = data['.//*[local-name()="AddressDetails"]'].attributes['Accuracy']
|
76
|
+
unless data_accuracy.nil?
|
77
|
+
data_accuracy = data_accuracy.to_i
|
78
|
+
end
|
79
|
+
placemarks << Geocoding::Placemark.new(data['address'].text,
|
80
|
+
data_country.nil? ? "" : data_country.text,
|
81
|
+
data_administrative.nil? ? "" : data_administrative.text,
|
82
|
+
data_sub_administrative.nil? ? "" : data_sub_administrative.text,
|
83
|
+
data_locality.nil? ? "" : data_locality.text,
|
84
|
+
data_dependent_locality.nil? ? "" : data_dependent_locality.text,
|
85
|
+
data_thoroughfare.nil? ? "" : data_thoroughfare.text,
|
86
|
+
data_postal_code.nil? ? "" : data_postal_code.text,
|
87
|
+
lon, lat, data_accuracy )
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
placemarks
|
92
|
+
end
|
69
93
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
94
|
+
#Group of placemarks returned by the Geocoding service. If the result is valid the +status+ attribute should be equal to <tt>Geocoding::GE0_SUCCESS</tt>
|
95
|
+
class Placemarks < Array
|
96
|
+
attr_accessor :name,:status
|
97
|
+
|
98
|
+
def initialize(name,status)
|
99
|
+
super(0)
|
100
|
+
@name = name
|
101
|
+
@status = status
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
#A result from the Geocoding service.
|
106
|
+
class Placemark < Struct.new(:address,:country_code,:administrative_area,:sub_administrative_area,:locality,:dependent_locality,:thoroughfare,:postal_code,:longitude,:latitude,:accuracy)
|
107
|
+
def lonlat
|
108
|
+
[longitude,latitude]
|
109
|
+
end
|
110
|
+
|
111
|
+
def latlon
|
112
|
+
[latitude,longitude]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.accuracy(code)
|
117
|
+
{0 => "Unknown location",
|
118
|
+
1 => "Country level accuracy",
|
119
|
+
2 => "Region (state, province, prefecture, etc.) level accuracy",
|
120
|
+
3 => "Sub-region (county, municipality, etc.) level accuracy",
|
121
|
+
4 => "Town (city, village) level accuracy",
|
122
|
+
5 => "Post code (zip code) level accuracy",
|
123
|
+
6 => "Street level accuracy",
|
124
|
+
7 => "Intersection level accuracy",
|
125
|
+
8 => "Address level accuracy"}[code]
|
126
|
+
|
127
|
+
end
|
74
128
|
|
75
129
|
#Raised when the connection to the service is not possible
|
76
130
|
class ConnectionException < StandardError
|
data/rakefile.rb
CHANGED
@@ -24,13 +24,13 @@ spec = Gem::Specification::new do |s|
|
|
24
24
|
s.platform = Gem::Platform::RUBY
|
25
25
|
|
26
26
|
s.name = 'ym4r'
|
27
|
-
s.version = "0.6.
|
27
|
+
s.version = "0.6.1"
|
28
28
|
s.summary = "Helping the use of Google Maps and Yahoo! Maps API's from Ruby and Rails"
|
29
29
|
s.description = <<EOF
|
30
30
|
EOF
|
31
31
|
s.author = 'Guilhem Vellut'
|
32
|
-
s.email = 'guilhem.vellut
|
33
|
-
s.homepage = "http://
|
32
|
+
s.email = 'guilhem.vellut@gmail.com'
|
33
|
+
s.homepage = "http://ym4r.rubyforge.org"
|
34
34
|
|
35
35
|
s.requirements << 'none'
|
36
36
|
s.require_path = 'lib'
|
metadata
CHANGED
@@ -1,33 +1,26 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.2
|
3
|
-
specification_version: 1
|
4
2
|
name: ym4r
|
5
3
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.6.
|
7
|
-
date: 2007-09-12 00:00:00 +02:00
|
8
|
-
summary: Helping the use of Google Maps and Yahoo! Maps API's from Ruby and Rails
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: guilhem.vellut+ym4r@gmail.com
|
12
|
-
homepage: http://thepochisuperstarmegashow.com/projects/#ym4r
|
13
|
-
rubyforge_project:
|
14
|
-
description: ""
|
15
|
-
autorequire:
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.0.0
|
24
|
-
version:
|
4
|
+
version: 0.6.1
|
25
5
|
platform: ruby
|
26
|
-
signing_key:
|
27
|
-
cert_chain:
|
28
|
-
post_install_message:
|
29
6
|
authors:
|
30
7
|
- Guilhem Vellut
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-02-07 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: ""
|
17
|
+
email: guilhem.vellut@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
31
24
|
files:
|
32
25
|
- lib/ym4r/google_maps/api_key.rb
|
33
26
|
- lib/ym4r/google_maps/geocoding.rb
|
@@ -50,22 +43,36 @@ files:
|
|
50
43
|
- README
|
51
44
|
- MIT-LICENSE
|
52
45
|
- rakefile.rb
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://ym4r.rubyforge.org
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --main
|
51
|
+
- README
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements:
|
67
|
+
- none
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.0.1
|
70
|
+
signing_key:
|
71
|
+
specification_version: 2
|
72
|
+
summary: Helping the use of Google Maps and Yahoo! Maps API's from Ruby and Rails
|
53
73
|
test_files:
|
54
74
|
- test/test_geocoding.rb
|
55
75
|
- test/test_gm_geocoding.rb
|
56
76
|
- test/test_local_search.rb
|
57
77
|
- test/test_map_image.rb
|
58
78
|
- test/test_traffic.rb
|
59
|
-
rdoc_options:
|
60
|
-
- --main
|
61
|
-
- README
|
62
|
-
extra_rdoc_files:
|
63
|
-
- README
|
64
|
-
executables: []
|
65
|
-
|
66
|
-
extensions: []
|
67
|
-
|
68
|
-
requirements:
|
69
|
-
- none
|
70
|
-
dependencies: []
|
71
|
-
|