1 class RelationController < ApplicationController
4 skip_before_filter :verify_authenticity_token
5 before_filter :authorize, :only => [:create, :update, :delete]
6 before_filter :require_allow_write_api, :only => [:create, :update, :delete]
7 before_filter :require_public_data, :only => [:create, :update, :delete]
8 before_filter :check_api_writable, :only => [:create, :update, :delete]
9 before_filter :check_api_readable, :except => [:create, :update, :delete]
10 after_filter :compress_output
11 around_filter :api_call_handle_error, :api_call_timeout
16 relation = Relation.from_xml(request.raw_post, true)
18 # We assume that an exception has been thrown if there was an error
19 # generating the relation
21 relation.create_with_history @user
22 render :text => relation.id.to_s, :content_type => "text/plain"
24 # render :text => "Couldn't get turn the input into a relation.", :status => :bad_request
29 relation = Relation.find(params[:id])
30 response.last_modified = relation.timestamp
32 render :text => relation.to_xml.to_s, :content_type => "text/xml"
34 render :text => "", :status => :gone
39 logger.debug request.raw_post
41 relation = Relation.find(params[:id])
42 new_relation = Relation.from_xml(request.raw_post)
44 if new_relation and new_relation.id == relation.id
45 relation.update_from new_relation, @user
46 render :text => relation.version.to_s, :content_type => "text/plain"
48 render :text => "", :status => :bad_request
53 relation = Relation.find(params[:id])
54 new_relation = Relation.from_xml(request.raw_post)
55 if new_relation and new_relation.id == relation.id
56 relation.delete_with_history!(new_relation, @user)
57 render :text => relation.version.to_s, :content_type => "text/plain"
59 render :text => "", :status => :bad_request
63 # -----------------------------------------------------------------
66 # input parameters: id
68 # returns XML representation of one relation object plus all its
69 # members, plus all nodes part of member ways
70 # -----------------------------------------------------------------
72 relation = Relation.find(params[:id])
76 # first find the ids of nodes, ways and relations referenced by this
77 # relation - note that we exclude this relation just in case.
79 node_ids = relation.members.select { |m| m[0] == 'Node' }.map { |m| m[1] }
80 way_ids = relation.members.select { |m| m[0] == 'Way' }.map { |m| m[1] }
81 relation_ids = relation.members.select { |m| m[0] == 'Relation' and m[1] != relation.id }.map { |m| m[1] }
83 # next load the relations and the ways.
85 relations = Relation.where(:id => relation_ids).includes(:relation_tags)
86 ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
88 # now additionally collect nodes referenced by ways. Note how we
89 # recursively evaluate ways but NOT relations.
91 way_node_ids = ways.collect { |way|
92 way.way_nodes.collect { |way_node| way_node.node_id }
94 node_ids += way_node_ids.flatten
95 nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
98 doc = OSM::API.new.get_xml_doc
100 visible_members = { "Node" => {}, "Way" => {}, "Relation" => {} }
102 user_display_name_cache = {}
105 if node.visible? # should be unnecessary if data is consistent.
106 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
107 visible_nodes[node.id] = node
108 visible_members["Node"][node.id] = true
112 if way.visible? # should be unnecessary if data is consistent.
113 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
114 visible_members["Way"][way.id] = true
117 relations.each do |rel|
118 if rel.visible? # should be unnecessary if data is consistent.
119 doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
120 visible_members["Relation"][rel.id] = true
123 # finally add self and output
124 doc.root << relation.to_xml_node(visible_members, changeset_cache, user_display_name_cache)
125 render :text => doc.to_s, :content_type => "text/xml"
128 render :text => "", :status => :gone
133 if not params['relations']
134 raise OSM::APIBadUserInput.new("The parameter relations is required, and must be of the form relations=id[,id[,id...]]")
137 ids = params['relations'].split(',').collect { |w| w.to_i }
140 raise OSM::APIBadUserInput.new("No relations were given to search for")
143 doc = OSM::API.new.get_xml_doc
145 Relation.find(ids).each do |relation|
146 doc.root << relation.to_xml_node
149 render :text => doc.to_s, :content_type => "text/xml"
152 def relations_for_way
153 relations_for_object("Way")
156 def relations_for_node
157 relations_for_object("Node")
160 def relations_for_relation
161 relations_for_object("Relation")
164 def relations_for_object(objtype)
165 relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect { |ws| ws.relation_id }.uniq
167 doc = OSM::API.new.get_xml_doc
169 Relation.find(relationids).each do |relation|
170 doc.root << relation.to_xml_node if relation.visible
173 render :text => doc.to_s, :content_type => "text/xml"