Class: MetaCartaGeoParser
- Inherits:
-
RCRest
- Object
- RCRest
- MetaCartaGeoParser
- Defined in:
- lib/metacarta_geoparser.rb
Overview
Library for looking up coordinates with MetaCarta’s GeoParser API.
Defined Under Namespace
Classes: AddressError, Error, Location
Constant Summary collapse
- VERSION =
'1.0.2'
Instance Method Summary collapse
-
#check_error(xml) ⇒ Object
:nodoc:.
-
#initialize ⇒ MetaCartaGeoParser
constructor
:nodoc:.
-
#locate(place) ⇒ Object
Locates
place
and returns a Location object. -
#locations(place) ⇒ Object
Retrieve all locations matching
place
. -
#make_url(method, params) ⇒ Object
:nodoc:.
-
#parse_response(xml) ⇒ Object
:nodoc:.
-
#viewbox_coords(viewbox) ⇒ Object
Turns a element containing a pair of coordinates into a pair of coordinate Arrays.
Constructor Details
#initialize ⇒ MetaCartaGeoParser
:nodoc:
22 23 24 |
# File 'lib/metacarta_geoparser.rb', line 22 def initialize # :nodoc: @url = URI.parse 'http://labs.metacarta.com/GeoParser/' end |
Instance Method Details
#check_error(xml) ⇒ Object
:nodoc:
44 45 46 |
# File 'lib/metacarta_geoparser.rb', line 44 def check_error(xml) # :nodoc: raise AddressError, 'bad location' unless xml.elements['Locations/Location'] end |
#locate(place) ⇒ Object
Locates place
and returns a Location object.
29 30 31 32 |
# File 'lib/metacarta_geoparser.rb', line 29 def locate(place) locations, = get nil, :q => place return locations.first end |
#locations(place) ⇒ Object
Retrieve all locations matching place
.
Returns an Array of Location objects and a pair of coordinates that will surround them.
40 41 42 |
# File 'lib/metacarta_geoparser.rb', line 40 def locations(place) get nil, :loc => place end |
#make_url(method, params) ⇒ Object
:nodoc:
48 49 50 51 52 |
# File 'lib/metacarta_geoparser.rb', line 48 def make_url(method, params) # :nodoc: params[:output] = 'locations' super method, params end |
#parse_response(xml) ⇒ Object
:nodoc:
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 |
# File 'lib/metacarta_geoparser.rb', line 54 def parse_response(xml) # :nodoc: locations = [] xml.elements['/Locations'].each do |l| next if REXML::Text === l or l.name == 'ViewBox' location = Location.new location.viewbox = viewbox_coords l.elements['ViewBox/gml:Box/gml:coordinates'] location.name = l.attributes['Name'] location.type = l.attributes['Type'] population = l.attributes['Population'].to_i location.population = population > 0 ? population : nil location.hierarchy = l.attributes['Hierarchy'] coords = l.elements['Centroid/gml:Point/gml:coordinates'].text.split ',' location.latitude = coords.last.to_f location.longitude = coords.first.to_f confidence = l.elements['Confidence'] location.confidence = confidence.text.to_f if confidence locations << location end query_viewbox = xml.elements['/Locations/ViewBox/gml:Box/gml:coordinates'] return locations, viewbox_coords(query_viewbox) end |
#viewbox_coords(viewbox) ⇒ Object
Turns a element containing a pair of coordinates into a pair of coordinate Arrays.
88 89 90 91 92 |
# File 'lib/metacarta_geoparser.rb', line 88 def viewbox_coords(viewbox) # :nodoc: return viewbox.text.split(' ').map do |coords| coords.split(',').map { |c| c.to_f } end end |