-
Notifications
You must be signed in to change notification settings - Fork 184
/
emulategoogle.rb
408 lines (381 loc) · 10.4 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# 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'
require 'uri'
require 'net/http'
WOE_TYPE_TOWN = 7
WOE_TYPE_REGION = 8
WOE_TYPE_POSTAL_CODE = 11
WOE_TYPE_COUNTRY = 12
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
result = google_style_street2coordinates(address)
if !result
result = google_style_twofishes(address)
end
if !result
result = google_style_geodict(address)
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
def google_style_street2coordinates(address)
found = street2coordinates([address])
info = found[address]
if !info
return nil
end
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,
}
result
end
def google_style_geodict(address)
capitalized_address = address.split(' ').select {|word| word.capitalize! || word }.join(' ')
location_string = 'at '+capitalized_address
locations = find_locations_in_text(location_string)
result = nil
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 = get_most_specific_token(found_tokens)
country_code = location[:country_code]
country_name = get_country_name_from_code(country_code)
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' ],
},
]
elsif type == :POSTAL_CODE
bounding_range = 0.1
type_name = 'postal_code'
address_components = [
{
'long_name' => location[:matched_string],
'short_name' => location[:code].strip,
'types' => [ 'postal_code', 'political' ],
},
]
found_tokens.each do |token|
if token[:type] == :CITY
address_components <<
{
'long_name' => token[:matched_string],
'short_name' => token[:matched_string],
'types' => [ 'locality', 'political' ],
}
end
end
address_components <<
{
'long_name' => location[:region_code],
'short_name' => location[:region_code].strip,
'types' => [ 'administrative_area_level_1', 'political' ],
}
elsif type == :REGION
bounding_range = 1.0
type_name = 'administrative_area_level_1'
address_components = [
{
'long_name' => location[:matched_string],
'short_name' => location[:code].strip,
'types' => [ 'administrative_area_level_1', 'political' ],
},
]
else
bounding_range = 5.0
type_name = 'country'
address_components = []
end
address_components <<
{
'long_name' => country_name,
'short_name' => country_code,
'types' => [ 'country', 'political' ],
}
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
result
end
def get_json(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
headers = {}
request = Net::HTTP::Get.new(uri.request_uri, headers)
begin
response = http.request(request)
result_string = nil
if response.code != '200'
log "Bad response code #{response.status} for '#{url}'"
result = nil
else
if response.header[ 'Content-Encoding' ].eql?( 'gzip' ) then
sio = StringIO.new( response.body )
gz = Zlib::GzipReader.new( sio )
result_string = gz.read()
else
result_string = response.body
end
end
rescue NoMethodError
result_string = nil
end
if !result_string
result = nil
else
result = JSON.parse(result_string)
end
result
end
def google_style_twofishes(address)
url = 'http://localhost/twofishes?query=' + URI.encode(address)
twofishes_data = get_json(url)
if !twofishes_data or !twofishes_data['interpretations'] then return nil end
interpretations = twofishes_data['interpretations']
if interpretations.length == 0 then return nil end
interpretation = interpretations[0]
if !interpretation['feature'] then return nil end
feature = interpretation['feature']
country_code = feature['cc']
country_name = get_country_name_from_code(country_code)
geometry = feature['geometry']
if !geometry
$stderr.puts "Missing geometry for '#{address}' - found '#{interpretation.to_json}'"
return nil
end
center = geometry['center']
lat = center['lat']
lon = center['lng']
bounds = geometry['bounds']
if !bounds
$stderr.puts "Missing bounds for '#{address}' - found '#{geometry.to_json}'"
return nil
end
ne = bounds['ne']
ne_lat = ne['lat']
ne_lon = ne['lng']
sw = bounds['sw']
sw_lat = sw['lat']
sw_lon = sw['lng']
short_name = feature['name']
long_name = feature['displayName']
woe_type = feature['woeType']
if woe_type == WOE_TYPE_TOWN
type_name = 'locality'
address_components = [
{
'long_name' => long_name,
'short_name' => short_name,
'types' => [ 'locality', 'political' ],
},
]
elsif woe_type == WOE_TYPE_POSTAL_CODE
type_name = 'postal_code'
address_components = [
{
'long_name' => long_name,
'short_name' => short_name,
'types' => [ 'postal_code', 'political' ],
},
]
elsif woe_type == WOE_TYPE_REGION
type_name = 'administrative_area_level_1'
address_components = [
{
'long_name' => long_name,
'short_name' => short_name,
'types' => [ 'administrative_area_level_1', 'political' ],
},
]
elsif woe_type == WOE_TYPE_COUNTRY
type_name = 'country'
address_components = []
end
address_components <<
{
'long_name' => country_name,
'short_name' => country_code,
'types' => [ 'country', 'political' ],
}
result = {
'address_components' => address_components,
'geometry' => {
'location' => {
'lat' => lat,
'lng' => lon,
},
'location_type' => 'APPROXIMATE',
'viewport' => {
'northeast' => {
'lat' => ne_lat,
'lng' => ne_lon,
},
'southwest' => {
'lat' => sw_lat,
'lng' => sw_lon,
},
}
},
'types' => [ type_name, 'political' ],
}
result
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