]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
Point people at the wiki when GPX imports fail.
[rails.git] / app / controllers / way_controller.rb
1 class WayController < ApplicationController
2   require 'xml/libxml'
3
4   session :off
5   before_filter :authorize, :only => [:create, :update, :delete]
6   before_filter :check_write_availability, :only => [:create, :update, :delete]
7   before_filter :check_read_availability, :except => [:create, :update, :delete]
8   after_filter :compress_output
9
10   def create
11     if request.put?
12       way = Way.from_xml(request.raw_post, true)
13
14       if way
15         if !way.preconditions_ok?
16           render :text => "", :status => :precondition_failed
17         else
18           way.user_id = @user.id
19           way.save_with_history!
20
21           render :text => way.id.to_s, :content_type => "text/plain"
22         end
23       else
24         render :nothing => true, :status => :bad_request
25       end
26     else
27       render :nothing => true, :status => :method_not_allowed
28     end
29   end
30
31   def read
32     begin
33       way = Way.find(params[:id])
34
35       if way.visible
36         render :text => way.to_xml.to_s, :content_type => "text/xml"
37       else
38         render :text => "", :status => :gone
39       end
40     rescue ActiveRecord::RecordNotFound
41       render :nothing => true, :status => :not_found
42     end
43   end
44
45   def update
46     begin
47       way = Way.find(params[:id])
48       new_way = Way.from_xml(request.raw_post)
49
50       if new_way and new_way.id == way.id
51         if !new_way.preconditions_ok?
52           render :text => "", :status => :precondition_failed
53         else
54           way.user_id = @user.id
55           way.tags = new_way.tags
56           way.nds = new_way.nds
57           way.visible = true
58           way.save_with_history!
59
60           render :nothing => true
61         end
62       else
63         render :nothing => true, :status => :bad_request
64       end
65     rescue ActiveRecord::RecordNotFound
66       render :nothing => true, :status => :not_found
67     end
68   end
69
70   def delete
71     begin
72       way = Way.find(params[:id])
73
74       if way.visible
75         if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='way' and member_id=?", params[:id]])
76           render :text => "", :status => :precondition_failed
77         else
78           way.user_id = @user.id
79           way.tags = []
80           way.nds = []
81           way.visible = false
82           way.save_with_history!
83
84           render :nothing => true
85         end
86       else
87         render :text => "", :status => :gone
88       end
89     rescue ActiveRecord::RecordNotFound
90       render :nothing => true, :status => :not_found
91     rescue => ex
92       puts ex
93     end
94   end
95
96   def full
97     begin
98       way = Way.find(params[:id])
99
100       if way.visible
101         nd_ids = way.nds + [-1]
102         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{nd_ids.join(',')})")
103
104         # Render
105         doc = OSM::API.new.get_xml_doc
106         nodes.each do |node|
107           doc.root << node.to_xml_node()
108         end
109         doc.root << way.to_xml_node()
110
111         render :text => doc.to_s, :content_type => "text/xml"
112       else
113         render :text => "", :status => :gone
114       end
115     rescue ActiveRecord::RecordNotFound
116       render :nothing => true, :status => :not_found
117     end
118   end
119
120   def ways
121     begin
122       ids = params['ways'].split(',').collect { |w| w.to_i }
123     rescue
124       ids = []
125     end
126
127     if ids.length > 0
128       doc = OSM::API.new.get_xml_doc
129
130       Way.find(ids).each do |way|
131         doc.root << way.to_xml_node
132       end
133
134       render :text => doc.to_s, :content_type => "text/xml"
135     else
136       render :nothing => true, :status => :bad_request
137     end
138   end
139
140   def ways_for_node
141     wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id }.uniq
142
143     doc = OSM::API.new.get_xml_doc
144
145     Way.find(wayids).each do |way|
146       doc.root << way.to_xml_node
147     end
148
149     render :text => doc.to_s, :content_type => "text/xml"
150   end
151 end