1 class ApiController < ApplicationController
2 skip_before_action :verify_authenticity_token
3 before_action :check_api_readable, :except => [:capabilities]
4 before_action :setup_user_auth, :only => [:permissions]
5 after_action :compress_output
6 around_action :api_call_handle_error, :api_call_timeout
8 # Get an XML response containing a list of tracepoints that have been uploaded
9 # within the specified bounding box, and in the specified page.
11 # retrieve the page number
12 page = params["page"].to_s.to_i
15 report_error("Page number must be greater than or equal to 0")
19 offset = page * TRACEPOINTS_PER_PAGE
22 # check boundary is sane and area within defined
23 # see /config/application.yml
25 bbox = BoundingBox.from_bbox_params(params)
28 rescue StandardError => err
29 report_error(err.message)
34 points = Tracepoint.bbox(bbox).offset(offset).limit(TRACEPOINTS_PER_PAGE).order("gpx_id DESC, trackid ASC, timestamp ASC")
36 doc = XML::Document.new
37 doc.encoding = XML::Encoding::UTF_8
38 root = XML::Node.new "gpx"
39 root["version"] = "1.0"
40 root["creator"] = "OpenStreetMap.org"
41 root["xmlns"] = "http://www.topografix.com/GPX/1/0"
45 # initialise these variables outside of the loop so that they
46 # stay in scope and don't get free'd up by the GC during the
57 points.each do |point|
58 if gpx_id != point.gpx_id
61 gpx_file = Trace.find(gpx_id)
63 if gpx_file.trackable?
64 track = XML::Node.new "trk"
68 if gpx_file.identifiable?
69 track << (XML::Node.new("name") << gpx_file.name)
70 track << (XML::Node.new("desc") << gpx_file.description)
71 track << (XML::Node.new("url") << url_for(:controller => "trace", :action => "view", :display_name => gpx_file.user.display_name, :id => gpx_file.id))
74 # use the anonymous track segment if the user hasn't allowed
75 # their GPX points to be tracked.
78 anon_track = XML::Node.new "trk"
79 doc.root << anon_track
85 if trackid != point.trackid
86 if gpx_file.trackable?
87 trkseg = XML::Node.new "trkseg"
89 trackid = point.trackid
92 anon_trkseg = XML::Node.new "trkseg"
93 anon_track << anon_trkseg
99 trkseg << point.to_xml_node(timestamps)
102 response.headers["Content-Disposition"] = "attachment; filename=\"tracks.gpx\""
104 render :text => doc.to_s, :content_type => "text/xml"
107 # This is probably the most common call of all. It is used for getting the
108 # OSM data for a specified bounding box, usually for editing. First the
109 # bounding box (bbox) is checked to make sure that it is sane. All nodes
110 # are searched, then all the ways that reference those nodes are found.
111 # All Nodes that are referenced by those ways are fetched and added to the list
113 # Then all the relations that reference the already found nodes and ways are
114 # fetched. All the nodes and ways that are referenced by those ways are then
115 # fetched. Finally all the xml is returned.
117 # Figure out the bbox
118 # check boundary is sane and area within defined
119 # see /config/application.yml
121 bbox = BoundingBox.from_bbox_params(params)
122 bbox.check_boundaries
124 rescue StandardError => err
125 report_error(err.message)
129 @nodes = Node.bbox(bbox).where(:visible => true).includes(:node_tags).limit(MAX_NUMBER_OF_NODES + 1)
131 node_ids = @nodes.collect(&:id)
132 if node_ids.length > MAX_NUMBER_OF_NODES
133 report_error("You requested too many nodes (limit is #{MAX_NUMBER_OF_NODES}). Either request a smaller area, or use planet.osm")
136 if node_ids.length == 0
137 render :text => "<osm version='#{API_VERSION}' generator='#{GENERATOR}'></osm>", :content_type => "text/xml"
141 doc = OSM::API.new.get_xml_doc
144 doc.root << bbox.add_bounds_to(XML::Node.new "bounds")
147 # find which ways are needed
149 if node_ids.length > 0
150 way_nodes = WayNode.where(:node_id => node_ids)
151 way_ids = way_nodes.collect { |way_node| way_node.id[0] }
152 ways = Way.preload(:way_nodes, :way_tags).find(way_ids)
154 list_of_way_nodes = ways.collect do |way|
155 way.way_nodes.collect(&:node_id)
157 list_of_way_nodes.flatten!
160 list_of_way_nodes = []
163 # - [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
164 nodes_to_fetch = (list_of_way_nodes.uniq - node_ids) - [0]
166 if nodes_to_fetch.length > 0
167 @nodes += Node.includes(:node_tags).find(nodes_to_fetch)
172 user_display_name_cache = {}
174 @nodes.each do |node|
176 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
177 visible_nodes[node.id] = node
184 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
189 relations = Relation.nodes(visible_nodes.keys).visible +
190 Relation.ways(way_ids).visible
192 # we do not normally return the "other" partners referenced by an relation,
193 # e.g. if we return a way A that is referenced by relation X, and there's
194 # another way B also referenced, that is not returned. But we do make
195 # an exception for cases where an relation references another *relation*;
196 # in that case we return that as well (but we don't go recursive here)
197 relations += Relation.relations(relations.collect(&:id)).visible
199 # this "uniq" may be slightly inefficient; it may be better to first collect and output
200 # all node-related relations, then find the *not yet covered* way-related ones etc.
201 relations.uniq.each do |relation|
202 doc.root << relation.to_xml_node(nil, changeset_cache, user_display_name_cache)
205 response.headers["Content-Disposition"] = "attachment; filename=\"map.osm\""
207 render :text => doc.to_s, :content_type => "text/xml"
210 # Get a list of the tiles that have changed within a specified time
213 zoom = (params[:zoom] || "12").to_i
215 if params.include?(:start) && params.include?(:end)
216 starttime = Time.parse(params[:start])
217 endtime = Time.parse(params[:end])
219 hours = (params[:hours] || "1").to_i.hours
220 endtime = Time.now.getutc
221 starttime = endtime - hours
224 if zoom >= 1 && zoom <= 16 &&
225 endtime > starttime && endtime - starttime <= 24.hours
226 mask = (1 << zoom) - 1
228 tiles = Node.where(:timestamp => starttime..endtime).group("maptile_for_point(latitude, longitude, #{zoom})").count
230 doc = OSM::API.new.get_xml_doc
231 changes = XML::Node.new "changes"
232 changes["starttime"] = starttime.xmlschema
233 changes["endtime"] = endtime.xmlschema
235 tiles.each do |tile, count|
236 x = (tile.to_i >> zoom) & mask
239 t = XML::Node.new "tile"
243 t["changes"] = count.to_s
250 render :text => doc.to_s, :content_type => "text/xml"
252 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
256 # External apps that use the api are able to query the api to find out some
257 # parameters of the API. It currently returns:
258 # * minimum and maximum API versions that can be used.
259 # * maximum area that can be requested in a bbox request in square degrees
260 # * number of tracepoints that are returned in each tracepoints page
262 doc = OSM::API.new.get_xml_doc
264 api = XML::Node.new "api"
265 version = XML::Node.new "version"
266 version["minimum"] = "#{API_VERSION}"
267 version["maximum"] = "#{API_VERSION}"
269 area = XML::Node.new "area"
270 area["maximum"] = MAX_REQUEST_AREA.to_s
272 tracepoints = XML::Node.new "tracepoints"
273 tracepoints["per_page"] = TRACEPOINTS_PER_PAGE.to_s
275 waynodes = XML::Node.new "waynodes"
276 waynodes["maximum"] = MAX_NUMBER_OF_WAY_NODES.to_s
278 changesets = XML::Node.new "changesets"
279 changesets["maximum_elements"] = Changeset::MAX_ELEMENTS.to_s
281 timeout = XML::Node.new "timeout"
282 timeout["seconds"] = API_TIMEOUT.to_s
284 status = XML::Node.new "status"
285 status["database"] = database_status.to_s
286 status["api"] = api_status.to_s
287 status["gpx"] = gpx_status.to_s
290 policy = XML::Node.new "policy"
291 blacklist = XML::Node.new "imagery"
292 IMAGERY_BLACKLIST.each do |url_regex|
293 xnd = XML::Node.new "blacklist"
294 xnd["regex"] = url_regex.to_s
300 render :text => doc.to_s, :content_type => "text/xml"
303 # External apps that use the api are able to query which permissions
304 # they have. This currently returns a list of permissions granted to the current user:
305 # * if authenticated via OAuth, this list will contain all permissions granted by the user to the access_token.
306 # * if authenticated via basic auth all permissions are granted, so the list will contain all permissions.
307 # * unauthenticated users have no permissions, so the list will be empty.
310 when current_token.present?
311 ClientApplication.all_permissions.select { |p| current_token.read_attribute(p) }
313 ClientApplication.all_permissions