1 class RelationController < ApplicationController
4 skip_before_action :verify_authenticity_token
5 before_action :authorize, :only => [:create, :update, :delete]
6 before_action :require_allow_write_api, :only => [:create, :update, :delete]
7 before_action :require_public_data, :only => [:create, :update, :delete]
8 before_action :check_api_writable, :only => [:create, :update, :delete]
9 before_action :check_api_readable, :except => [:create, :update, :delete]
10 around_action :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 && 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 :text => "", :status => :bad_request
52 relation = Relation.find(params[:id])
53 new_relation = Relation.from_xml(request.raw_post)
54 if new_relation && 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 :text => "", :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" && 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 do |way|
91 way.way_nodes.collect(&: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 next unless 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 next unless way.visible? # should be unnecessary if data is consistent.
114 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
115 visible_members["Way"][way.id] = true
118 relations.each do |rel|
119 next unless rel.visible? # should be unnecessary if data is consistent.
121 doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
122 visible_members["Relation"][rel.id] = true
125 # finally add self and output
126 doc.root << relation.to_xml_node(visible_members, changeset_cache, user_display_name_cache)
127 render :text => doc.to_s, :content_type => "text/xml"
130 render :text => "", :status => :gone
135 unless params["relations"]
136 fail OSM::APIBadUserInput.new("The parameter relations is required, and must be of the form relations=id[,id[,id...]]")
139 ids = params["relations"].split(",").collect(&:to_i)
142 fail OSM::APIBadUserInput.new("No relations were given to search for")
145 doc = OSM::API.new.get_xml_doc
147 Relation.find(ids).each do |relation|
148 doc.root << relation.to_xml_node
151 render :text => doc.to_s, :content_type => "text/xml"
154 def relations_for_way
155 relations_for_object("Way")
158 def relations_for_node
159 relations_for_object("Node")
162 def relations_for_relation
163 relations_for_object("Relation")
166 def relations_for_object(objtype)
167 relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
169 doc = OSM::API.new.get_xml_doc
171 Relation.find(relationids).each do |relation|
172 doc.root << relation.to_xml_node if relation.visible
175 render :text => doc.to_s, :content_type => "text/xml"