1 class ApiController < ApplicationController
4 response.headers["Content-Type"] = 'application/xml'
7 unless bbox and bbox.count(',') == 3
8 render :nothing => true, :status => 400
12 bbox = bbox.split(',')
14 min_lon = bbox[0].to_f
15 min_lat = bbox[1].to_f
16 max_lon = bbox[2].to_f
17 max_lat = bbox[3].to_f
20 nodes = Node.find(:all, :conditions => ['latitude > ? AND longitude > ? AND latitude < ? AND longitude < ? AND visible = 1', min_lat, min_lon, max_lat, max_lon])
22 node_ids = nodes.collect {|node| node.id }
24 if node_ids.length > 0
25 node_ids_sql = "(#{node_ids.join(',')})"
26 # get the referenced segments
27 segments = Segment.find_by_sql "select * from segments where node_a in #{node_ids_sql} or node_b in #{node_ids_sql}"
29 # see if we have nay missing nodes
30 segments_nodes = segments.collect {|segment| segment.node_a }
31 segments_nodes += segments.collect {|segment| segment.node_b }
35 missing_nodes = segments_nodes - node_ids
37 # get missing nodes if there are any
38 nodes += Node.find(missing_nodes) if missing_nodes.length > 0
40 doc = XML::Document.new
41 doc.encoding = 'UTF-8'
42 root = XML::Node.new 'osm'
43 root['version'] = API_VERSION
44 root['generator'] = 'OpenStreetMap server'
48 # find which ways are needed
49 segment_ids = segments.collect {|segment| segment.id }
51 if segment_ids.length > 0
52 way_segments = WaySegment.find_by_segment_id(segment_ids)
53 way_ids = way_segments.collect {|way_segment| way_segment.id }
55 ways = Way.find(segment_ids)
59 root << node.to_xml_node()
62 segments.each do |segment|
63 root << segment.to_xml_node()
67 root << way.to_xml_node()
70 render :text => doc.to_s