/
emulategoogle.rb
215 lines (203 loc) · 6.03 KB
/
emulategoogle.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# EmulateGoogle
#
# This module takes provides an interface that's compatible with Google's geocoding
# API. See https://developers.google.com/maps/documentation/geocoding/ for details.
#
# Copyright (C) 2012 Pete Warden <pete@petewarden.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'rubygems' if RUBY_VERSION < '1.9'
require 'json'
cwd = File.expand_path(File.dirname(__FILE__))
require File.join(cwd, 'street2coordinates')
# Emulates the interface to Google's geocoding API.
def google_geocoder_api_call(params)
format = params[:format].downcase
address = params[:address]
latlng = params[:latlng]
result = nil
if address
found = street2coordinates([address])
info = found[address]
if !info
locations = find_locations_in_text('at '+address.capitalize)
locations.each_with_index do |location_info, index|
found_tokens = location_info[:found_tokens]
match_start_index = found_tokens[0][:start_index]
match_end_index = found_tokens[found_tokens.length-1][:end_index]
location = found_tokens[0]
lat = location[:lat].to_f
lon = location[:lon].to_f
type = location[:type]
if type == :CITY
bounding_range = 0.1
type_name = 'locality'
address_components = [
{
'long_name' => location[:matched_string],
'short_name' => location[:matched_string],
'types' => [ 'locality', 'political' ],
},
{
'long_name' => location[:country_code],
'short_name' => location[:country_code],
'types' => [ 'country', 'political' ],
},
]
else
bounding_range = 5.0
type_name = 'country'
address_components = [
{
'long_name' => location[:matched_string],
'short_name' => location[:code],
'types' => [ 'country', 'political' ],
},
]
end
result = {
'address_components' => address_components,
'geometry' => {
'location' => {
'lat' => lat,
'lng' => lon,
},
'location_type' => 'APPROXIMATE',
'viewport' => {
'northeast' => {
'lat' => lat + bounding_range,
'lng' => lon + bounding_range,
},
'southwest' => {
'lat' => lat - bounding_range,
'lng' => lon - bounding_range,
},
}
},
'types' => [ type_name, 'political' ],
}
break
end
else
lat = info[:latitude].to_f
lon = info[:longitude].to_f
street_number = info[:street_number]
street_name = info[:street_name]
locality = info[:locality]
region = info[:region]
country_name = info[:country_name]
country_code = info[:country_code]
address_components = []
types = nil
if street_number
address_components << {
'long_name' => street_number,
'short_name' => street_number,
'types' => ['street_number'],
}
end
if street_name
address_components << {
'long_name' => street_name,
'short_name' => street_name,
'types' => ['route'],
}
if !types
types = ['street_address']
end
end
if locality
address_components << {
'long_name' => info[:locality],
'short_name' => info[:locality],
'types' => [ 'locality', 'political' ],
}
if !types
types = ['locality', 'political']
end
end
if region
address_components << {
'long_name' => region,
'short_name' => region,
'types' => ['administrative_area_level_1', 'political'],
}
if !types
types = ['administrative_area_level_1', 'political']
end
end
if country_name and country_code
address_components << {
'long_name' => country_name,
'short_name' => country_code,
'types' => ['country', 'political'],
}
if !types
types = ['country', 'political']
end
end
result = {
'address_components' => address_components,
'formatted_address' => address,
'geometry' => {
'location' => {
'lat' => lat,
'lng' => lon,
},
'location_type' => 'ROOFTOP',
'viewport' => {
'northeast' => {
'lat' => lat + 0.001,
'lng' => lon + 0.001,
},
'southwest' => {
'lat' => lat - 0.001,
'lng' => lon - 0.001,
},
}
},
'types' => types,
}
end
elsif latlng
halt 404, "Reverse geocoding not supported"
else
halt 404, "Format not found"
end
if !result
output = {
'results' => [],
'status' => 'ZERO_RESULTS',
}
else
output = {
'results' => [result],
'status' => 'OK',
}
end
output
end
if __FILE__ == $0
if ARGV.length < 1
address = '2543 Graystone Place, Simi Valley, CA 93065'
else
address = ARGV[0]
end
params = {
:format => 'json',
:address => address,
}
result = google_geocoder_api_call(params)
$stderr.puts "result=#{JSON.pretty_generate(result)}"
end