1 class RelationController < ApplicationController
4 before_filter :authorize, :only => [:create, :update, :delete]
5 before_filter :require_public_data, :only => [:create, :update, :delete]
6 before_filter :check_api_writable, :only => [:create, :update, :delete]
7 before_filter :check_api_readable, :except => [:create, :update, :delete]
8 after_filter :compress_output
9 around_filter :api_call_handle_error, :api_call_timeout
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
27 relation = Relation.find(params[:id])
28 response.headers['Last-Modified'] = relation.timestamp.rfc822
30 render :text => relation.to_xml.to_s, :content_type => "text/xml"
32 render :text => "", :status => :gone
37 logger.debug request.raw_post
39 relation = Relation.find(params[:id])
40 new_relation = Relation.from_xml(request.raw_post)
42 if new_relation and new_relation.id == relation.id
43 relation.update_from new_relation, @user
44 render :text => relation.version.to_s, :content_type => "text/plain"
46 render :nothing => true, :status => :bad_request
51 relation = Relation.find(params[:id])
52 new_relation = Relation.from_xml(request.raw_post)
53 if new_relation and new_relation.id == relation.id
54 relation.delete_with_history!(new_relation, @user)
55 render :text => relation.version.to_s, :content_type => "text/plain"
57 render :nothing => true, :status => :bad_request
61 # -----------------------------------------------------------------
64 # input parameters: id
66 # returns XML representation of one relation object plus all its
67 # members, plus all nodes part of member ways
68 # -----------------------------------------------------------------
70 relation = Relation.find(params[:id])
74 # first find the ids of nodes, ways and relations referenced by this
75 # relation - note that we exclude this relation just in case.
77 node_ids = relation.members.select { |m| m[0] == 'Node' }.map { |m| m[1] }
78 way_ids = relation.members.select { |m| m[0] == 'Way' }.map { |m| m[1] }
79 relation_ids = relation.members.select { |m| m[0] == 'Relation' and m[1] != relation.id }.map { |m| m[1] }
81 # next load the relations and the ways.
83 relations = Relation.find(relation_ids, :include => [:relation_tags])
84 ways = Way.find(way_ids, :include => [:way_nodes, :way_tags])
86 # now additionally collect nodes referenced by ways. Note how we
87 # recursively evaluate ways but NOT relations.
89 way_node_ids = ways.collect { |way|
90 way.way_nodes.collect { |way_node| way_node.node_id }
92 node_ids += way_node_ids.flatten
93 nodes = Node.find(node_ids.uniq, :include => :node_tags)
96 doc = OSM::API.new.get_xml_doc
98 visible_members = { "Node" => {}, "Way" => {}, "Relation" => {} }
100 user_display_name_cache = {}
103 if node.visible? # should be unnecessary if data is consistent.
104 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
105 visible_nodes[node.id] = node
106 visible_members["Node"][node.id] = true
110 if way.visible? # should be unnecessary if data is consistent.
111 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
112 visible_members["Way"][way.id] = true
115 relations.each do |rel|
116 if rel.visible? # should be unnecessary if data is consistent.
117 doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
118 visible_members["Relation"][rel.id] = true
121 # finally add self and output
122 doc.root << relation.to_xml_node(visible_members, changeset_cache, user_display_name_cache)
123 render :text => doc.to_s, :content_type => "text/xml"
126 render :nothing => true, :status => :gone
131 ids = params['relations'].split(',').collect { |w| w.to_i }
134 doc = OSM::API.new.get_xml_doc
136 Relation.find(ids).each do |relation|
137 doc.root << relation.to_xml_node
140 render :text => doc.to_s, :content_type => "text/xml"
142 render :text => "You need to supply a comma separated list of ids.", :status => :bad_request
146 def relations_for_way
147 relations_for_object("Way")
150 def relations_for_node
151 relations_for_object("Node")
154 def relations_for_relation
155 relations_for_object("Relation")
158 def relations_for_object(objtype)
159 relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id[0] }.uniq
161 doc = OSM::API.new.get_xml_doc
163 Relation.find(relationids).each do |relation|
164 doc.root << relation.to_xml_node if relation.visible
167 render :text => doc.to_s, :content_type => "text/xml"