GUI-graticule 0.2.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/CHANGELOG.txt +46 -0
  2. data/LICENSE.txt +30 -0
  3. data/Manifest.txt +73 -0
  4. data/README.txt +29 -0
  5. data/Rakefile +86 -0
  6. data/bin/geocode +5 -0
  7. data/init.rb +2 -0
  8. data/lib/graticule.rb +26 -0
  9. data/lib/graticule/cli.rb +64 -0
  10. data/lib/graticule/distance.rb +29 -0
  11. data/lib/graticule/distance/haversine.rb +40 -0
  12. data/lib/graticule/distance/spherical.rb +52 -0
  13. data/lib/graticule/distance/vincenty.rb +76 -0
  14. data/lib/graticule/geocoder.rb +21 -0
  15. data/lib/graticule/geocoder/base.rb +138 -0
  16. data/lib/graticule/geocoder/bogus.rb +15 -0
  17. data/lib/graticule/geocoder/geocoder_ca.rb +54 -0
  18. data/lib/graticule/geocoder/geocoder_us.rb +49 -0
  19. data/lib/graticule/geocoder/google.rb +122 -0
  20. data/lib/graticule/geocoder/host_ip.rb +41 -0
  21. data/lib/graticule/geocoder/local_search_maps.rb +45 -0
  22. data/lib/graticule/geocoder/map_quest.rb +111 -0
  23. data/lib/graticule/geocoder/meta_carta.rb +33 -0
  24. data/lib/graticule/geocoder/multi.rb +80 -0
  25. data/lib/graticule/geocoder/postcode_anywhere.rb +63 -0
  26. data/lib/graticule/geocoder/rest.rb +18 -0
  27. data/lib/graticule/geocoder/yahoo.rb +102 -0
  28. data/lib/graticule/location.rb +488 -0
  29. data/lib/graticule/version.rb +9 -0
  30. data/site/index.html +114 -0
  31. data/site/plugin.html +82 -0
  32. data/site/stylesheets/style.css +73 -0
  33. data/test/config.yml.default +36 -0
  34. data/test/fixtures/responses/geocoder_us/success.xml +18 -0
  35. data/test/fixtures/responses/geocoder_us/unknown.xml +1 -0
  36. data/test/fixtures/responses/google/badkey.xml +1 -0
  37. data/test/fixtures/responses/google/limit.xml +10 -0
  38. data/test/fixtures/responses/google/missing_address.xml +1 -0
  39. data/test/fixtures/responses/google/only_coordinates.xml +1 -0
  40. data/test/fixtures/responses/google/partial.xml +1 -0
  41. data/test/fixtures/responses/google/server_error.xml +10 -0
  42. data/test/fixtures/responses/google/success.xml +1 -0
  43. data/test/fixtures/responses/google/unavailable.xml +1 -0
  44. data/test/fixtures/responses/google/unknown_address.xml +1 -0
  45. data/test/fixtures/responses/host_ip/private.txt +4 -0
  46. data/test/fixtures/responses/host_ip/success.txt +4 -0
  47. data/test/fixtures/responses/host_ip/unknown.txt +4 -0
  48. data/test/fixtures/responses/local_search_maps/empty.txt +1 -0
  49. data/test/fixtures/responses/local_search_maps/not_found.txt +1 -0
  50. data/test/fixtures/responses/local_search_maps/success.txt +1 -0
  51. data/test/fixtures/responses/meta_carta/bad_address.xml +17 -0
  52. data/test/fixtures/responses/meta_carta/multiple.xml +33 -0
  53. data/test/fixtures/responses/meta_carta/success.xml +31 -0
  54. data/test/fixtures/responses/postcode_anywhere/badkey.xml +9 -0
  55. data/test/fixtures/responses/postcode_anywhere/canada.xml +16 -0
  56. data/test/fixtures/responses/postcode_anywhere/empty.xml +16 -0
  57. data/test/fixtures/responses/postcode_anywhere/success.xml +16 -0
  58. data/test/fixtures/responses/postcode_anywhere/uk.xml +18 -0
  59. data/test/fixtures/responses/yahoo/success.xml +3 -0
  60. data/test/fixtures/responses/yahoo/unknown_address.xml +6 -0
  61. data/test/mocks/uri.rb +52 -0
  62. data/test/test_helper.rb +32 -0
  63. data/test/unit/graticule/distance_test.rb +58 -0
  64. data/test/unit/graticule/geocoder/geocoder_us_test.rb +43 -0
  65. data/test/unit/graticule/geocoder/google_test.rb +126 -0
  66. data/test/unit/graticule/geocoder/host_ip_test.rb +40 -0
  67. data/test/unit/graticule/geocoder/local_search_maps_test.rb +30 -0
  68. data/test/unit/graticule/geocoder/meta_carta_test.rb +44 -0
  69. data/test/unit/graticule/geocoder/multi_test.rb +43 -0
  70. data/test/unit/graticule/geocoder/postcode_anywhere_test.rb +50 -0
  71. data/test/unit/graticule/geocoder/yahoo_test.rb +58 -0
  72. data/test/unit/graticule/geocoder_test.rb +27 -0
  73. data/test/unit/graticule/location_test.rb +66 -0
  74. metadata +158 -0
@@ -0,0 +1,488 @@
1
+ module Graticule
2
+
3
+ # A geographic location
4
+ class Location
5
+ attr_accessor :latitude, :longitude, :premise, :street, :locality, :region, :postal_code, :country, :precision, :warning, :geocoder
6
+ alias_method :city, :locality
7
+ alias_method :state, :region
8
+ alias_method :zip, :postal_code
9
+
10
+ def initialize(attrs = {})
11
+ attrs.each do |key,value|
12
+ instance_variable_set "@#{key}", value
13
+ end
14
+ self.precision ||= Precision.unknown
15
+ end
16
+
17
+ def attributes
18
+ [:latitude, :longitude, :street, :locality, :region, :postal_code, :country, :precision].inject({}) do |result,attr|
19
+ result[attr] = self.send(attr) unless self.send(attr).blank?
20
+ result
21
+ end
22
+ end
23
+
24
+ def normalized_country
25
+ normalize_string(country)
26
+ end
27
+
28
+ def normalized_region
29
+ normalize_string(region)
30
+ end
31
+
32
+ def normalized_locality
33
+ normalize_string(normalize_abbreviations(locality))
34
+ end
35
+
36
+ def normalized_street
37
+ normalize_string(normalize_abbreviations(street))
38
+ end
39
+
40
+ def normalized_premise
41
+ normalize_string(normalize_abbreviations(premise))
42
+ end
43
+
44
+ def postal_code_base
45
+ base = case country
46
+ when "CA"
47
+ postal_code.to_s[0..2]
48
+ else
49
+ postal_code.to_s[0..4]
50
+ end
51
+
52
+ base.strip!
53
+
54
+ base
55
+ end
56
+
57
+ # Returns an Array with latitude and longitude.
58
+ def coordinates
59
+ [latitude, longitude]
60
+ end
61
+
62
+ def ==(other)
63
+ other.respond_to?(:attributes) ? attributes == other.attributes : false
64
+ end
65
+
66
+ def eql?(other)
67
+ self == other
68
+ end
69
+
70
+ def hash
71
+ attributes.to_s.hash
72
+ end
73
+
74
+ # Calculate the distance to another location. See the various Distance formulas
75
+ # for more information
76
+ def distance_to(destination, options = {})
77
+ options = {:formula => :haversine, :units => :miles}.merge(options)
78
+ "Graticule::Distance::#{options[:formula].to_s.titleize}".constantize.distance(self, destination, options[:units])
79
+ end
80
+
81
+ # Where would I be if I dug through the center of the earth?
82
+ def antipode
83
+ Location.new :latitude => -latitude, :longitude => longitude + (longitude >= 0 ? -180 : 180)
84
+ end
85
+ alias_method :antipodal_location, :antipode
86
+
87
+ def to_s(options = {})
88
+ options = {:coordinates => false, :country => true}.merge(options)
89
+ result = ""
90
+ result << "#{street}\n" if street
91
+ result << [locality, [region, postal_code].compact.join(" ")].compact.join(", ")
92
+ result << " #{country}" if options[:country] && country
93
+ result << "\nlatitude: #{latitude}, longitude: #{longitude}" if options[:coordinates] && [latitude, longitude].any?
94
+ result
95
+ end
96
+
97
+ private
98
+
99
+ # A list of standard USPS street abbreviations. Taken from:
100
+ # http://www.usps.com/ncsc/lookups/usps_abbreviations.html
101
+ USPS_STREET_ABBREVIATIONS = {
102
+ "allee" => "aly",
103
+ "alley" => "aly",
104
+ "ally" => "aly",
105
+ "anex" => "anx",
106
+ "annex" => "anx",
107
+ "annx" => "anx",
108
+ "arcade" => "arc",
109
+ "av" => "ave",
110
+ "aven" => "ave",
111
+ "avenu" => "ave",
112
+ "avenue" => "ave",
113
+ "avn" => "ave",
114
+ "avnue" => "ave",
115
+ "bayoo" => "byu",
116
+ "bayou" => "byu",
117
+ "beach" => "bch",
118
+ "bend" => "bnd",
119
+ "bluf" => "blf",
120
+ "bluff" => "blf",
121
+ "bluffs" => "blfs",
122
+ "bot" => "btm",
123
+ "bottm" => "btm",
124
+ "bottom" => "btm",
125
+ "boul" => "blvd",
126
+ "boulevard" => "blvd",
127
+ "boulv" => "blvd",
128
+ "branch" => "br",
129
+ "brdge" => "brg",
130
+ "bridge" => "brg",
131
+ "brnch" => "br",
132
+ "brook" => "brk",
133
+ "brooks" => "brks",
134
+ "burg" => "bg",
135
+ "burgs" => "bgs",
136
+ "bypa" => "byp",
137
+ "bypas" => "byp",
138
+ "bypass" => "byp",
139
+ "byps" => "byp",
140
+ "camp" => "cp",
141
+ "canyn" => "cyn",
142
+ "canyon" => "cyn",
143
+ "cape" => "cpe",
144
+ "causeway" => "cswy",
145
+ "causway" => "cswy",
146
+ "cen" => "ctr",
147
+ "cent" => "ctr",
148
+ "center" => "ctr",
149
+ "centers" => "ctrs",
150
+ "centr" => "ctr",
151
+ "centre" => "ctr",
152
+ "circ" => "cir",
153
+ "circl" => "cir",
154
+ "circle" => "cir",
155
+ "circles" => "cirs",
156
+ "ck" => "crk",
157
+ "cliff" => "clf",
158
+ "cliffs" => "clfs",
159
+ "club" => "clb",
160
+ "cmp" => "cp",
161
+ "cnter" => "ctr",
162
+ "cntr" => "ctr",
163
+ "cnyn" => "cyn",
164
+ "common" => "cmn",
165
+ "corner" => "cor",
166
+ "corners" => "cors",
167
+ "course" => "crse",
168
+ "court" => "ct",
169
+ "courts" => "cts",
170
+ "cove" => "cv",
171
+ "coves" => "cvs",
172
+ "cr" => "crk",
173
+ "crcl" => "cir",
174
+ "crcle" => "cir",
175
+ "crecent" => "cres",
176
+ "creek" => "crk",
177
+ "crescent" => "cres",
178
+ "cresent" => "cres",
179
+ "crest" => "crst",
180
+ "crossing" => "xing",
181
+ "crossroad" => "xrd",
182
+ "crscnt" => "cres",
183
+ "crsent" => "cres",
184
+ "crsnt" => "cres",
185
+ "crssing" => "xing",
186
+ "crssng" => "xing",
187
+ "crt" => "ct",
188
+ "curve" => "curv",
189
+ "dale" => "dl",
190
+ "dam" => "dm",
191
+ "div" => "dv",
192
+ "divide" => "dv",
193
+ "driv" => "dr",
194
+ "drive" => "dr",
195
+ "drives" => "drs",
196
+ "drv" => "dr",
197
+ "dvd" => "dv",
198
+ "estate" => "est",
199
+ "estates" => "ests",
200
+ "exp" => "expy",
201
+ "expr" => "expy",
202
+ "express" => "expy",
203
+ "expressway" => "expy",
204
+ "expw" => "expy",
205
+ "extension" => "ext",
206
+ "extensions" => "exts",
207
+ "extn" => "ext",
208
+ "extnsn" => "ext",
209
+ "falls" => "fls",
210
+ "ferry" => "fry",
211
+ "field" => "fld",
212
+ "fields" => "flds",
213
+ "flat" => "flt",
214
+ "flats" => "flts",
215
+ "ford" => "frd",
216
+ "fords" => "frds",
217
+ "forest" => "frst",
218
+ "forests" => "frst",
219
+ "forg" => "frg",
220
+ "forge" => "frg",
221
+ "forges" => "frgs",
222
+ "fork" => "frk",
223
+ "forks" => "frks",
224
+ "fort" => "ft",
225
+ "freeway" => "fwy",
226
+ "freewy" => "fwy",
227
+ "frry" => "fry",
228
+ "frt" => "ft",
229
+ "frway" => "fwy",
230
+ "frwy" => "fwy",
231
+ "garden" => "gdn",
232
+ "gardens" => "gdns",
233
+ "gardn" => "gdn",
234
+ "gateway" => "gtwy",
235
+ "gatewy" => "gtwy",
236
+ "gatway" => "gtwy",
237
+ "glen" => "gln",
238
+ "glens" => "glns",
239
+ "grden" => "gdn",
240
+ "grdn" => "gdn",
241
+ "grdns" => "gdns",
242
+ "green" => "grn",
243
+ "greens" => "grns",
244
+ "grov" => "grv",
245
+ "grove" => "grv",
246
+ "groves" => "grvs",
247
+ "gtway" => "gtwy",
248
+ "harb" => "hbr",
249
+ "harbor" => "hbr",
250
+ "harbors" => "hbrs",
251
+ "harbr" => "hbr",
252
+ "haven" => "hvn",
253
+ "havn" => "hvn",
254
+ "height" => "hts",
255
+ "heights" => "hts",
256
+ "hgts" => "hts",
257
+ "highway" => "hwy",
258
+ "highwy" => "hwy",
259
+ "hill" => "hl",
260
+ "hills" => "hls",
261
+ "hiway" => "hwy",
262
+ "hiwy" => "hwy",
263
+ "hllw" => "holw",
264
+ "hollow" => "holw",
265
+ "hollows" => "holw",
266
+ "holws" => "holw",
267
+ "hrbor" => "hbr",
268
+ "ht" => "hts",
269
+ "hway" => "hwy",
270
+ "inlet" => "inlt",
271
+ "island" => "is",
272
+ "islands" => "iss",
273
+ "isles" => "isle",
274
+ "islnd" => "is",
275
+ "islnds" => "iss",
276
+ "jction" => "jct",
277
+ "jctn" => "jct",
278
+ "jctns" => "jcts",
279
+ "junction" => "jct",
280
+ "junctions" => "jcts",
281
+ "junctn" => "jct",
282
+ "juncton" => "jct",
283
+ "key" => "ky",
284
+ "keys" => "kys",
285
+ "knol" => "knl",
286
+ "knoll" => "knl",
287
+ "knolls" => "knls",
288
+ "la" => "ln",
289
+ "lake" => "lk",
290
+ "lakes" => "lks",
291
+ "landing" => "lndg",
292
+ "lane" => "ln",
293
+ "lanes" => "ln",
294
+ "ldge" => "ldg",
295
+ "light" => "lgt",
296
+ "lights" => "lgts",
297
+ "lndng" => "lndg",
298
+ "loaf" => "lf",
299
+ "lock" => "lck",
300
+ "locks" => "lcks",
301
+ "lodg" => "ldg",
302
+ "lodge" => "ldg",
303
+ "loops" => "loop",
304
+ "manor" => "mnr",
305
+ "manors" => "mnrs",
306
+ "meadow" => "mdw",
307
+ "meadows" => "mdws",
308
+ "medows" => "mdws",
309
+ "mill" => "ml",
310
+ "mills" => "mls",
311
+ "mission" => "msn",
312
+ "missn" => "msn",
313
+ "mnt" => "mt",
314
+ "mntain" => "mtn",
315
+ "mntn" => "mtn",
316
+ "mntns" => "mtns",
317
+ "motorway" => "mtwy",
318
+ "mount" => "mt",
319
+ "mountain" => "mtn",
320
+ "mountains" => "mtns",
321
+ "mountin" => "mtn",
322
+ "mssn" => "msn",
323
+ "mtin" => "mtn",
324
+ "neck" => "nck",
325
+ "orchard" => "orch",
326
+ "orchrd" => "orch",
327
+ "overpass" => "opas",
328
+ "ovl" => "oval",
329
+ "parks" => "park",
330
+ "parkway" => "pkwy",
331
+ "parkways" => "pkwy",
332
+ "parkwy" => "pkwy",
333
+ "passage" => "psge",
334
+ "paths" => "path",
335
+ "pikes" => "pike",
336
+ "pine" => "pne",
337
+ "pines" => "pnes",
338
+ "pk" => "park",
339
+ "pkway" => "pkwy",
340
+ "pkwys" => "pkwy",
341
+ "pky" => "pkwy",
342
+ "place" => "pl",
343
+ "plain" => "pln",
344
+ "plaines" => "plns",
345
+ "plains" => "plns",
346
+ "plaza" => "plz",
347
+ "plza" => "plz",
348
+ "point" => "pt",
349
+ "points" => "pts",
350
+ "port" => "prt",
351
+ "ports" => "prts",
352
+ "prairie" => "pr",
353
+ "prarie" => "pr",
354
+ "prk" => "park",
355
+ "prr" => "pr",
356
+ "rad" => "radl",
357
+ "radial" => "radl",
358
+ "radiel" => "radl",
359
+ "ranch" => "rnch",
360
+ "ranches" => "rnch",
361
+ "rapid" => "rpd",
362
+ "rapids" => "rpds",
363
+ "rdge" => "rdg",
364
+ "rest" => "rst",
365
+ "ridge" => "rdg",
366
+ "ridges" => "rdgs",
367
+ "river" => "riv",
368
+ "rivr" => "riv",
369
+ "rnchs" => "rnch",
370
+ "road" => "rd",
371
+ "roads" => "rds",
372
+ "route" => "rte",
373
+ "rvr" => "riv",
374
+ "shoal" => "shl",
375
+ "shoals" => "shls",
376
+ "shoar" => "shr",
377
+ "shoars" => "shrs",
378
+ "shore" => "shr",
379
+ "shores" => "shrs",
380
+ "skyway" => "skwy",
381
+ "spng" => "spg",
382
+ "spngs" => "spgs",
383
+ "spring" => "spg",
384
+ "springs" => "spgs",
385
+ "sprng" => "spg",
386
+ "sprngs" => "spgs",
387
+ "spurs" => "spur",
388
+ "sqr" => "sq",
389
+ "sqre" => "sq",
390
+ "sqrs" => "sqs",
391
+ "squ" => "sq",
392
+ "square" => "sq",
393
+ "squares" => "sqs",
394
+ "station" => "sta",
395
+ "statn" => "sta",
396
+ "stn" => "sta",
397
+ "str" => "st",
398
+ "strav" => "stra",
399
+ "strave" => "stra",
400
+ "straven" => "stra",
401
+ "stravenue" => "stra",
402
+ "stravn" => "stra",
403
+ "stream" => "strm",
404
+ "street" => "st",
405
+ "streets" => "sts",
406
+ "streme" => "strm",
407
+ "strt" => "st",
408
+ "strvn" => "stra",
409
+ "strvnue" => "stra",
410
+ "sumit" => "smt",
411
+ "sumitt" => "smt",
412
+ "summit" => "smt",
413
+ "terr" => "ter",
414
+ "terrace" => "ter",
415
+ "throughway" => "trwy",
416
+ "tpk" => "tpke",
417
+ "tr" => "trl",
418
+ "trace" => "trce",
419
+ "traces" => "trce",
420
+ "track" => "trak",
421
+ "tracks" => "trak",
422
+ "trafficway" => "trfy",
423
+ "trail" => "trl",
424
+ "trails" => "trl",
425
+ "trk" => "trak",
426
+ "trks" => "trak",
427
+ "trls" => "trl",
428
+ "trnpk" => "tpke",
429
+ "trpk" => "tpke",
430
+ "tunel" => "tunl",
431
+ "tunls" => "tunl",
432
+ "tunnel" => "tunl",
433
+ "tunnels" => "tunl",
434
+ "tunnl" => "tunl",
435
+ "turnpike" => "tpke",
436
+ "turnpk" => "tpke",
437
+ "underpass" => "upas",
438
+ "union" => "un",
439
+ "unions" => "uns",
440
+ "valley" => "vly",
441
+ "valleys" => "vlys",
442
+ "vally" => "vly",
443
+ "vdct" => "via",
444
+ "viadct" => "via",
445
+ "viaduct" => "via",
446
+ "view" => "vw",
447
+ "views" => "vws",
448
+ "vill" => "vlg",
449
+ "villag" => "vlg",
450
+ "village" => "vlg",
451
+ "villages" => "vlgs",
452
+ "ville" => "vl",
453
+ "villg" => "vlg",
454
+ "villiage" => "vlg",
455
+ "vist" => "vis",
456
+ "vista" => "vis",
457
+ "vlly" => "vly",
458
+ "vst" => "vis",
459
+ "vsta" => "vis",
460
+ "walks" => "walk",
461
+ "well" => "wl",
462
+ "wells" => "wls",
463
+ "wy" => "way",
464
+ }
465
+
466
+ CUSTOM_ABBREVIATIONS = {
467
+ "county road" => "cr",
468
+ "north" => "n",
469
+ "south" => "s",
470
+ "east" => "e",
471
+ "west" => "w",
472
+ }
473
+
474
+ def normalize_string(string)
475
+ string.to_s.gsub(/\s/, "").downcase
476
+ end
477
+
478
+ def normalize_abbreviations(string)
479
+ string = string.to_s.downcase
480
+
481
+ USPS_STREET_ABBREVIATIONS.merge(CUSTOM_ABBREVIATIONS).each do |name, abbreviation|
482
+ string.gsub!(/\b#{name}\b/, abbreviation)
483
+ end
484
+
485
+ string
486
+ end
487
+ end
488
+ end