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 # Assume that Relation.from_xml has thrown an exception if there is an error parsing the xml
18 relation.create_with_history @user
19 render :text => relation.id.to_s, :content_type => "text/plain"
23 relation = Relation.find(params[:id])
24 response.last_modified = relation.timestamp
26 render :text => relation.to_xml.to_s, :content_type => "text/xml"
28 render :text => "", :status => :gone
33 logger.debug request.raw_post
35 relation = Relation.find(params[:id])
36 new_relation = Relation.from_xml(request.raw_post)
38 unless new_relation && new_relation.id == relation.id
39 fail OSM::APIBadUserInput.new("The id in the url (#{relation.id}) is not the same as provided in the xml (#{new_relation.id})")
42 relation.update_from new_relation, @user
43 render :text => relation.version.to_s, :content_type => "text/plain"
47 relation = Relation.find(params[:id])
48 new_relation = Relation.from_xml(request.raw_post)
49 if new_relation && new_relation.id == relation.id
50 relation.delete_with_history!(new_relation, @user)
51 render :text => relation.version.to_s, :content_type => "text/plain"
53 render :text => "", :status => :bad_request
57 # -----------------------------------------------------------------
60 # input parameters: id
62 # returns XML representation of one relation object plus all its
63 # members, plus all nodes part of member ways
64 # -----------------------------------------------------------------
66 relation = Relation.find(params[:id])
70 # first find the ids of nodes, ways and relations referenced by this
71 # relation - note that we exclude this relation just in case.
73 node_ids = relation.members.select { |m| m[0] == "Node" }.map { |m| m[1] }
74 way_ids = relation.members.select { |m| m[0] == "Way" }.map { |m| m[1] }
75 relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.map { |m| m[1] }
77 # next load the relations and the ways.
79 relations = Relation.where(:id => relation_ids).includes(:relation_tags)
80 ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
82 # now additionally collect nodes referenced by ways. Note how we
83 # recursively evaluate ways but NOT relations.
85 way_node_ids = ways.collect do |way|
86 way.way_nodes.collect(&:node_id)
88 node_ids += way_node_ids.flatten
89 nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
92 doc = OSM::API.new.get_xml_doc
94 visible_members = { "Node" => {}, "Way" => {}, "Relation" => {} }
96 user_display_name_cache = {}
99 next unless node.visible? # should be unnecessary if data is consistent.
101 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
102 visible_nodes[node.id] = node
103 visible_members["Node"][node.id] = true
107 next unless way.visible? # should be unnecessary if data is consistent.
109 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
110 visible_members["Way"][way.id] = true
113 relations.each do |rel|
114 next unless rel.visible? # should be unnecessary if data is consistent.
116 doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
117 visible_members["Relation"][rel.id] = true
120 # finally add self and output
121 doc.root << relation.to_xml_node(visible_members, changeset_cache, user_display_name_cache)
122 render :text => doc.to_s, :content_type => "text/xml"
125 render :text => "", :status => :gone
130 unless params["relations"]
131 fail OSM::APIBadUserInput.new("The parameter relations is required, and must be of the form relations=id[,id[,id...]]")
134 ids = params["relations"].split(",").collect(&:to_i)
137 fail OSM::APIBadUserInput.new("No relations were given to search for")
140 doc = OSM::API.new.get_xml_doc
142 Relation.find(ids).each do |relation|
143 doc.root << relation.to_xml_node
146 render :text => doc.to_s, :content_type => "text/xml"
149 def relations_for_way
150 relations_for_object("Way")
153 def relations_for_node
154 relations_for_object("Node")
157 def relations_for_relation
158 relations_for_object("Relation")
161 def relations_for_object(objtype)
162 relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
164 doc = OSM::API.new.get_xml_doc
166 Relation.find(relationids).each do |relation|
167 doc.root << relation.to_xml_node if relation.visible
170 render :text => doc.to_s, :content_type => "text/xml"