1 class ApiController < ApplicationController
4 before_filter :check_read_availability, :except => [:capabilities]
5 after_filter :compress_output
7 # Help methods for checking boundary sanity and area size
10 #COUNT is the number of map requests to allow before exiting and starting a new process
13 # The maximum area you're allowed to request, in square degrees
14 MAX_REQUEST_AREA = 0.25
16 # Number of GPS trace/trackpoints returned per-page
17 TRACEPOINTS_PER_PAGE = 5000
22 #retrieve the page number
23 page = params['page'].to_i
29 report_error("Page number must be greater than or equal to 0")
33 offset = page * TRACEPOINTS_PER_PAGE
37 unless bbox and bbox.count(',') == 3
38 report_error("The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat")
42 bbox = bbox.split(',')
43 min_lon, min_lat, max_lon, max_lat = sanitise_boundaries(bbox)
44 # check boundary is sane and area within defined
45 # see /config/application.yml
47 check_boundaries(min_lon, min_lat, max_lon, max_lat)
48 rescue Exception => err
49 report_error(err.message)
54 points = Tracepoint.find_by_area(min_lat, min_lon, max_lat, max_lon, :offset => offset, :limit => TRACEPOINTS_PER_PAGE, :order => "timestamp DESC" )
56 doc = XML::Document.new
57 doc.encoding = 'UTF-8'
58 root = XML::Node.new 'gpx'
59 root['version'] = '1.0'
60 root['creator'] = 'OpenStreetMap.org'
61 root['xmlns'] = "http://www.topografix.com/GPX/1/0/"
65 track = XML::Node.new 'trk'
68 trkseg = XML::Node.new 'trkseg'
71 points.each do |point|
72 trkseg << point.to_xml_node()
75 #exit when we have too many requests
76 if @@count > MAX_COUNT
77 render :text => doc.to_s, :content_type => "text/xml"
82 render :text => doc.to_s, :content_type => "text/xml"
92 unless bbox and bbox.count(',') == 3
93 # alternatively: report_error(TEXT['boundary_parameter_required']
94 report_error("The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat")
98 bbox = bbox.split(',')
100 min_lon, min_lat, max_lon, max_lat = sanitise_boundaries(bbox)
102 # check boundary is sane and area within defined
103 # see /config/application.yml
105 check_boundaries(min_lon, min_lat, max_lon, max_lat)
106 rescue Exception => err
107 report_error(err.message)
111 @nodes = Node.find_by_area(min_lat, min_lon, max_lat, max_lon, :conditions => "visible = 1", :limit => APP_CONFIG['max_number_of_nodes']+1)
112 # get all the nodes, by tag not yet working, waiting for change from NickB
113 # need to be @nodes (instance var) so tests in /spec can be performed
114 #@nodes = Node.search(bbox, params[:tag])
116 node_ids = @nodes.collect(&:id)
117 if node_ids.length > APP_CONFIG['max_number_of_nodes']
118 report_error("You requested too many nodes (limit is 50,000). Either request a smaller area, or use planet.osm")
121 if node_ids.length == 0
122 render :text => "<osm version='0.5'></osm>", :content_type => "text/xml"
126 relations = Array.new
128 doc = OSM::API.new.get_xml_doc
131 # find which ways are needed
133 if node_ids.length > 0
134 way_nodes = WayNode.find_all_by_node_id(node_ids)
135 way_ids = way_nodes.collect { |way_node| way_node.id[0] }
136 ways = Way.find(way_ids)
138 list_of_way_nodes = ways.collect { |way|
139 way.way_nodes.collect { |way_node| way_node.node_id }
141 list_of_way_nodes.flatten!
144 list_of_way_nodes = Array.new
147 # - [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
148 nodes_to_fetch = (list_of_way_nodes.uniq - node_ids) - [0]
150 if nodes_to_fetch.length > 0
151 @nodes += Node.find(nodes_to_fetch)
155 user_display_name_cache = {}
157 @nodes.each do |node|
159 doc.root << node.to_xml_node(user_display_name_cache)
160 visible_nodes[node.id] = node
167 doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
172 relations = Relation.find_for_nodes_and_ways(visible_nodes.keys, way_ids)
174 # we do not normally return the "other" partners referenced by an relation,
175 # e.g. if we return a way A that is referenced by relation X, and there's
176 # another way B also referenced, that is not returned. But we do make
177 # an exception for cases where an relation references another *relation*;
178 # in that case we return that as well (but we don't go recursive here)
179 relation_ids = relations.collect { |relation| relation.id }
180 if relation_ids.length > 0
181 relations += Relation.find_by_sql("select e.* from current_relations e,current_relation_members em where " +
183 "em.id = e.id and em.member_type='relation' and em.member_id in (#{relation_ids.join(',')})")
186 # this "uniq" may be slightly inefficient; it may be better to first collect and output
187 # all node-related relations, then find the *not yet covered* way-related ones etc.
188 relations.uniq.each do |relation|
189 doc.root << relation.to_xml_node(user_display_name_cache)
192 render :text => doc.to_s, :content_type => "text/xml"
194 #exit when we have too many requests
195 if @@count > MAX_COUNT
203 zoom = (params[:zoom] || '12').to_i
205 if params.include?(:start) and params.include?(:end)
206 starttime = Time.parse(params[:start])
207 endtime = Time.parse(params[:end])
209 hours = (params[:hours] || '1').to_i.hours
211 starttime = endtime - hours
214 if zoom >= 1 and zoom <= 16 and
215 endtime >= starttime and endtime - starttime <= 24.hours
216 mask = (1 << zoom) - 1
218 tiles = Node.count(:conditions => ["timestamp BETWEEN ? AND ?", starttime, endtime],
219 :group => "maptile_for_point(latitude, longitude, #{zoom})")
221 doc = OSM::API.new.get_xml_doc
222 changes = XML::Node.new 'changes'
223 changes["starttime"] = starttime.xmlschema
224 changes["endtime"] = endtime.xmlschema
226 tiles.each do |tile, count|
227 x = (tile.to_i >> zoom) & mask
230 t = XML::Node.new 'tile'
234 t["changes"] = count.to_s
241 render :text => doc.to_s, :content_type => "text/xml"
243 render :nothing => true, :status => :bad_request
248 doc = OSM::API.new.get_xml_doc
250 api = XML::Node.new 'api'
251 version = XML::Node.new 'version'
252 version['minimum'] = '0.5';
253 version['maximum'] = '0.5';
255 area = XML::Node.new 'area'
256 area['maximum'] = MAX_REQUEST_AREA.to_s;
261 render :text => doc.to_s, :content_type => "text/xml"