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 && 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 && 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' && 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 do |way|
92 way.way_nodes.collect(&: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 next unless node.visible? # should be unnecessary if data is consistent.
107 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
108 visible_nodes[node.id] = node
109 visible_members["Node"][node.id] = true
113 next unless way.visible? # should be unnecessary if data is consistent.
115 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
116 visible_members["Way"][way.id] = true
119 relations.each do |rel|
120 next unless rel.visible? # should be unnecessary if data is consistent.
122 doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
123 visible_members["Relation"][rel.id] = true
126 # finally add self and output
127 doc.root << relation.to_xml_node(visible_members, changeset_cache, user_display_name_cache)
128 render :text => doc.to_s, :content_type => "text/xml"
131 render :text => "", :status => :gone
136 unless params['relations']
137 fail OSM::APIBadUserInput.new("The parameter relations is required, and must be of the form relations=id[,id[,id...]]")
140 ids = params['relations'].split(',').collect(&:to_i)
143 fail OSM::APIBadUserInput.new("No relations were given to search for")
146 doc = OSM::API.new.get_xml_doc
148 Relation.find(ids).each do |relation|
149 doc.root << relation.to_xml_node
152 render :text => doc.to_s, :content_type => "text/xml"
155 def relations_for_way
156 relations_for_object("Way")
159 def relations_for_node
160 relations_for_object("Node")
163 def relations_for_relation
164 relations_for_object("Relation")
167 def relations_for_object(objtype)
168 relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
170 doc = OSM::API.new.get_xml_doc
172 Relation.find(relationids).each do |relation|
173 doc.root << relation.to_xml_node if relation.visible
176 render :text => doc.to_s, :content_type => "text/xml"