Class OSM::API
In: lib/OSM/API.rb
Parent: Object

The OSM::API class handles all calls to the OpenStreetMap API.

Usage:

  require 'OSM/API'

  @api = OSM::API.new
  node = @api.get_node(3437)

In most cases you can use the more convenient methods on the OSM::Node, OSM::Way, or OSM::Relation objects.

Methods

Constants

DEFAULT_BASE_URI = 'http://www.openstreetmap.org/api/0.5/'   the default base URI for the API

Public Class methods

Creates a new API object. Without any arguments it uses the default API at DEFAULT_BASE_URI. If you want to use a different API, give the base URI as parameter to this method.

[Source]

    # File lib/OSM/API.rb, line 53
53:         def initialize(uri=DEFAULT_BASE_URI)
54:             @base_uri = uri
55:         end

Public Instance methods

Get all objects in the bounding box (bbox) given by the left, bottom, right, and top parameters. They will be put into a OSM::Database object which is returned.

[Source]

     # File lib/OSM/API.rb, line 125
125:         def get_bbox(left, bottom, right, top)
126:             raise TypeError.new('"left" value needs to be a number between -180 and 180') unless(left.kind_of?(Float) && left >= -180 && left <= 180)
127:             raise TypeError.new('"bottom" value needs to be a number between -90 and 90') unless(bottom.kind_of?(Float) && bottom >= -90 && bottom <= 90)
128:             raise TypeError.new('"right" value needs to be a number between -180 and 180') unless(right.kind_of?(Float) && right >= -180 && right <= 180)
129:             raise TypeError.new('"top" value needs to be a number between -90 and 90') unless(top.kind_of?(Float) && top >= -90 && top <= 90)
130:             response = get("map?bbox=#{left},#{bottom},#{right},#{top}")
131:             check_response_codes(response)
132:             db = OSM::Database.new
133:             parser = OSM::StreamParser.new(:string => response.body, :db => db)
134:             parser.parse
135:             db
136:         end

Get all historic versions of an object of specified type and with specified ID from API.

[Source]

     # File lib/OSM/API.rb, line 116
116:         def get_history(type, id)
117:             api_call_with_type(type, id, "#{type}/#{id}/history")
118:         end

Get a node with specified ID from API.

[Source]

    # File lib/OSM/API.rb, line 76
76:         def get_node(id)
77:             get_object('node', id)
78:         end

Get an object (‘node’, ‘way’, or ‘relation’) with specified ID from API.

[Source]

    # File lib/OSM/API.rb, line 61
61:         def get_object(type, id)
62:             raise ArgumentError.new("type needs to be one of 'node', 'way', and 'relation'") unless type =~ /^(node|way|relation)$/
63:             raise TypeError.new('id needs to be a positive integer') unless(id.kind_of?(Fixnum) && id > 0)
64:             response = get("#{type}/#{id}")
65:             check_response_codes(response)
66:             parser = OSM::StreamParser.new(:string => response.body, :callbacks => OSM::ObjectListCallbacks.new)
67:             list = parser.parse
68:             raise APITooManyObjects if list.size > 1
69:             list[0]
70:         end

Get a relation with specified ID from API.

[Source]

    # File lib/OSM/API.rb, line 92
92:         def get_relation(id)
93:             get_object('relation', id)
94:         end

Get all relations which refer to the object of specified type and with specified ID from API.

[Source]

     # File lib/OSM/API.rb, line 108
108:         def get_relations_referring_to_object(type, id)
109:             api_call_with_type(type, id, "#{type}/#{id}/relations")
110:         end

Get a way with specified ID from API.

[Source]

    # File lib/OSM/API.rb, line 84
84:         def get_way(id)
85:             get_object('way', id)
86:         end

Get all ways using the node with specified ID from API.

[Source]

     # File lib/OSM/API.rb, line 100
100:         def get_ways_using_node(id)
101:             api_call(id, "node/#{id}/ways")
102:         end

[Validate]