]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/map_controller.rb
Enable the ActionOrder cop for remaining controllers
[rails.git] / app / controllers / api / map_controller.rb
1 module Api
2   class MapController < ApiController
3     before_action :check_api_readable
4
5     authorize_resource :class => false
6
7     around_action :api_call_handle_error, :api_call_timeout
8
9     before_action :set_request_formats
10
11     # This is probably the most common call of all. It is used for getting the
12     # OSM data for a specified bounding box, usually for editing. First the
13     # bounding box (bbox) is checked to make sure that it is sane. All nodes
14     # are searched, then all the ways that reference those nodes are found.
15     # All Nodes that are referenced by those ways are fetched and added to the list
16     # of nodes.
17     # Then all the relations that reference the already found nodes and ways are
18     # fetched. All the nodes and ways that are referenced by those ways are then
19     # fetched. Finally all the xml is returned.
20     def index
21       # Figure out the bbox
22       # check boundary is sane and area within defined
23       # see /config/application.yml
24       begin
25         @bounds = BoundingBox.from_bbox_params(params)
26         @bounds.check_boundaries
27         @bounds.check_size
28       rescue StandardError => e
29         report_error(e.message)
30         return
31       end
32
33       nodes = Node.bbox(@bounds).where(:visible => true).includes(:node_tags).limit(Settings.max_number_of_nodes + 1)
34
35       node_ids = nodes.collect(&:id)
36       if node_ids.length > Settings.max_number_of_nodes
37         report_error("You requested too many nodes (limit is #{Settings.max_number_of_nodes}). Either request a smaller area, or use planet.osm")
38         return
39       end
40
41       # get ways
42       # find which ways are needed
43       ways = []
44       if node_ids.empty?
45         list_of_way_nodes = []
46       else
47         way_nodes = WayNode.where(:node_id => node_ids)
48         way_ids = way_nodes.collect { |way_node| way_node.id[0] }
49         ways = Way.preload(:way_nodes, :way_tags).find(way_ids)
50
51         list_of_way_nodes = ways.collect do |way|
52           way.way_nodes.collect(&:node_id)
53         end
54         list_of_way_nodes.flatten!
55       end
56
57       # - [0] in case some thing links to node 0 which doesn't exist. Shouldn't actually ever happen but it does. FIXME: file a ticket for this
58       nodes_to_fetch = (list_of_way_nodes.uniq - node_ids) - [0]
59
60       nodes += Node.includes(:node_tags).find(nodes_to_fetch) unless nodes_to_fetch.empty?
61
62       visible_nodes = {}
63       @nodes = []
64       nodes.each do |node|
65         if node.visible?
66           visible_nodes[node.id] = node
67           @nodes << node
68         end
69       end
70
71       @ways = []
72       way_ids = []
73       ways.each do |way|
74         if way.visible?
75           way_ids << way.id
76           @ways << way
77         end
78       end
79
80       @relations = Relation.nodes(visible_nodes.keys).visible +
81                    Relation.ways(way_ids).visible
82
83       # we do not normally return the "other" partners referenced by an relation,
84       # e.g. if we return a way A that is referenced by relation X, and there's
85       # another way B also referenced, that is not returned. But we do make
86       # an exception for cases where an relation references another *relation*;
87       # in that case we return that as well (but we don't go recursive here)
88       @relations += Relation.relations(@relations.collect(&:id)).visible
89
90       # this "uniq" may be slightly inefficient; it may be better to first collect and output
91       # all node-related relations, then find the *not yet covered* way-related ones etc.
92       @relations.uniq!
93
94       response.headers["Content-Disposition"] = "attachment; filename=\"map.osm\""
95       # Render the result
96       respond_to do |format|
97         format.xml
98         format.json
99       end
100     end
101   end
102 end