1 class ApiController < ApplicationController
3 before_filter :check_api_readable, :except => [:capabilities]
4 after_filter :compress_output
5 around_filter :api_call_handle_error, :api_call_timeout
7 # Help methods for checking boundary sanity and area size
10 # Get an XML response containing a list of tracepoints that have been uploaded
11 # within the specified bounding box, and in the specified page.
13 #retrieve the page number
14 page = params['page'].to_i
20 report_error("Page number must be greater than or equal to 0")
24 offset = page * APP_CONFIG['tracepoints_per_page']
28 unless bbox and bbox.count(',') == 3
29 report_error("The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat")
33 bbox = bbox.split(',')
34 min_lon, min_lat, max_lon, max_lat = sanitise_boundaries(bbox)
35 # check boundary is sane and area within defined
36 # see /config/application.yml
38 check_boundaries(min_lon, min_lat, max_lon, max_lat)
39 rescue Exception => err
40 report_error(err.message)
45 points = Tracepoint.find_by_area(min_lat, min_lon, max_lat, max_lon, :offset => offset, :limit => APP_CONFIG['tracepoints_per_page'], :order => "timestamp DESC" )
47 doc = XML::Document.new
48 doc.encoding = XML::Encoding::UTF_8
49 root = XML::Node.new 'gpx'
50 root['version'] = '1.0'
51 root['creator'] = 'OpenStreetMap.org'
52 root['xmlns'] = "http://www.topografix.com/GPX/1/0/"
56 track = XML::Node.new 'trk'
59 trkseg = XML::Node.new 'trkseg'
62 points.each do |point|
63 trkseg << point.to_xml_node()
66 response.headers["Content-Disposition"] = "attachment; filename=\"map.osm\""
68 render :text => doc.to_s, :content_type => "text/xml"
71 # This is probably the most common call of all. It is used for getting the
72 # OSM data for a specified bounding box, usually for editing. First the
73 # bounding box (bbox) is checked to make sure that it is sane. All nodes
74 # are searched, then all the ways that reference those nodes are found.
75 # All Nodes that are referenced by those ways are fetched and added to the list
77 # Then all the relations that reference the already found nodes and ways are
78 # fetched. All the nodes and ways that are referenced by those ways are then
79 # fetched. Finally all the xml is returned.
84 unless bbox and bbox.count(',') == 3
85 # alternatively: report_error(TEXT['boundary_parameter_required']
86 report_error("The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat")
90 bbox = bbox.split(',')
92 min_lon, min_lat, max_lon, max_lat = sanitise_boundaries(bbox)
94 # check boundary is sane and area within defined
95 # see /config/application.yml
97 check_boundaries(min_lon, min_lat, max_lon, max_lat)
98 rescue Exception => err
99 report_error(err.message)
103 # FIXME um why is this area using a different order for the lat/lon from above???
104 @nodes = Node.find_by_area(min_lat, min_lon, max_lat, max_lon, :conditions => {:visible => true}, :include => :node_tags, :limit => APP_CONFIG['max_number_of_nodes']+1)
105 # get all the nodes, by tag not yet working, waiting for change from NickB
106 # need to be @nodes (instance var) so tests in /spec can be performed
107 #@nodes = Node.search(bbox, params[:tag])
109 node_ids = @nodes.collect(&:id)
110 if node_ids.length > APP_CONFIG['max_number_of_nodes']
111 report_error("You requested too many nodes (limit is #{APP_CONFIG['max_number_of_nodes']}). Either request a smaller area, or use planet.osm")
114 if node_ids.length == 0
115 render :text => "<osm version='#{API_VERSION}' generator='#{GENERATOR}'></osm>", :content_type => "text/xml"
119 doc = OSM::API.new.get_xml_doc
122 bounds = XML::Node.new 'bounds'
123 bounds['minlat'] = min_lat.to_s
124 bounds['minlon'] = min_lon.to_s
125 bounds['maxlat'] = max_lat.to_s
126 bounds['maxlon'] = max_lon.to_s
130 # find which ways are needed
132 if node_ids.length > 0
133 way_nodes = WayNode.find_all_by_node_id(node_ids)
134 way_ids = way_nodes.collect { |way_node| way_node.id[0] }
135 ways = Way.find(way_ids, :include => [:way_nodes, :way_tags])
137 list_of_way_nodes = ways.collect { |way|
138 way.way_nodes.collect { |way_node| way_node.node_id }
140 list_of_way_nodes.flatten!
143 list_of_way_nodes = Array.new
146 # - [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
147 nodes_to_fetch = (list_of_way_nodes.uniq - node_ids) - [0]
149 if nodes_to_fetch.length > 0
150 @nodes += Node.find(nodes_to_fetch, :include => :node_tags)
155 user_display_name_cache = {}
157 @nodes.each do |node|
159 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
160 visible_nodes[node.id] = node
167 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
172 relations = Relation.find_for_nodes(visible_nodes.keys, :conditions => {:visible => true}) +
173 Relation.find_for_ways(way_ids, :conditions => {:visible => true})
175 # we do not normally return the "other" partners referenced by an relation,
176 # e.g. if we return a way A that is referenced by relation X, and there's
177 # another way B also referenced, that is not returned. But we do make
178 # an exception for cases where an relation references another *relation*;
179 # in that case we return that as well (but we don't go recursive here)
180 relations += Relation.find_for_relations(relations.collect { |r| r.id }, :conditions => {:visible => true})
182 # this "uniq" may be slightly inefficient; it may be better to first collect and output
183 # all node-related relations, then find the *not yet covered* way-related ones etc.
184 relations.uniq.each do |relation|
185 doc.root << relation.to_xml_node(nil, changeset_cache, user_display_name_cache)
188 response.headers["Content-Disposition"] = "attachment; filename=\"map.osm\""
190 render :text => doc.to_s, :content_type => "text/xml"
193 # Get a list of the tiles that have changed within a specified time
196 zoom = (params[:zoom] || '12').to_i
198 if params.include?(:start) and params.include?(:end)
199 starttime = Time.parse(params[:start])
200 endtime = Time.parse(params[:end])
202 hours = (params[:hours] || '1').to_i.hours
203 endtime = Time.now.getutc
204 starttime = endtime - hours
207 if zoom >= 1 and zoom <= 16 and
208 endtime > starttime and endtime - starttime <= 24.hours
209 mask = (1 << zoom) - 1
211 tiles = Node.count(:conditions => ["timestamp BETWEEN ? AND ?", starttime, endtime],
212 :group => "maptile_for_point(latitude, longitude, #{zoom})")
214 doc = OSM::API.new.get_xml_doc
215 changes = XML::Node.new 'changes'
216 changes["starttime"] = starttime.xmlschema
217 changes["endtime"] = endtime.xmlschema
219 tiles.each do |tile, count|
220 x = (tile.to_i >> zoom) & mask
223 t = XML::Node.new 'tile'
227 t["changes"] = count.to_s
234 render :text => doc.to_s, :content_type => "text/xml"
236 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
240 # External apps that use the api are able to query the api to find out some
241 # parameters of the API. It currently returns:
242 # * minimum and maximum API versions that can be used.
243 # * maximum area that can be requested in a bbox request in square degrees
244 # * number of tracepoints that are returned in each tracepoints page
246 doc = OSM::API.new.get_xml_doc
248 api = XML::Node.new 'api'
249 version = XML::Node.new 'version'
250 version['minimum'] = "#{API_VERSION}";
251 version['maximum'] = "#{API_VERSION}";
253 area = XML::Node.new 'area'
254 area['maximum'] = APP_CONFIG['max_request_area'].to_s;
256 tracepoints = XML::Node.new 'tracepoints'
257 tracepoints['per_page'] = APP_CONFIG['tracepoints_per_page'].to_s
259 waynodes = XML::Node.new 'waynodes'
260 waynodes['maximum'] = APP_CONFIG['max_number_of_way_nodes'].to_s
262 changesets = XML::Node.new 'changesets'
263 changesets['maximum_elements'] = Changeset::MAX_ELEMENTS.to_s
265 timeout = XML::Node.new 'timeout'
266 timeout['seconds'] = APP_CONFIG['api_timeout'].to_s
271 render :text => doc.to_s, :content_type => "text/xml"