metacarta-geoparser 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright 2006 Eric Hodel, The Robot Co-op. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions
5
+ are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright
8
+ notice, this list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright
10
+ notice, this list of conditions and the following disclaimer in the
11
+ documentation and/or other materials provided with the distribution.
12
+ 3. Neither the names of the authors nor the names of their contributors
13
+ may be used to endorse or promote products derived from this software
14
+ without specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
17
+ OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
20
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
+ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22
+ OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23
+ BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25
+ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+
data/Manifest.txt ADDED
@@ -0,0 +1,6 @@
1
+ LICENSE
2
+ Manifest.txt
3
+ README
4
+ Rakefile
5
+ lib/metacarta_geoparser.rb
6
+ test/test_metacarta_geoparser.rb
data/README ADDED
@@ -0,0 +1,41 @@
1
+ = metacarta-geoparser
2
+
3
+ Rubyforge Project:
4
+
5
+ http://rubyforge.org/projects/rctools
6
+
7
+ Documentation:
8
+
9
+ http://dev.robotcoop.com/Libraries/metacarta-geoparser
10
+
11
+ == About
12
+
13
+ metacarta-geoparser implements MetaCarta's GeoParser API:
14
+
15
+ http://labs.metacarta.com/GeoParser/documentation.html
16
+
17
+ == Installing metacarta-geoparser
18
+
19
+ Just install the gem:
20
+
21
+ $ sudo gem install metacarta-geoparser
22
+
23
+ == Using metacarta-geoparser
24
+
25
+ You can use either +locate+ or +locations+ to locate places:
26
+
27
+ +locate+ returns just one location, but includes a confidence estimate:
28
+
29
+ gp = MetaCartaGeoParser.new
30
+ location = gp.locate 'baghdad'
31
+ p location.coordinates
32
+ p location.confidence
33
+
34
+ +locations+ returns a list of locations and a viewbox surrounding all of them.
35
+
36
+ gp = MetaCartaGeoParser.new
37
+ locations, viewbox = gp.locations 'seattle'
38
+ p locations.length
39
+ p location.first.coordinates
40
+ p viewbox
41
+
data/Rakefile ADDED
@@ -0,0 +1,69 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/gempackagetask'
6
+
7
+ $VERBOSE = nil
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'metacarta-geoparser'
11
+ s.version = '1.0.0'
12
+ s.summary = 'MetaCarta GeoParser API Library'
13
+ s.description = 'Map addresses to latitude and longitude with MetaCarta\'s
14
+ GeoParser API.'
15
+ s.author = 'Eric Hodel'
16
+ s.email = 'eric@robotcoop.com'
17
+
18
+ s.has_rdoc = true
19
+ s.files = File.read('Manifest.txt').split($/)
20
+ s.require_path = 'lib'
21
+
22
+ s.add_dependency 'rc-rest', '>= 1.0.0'
23
+ end
24
+
25
+ desc 'Run tests'
26
+ task :default => [ :test ]
27
+
28
+ Rake::TestTask.new('test') do |t|
29
+ t.libs << '../rc-rest/lib'
30
+ t.pattern = 'test/test_*.rb'
31
+ t.verbose = true
32
+ end
33
+
34
+ desc 'Update Manifest.txt'
35
+ task :update_manifest do
36
+ sh "find . -type f | sed -e 's%./%%' | egrep -v 'svn|swp|~' | egrep -v '^(doc|pkg)/' | sort > Manifest.txt"
37
+ end
38
+
39
+ desc 'Generate RDoc'
40
+ Rake::RDocTask.new :rdoc do |rd|
41
+ rd.rdoc_dir = 'doc'
42
+ rd.rdoc_files.add 'lib', 'README', 'LICENSE'
43
+ rd.main = 'README'
44
+ rd.options << '-d' if `which dot` =~ /\/dot/
45
+ rd.options << '-t MetaCarta GeoParser'
46
+ end
47
+
48
+ desc 'Generate RDoc for dev.robotcoop.com'
49
+ Rake::RDocTask.new :dev_rdoc do |rd|
50
+ rd.rdoc_dir = '../../../www/trunk/dev/html/Libraries/google-geocode'
51
+ rd.rdoc_files.add 'lib', 'README', 'LICENSE'
52
+ rd.main = 'README'
53
+ rd.options << '-d' if `which dot` =~ /\/dot/
54
+ rd.options << '-t MetaCarta GeoParser'
55
+ end
56
+
57
+ desc 'Build Gem'
58
+ Rake::GemPackageTask.new spec do |pkg|
59
+ pkg.need_tar = true
60
+ end
61
+
62
+ desc 'Clean up'
63
+ task :clean => [ :clobber_rdoc, :clobber_package ]
64
+
65
+ desc 'Clean up'
66
+ task :clobber => [ :clean ]
67
+
68
+ # vim: syntax=Ruby
69
+
@@ -0,0 +1,119 @@
1
+ require 'rc_rest'
2
+
3
+ ##
4
+ # Library for looking up coordinates with MetaCarta's GeoParser API.
5
+ #
6
+ # http://labs.metacarta.com/GeoParser/documentation.html
7
+
8
+ class MetaCartaGeoParser < RCRest
9
+
10
+ class Error < RCRest::Error; end
11
+
12
+ ##
13
+ # Raised if a location couldn't be found.
14
+
15
+ class AddressError < Error; end
16
+
17
+ Location = Struct.new :name, :type, :population, :hierarchy,
18
+ :latitude, :longitude, :confidence, :viewbox
19
+
20
+ def initialize # :nodoc:
21
+ @url = URI.parse 'http://labs.metacarta.com/GeoParser/'
22
+ end
23
+
24
+ ##
25
+ # Locates +place+ and returns a Location object.
26
+
27
+ def locate(place)
28
+ locations, = get :q => place
29
+ return locations.first
30
+ end
31
+
32
+ ##
33
+ # Retrieve all locations matching +place+.
34
+ #
35
+ # Returns an Array of Location objects and a pair of coordinates that will
36
+ # surround them.
37
+
38
+ def locations(place)
39
+ get :loc => place
40
+ end
41
+
42
+ def check_error(xml) # :nodoc:
43
+ raise AddressError, 'bad location' unless xml.elements['Locations/Location']
44
+ end
45
+
46
+ def make_url(params) # :nodoc:
47
+ params[:output] = 'locations'
48
+
49
+ super params
50
+ end
51
+
52
+ def parse_response(xml) # :nodoc:
53
+ locations = []
54
+
55
+ xml.elements['/Locations'].each do |l|
56
+ next if REXML::Text === l or l.name == 'ViewBox'
57
+ location = Location.new
58
+
59
+ location.viewbox = viewbox_coords l.elements['ViewBox/gml:Box/gml:coordinates']
60
+
61
+ location.name = l.attributes['Name']
62
+ location.type = l.attributes['Type']
63
+ population = l.attributes['Population'].to_i
64
+ location.population = population > 0 ? population : nil
65
+ location.hierarchy = l.attributes['Hierarchy']
66
+
67
+ coords = l.elements['Centroid/gml:Point/gml:coordinates'].text.split ','
68
+ location.latitude = coords.first.to_f
69
+ location.longitude = coords.last.to_f
70
+
71
+ confidence = l.elements['Confidence']
72
+ location.confidence = confidence.text.to_f if confidence
73
+
74
+ locations << location
75
+ end
76
+
77
+ query_viewbox = xml.elements['/Locations/ViewBox/gml:Box/gml:coordinates']
78
+
79
+ return locations, viewbox_coords(query_viewbox)
80
+ end
81
+
82
+ ##
83
+ # Turns a element containing a pair of coordinates into a pair of coordinate
84
+ # Arrays.
85
+
86
+ def viewbox_coords(viewbox) # :nodoc:
87
+ return viewbox.text.split(' ').map do |coords|
88
+ coords.split(',').map { |c| c.to_f }
89
+ end
90
+
91
+ end
92
+
93
+ end
94
+
95
+ ##
96
+ # A Location contains the following fields:
97
+ #
98
+ # +name+:: The name of this location
99
+ # +type+:: The type of this location (no clue what it means)
100
+ # +population+:: The number of people who live here or nil
101
+ # +hierarchy+:: The places above this place
102
+ # +latitude+:: Latitude of the location
103
+ # +longitude+:: Longitude of the location
104
+ # +confidence+:: Accuracy confidence (if any)
105
+ # +viewbox+:: Pair of coordinates forming a box around this place
106
+ #
107
+ # viewbox runs from lower left to upper right.
108
+
109
+ class MetaCartaGeoParser::Location
110
+
111
+ ##
112
+ # The latitude and longitude for this location.
113
+
114
+ def coordinates
115
+ [latitude, longitude]
116
+ end
117
+
118
+ end
119
+
@@ -0,0 +1,200 @@
1
+ require 'test/unit'
2
+ require 'rc_rest/uri_stub'
3
+ require 'metacarta_geoparser'
4
+
5
+ class TestMetaCartaGeoParser < Test::Unit::TestCase
6
+
7
+ def setup
8
+ URI::HTTP.responses = []
9
+ URI::HTTP.uris = []
10
+
11
+ @mg = MetaCartaGeoParser.new
12
+ end
13
+
14
+ def test_locate
15
+ URI::HTTP.responses << <<-EOF.strip
16
+ <?xml version="1.0" encoding="UTF-8" ?>
17
+
18
+ <!DOCTYPE Locations SYSTEM "Locations.dtd">
19
+
20
+ <Locations
21
+ xmlns:gml="http://www.opengis.net/gml"
22
+ CreatedBy="MetaCarta GeoParser v3.5.0-labs-alpha"
23
+ CreatedOn="Thu Jun 15 23:56:50 2006"
24
+ ConvertedFromBinaryFormat="False"
25
+ InputByteLength="0">
26
+ <ViewBox>
27
+
28
+ <gml:Box srsName="epsg:4326">
29
+ <gml:coordinates>26.238611,37.293889 40.438611,51.493889</gml:coordinates>
30
+ </gml:Box>
31
+ </ViewBox>
32
+ <Location
33
+
34
+ Name="Baghdad"
35
+ Type="PPLC"
36
+ Population="5672516"
37
+ Hierarchy="Iraq/Baghdad/Baghdad">
38
+ <ViewBox>
39
+ <gml:Box srsName="epsg:4326">
40
+ <gml:coordinates>26.238611,37.293889 40.438611,51.493889</gml:coordinates>
41
+ </gml:Box>
42
+ </ViewBox>
43
+ <RemainingQuery></RemainingQuery>
44
+ <Confidence>0.195185</Confidence> <Centroid><gml:Point><gml:coordinates>44.393889,33.338611</gml:coordinates></gml:Point></Centroid>
45
+ </Location>
46
+ </Locations>
47
+ EOF
48
+
49
+ expected = MetaCartaGeoParser::Location.new 'Baghdad', 'PPLC', 5672516,
50
+ 'Iraq/Baghdad/Baghdad',
51
+ 44.393889, 33.338611, 0.195185,
52
+ [[26.238611, 37.293889],
53
+ [40.438611, 51.493889]]
54
+
55
+ location = @mg.locate('baghdad')
56
+ assert_equal expected, location
57
+ assert_equal [44.393889, 33.338611], location.coordinates
58
+
59
+ assert_equal true, URI::HTTP.responses.empty?
60
+ assert_equal 1, URI::HTTP.uris.length
61
+ assert_equal 'http://labs.metacarta.com/GeoParser/?output=locations&q=baghdad',
62
+ URI::HTTP.uris.first
63
+ end
64
+
65
+ def test_locate_bad_address
66
+ URI::HTTP.responses << <<-EOF.strip
67
+ <?xml version="1.0" encoding="UTF-8" ?>
68
+
69
+ <!DOCTYPE Locations SYSTEM "Locations.dtd">
70
+
71
+ <Locations
72
+ xmlns:gml="http://www.opengis.net/gml"
73
+ CreatedBy="MetaCarta GeoParser v3.5.0-labs-alpha"
74
+ CreatedOn="Sat Jun 17 02:05:42 2006"
75
+ ConvertedFromBinaryFormat="False"
76
+ InputByteLength="0">
77
+ <ViewBox>
78
+
79
+ <gml:Box srsName="epsg:4326">
80
+ <gml:coordinates>-90.000000,-180.000000 90.000000,180.000000</gml:coordinates>
81
+ </gml:Box>
82
+ </ViewBox>
83
+ </Locations>
84
+ EOF
85
+
86
+ @mg.locate('aoeueou')
87
+ rescue MetaCartaGeoParser::AddressError => e
88
+ assert_equal 'bad location', e.message
89
+
90
+ assert_equal true, URI::HTTP.responses.empty?
91
+ assert_equal 1, URI::HTTP.uris.length
92
+ assert_equal 'http://labs.metacarta.com/GeoParser/?output=locations&q=aoeueou',
93
+ URI::HTTP.uris.first
94
+ else
95
+ flunk 'expected MetaCartaGeoParser::AddressError'
96
+ end
97
+
98
+ def test_locations
99
+ URI::HTTP.responses << <<-EOF.strip
100
+ <?xml version="1.0" encoding="UTF-8" ?>
101
+
102
+ <!DOCTYPE Locations SYSTEM "Locations.dtd">
103
+
104
+ <Locations
105
+ xmlns:gml="http://www.opengis.net/gml"
106
+ CreatedBy="MetaCarta GeoParser v3.5.0-labs-alpha"
107
+ CreatedOn="Sat Jun 17 02:14:05 2006"
108
+ ConvertedFromBinaryFormat="False"
109
+ InputByteLength="1">
110
+ <ViewBox>
111
+
112
+ <gml:Box srsName="epsg:4326">
113
+ <gml:coordinates>43.800000,-126.133333 51.406390,-118.530830</gml:coordinates>
114
+ </gml:Box>
115
+ </ViewBox>
116
+ <Location
117
+
118
+ Name="Seattle"
119
+ Type="PPL"
120
+ Population="563374"
121
+ Hierarchy="United States/Washington/King/Seattle">
122
+ <ViewBox>
123
+ <gml:Box srsName="epsg:4326">
124
+ <gml:coordinates>43.806390,-126.130830 51.406390,-118.530830</gml:coordinates>
125
+ </gml:Box>
126
+ </ViewBox> <Centroid><gml:Point><gml:coordinates>-122.330830000000006,47.606389999999998</gml:coordinates></gml:Point></Centroid>
127
+ </Location>
128
+ <Location
129
+
130
+ Name="Seattle"
131
+ Type="PRT"
132
+ Population="-1"
133
+ Hierarchy="United States/Seattle">
134
+ <ViewBox>
135
+ <gml:Box srsName="epsg:4326">
136
+ <gml:coordinates>43.800000,-126.133333 51.400000,-118.533333</gml:coordinates>
137
+ </gml:Box>
138
+ </ViewBox> <Centroid><gml:Point><gml:coordinates>-122.333332999999996,47.6</gml:coordinates></gml:Point></Centroid>
139
+ </Location>
140
+ </Locations>
141
+ EOF
142
+
143
+ expected = [
144
+ MetaCartaGeoParser::Location.new('Seattle', 'PPL', 563374,
145
+ 'United States/Washington/King/Seattle',
146
+ -122.33083, 47.60639, nil,
147
+ [[43.806390, -126.130830],
148
+ [51.406390, -118.530830]]),
149
+ MetaCartaGeoParser::Location.new('Seattle', 'PRT', nil,
150
+ 'United States/Seattle',
151
+ -122.333333, 47.6, nil,
152
+ [[43.8, -126.133333],
153
+ [51.4, -118.533333]]),
154
+ ]
155
+
156
+ locations, viewbox = @mg.locations('seattle')
157
+ assert_equal expected, locations
158
+ assert_equal [[43.8, -126.133333], [51.40639, -118.53083]], viewbox
159
+
160
+ assert_equal true, URI::HTTP.responses.empty?
161
+ assert_equal 1, URI::HTTP.uris.length
162
+ assert_equal 'http://labs.metacarta.com/GeoParser/?loc=seattle&output=locations',
163
+ URI::HTTP.uris.first
164
+ end
165
+
166
+ def test_locations_bad_address
167
+ URI::HTTP.responses << <<-EOF.strip
168
+ <?xml version="1.0" encoding="UTF-8" ?>
169
+
170
+ <!DOCTYPE Locations SYSTEM "Locations.dtd">
171
+
172
+ <Locations
173
+ xmlns:gml="http://www.opengis.net/gml"
174
+ CreatedBy="MetaCarta GeoParser v3.5.0-labs-alpha"
175
+ CreatedOn="Sat Jun 17 02:05:42 2006"
176
+ ConvertedFromBinaryFormat="False"
177
+ InputByteLength="0">
178
+ <ViewBox>
179
+
180
+ <gml:Box srsName="epsg:4326">
181
+ <gml:coordinates>-90.000000,-180.000000 90.000000,180.000000</gml:coordinates>
182
+ </gml:Box>
183
+ </ViewBox>
184
+ </Locations>
185
+ EOF
186
+
187
+ @mg.locations('aoeueou')
188
+ rescue MetaCartaGeoParser::AddressError => e
189
+ assert_equal 'bad location', e.message
190
+
191
+ assert_equal true, URI::HTTP.responses.empty?
192
+ assert_equal 1, URI::HTTP.uris.length
193
+ assert_equal 'http://labs.metacarta.com/GeoParser/?loc=aoeueou&output=locations',
194
+ URI::HTTP.uris.first
195
+ else
196
+ flunk 'expected MetaCartaGeoParser::AddressError'
197
+ end
198
+
199
+ end
200
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.99
3
+ specification_version: 1
4
+ name: metacarta-geoparser
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2006-06-16 00:00:00 -07:00
8
+ summary: MetaCarta GeoParser API Library
9
+ require_paths:
10
+ - lib
11
+ email: eric@robotcoop.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description: Map addresses to latitude and longitude with MetaCarta's GeoParser API.
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:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Eric Hodel
31
+ files:
32
+ - LICENSE
33
+ - Manifest.txt
34
+ - README
35
+ - Rakefile
36
+ - lib/metacarta_geoparser.rb
37
+ - test/test_metacarta_geoparser.rb
38
+ test_files: []
39
+
40
+ rdoc_options: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ requirements: []
49
+
50
+ dependencies:
51
+ - !ruby/object:Gem::Dependency
52
+ name: rc-rest
53
+ version_requirement:
54
+ version_requirements: !ruby/object:Gem::Version::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 1.0.0
59
+ version: