norman-graticule 0.2.7

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.
Files changed (71) 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/cli.rb +64 -0
  9. data/lib/graticule/distance/haversine.rb +40 -0
  10. data/lib/graticule/distance/spherical.rb +52 -0
  11. data/lib/graticule/distance/vincenty.rb +76 -0
  12. data/lib/graticule/distance.rb +29 -0
  13. data/lib/graticule/geocoder/base.rb +113 -0
  14. data/lib/graticule/geocoder/bogus.rb +15 -0
  15. data/lib/graticule/geocoder/geocoder_ca.rb +54 -0
  16. data/lib/graticule/geocoder/geocoder_us.rb +49 -0
  17. data/lib/graticule/geocoder/google.rb +120 -0
  18. data/lib/graticule/geocoder/host_ip.rb +41 -0
  19. data/lib/graticule/geocoder/local_search_maps.rb +45 -0
  20. data/lib/graticule/geocoder/map_quest.rb +109 -0
  21. data/lib/graticule/geocoder/meta_carta.rb +33 -0
  22. data/lib/graticule/geocoder/multi.rb +46 -0
  23. data/lib/graticule/geocoder/postcode_anywhere.rb +63 -0
  24. data/lib/graticule/geocoder/rest.rb +18 -0
  25. data/lib/graticule/geocoder/yahoo.rb +102 -0
  26. data/lib/graticule/geocoder.rb +21 -0
  27. data/lib/graticule/location.rb +57 -0
  28. data/lib/graticule/version.rb +9 -0
  29. data/lib/graticule.rb +24 -0
  30. data/test/config.yml.default +36 -0
  31. data/test/fixtures/responses/geocoder_us/success.xml +18 -0
  32. data/test/fixtures/responses/geocoder_us/unknown.xml +1 -0
  33. data/test/fixtures/responses/google/badkey.xml +1 -0
  34. data/test/fixtures/responses/google/limit.xml +10 -0
  35. data/test/fixtures/responses/google/missing_address.xml +1 -0
  36. data/test/fixtures/responses/google/only_coordinates.xml +1 -0
  37. data/test/fixtures/responses/google/partial.xml +1 -0
  38. data/test/fixtures/responses/google/server_error.xml +10 -0
  39. data/test/fixtures/responses/google/success.xml +1 -0
  40. data/test/fixtures/responses/google/unavailable.xml +1 -0
  41. data/test/fixtures/responses/google/unknown_address.xml +1 -0
  42. data/test/fixtures/responses/host_ip/private.txt +4 -0
  43. data/test/fixtures/responses/host_ip/success.txt +4 -0
  44. data/test/fixtures/responses/host_ip/unknown.txt +4 -0
  45. data/test/fixtures/responses/local_search_maps/empty.txt +1 -0
  46. data/test/fixtures/responses/local_search_maps/not_found.txt +1 -0
  47. data/test/fixtures/responses/local_search_maps/success.txt +1 -0
  48. data/test/fixtures/responses/meta_carta/bad_address.xml +17 -0
  49. data/test/fixtures/responses/meta_carta/multiple.xml +33 -0
  50. data/test/fixtures/responses/meta_carta/success.xml +31 -0
  51. data/test/fixtures/responses/postcode_anywhere/badkey.xml +9 -0
  52. data/test/fixtures/responses/postcode_anywhere/canada.xml +16 -0
  53. data/test/fixtures/responses/postcode_anywhere/empty.xml +16 -0
  54. data/test/fixtures/responses/postcode_anywhere/success.xml +16 -0
  55. data/test/fixtures/responses/postcode_anywhere/uk.xml +18 -0
  56. data/test/fixtures/responses/yahoo/success.xml +3 -0
  57. data/test/fixtures/responses/yahoo/unknown_address.xml +6 -0
  58. data/test/mocks/uri.rb +52 -0
  59. data/test/test_helper.rb +32 -0
  60. data/test/unit/graticule/distance_test.rb +58 -0
  61. data/test/unit/graticule/geocoder/geocoder_us_test.rb +43 -0
  62. data/test/unit/graticule/geocoder/google_test.rb +107 -0
  63. data/test/unit/graticule/geocoder/host_ip_test.rb +40 -0
  64. data/test/unit/graticule/geocoder/local_search_maps_test.rb +30 -0
  65. data/test/unit/graticule/geocoder/meta_carta_test.rb +44 -0
  66. data/test/unit/graticule/geocoder/multi_test.rb +43 -0
  67. data/test/unit/graticule/geocoder/postcode_anywhere_test.rb +50 -0
  68. data/test/unit/graticule/geocoder/yahoo_test.rb +58 -0
  69. data/test/unit/graticule/geocoder_test.rb +27 -0
  70. data/test/unit/graticule/location_test.rb +66 -0
  71. metadata +123 -0
@@ -0,0 +1,21 @@
1
+
2
+ module Graticule
3
+
4
+ # Get a geocoder for the given service
5
+ #
6
+ # geocoder = Graticule.service(:google).new "api_key"
7
+ #
8
+ # See the documentation for your specific geocoder for more information
9
+ #
10
+ def self.service(name)
11
+ Geocoder.const_get name.to_s.camelize
12
+ end
13
+
14
+ # Base error class
15
+ class Error < RuntimeError; end
16
+ class CredentialsError < Error; end
17
+
18
+ # Raised when you try to locate an invalid address.
19
+ class AddressError < Error; end
20
+
21
+ end
@@ -0,0 +1,57 @@
1
+ module Graticule
2
+
3
+ # A geographic location
4
+ class Location
5
+ attr_accessor :latitude, :longitude, :street, :locality, :region, :postal_code, :country, :precision, :warning
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 ||= :unknown
15
+ end
16
+
17
+ def attributes
18
+ [:latitude, :longitude, :street, :locality, :region, :postal_code, :country].inject({}) do |result,attr|
19
+ result[attr] = self.send(attr) unless self.send(attr).blank?
20
+ result
21
+ end
22
+ end
23
+
24
+ # Returns an Array with latitude and longitude.
25
+ def coordinates
26
+ [latitude, longitude]
27
+ end
28
+
29
+ def ==(other)
30
+ other.respond_to?(:attributes) ? attributes == other.attributes : false
31
+ end
32
+
33
+ # Calculate the distance to another location. See the various Distance formulas
34
+ # for more information
35
+ def distance_to(destination, options = {})
36
+ options = {:formula => :haversine, :units => :miles}.merge(options)
37
+ "Graticule::Distance::#{options[:formula].to_s.titleize}".constantize.distance(self, destination, options[:units])
38
+ end
39
+
40
+ # Where would I be if I dug through the center of the earth?
41
+ def antipode
42
+ Location.new :latitude => -latitude, :longitude => longitude + (longitude >= 0 ? -180 : 180)
43
+ end
44
+ alias_method :antipodal_location, :antipode
45
+
46
+ def to_s(options = {})
47
+ options = {:coordinates => false, :country => true}.merge(options)
48
+ result = ""
49
+ result << "#{street}\n" if street
50
+ result << [locality, [region, postal_code].compact.join(" ")].compact.join(", ")
51
+ result << " #{country}" if options[:country] && country
52
+ result << "\nlatitude: #{latitude}, longitude: #{longitude}" if options[:coordinates] && [latitude, longitude].any?
53
+ result
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,9 @@
1
+ module Graticule #:nodoc:
2
+ module Version #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 2
5
+ TINY = 7
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
data/lib/graticule.rb ADDED
@@ -0,0 +1,24 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'active_support'
4
+
5
+ require 'graticule/version'
6
+ require 'graticule/location'
7
+ require 'graticule/geocoder'
8
+ require 'graticule/geocoder/base'
9
+ require 'graticule/geocoder/bogus'
10
+ require 'graticule/geocoder/rest'
11
+ require 'graticule/geocoder/google'
12
+ require 'graticule/geocoder/host_ip'
13
+ require 'graticule/geocoder/map_quest'
14
+ require 'graticule/geocoder/multi'
15
+ require 'graticule/geocoder/yahoo'
16
+ require 'graticule/geocoder/geocoder_ca'
17
+ require 'graticule/geocoder/geocoder_us'
18
+ require 'graticule/geocoder/local_search_maps'
19
+ require 'graticule/geocoder/meta_carta'
20
+ require 'graticule/geocoder/postcode_anywhere'
21
+ require 'graticule/distance'
22
+ require 'graticule/distance/haversine'
23
+ require 'graticule/distance/spherical'
24
+ require 'graticule/distance/vincenty'
@@ -0,0 +1,36 @@
1
+ google:
2
+ key: PUT YOUR KEY HERE
3
+ responses:
4
+ success.xml: http://maps.google.com/maps/geo?q=1600+amphitheatre+mtn+view+ca&output=xml&key=:key
5
+ badkey.xml: http://maps.google.com/maps/geo?q=1600+amphitheatre+mtn+view+ca&output=xml&key=this_is_a_bad_key
6
+ missing_address.xml: http://maps.google.com/maps/geo?output=xml&key=:key
7
+ unavailable.xml: http://maps.google.com/maps/geo?q=42-44+Hanway+Street,+London&output=xml&key=:key
8
+ unknown_address.xml: http://maps.google.com/maps/geo?q=1600&output=xml&key=:key
9
+ partial.xml: http://maps.google.com/maps/geo?q=sf+ca&output=xml&key=:key
10
+ #limit.xml: no way to reproduce this
11
+ #server_error.xml: no way to reproduce this
12
+ geocoder_us:
13
+ responses:
14
+ success.xml: http://rpc.geocoder.us/service/rest/geocode?address=1600%20Pennsylvania%20Ave%20NW,%20Washington%20DC
15
+ unknown.xml: http://rpc.geocoder.us/service/rest/geocode?address=1600
16
+ host_ip:
17
+ responses:
18
+ private.txt: http://api.hostip.info/get_html.php?ip=192.168.0.1&position=true
19
+ success.txt: http://api.hostip.info/get_html.php?ip=64.233.167.99&position=true
20
+ unknown.txt: http://api.hostip.info/get_html.php?ip=254.254.254.254&position=true
21
+ local_search_maps:
22
+ responses:
23
+ success.txt: http://geo.localsearchmaps.com/?street=48+Leicester+Square&city=London&country=UK
24
+ empty.txt: http://geo.localsearchmaps.com/
25
+ not_found.txt: http://geo.localsearchmaps.com/?street=48
26
+ meta_carta:
27
+ responses:
28
+ bad_address.xml: http://labs.metacarta.com/GeoParser/?q=aoeueou&output=locations
29
+ # doesn't work
30
+ #multiple.xml: http://labs.metacarta.com/GeoParser/?q=seattle&output=locations
31
+ success.xml: http://labs.metacarta.com/GeoParser/?q=baghdad&output=locations
32
+ yahoo:
33
+ key: PUT YOUR KEY HERE
34
+ responses:
35
+ success.xml: http://api.local.yahoo.com/MapsService/V1/geocode?location=701%20First%20Street,%20Sunnyvale,%20CA&output=xml&appid=:key
36
+ unknown_address.xml: http://api.local.yahoo.com/MapsService/V1/geocode?location=thisprobablycantbefound&output=xml&appid=:key
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0"?>
2
+ <rdf:RDF
3
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
4
+ xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
5
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
6
+ >
7
+
8
+ <geo:Point rdf:nodeID="aid15626963">
9
+
10
+ <dc:description>1600 Pennsylvania Ave NW, Washington DC 20502</dc:description>
11
+ <geo:long>-77.037684</geo:long>
12
+ <geo:lat>38.898748</geo:lat>
13
+
14
+ </geo:Point>
15
+
16
+
17
+ </rdf:RDF>
18
+
@@ -0,0 +1 @@
1
+ couldn't find this address! sorry
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.1"><Response><name>1600 amphitheatre mtn view ca</name><Status><code>610</code><request>geocode</request></Status></Response></kml>
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <kml xmlns="http://earth.google.com/kml/2.0">
3
+ <Response>
4
+ <name>1600 Amphitheater Pkwy, Mountain View, CA</name>
5
+ <Status>
6
+ <code>620</code>
7
+ <request>geocode</request>
8
+ </Status>
9
+ </Response>
10
+ </kml>
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.1"><Response><name></name><Status><code>601</code><request>geocode</request></Status></Response></kml>
@@ -0,0 +1 @@
1
+ <?xml version='1.0' encoding='UTF-8'?><kml xmlns='http://earth.google.com/kml/2.0'><Response><name>15-17 </name><Status><code>200</code><request>geocode</request></Status><Placemark id='p1'><Point><coordinates>-17.000000,15.000000,0</coordinates></Point></Placemark></Response></kml>
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.1"><Response><name>sf ca</name><Status><code>200</code><request>geocode</request></Status><Placemark><address>San Francisco, CA, USA</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><AdministrativeArea><AdministrativeAreaName>CA</AdministrativeAreaName><Locality><LocalityName>San Francisco</LocalityName></Locality></AdministrativeArea></Country></AddressDetails><Point><coordinates>-122.418333,37.775000,0</coordinates></Point></Placemark></Response></kml>
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <kml xmlns="http://earth.google.com/kml/2.0">
3
+ <Response>
4
+ <name>1600 Amphitheater Pkwy, Mountain View, CA</name>
5
+ <Status>
6
+ <code>500</code>
7
+ <request>geocode</request>
8
+ </Status>
9
+ </Response>
10
+ </kml>
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.1"><Response><name>1600 amphitheatre mtn view ca</name><Status><code>200</code><request>geocode</request></Status><Placemark><address>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</address><AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><AdministrativeArea><AdministrativeAreaName>CA</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Santa Clara</SubAdministrativeAreaName><Locality><LocalityName>Mountain View</LocalityName><Thoroughfare><ThoroughfareName>1600 Amphitheatre Pkwy</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>94043</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><Point><coordinates>-122.083739,37.423021,0</coordinates></Point></Placemark></Response></kml>
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.1"><Response><name>42-44 Hanway Street, London</name><Status><code>602</code><request>geocode</request></Status></Response></kml>
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.1"><Response><name>1600</name><Status><code>602</code><request>geocode</request></Status></Response></kml>
@@ -0,0 +1,4 @@
1
+ Country: (Private Address) (XX)
2
+ City: (Private Address)
3
+ Latitude:
4
+ Longitude:
@@ -0,0 +1,4 @@
1
+ Country: UNITED STATES (US)
2
+ City: Mountain View, CA
3
+ Latitude: 37.402
4
+ Longitude: -122.078
@@ -0,0 +1,4 @@
1
+ Country: (Unknown Country?) (XX)
2
+ City: (Unknown City?)
3
+ Latitude:
4
+ Longitude:
@@ -0,0 +1 @@
1
+ alert('Please provide a location');
@@ -0,0 +1 @@
1
+ alert('location not found');
@@ -0,0 +1 @@
1
+ map.centerAndZoom(new GPoint(-0.130427, 51.510036), 4);
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8" ?>
2
+
3
+ <!DOCTYPE Locations SYSTEM "Locations.dtd">
4
+
5
+ <Locations
6
+ xmlns:gml="http://www.opengis.net/gml"
7
+ CreatedBy="MetaCarta GeoParser v3.7.0-labs"
8
+ CreatedOn="Wed Apr 25 22:12:33 2007"
9
+ ConvertedFromBinaryFormat="0"
10
+ InputByteLength="0">
11
+ <ViewBox>
12
+
13
+ <gml:Box srsName="epsg:4326">
14
+ <gml:coordinates>-180.000000,-90.000000 180.000000,90.000000</gml:coordinates>
15
+ </gml:Box>
16
+ </ViewBox>
17
+ </Locations>
@@ -0,0 +1,33 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE Locations SYSTEM "Locations.dtd">
3
+ <Locations xmlns:gml="http://www.opengis.net/gml" CreatedBy="MetaCarta GeoParser v3.5.0-labs-alpha" CreatedOn="Sat Jun 17 02:14:05 2006" ConvertedFromBinaryFormat="False" InputByteLength="1">
4
+ <ViewBox>
5
+ <gml:Box srsName="epsg:4326">
6
+ <gml:coordinates>43.800000,-126.133333 51.406390,-118.530830</gml:coordinates>
7
+ </gml:Box>
8
+ </ViewBox>
9
+ <Location Name="Seattle" Type="PPL" Population="563374" Hierarchy="United States/Washington/King/Seattle">
10
+ <ViewBox>
11
+ <gml:Box srsName="epsg:4326">
12
+ <gml:coordinates>43.806390,-126.130830 51.406390,-118.530830</gml:coordinates>
13
+ </gml:Box>
14
+ </ViewBox>
15
+ <Centroid>
16
+ <gml:Point>
17
+ <gml:coordinates>-122.330830000000006,47.606389999999998</gml:coordinates>
18
+ </gml:Point>
19
+ </Centroid>
20
+ </Location>
21
+ <Location Name="Seattle" Type="PRT" Population="-1" Hierarchy="United States/Seattle">
22
+ <ViewBox>
23
+ <gml:Box srsName="epsg:4326">
24
+ <gml:coordinates>43.800000,-126.133333 51.400000,-118.533333</gml:coordinates>
25
+ </gml:Box>
26
+ </ViewBox>
27
+ <Centroid>
28
+ <gml:Point>
29
+ <gml:coordinates>-122.333332999999996,47.6</gml:coordinates>
30
+ </gml:Point>
31
+ </Centroid>
32
+ </Location>
33
+ </Locations>
@@ -0,0 +1,31 @@
1
+ <?xml version="1.0" encoding="UTF-8" ?>
2
+
3
+ <!DOCTYPE Locations SYSTEM "Locations.dtd">
4
+
5
+ <Locations
6
+ xmlns:gml="http://www.opengis.net/gml"
7
+ CreatedBy="MetaCarta GeoParser v3.7.0-labs"
8
+ CreatedOn="Wed Apr 25 22:12:33 2007"
9
+ ConvertedFromBinaryFormat="0"
10
+ InputByteLength="0">
11
+ <ViewBox>
12
+
13
+ <gml:Box srsName="epsg:4326">
14
+ <gml:coordinates>43.593900,32.538600 45.193900,34.138600</gml:coordinates>
15
+ </gml:Box>
16
+ </ViewBox>
17
+ <Location
18
+
19
+ Name="Baghdad"
20
+ Type=""
21
+ Population="-1"
22
+ Hierarchy="">
23
+ <ViewBox>
24
+ <gml:Box srsName="epsg:4326">
25
+ <gml:coordinates>43.593900,32.538600 45.193900,34.138600</gml:coordinates>
26
+ </gml:Box>
27
+ </ViewBox>
28
+ <RemainingQuery></RemainingQuery>
29
+ <Confidence>0.566875</Confidence> <Centroid><gml:Point><gml:coordinates>44.393900000000002,33.3386</gml:coordinates></gml:Point></Centroid>
30
+ </Location>
31
+ </Locations>
@@ -0,0 +1,9 @@
1
+ <PostcodeAnywhere Server="WS12" Version="3.0" Date="12/03/2007 15:43:33" Duration="0.016s">
2
+ <Schema Items="2">
3
+ <Field Name="error_number"/>
4
+ <Field Name="message"/>
5
+ </Schema>
6
+ <Data Items="1">
7
+ <Item error_number="6" message="License key was not recognised"/>
8
+ </Data>
9
+ </PostcodeAnywhere>
@@ -0,0 +1,16 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <PostcodeAnywhere Server="WS12" Version="3.0" Date="17/03/2007 19:01:05" Duration="1.109s">
3
+ <Schema Items="8">
4
+ <Field Name="line1"/>
5
+ <Field Name="line2"/>
6
+ <Field Name="city"/>
7
+ <Field Name="state"/>
8
+ <Field Name="postal_code"/>
9
+ <Field Name="country"/>
10
+ <Field Name="longitude"/>
11
+ <Field Name="latitude"/>
12
+ </Schema>
13
+ <Data Items="1">
14
+ <Item line1="204 Campbell Ave" city="Revelstoke" state="BC" postal_code="V0E" country="Canada" longitude="-118.196970002204" latitude="50.9997350418267"/>
15
+ </Data>
16
+ </PostcodeAnywhere>
@@ -0,0 +1,16 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <PostcodeAnywhere Version="3.0" Server="WS03" Date="17/03/2007 19:26:33" Duration="0.016s">
3
+ <Schema Items="10">
4
+ <Field Name="id"/>
5
+ <Field Name="seq"/>
6
+ <Field Name="location"/>
7
+ <Field Name="grid_east_m"/>
8
+ <Field Name="grid_north_m"/>
9
+ <Field Name="longitude"/>
10
+ <Field Name="latitude"/>
11
+ <Field Name="os_reference"/>
12
+ <Field Name="wgs84_longitude"/>
13
+ <Field Name="wgs84_latitude"/>
14
+ </Schema>
15
+ <Data Items="0"/>
16
+ </PostcodeAnywhere>
@@ -0,0 +1,16 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <PostcodeAnywhere Server="WS11" Version="3.0" Date="17/03/2007 18:12:11" Duration="1.703s">
3
+ <Schema Items="8">
4
+ <Field Name="line1"/>
5
+ <Field Name="line2"/>
6
+ <Field Name="city"/>
7
+ <Field Name="state"/>
8
+ <Field Name="postal_code"/>
9
+ <Field Name="country"/>
10
+ <Field Name="longitude"/>
11
+ <Field Name="latitude"/>
12
+ </Schema>
13
+ <Data Items="1">
14
+ <Item line1="204 Campbell Ave" city="Revelstoke" state="BC" postal_code="V0E" country="Canada" longitude="-118.196970002204" latitude="50.9997350418267"/>
15
+ </Data>
16
+ </PostcodeAnywhere>
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <PostcodeAnywhere Server="WS12" Version="3.0" Date="17/03/2007 19:01:39" Duration="2.422s">
3
+ <Schema Items="10">
4
+ <Field Name="id"/>
5
+ <Field Name="seq"/>
6
+ <Field Name="location"/>
7
+ <Field Name="grid_east_m"/>
8
+ <Field Name="grid_north_m"/>
9
+ <Field Name="longitude"/>
10
+ <Field Name="latitude"/>
11
+ <Field Name="os_reference"/>
12
+ <Field Name="wgs84_longitude"/>
13
+ <Field Name="wgs84_latitude"/>
14
+ </Schema>
15
+ <Data Items="1">
16
+ <Item id="17783983.00" seq="0" location="80 Wood Lane London NW9" grid_east_m="521000" grid_north_m="187500" longitude="-0.253788666693255" latitude="51.5728910186362" os_reference="TQ 21000 87500" wgs84_longitude="-0.255365891581496" wgs84_latitude="51.5733397173092"/>
17
+ </Data>
18
+ </PostcodeAnywhere>
@@ -0,0 +1,3 @@
1
+ <?xml version="1.0"?>
2
+ <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd"><Result precision="address" warning="The exact location could not be found, here is the closest match: 701 First Ave, Sunnyvale, CA 94089"><Latitude>37.416384</Latitude><Longitude>-122.024853</Longitude><Address>701 FIRST AVE</Address><City>SUNNYVALE</City><State>CA</State><Zip>94089-1019</Zip><Country>US</Country></Result></ResultSet>
3
+ <!-- ws06.search.re2.yahoo.com uncompressed/chunked Wed Apr 25 15:13:01 PDT 2007 -->
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Error xmlns="urn:yahoo:api">
3
+ The following errors were detected:
4
+ <Message>unable to parse location</Message>
5
+ </Error>
6
+ <!-- ws05.search.re2.yahoo.com uncompressed/chunked Wed Apr 25 15:13:01 PDT 2007 -->
data/test/mocks/uri.rb ADDED
@@ -0,0 +1,52 @@
1
+ require 'uri/http'
2
+ require 'open-uri'
3
+
4
+ ##
5
+ # This stub overrides OpenURI's open method to allow programs that use OpenURI
6
+ # to be easily tested.
7
+ #
8
+ # == Usage
9
+ #
10
+ # require 'rc_rest/uri_stub'
11
+ #
12
+ # class TestMyClass < Test::Unit::TestCase
13
+ #
14
+ # def setup
15
+ # URI::HTTP.responses = []
16
+ # URI::HTTP.uris = []
17
+ #
18
+ # @obj = MyClass.new
19
+ # end
20
+ #
21
+ # def test_my_method
22
+ # URI::HTTP.responses << 'some text open would ordinarily return'
23
+ #
24
+ # result = @obj.my_method
25
+ #
26
+ # assert_equal :something_meaninfgul, result
27
+ #
28
+ # assert_equal true, URI::HTTP.responses.empty?
29
+ # assert_equal 1, URI::HTTP.uris.length
30
+ # assert_equal 'http://example.com/path', URI::HTTP.uris.first
31
+ # end
32
+ #
33
+ # end
34
+
35
+ class URI::HTTP # :nodoc:
36
+
37
+ class << self
38
+ attr_accessor :responses, :uris
39
+ end
40
+
41
+ alias original_open open
42
+
43
+ def open(*args)
44
+ self.class.uris << self.to_s
45
+ io = StringIO.new(self.class.responses.shift)
46
+ OpenURI::Meta.init(io)
47
+ yield io if block_given?
48
+ io
49
+ end
50
+
51
+ end
52
+
@@ -0,0 +1,32 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ $:.unshift(File.dirname(__FILE__) + '/mocks')
3
+
4
+ require 'rubygems'
5
+ require 'yaml'
6
+ require 'test/unit'
7
+ require 'breakpoint'
8
+ require 'graticule'
9
+ require 'mocha'
10
+
11
+ TEST_RESPONSE_PATH = File.dirname(__FILE__) + '/fixtures/responses'
12
+
13
+ module Test
14
+ module Unit
15
+ module Assertions
16
+
17
+ private
18
+ def response(geocoder, response, extension = 'xml')
19
+ clean_backtrace do
20
+ File.read(File.dirname(__FILE__) + "/fixtures/responses/#{geocoder}/#{response}.#{extension}")
21
+ end
22
+ end
23
+
24
+ def clean_backtrace(&block)
25
+ yield
26
+ rescue AssertionFailedError => e
27
+ path = File.expand_path(__FILE__)
28
+ raise AssertionFailedError, e.message, e.backtrace.reject { |line| File.expand_path(line) =~ /#{path}/ }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ module Graticule
4
+ module Distance
5
+ class DistanceFormulaTest < Test::Unit::TestCase
6
+ EARTH_RADIUS_IN_MILES = 3963.1676
7
+ EARTH_RADIUS_IN_KILOMETERS = 6378.135
8
+
9
+ FORMULAS = [Haversine, Spherical, Vincenty]
10
+
11
+ def test_earth_radius
12
+ assert_equal EARTH_RADIUS_IN_MILES, EARTH_RADIUS[:miles]
13
+ assert_equal EARTH_RADIUS_IN_KILOMETERS, EARTH_RADIUS[:kilometers]
14
+ end
15
+
16
+ def test_distance
17
+ washington_dc = Location.new(:latitude => 38.898748, :longitude => -77.037684)
18
+ chicago = Location.new(:latitude => 41.85, :longitude => -87.65)
19
+
20
+ FORMULAS.each do |formula|
21
+ assert_in_delta formula.distance(washington_dc, chicago), formula.distance(chicago, washington_dc), 0.00001
22
+ assert_in_delta 594.820, formula.distance(washington_dc, chicago), 1.0
23
+ assert_in_delta 594.820, formula.distance(washington_dc, chicago, :miles), 1.0
24
+ assert_in_delta 957.275, formula.distance(washington_dc, chicago, :kilometers), 1.0
25
+ end
26
+ end
27
+
28
+ def test_distance_between_antipodal_points
29
+ # The Vincenty formula will be indeterminant with antipodal points.
30
+ # See http://mathworld.wolfram.com/AntipodalPoints.html
31
+ washington_dc = Location.new(:latitude => 38.898748, :longitude => -77.037684)
32
+ chicago = Location.new(:latitude => 41.85, :longitude => -87.65)
33
+
34
+ # First, test the deltas.
35
+ FORMULAS.each do |formula|
36
+ assert_in_delta 12450.6582171051,
37
+ formula.distance(chicago, chicago.antipodal_location), 1.0
38
+ assert_in_delta 12450.6582171051,
39
+ formula.distance(washington_dc, washington_dc.antipodal_location), 1.0
40
+ assert_in_delta 12450.6582171051,
41
+ formula.distance(chicago, chicago.antipodal_location, :miles), 1.0
42
+ assert_in_delta 12450.6582171051,
43
+ formula.distance(washington_dc, washington_dc.antipodal_location, :miles), 1.0
44
+ assert_in_delta 20037.50205960391,
45
+ formula.distance(chicago, chicago.antipodal_location, :kilometers), 1.0
46
+ assert_in_delta 20037.5020596039,
47
+ formula.distance(washington_dc, washington_dc.antipodal_location, :kilometers), 1.0
48
+ end
49
+
50
+ # Next, test Vincenty. Vincenty will use haversine instead of returning NaN on antipodal points
51
+ assert_equal Haversine.distance(washington_dc, washington_dc.antipodal_location),
52
+ Vincenty.distance(washington_dc, washington_dc.antipodal_location)
53
+ assert_equal Haversine.distance(chicago, chicago.antipodal_location),
54
+ Vincenty.distance(chicago, chicago.antipodal_location)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Graticule
4
+ module Geocoder
5
+ class GeocoderUsTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ URI::HTTP.responses = []
9
+ URI::HTTP.uris = []
10
+
11
+ @geocoder = GeocoderUs.new
12
+ @location = Location.new(
13
+ :street => "1600 Pennsylvania Ave NW, Washington DC 20502",
14
+ :longitude => -77.037684,
15
+ :latitude => 38.898748
16
+ )
17
+ end
18
+
19
+ def test_success
20
+ prepare_response(:success)
21
+ assert_equal @location, @geocoder.locate('1600 Pennsylvania Ave, Washington DC')
22
+ end
23
+
24
+ def test_url
25
+ prepare_response(:success)
26
+ @geocoder.locate('1600 Pennsylvania Ave, Washington DC')
27
+ assert_equal 'http://rpc.geocoder.us/service/rest/geocode?address=1600%20Pennsylvania%20Ave,%20Washington%20DC',
28
+ URI::HTTP.uris.first
29
+ end
30
+
31
+ def test_locate_bad_address
32
+ prepare_response(:unknown)
33
+ assert_raises(AddressError) { @geocoder.locate('yuck') }
34
+ end
35
+
36
+ protected
37
+ def prepare_response(id)
38
+ URI::HTTP.responses << response('geocoder_us', id)
39
+ end
40
+
41
+ end
42
+ end
43
+ end