1 class RelationController < ApplicationController
4 before_filter :authorize, :only => [:create, :update, :delete]
5 before_filter :require_allow_write_api, :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
10 around_filter :api_call_handle_error, :api_call_timeout
15 relation = Relation.from_xml(request.raw_post, true)
17 # We assume that an exception has been thrown if there was an error
18 # generating the relation
20 relation.create_with_history @user
21 render :text => relation.id.to_s, :content_type => "text/plain"
23 # render :text => "Couldn't get turn the input into a relation.", :status => :bad_request
28 relation = Relation.find(params[:id])
29 response.last_modified = relation.timestamp
31 render :text => relation.to_xml.to_s, :content_type => "text/xml"
33 render :text => "", :status => :gone
38 logger.debug request.raw_post
40 relation = Relation.find(params[:id])
41 new_relation = Relation.from_xml(request.raw_post)
43 if new_relation and new_relation.id == relation.id
44 relation.update_from new_relation, @user
45 render :text => relation.version.to_s, :content_type => "text/plain"
47 render :nothing => true, :status => :bad_request
52 relation = Relation.find(params[:id])
53 new_relation = Relation.from_xml(request.raw_post)
54 if new_relation and new_relation.id == relation.id
55 relation.delete_with_history!(new_relation, @user)
56 render :text => relation.version.to_s, :content_type => "text/plain"
58 render :nothing => true, :status => :bad_request
62 # -----------------------------------------------------------------
65 # input parameters: id
67 # returns XML representation of one relation object plus all its
68 # members, plus all nodes part of member ways
69 # -----------------------------------------------------------------
71 relation = Relation.find(params[:id])
75 # first find the ids of nodes, ways and relations referenced by this
76 # relation - note that we exclude this relation just in case.
78 node_ids = relation.members.select { |m| m[0] == 'Node' }.map { |m| m[1] }
79 way_ids = relation.members.select { |m| m[0] == 'Way' }.map { |m| m[1] }
80 relation_ids = relation.members.select { |m| m[0] == 'Relation' and m[1] != relation.id }.map { |m| m[1] }
82 # next load the relations and the ways.
84 relations = Relation.where(:id => relation_ids).includes(:relation_tags)
85 ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
87 # now additionally collect nodes referenced by ways. Note how we
88 # recursively evaluate ways but NOT relations.
90 way_node_ids = ways.collect { |way|
91 way.way_nodes.collect { |way_node| way_node.node_id }
93 node_ids += way_node_ids.flatten
94 nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
97 doc = OSM::API.new.get_xml_doc
99 visible_members = { "Node" => {}, "Way" => {}, "Relation" => {} }
101 user_display_name_cache = {}
104 if node.visible? # should be unnecessary if data is consistent.
105 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
106 visible_nodes[node.id] = node
107 visible_members["Node"][node.id] = true
111 if way.visible? # should be unnecessary if data is consistent.
112 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
113 visible_members["Way"][way.id] = true
116 relations.each do |rel|
117 if rel.visible? # should be unnecessary if data is consistent.
118 doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
119 visible_members["Relation"][rel.id] = true
122 # finally add self and output
123 doc.root << relation.to_xml_node(visible_members, changeset_cache, user_display_name_cache)
124 render :text => doc.to_s, :content_type => "text/xml"
127 render :nothing => true, :status => :gone
132 ids = params['relations'].split(',').collect { |w| w.to_i }
135 doc = OSM::API.new.get_xml_doc
137 Relation.find(ids).each do |relation|
138 doc.root << relation.to_xml_node
141 render :text => doc.to_s, :content_type => "text/xml"
143 render :text => "You need to supply a comma separated list of ids.", :status => :bad_request
147 def relations_for_way
148 relations_for_object("Way")
151 def relations_for_node
152 relations_for_object("Node")
155 def relations_for_relation
156 relations_for_object("Relation")
159 def relations_for_object(objtype)
160 relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect { |ws| ws.relation_id }.uniq
162 doc = OSM::API.new.get_xml_doc
164 Relation.find(relationids).each do |relation|
165 doc.root << relation.to_xml_node if relation.visible
168 render :text => doc.to_s, :content_type => "text/xml"