1 class ApiController < ApplicationController
3 before_filter :authorize
4 after_filter :compress_output
10 @current_user = User.find_by_token(session[:token])
13 # The maximum area you're allowed to request, in square degrees
14 MAX_REQUEST_AREA = 0.25
18 response.headers["Content-Type"] = 'text/xml'
21 unless bbox and bbox.count(',') == 3
22 report_error("The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat")
26 bbox = bbox.split(',')
28 min_lon = bbox[0].to_f
29 min_lat = bbox[1].to_f
30 max_lon = bbox[2].to_f
31 max_lat = bbox[3].to_f
33 # check the bbox is sane
34 unless min_lon <= max_lon
35 report_error("The minimum longitude must be less than the maximum longitude, but it wasn't")
38 unless min_lat <= max_lat
39 report_error("The minimum latitude must be less than the maximum latitude, but it wasn't")
42 unless min_lon >= -180 && min_lat >= -90 && max_lon <= 180 && max_lat <= 90
43 report_error("The latitudes must be between -90 and 90, and longitudes between -180 and 180")
47 # check the bbox isn't too large
48 requested_area = (max_lat-min_lat)*(max_lon-min_lon)
49 if requested_area > MAX_REQUEST_AREA
50 report_error("The maximum bbox size is " + MAX_REQUEST_AREA.to_s + ", and your request was too large. Either request a smaller area, or use planet.osm")
55 nodes = Node.find(:all, :conditions => ['latitude > ? AND longitude > ? AND latitude < ? AND longitude < ? AND visible = 1', min_lat, min_lon, max_lat, max_lon])
57 node_ids = nodes.collect {|node| node.id }
59 if node_ids.length > 50_000
60 report_error("You requested too many nodes (limit is 50,000). Either request a smaller area, or use planet.osm")
65 if node_ids.length > 0
66 node_ids_sql = "(#{node_ids.join(',')})"
67 # get the referenced segments
68 segments = Segment.find_by_sql "select * from current_segments where visible = 1 and (node_a in #{node_ids_sql} or node_b in #{node_ids_sql})"
70 # see if we have any missing nodes
71 segments_nodes = segments.collect {|segment| segment.node_a }
72 segments_nodes += segments.collect {|segment| segment.node_b }
76 missing_nodes = segments_nodes - node_ids
78 # get missing nodes if there are any
79 nodes += Node.find(missing_nodes) if missing_nodes.length > 0
81 doc = OSM::API.new.get_xml_doc
84 # find which ways are needed
85 segment_ids = segments.collect {|segment| segment.id }
87 if segment_ids.length > 0
88 way_segments = WaySegment.find_all_by_segment_id(segment_ids)
89 way_ids = way_segments.collect {|way_segment| way_segment.id }
90 ways = Way.find(way_ids) # NB: doesn't pick up segments, tags from db until accessed via way.way_segments etc.
92 # seg_ids = way_segments.collect {|way_segment| way_segment.segment_id }
94 list_of_way_segs = ways.collect {|way| way.way_segments}
95 list_of_way_segs.flatten!
97 list_of_way_segments = list_of_way_segs.collect { |way_seg| way_seg.segment_id }
101 segments_to_fetch = list_of_way_segments.uniq - segment_ids
103 if segments_to_fetch.length > 0
104 segments += Segment.find(segments_to_fetch)
110 segments_nodes = segments.collect {|segment| segment.node_a }
111 segments_nodes += segments.collect {|segment| segment.node_b }
113 node_ids_a = nodes.collect {|node| node.id }
115 nodes_to_get = segments_nodes - node_ids_a
116 nodes += Node.find(nodes_to_get) if nodes_to_get.length > 0
119 user_display_name_cache = {}
123 doc.root << node.to_xml_node(user_display_name_cache)
124 visible_nodes[node.id] = node
128 visible_segments = {}
130 segments.each do |segment|
131 if visible_nodes[segment.node_a] and visible_nodes[segment.node_b] and segment.visible?
132 doc.root << segment.to_xml_node(user_display_name_cache)
133 visible_segments[segment.id] = segment
138 doc.root << way.to_xml_node(visible_segments, user_display_name_cache) if way.visible?
141 render :text => doc.to_s