1 class RelationController < ApplicationController
5 before_filter :authorize, :only => [:create, :update, :delete]
6 before_filter :require_public_data, :only => [:create, :update, :delete]
7 before_filter :check_api_writable, :only => [:create, :update, :delete]
8 before_filter :check_api_readable, :except => [:create, :update, :delete]
9 after_filter :compress_output
14 relation = Relation.from_xml(request.raw_post, true)
16 # We assume that an exception has been thrown if there was an error
17 # generating the relation
19 relation.create_with_history @user
20 render :text => relation.id.to_s, :content_type => "text/plain"
22 # render :text => "Couldn't get turn the input into a relation.", :status => :bad_request
25 render :nothing => true, :status => :method_not_allowed
27 rescue OSM::APIError => ex
34 relation = Relation.find(params[:id])
35 response.headers['Last-Modified'] = relation.timestamp.rfc822
37 render :text => relation.to_xml.to_s, :content_type => "text/xml"
39 render :text => "", :status => :gone
41 rescue ActiveRecord::RecordNotFound
42 render :nothing => true, :status => :not_found
44 render :nothing => true, :status => :internal_server_error
49 logger.debug request.raw_post
51 relation = Relation.find(params[:id])
52 new_relation = Relation.from_xml(request.raw_post)
54 if new_relation and new_relation.id == relation.id
55 relation.update_from new_relation, @user
56 render :text => relation.version.to_s, :content_type => "text/plain"
58 render :nothing => true, :status => :bad_request
60 rescue ActiveRecord::RecordNotFound
61 render :nothing => true, :status => :not_found
62 rescue OSM::APIError => ex
69 relation = Relation.find(params[:id])
70 new_relation = Relation.from_xml(request.raw_post)
71 if new_relation and new_relation.id == relation.id
72 relation.delete_with_history!(new_relation, @user)
73 render :text => relation.version.to_s, :content_type => "text/plain"
75 render :nothing => true, :status => :bad_request
77 rescue OSM::APIError => ex
79 rescue ActiveRecord::RecordNotFound
80 render :nothing => true, :status => :not_found
84 # -----------------------------------------------------------------
87 # input parameters: id
89 # returns XML representation of one relation object plus all its
90 # members, plus all nodes part of member ways
91 # -----------------------------------------------------------------
94 relation = Relation.find(params[:id])
98 # first collect nodes, ways, and relations referenced by this relation.
100 ways = Way.find_by_sql("select w.* from current_ways w,current_relation_members rm where "+
101 "rm.member_type='Way' and rm.member_id=w.id and rm.id=#{relation.id}");
102 nodes = Node.find_by_sql("select n.* from current_nodes n,current_relation_members rm where "+
103 "rm.member_type='Node' and rm.member_id=n.id and rm.id=#{relation.id}");
104 # note query is built to exclude self just in case.
105 relations = Relation.find_by_sql("select r.* from current_relations r,current_relation_members rm where "+
106 "rm.member_type='Relation' and rm.member_id=r.id and rm.id=#{relation.id} and r.id<>rm.id");
108 # now additionally collect nodes referenced by ways. Note how we recursively
109 # evaluate ways but NOT relations.
111 node_ids = nodes.collect {|node| node.id }
112 way_node_ids = ways.collect { |way|
113 way.way_nodes.collect { |way_node| way_node.node_id }
115 way_node_ids.flatten!
117 way_node_ids -= node_ids
118 nodes += Node.find(way_node_ids)
121 doc = OSM::API.new.get_xml_doc
123 user_display_name_cache = {}
126 if node.visible? # should be unnecessary if data is consistent.
127 doc.root << node.to_xml_node(user_display_name_cache)
128 visible_nodes[node.id] = node
132 if way.visible? # should be unnecessary if data is consistent.
133 doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
136 relations.each do |rel|
137 if rel.visible? # should be unnecessary if data is consistent.
138 doc.root << rel.to_xml_node(user_display_name_cache)
141 # finally add self and output
142 doc.root << relation.to_xml_node(user_display_name_cache)
143 render :text => doc.to_s, :content_type => "text/xml"
146 render :nothing => true, :status => :gone
149 rescue ActiveRecord::RecordNotFound
150 render :nothing => true, :status => :not_found
153 render :nothing => true, :status => :internal_server_error
158 ids = params['relations'].split(',').collect { |w| w.to_i }
161 doc = OSM::API.new.get_xml_doc
163 Relation.find(ids).each do |relation|
164 doc.root << relation.to_xml_node
167 render :text => doc.to_s, :content_type => "text/xml"
169 render :text => "You need to supply a comma separated list of ids.", :status => :bad_request
171 rescue ActiveRecord::RecordNotFound
172 render :text => "Could not find one of the relations", :status => :not_found
175 def relations_for_way
176 relations_for_object("Way")
178 def relations_for_node
179 relations_for_object("Node")
181 def relations_for_relation
182 relations_for_object("Relation")
185 def relations_for_object(objtype)
186 relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id[0] }.uniq
188 doc = OSM::API.new.get_xml_doc
190 Relation.find(relationids).each do |relation|
191 doc.root << relation.to_xml_node if relation.visible
194 render :text => doc.to_s, :content_type => "text/xml"