1 class RelationController < ApplicationController
5 before_filter :authorize, :only => [:create, :update, :delete]
6 before_filter :check_write_availability, :only => [:create, :update, :delete]
7 before_filter :check_read_availability, :except => [:create, :update, :delete]
8 after_filter :compress_output
13 relation = Relation.from_xml(request.raw_post, true)
16 relation.create_with_history @user
17 render :text => relation.id.to_s, :content_type => "text/plain"
19 render :nothing => true, :status => :bad_request
22 render :nothing => true, :status => :method_not_allowed
24 rescue OSM::APIError => ex
31 relation = Relation.find(params[:id])
32 response.headers['Last-Modified'] = relation.timestamp.rfc822
34 render :text => relation.to_xml.to_s, :content_type => "text/xml"
36 render :text => "", :status => :gone
38 rescue ActiveRecord::RecordNotFound
39 render :nothing => true, :status => :not_found
41 render :nothing => true, :status => :internal_server_error
46 logger.debug request.raw_post
48 relation = Relation.find(params[:id])
49 new_relation = Relation.from_xml(request.raw_post)
51 if new_relation and new_relation.id == relation.id
52 relation.update_from new_relation, @user
53 render :text => relation.version.to_s, :content_type => "text/plain"
55 render :nothing => true, :status => :bad_request
57 rescue ActiveRecord::RecordNotFound
58 render :nothing => true, :status => :not_found
59 rescue OSM::APIError => ex
65 #XXX check if member somewhere!
67 relation = Relation.find(params[:id])
68 new_relation = Relation.from_xml(request.raw_post)
69 if new_relation and new_relation.id == relation.id
70 relation.delete_with_history!(new_relation, @user)
71 render :text => relation.version.to_s, :content_type => "text/plain"
73 render :nothing => true, :status => :bad_request
75 rescue OSM::APIError => ex
77 rescue ActiveRecord::RecordNotFound
78 render :nothing => true, :status => :not_found
82 # -----------------------------------------------------------------
85 # input parameters: id
87 # returns XML representation of one relation object plus all its
88 # members, plus all nodes part of member ways
89 # -----------------------------------------------------------------
92 relation = Relation.find(params[:id])
96 # first collect nodes, ways, and relations referenced by this relation.
98 ways = Way.find_by_sql("select w.* from current_ways w,current_relation_members rm where "+
99 "rm.member_type='way' and rm.member_id=w.id and rm.id=#{relation.id}");
100 nodes = Node.find_by_sql("select n.* from current_nodes n,current_relation_members rm where "+
101 "rm.member_type='node' and rm.member_id=n.id and rm.id=#{relation.id}");
102 # note query is built to exclude self just in case.
103 relations = Relation.find_by_sql("select r.* from current_relations r,current_relation_members rm where "+
104 "rm.member_type='relation' and rm.member_id=r.id and rm.id=#{relation.id} and r.id<>rm.id");
106 # now additionally collect nodes referenced by ways. Note how we recursively
107 # evaluate ways but NOT relations.
109 node_ids = nodes.collect {|node| node.id }
110 way_node_ids = ways.collect { |way|
111 way.way_nodes.collect { |way_node| way_node.node_id }
113 way_node_ids.flatten!
115 way_node_ids -= node_ids
116 nodes += Node.find(way_node_ids)
119 doc = OSM::API.new.get_xml_doc
121 user_display_name_cache = {}
124 if node.visible? # should be unnecessary if data is consistent.
125 doc.root << node.to_xml_node(user_display_name_cache)
126 visible_nodes[node.id] = node
130 if way.visible? # should be unnecessary if data is consistent.
131 doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
134 relations.each do |rel|
135 if rel.visible? # should be unnecessary if data is consistent.
136 doc.root << rel.to_xml_node(user_display_name_cache)
139 # finally add self and output
140 doc.root << relation.to_xml_node(user_display_name_cache)
141 render :text => doc.to_s, :content_type => "text/xml"
145 render :text => "", :status => :gone
148 rescue ActiveRecord::RecordNotFound
149 render :nothing => true, :status => :not_found
152 render :nothing => true, :status => :internal_server_error
157 ids = params['relations'].split(',').collect { |w| w.to_i }
160 doc = OSM::API.new.get_xml_doc
162 Relation.find(ids).each do |relation|
163 doc.root << relation.to_xml_node
166 render :text => doc.to_s, :content_type => "text/xml"
168 render :nothing => true, :status => :bad_request
172 def relations_for_way
173 relations_for_object("way")
175 def relations_for_node
176 relations_for_object("node")
178 def relations_for_relation
179 relations_for_object("relation")
182 def relations_for_object(objtype)
183 relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id[0] }.uniq
185 doc = OSM::API.new.get_xml_doc
187 Relation.find(relationids).each do |relation|
188 doc.root << relation.to_xml_node if relation.visible
191 render :text => doc.to_s, :content_type => "text/xml"