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 find the ids of nodes, ways and relations referenced by this
99 # relation - note that we exclude this relation just in case.
101 node_ids = relation.members.select { |m| m[0] == 'Node' }.map { |m| m[1] }
102 way_ids = relation.members.select { |m| m[0] == 'Way' }.map { |m| m[1] }
103 relation_ids = relation.members.select { |m| m[0] == 'Relation' and m[1] != relation.id }.map { |m| m[1] }
105 # next load the relations and the ways.
107 relations = Relation.find(relation_ids, :include => [:relation_tags])
108 ways = Way.find(way_ids, :include => [:way_nodes, :way_tags])
110 # now additionally collect nodes referenced by ways. Note how we
111 # recursively evaluate ways but NOT relations.
113 way_node_ids = ways.collect { |way|
114 way.way_nodes.collect { |way_node| way_node.node_id }
116 node_ids += way_node_ids.flatten
117 nodes = Node.find(node_ids.uniq, :include => :node_tags)
120 doc = OSM::API.new.get_xml_doc
122 visible_members = { "Node" => [], "Way" => [], "Relation" => [] }
124 user_display_name_cache = {}
127 if node.visible? # should be unnecessary if data is consistent.
128 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
129 visible_nodes[node.id] = node
130 visible_members["Node"][node.id] = true
134 if way.visible? # should be unnecessary if data is consistent.
135 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
136 visible_members["Way"][way.id] = true
139 relations.each do |rel|
140 if rel.visible? # should be unnecessary if data is consistent.
141 doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
142 visible_members["Relation"][rel.id] = true
145 # finally add self and output
146 doc.root << relation.to_xml_node(visible_members, changeset_cache, user_display_name_cache)
147 render :text => doc.to_s, :content_type => "text/xml"
150 render :nothing => true, :status => :gone
153 rescue ActiveRecord::RecordNotFound
154 render :nothing => true, :status => :not_found
157 render :nothing => true, :status => :internal_server_error
162 ids = params['relations'].split(',').collect { |w| w.to_i }
165 doc = OSM::API.new.get_xml_doc
167 Relation.find(ids).each do |relation|
168 doc.root << relation.to_xml_node
171 render :text => doc.to_s, :content_type => "text/xml"
173 render :text => "You need to supply a comma separated list of ids.", :status => :bad_request
175 rescue ActiveRecord::RecordNotFound
176 render :text => "Could not find one of the relations", :status => :not_found
179 def relations_for_way
180 relations_for_object("Way")
182 def relations_for_node
183 relations_for_object("Node")
185 def relations_for_relation
186 relations_for_object("Relation")
189 def relations_for_object(objtype)
190 relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id[0] }.uniq
192 doc = OSM::API.new.get_xml_doc
194 Relation.find(relationids).each do |relation|
195 doc.root << relation.to_xml_node if relation.visible
198 render :text => doc.to_s, :content_type => "text/xml"