1 class ApiController < ApplicationController
3 skip_before_filter :verify_authenticity_token
4 before_filter :check_api_readable, :except => [:capabilities]
5 before_filter :setup_user_auth, :only => [:permissions]
6 after_filter :compress_output
7 around_filter :api_call_handle_error, :api_call_timeout
9 # Get an XML response containing a list of tracepoints that have been uploaded
10 # within the specified bounding box, and in the specified page.
12 #retrieve the page number
13 page = params['page'].to_s.to_i
16 report_error("Page number must be greater than or equal to 0")
20 offset = page * TRACEPOINTS_PER_PAGE
23 # check boundary is sane and area within defined
24 # see /config/application.yml
26 bbox = BoundingBox.from_bbox_params(params)
29 rescue Exception => err
30 report_error(err.message)
35 points = Tracepoint.bbox(bbox).offset(offset).limit(TRACEPOINTS_PER_PAGE).order("gpx_id DESC, trackid ASC, timestamp ASC")
37 doc = XML::Document.new
38 doc.encoding = XML::Encoding::UTF_8
39 root = XML::Node.new 'gpx'
40 root['version'] = '1.0'
41 root['creator'] = 'OpenStreetMap.org'
42 root['xmlns'] = "http://www.topografix.com/GPX/1/0"
46 # initialise these variables outside of the loop so that they
47 # stay in scope and don't get free'd up by the GC during the
58 points.each do |point|
59 if gpx_id != point.gpx_id
62 gpx_file = Trace.find(gpx_id)
64 if gpx_file.trackable?
65 track = XML::Node.new 'trk'
69 if gpx_file.identifiable?
70 track << (XML::Node.new("name") << gpx_file.name)
71 track << (XML::Node.new("desc") << gpx_file.description)
72 track << (XML::Node.new("url") << url_for(:controller => 'trace', :action => 'view', :display_name => gpx_file.user.display_name, :id => gpx_file.id))
75 # use the anonymous track segment if the user hasn't allowed
76 # their GPX points to be tracked.
79 anon_track = XML::Node.new 'trk'
80 doc.root << anon_track
86 if trackid != point.trackid
87 if gpx_file.trackable?
88 trkseg = XML::Node.new 'trkseg'
90 trackid = point.trackid
93 anon_trkseg = XML::Node.new 'trkseg'
94 anon_track << anon_trkseg
100 trkseg << point.to_xml_node(timestamps)
103 response.headers["Content-Disposition"] = "attachment; filename=\"tracks.gpx\""
105 render :text => doc.to_s, :content_type => "text/xml"
108 # This is probably the most common call of all. It is used for getting the
109 # OSM data for a specified bounding box, usually for editing. First the
110 # bounding box (bbox) is checked to make sure that it is sane. All nodes
111 # are searched, then all the ways that reference those nodes are found.
112 # All Nodes that are referenced by those ways are fetched and added to the list
114 # Then all the relations that reference the already found nodes and ways are
115 # fetched. All the nodes and ways that are referenced by those ways are then
116 # fetched. Finally all the xml is returned.
118 # Figure out the bbox
119 # check boundary is sane and area within defined
120 # see /config/application.yml
122 bbox = BoundingBox.from_bbox_params(params)
123 bbox.check_boundaries
125 rescue Exception => err
126 report_error(err.message)
130 @nodes = Node.bbox(bbox).where(:visible => true).includes(:node_tags).limit(MAX_NUMBER_OF_NODES+1)
132 node_ids = @nodes.collect(&:id)
133 if node_ids.length > MAX_NUMBER_OF_NODES
134 report_error("You requested too many nodes (limit is #{MAX_NUMBER_OF_NODES}). Either request a smaller area, or use planet.osm")
137 if node_ids.length == 0
138 render :text => "<osm version='#{API_VERSION}' generator='#{GENERATOR}'></osm>", :content_type => "text/xml"
142 doc = OSM::API.new.get_xml_doc
145 doc.root << bbox.add_bounds_to(XML::Node.new 'bounds')
148 # find which ways are needed
150 if node_ids.length > 0
151 way_nodes = WayNode.where(:node_id => node_ids)
152 way_ids = way_nodes.collect { |way_node| way_node.id[0] }
153 ways = Way.preload(:way_nodes, :way_tags).find(way_ids)
155 list_of_way_nodes = ways.collect { |way|
156 way.way_nodes.collect { |way_node| way_node.node_id }
158 list_of_way_nodes.flatten!
161 list_of_way_nodes = Array.new
164 # - [0] in case some thing links to node 0 which doesn't exist. Shouldn't actually ever happen but it does. FIXME: file a ticket for this
165 nodes_to_fetch = (list_of_way_nodes.uniq - node_ids) - [0]
167 if nodes_to_fetch.length > 0
168 @nodes += Node.includes(:node_tags).find(nodes_to_fetch)
173 user_display_name_cache = {}
175 @nodes.each do |node|
177 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
178 visible_nodes[node.id] = node
185 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
190 relations = Relation.nodes(visible_nodes.keys).visible +
191 Relation.ways(way_ids).visible
193 # we do not normally return the "other" partners referenced by an relation,
194 # e.g. if we return a way A that is referenced by relation X, and there's
195 # another way B also referenced, that is not returned. But we do make
196 # an exception for cases where an relation references another *relation*;
197 # in that case we return that as well (but we don't go recursive here)
198 relations += Relation.relations(relations.collect { |r| r.id }).visible
200 # this "uniq" may be slightly inefficient; it may be better to first collect and output
201 # all node-related relations, then find the *not yet covered* way-related ones etc.
202 relations.uniq.each do |relation|
203 doc.root << relation.to_xml_node(nil, changeset_cache, user_display_name_cache)
206 response.headers["Content-Disposition"] = "attachment; filename=\"map.osm\""
208 render :text => doc.to_s, :content_type => "text/xml"
211 # Get a list of the tiles that have changed within a specified time
214 zoom = (params[:zoom] || '12').to_i
216 if params.include?(:start) and params.include?(:end)
217 starttime = Time.parse(params[:start])
218 endtime = Time.parse(params[:end])
220 hours = (params[:hours] || '1').to_i.hours
221 endtime = Time.now.getutc
222 starttime = endtime - hours
225 if zoom >= 1 and zoom <= 16 and
226 endtime > starttime and endtime - starttime <= 24.hours
227 mask = (1 << zoom) - 1
229 tiles = Node.where(:timestamp => starttime .. endtime).group("maptile_for_point(latitude, longitude, #{zoom})").count
231 doc = OSM::API.new.get_xml_doc
232 changes = XML::Node.new 'changes'
233 changes["starttime"] = starttime.xmlschema
234 changes["endtime"] = endtime.xmlschema
236 tiles.each do |tile, count|
237 x = (tile.to_i >> zoom) & mask
240 t = XML::Node.new 'tile'
244 t["changes"] = count.to_s
251 render :text => doc.to_s, :content_type => "text/xml"
253 render :text => "Requested zoom is invalid, or the supplied start is after the end time, or the start duration is more than 24 hours", :status => :bad_request
257 # External apps that use the api are able to query the api to find out some
258 # parameters of the API. It currently returns:
259 # * minimum and maximum API versions that can be used.
260 # * maximum area that can be requested in a bbox request in square degrees
261 # * number of tracepoints that are returned in each tracepoints page
263 doc = OSM::API.new.get_xml_doc
265 api = XML::Node.new 'api'
266 version = XML::Node.new 'version'
267 version['minimum'] = "#{API_VERSION}";
268 version['maximum'] = "#{API_VERSION}";
270 area = XML::Node.new 'area'
271 area['maximum'] = MAX_REQUEST_AREA.to_s;
273 tracepoints = XML::Node.new 'tracepoints'
274 tracepoints['per_page'] = TRACEPOINTS_PER_PAGE.to_s
276 waynodes = XML::Node.new 'waynodes'
277 waynodes['maximum'] = MAX_NUMBER_OF_WAY_NODES.to_s
279 changesets = XML::Node.new 'changesets'
280 changesets['maximum_elements'] = Changeset::MAX_ELEMENTS.to_s
282 timeout = XML::Node.new 'timeout'
283 timeout['seconds'] = API_TIMEOUT.to_s
285 status = XML::Node.new 'status'
286 status['database'] = database_status.to_s
287 status['api'] = api_status.to_s
288 status['gpx'] = gpx_status.to_s
291 policy = XML::Node.new 'policy'
292 blacklist = XML::Node.new 'imagery'
293 IMAGERY_BLACKLIST.each do |url_regex|
294 xnd = XML::Node.new 'blacklist'
295 xnd['regex'] = url_regex.to_s
301 render :text => doc.to_s, :content_type => "text/xml"
304 # External apps that use the api are able to query which permissions
305 # they have. This currently returns a list of permissions granted to the current user:
306 # * if authenticated via OAuth, this list will contain all permissions granted by the user to the access_token.
307 # * if authenticated via basic auth all permissions are granted, so the list will contain all permissions.
308 # * unauthenticated users have no permissions, so the list will be empty.
311 when current_token.present?
312 ClientApplication.all_permissions.select { |p| current_token.read_attribute(p) }
314 ClientApplication.all_permissions