1 class ApiController < ApplicationController
4 before_filter :check_api_readable, :except => [:capabilities]
5 after_filter :compress_output
7 # Help methods for checking boundary sanity and area size
10 # The maximum area you're allowed to request, in square degrees
11 MAX_REQUEST_AREA = APP_CONFIG['max_request_area']
13 # Number of GPS trace/trackpoints returned per-page
14 TRACEPOINTS_PER_PAGE = APP_CONFIG['tracepoints_per_page']
16 # Get an XML response containing a list of tracepoints that have been uploaded
17 # within the specified bounding box, and in the specified page.
19 #retrieve the page number
20 page = params['page'].to_i
26 report_error("Page number must be greater than or equal to 0")
30 offset = page * TRACEPOINTS_PER_PAGE
34 unless bbox and bbox.count(',') == 3
35 report_error("The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat")
39 bbox = bbox.split(',')
40 min_lon, min_lat, max_lon, max_lat = sanitise_boundaries(bbox)
41 # check boundary is sane and area within defined
42 # see /config/application.yml
44 check_boundaries(min_lon, min_lat, max_lon, max_lat)
45 rescue Exception => err
46 report_error(err.message)
51 points = Tracepoint.find_by_area(min_lat, min_lon, max_lat, max_lon, :offset => offset, :limit => TRACEPOINTS_PER_PAGE, :order => "timestamp DESC" )
53 doc = XML::Document.new
54 doc.encoding = XML::Encoding::UTF_8
55 root = XML::Node.new 'gpx'
56 root['version'] = '1.0'
57 root['creator'] = 'OpenStreetMap.org'
58 root['xmlns'] = "http://www.topografix.com/GPX/1/0/"
62 track = XML::Node.new 'trk'
65 trkseg = XML::Node.new 'trkseg'
68 points.each do |point|
69 trkseg << point.to_xml_node()
72 response.headers["Content-Disposition"] = "attachment; filename=\"map.osm\""
74 render :text => doc.to_s, :content_type => "text/xml"
77 # This is probably the most common call of all. It is used for getting the
78 # OSM data for a specified bounding box, usually for editing. First the
79 # bounding box (bbox) is checked to make sure that it is sane. All nodes
80 # are searched, then all the ways that reference those nodes are found.
81 # All Nodes that are referenced by those ways are fetched and added to the list
83 # Then all the relations that reference the already found nodes and ways are
84 # fetched. All the nodes and ways that are referenced by those ways are then
85 # fetched. Finally all the xml is returned.
90 unless bbox and bbox.count(',') == 3
91 # alternatively: report_error(TEXT['boundary_parameter_required']
92 report_error("The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat")
96 bbox = bbox.split(',')
98 min_lon, min_lat, max_lon, max_lat = sanitise_boundaries(bbox)
100 # check boundary is sane and area within defined
101 # see /config/application.yml
103 check_boundaries(min_lon, min_lat, max_lon, max_lat)
104 rescue Exception => err
105 report_error(err.message)
109 # FIXME um why is this area using a different order for the lat/lon from above???
110 @nodes = Node.find_by_area(min_lat, min_lon, max_lat, max_lon, :conditions => {:visible => true}, :limit => APP_CONFIG['max_number_of_nodes']+1)
111 # get all the nodes, by tag not yet working, waiting for change from NickB
112 # need to be @nodes (instance var) so tests in /spec can be performed
113 #@nodes = Node.search(bbox, params[:tag])
115 node_ids = @nodes.collect(&:id)
116 if node_ids.length > APP_CONFIG['max_number_of_nodes']
117 report_error("You requested too many nodes (limit is #{APP_CONFIG['max_number_of_nodes']}). Either request a smaller area, or use planet.osm")
120 if node_ids.length == 0
121 render :text => "<osm version='#{API_VERSION}' generator='#{GENERATOR}'></osm>", :content_type => "text/xml"
125 doc = OSM::API.new.get_xml_doc
128 bounds = XML::Node.new 'bounds'
129 bounds['minlat'] = min_lat.to_s
130 bounds['minlon'] = min_lon.to_s
131 bounds['maxlat'] = max_lat.to_s
132 bounds['maxlon'] = max_lon.to_s
136 # find which ways are needed
138 if node_ids.length > 0
139 way_nodes = WayNode.find_all_by_node_id(node_ids)
140 way_ids = way_nodes.collect { |way_node| way_node.id[0] }
141 ways = Way.find(way_ids)
143 list_of_way_nodes = ways.collect { |way|
144 way.way_nodes.collect { |way_node| way_node.node_id }
146 list_of_way_nodes.flatten!
149 list_of_way_nodes = Array.new
152 # - [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
153 nodes_to_fetch = (list_of_way_nodes.uniq - node_ids) - [0]
155 if nodes_to_fetch.length > 0
156 @nodes += Node.find(nodes_to_fetch)
161 user_display_name_cache = {}
163 @nodes.each do |node|
165 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
166 visible_nodes[node.id] = node
173 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
178 relations = Relation.find_for_nodes(visible_nodes.keys, :conditions => {:visible => true}) +
179 Relation.find_for_ways(way_ids, :conditions => {:visible => true})
181 # we do not normally return the "other" partners referenced by an relation,
182 # e.g. if we return a way A that is referenced by relation X, and there's
183 # another way B also referenced, that is not returned. But we do make
184 # an exception for cases where an relation references another *relation*;
185 # in that case we return that as well (but we don't go recursive here)
186 relations += Relation.find_for_relations(relations.collect { |r| r.id }, :conditions => {:visible => true})
188 # this "uniq" may be slightly inefficient; it may be better to first collect and output
189 # all node-related relations, then find the *not yet covered* way-related ones etc.
190 relations.uniq.each do |relation|
191 doc.root << relation.to_xml_node(changeset_cache, user_display_name_cache)
194 response.headers["Content-Disposition"] = "attachment; filename=\"map.osm\""
196 render :text => doc.to_s, :content_type => "text/xml"
199 # Get a list of the tiles that have changed within a specified time
202 zoom = (params[:zoom] || '12').to_i
204 if params.include?(:start) and params.include?(:end)
205 starttime = Time.parse(params[:start])
206 endtime = Time.parse(params[:end])
208 hours = (params[:hours] || '1').to_i.hours
209 endtime = Time.now.getutc
210 starttime = endtime - hours
213 if zoom >= 1 and zoom <= 16 and
214 endtime > starttime and endtime - starttime <= 24.hours
215 mask = (1 << zoom) - 1
217 tiles = Node.count(:conditions => ["timestamp BETWEEN ? AND ?", starttime, endtime],
218 :group => "maptile_for_point(latitude, longitude, #{zoom})")
220 doc = OSM::API.new.get_xml_doc
221 changes = XML::Node.new 'changes'
222 changes["starttime"] = starttime.xmlschema
223 changes["endtime"] = endtime.xmlschema
225 tiles.each do |tile, count|
226 x = (tile.to_i >> zoom) & mask
229 t = XML::Node.new 'tile'
233 t["changes"] = count.to_s
240 render :text => doc.to_s, :content_type => "text/xml"
242 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
246 # External apps that use the api are able to query the api to find out some
247 # parameters of the API. It currently returns:
248 # * minimum and maximum API versions that can be used.
249 # * maximum area that can be requested in a bbox request in square degrees
250 # * number of tracepoints that are returned in each tracepoints page
252 doc = OSM::API.new.get_xml_doc
254 api = XML::Node.new 'api'
255 version = XML::Node.new 'version'
256 version['minimum'] = "#{API_VERSION}";
257 version['maximum'] = "#{API_VERSION}";
259 area = XML::Node.new 'area'
260 area['maximum'] = MAX_REQUEST_AREA.to_s;
262 tracepoints = XML::Node.new 'tracepoints'
263 tracepoints['per_page'] = APP_CONFIG['tracepoints_per_page'].to_s
265 waynodes = XML::Node.new 'waynodes'
266 waynodes['maximum'] = APP_CONFIG['max_number_of_way_nodes'].to_s
268 changesets = XML::Node.new 'changesets'
269 changesets['maximum_elements'] = Changeset::MAX_ELEMENTS.to_s
274 render :text => doc.to_s, :content_type => "text/xml"