]> git.openstreetmap.org Git - rails.git/blob - app/controllers/search_controller.rb
Refactor GPX creation routines to share common code.
[rails.git] / app / controllers / search_controller.rb
1 class SearchController < ApplicationController
2   # Support searching for nodes, segments, ways, or all
3   # Can search by tag k, v, or both (type->k,value->v)
4   # Can search by name (k=name,v=....)
5
6   after_filter :compress_output
7
8   def search_all
9     do_search(true,true,true)
10   end
11
12   def search_ways
13     do_search(true,false,false)
14   end
15   def search_segments
16     do_search(false,true,false)
17   end
18   def search_nodes
19     do_search(false,false,true)
20   end
21
22
23   def do_search(do_ways,do_segments,do_nodes)
24     type = params['type']
25     value = params['value']
26     unless type or value
27       name = params['name']
28       if name
29         type = 'name'
30         value = name
31       end
32     end
33
34     way_ids = Array.new
35     ways = Array.new
36     segments = Array.new
37     nodes = Array.new
38
39     # Matching for tags table
40     cond_tbl = Array.new
41     sql = '1=1'
42     if type
43       sql += ' AND k=?'
44       cond_tbl += [type]
45     end
46     if value
47       sql += ' AND v=?'
48       cond_tbl += [value]
49     end
50     cond_tbl = [sql] + cond_tbl
51
52     # Matching for tags column
53     if type and value
54       cond_tags = ['tags LIKE ? OR tags LIKE ? OR tags LIKE ? OR tags LIKE ?', 
55       ''+type+'='+value+'',
56       ''+type+'='+value+';%',
57       '%;'+type+'='+value+';%',
58       '%;'+type+'='+value+'' ]
59     elsif type
60       cond_tags = ['tags LIKE ? OR tags LIKE ?',
61       ''+type+'=%',
62       '%;'+type+'=%' ]
63     elsif value
64       cond_tags = ['tags LIKE ? OR tags LIKE ?',
65       '%='+value+';%',
66       '%='+value+'' ]
67     else
68       cond_tags = ['1=1']
69     end
70
71
72     # First up, look for the ids of the ways we want
73     if do_ways
74       ways_tmp = WayTag.find(:all, :conditions => cond_tbl)
75       way_ids = ways_tmp.collect {|way| way.id }
76     end
77
78     # Now, segments matching
79     if do_segments
80       segs = Segment.find(:all, :conditions => cond_tags)
81     end
82
83     # Now, nodes
84     if do_nodes
85       nodes = Node.find(:all, :conditions => cond_tags)
86     end
87
88     # Get the remaining objects:
89     # Fetch the ways (until now only had their ids)
90     ways = Way.find(way_ids)
91
92     # Fetch any segments needed for our ways (only have matching segments so far)
93     seg_ids = Array.new
94     ways.each do |way|
95       seg_ids += way.segs
96     end
97     segments += Segment.find(seg_ids)
98
99     # Fetch any nodes needed for our segments (only have matching nodes so far)
100     node_ids = Array.new
101     segments.each do |seg|
102       node_ids += [seg.node_a, seg.node_b]
103     end
104     nodes += Node.find(node_ids)
105
106
107     # Print
108     doc = OSM::API.new.get_xml_doc
109     nodes.each do |node|
110       doc.root << node.to_xml_node()
111     end
112
113     segments.each do |segment|
114       doc.root << segment.to_xml_node()
115     end 
116
117     ways.each do |way|
118       doc.root << way.to_xml_node()
119     end 
120
121     render :text => doc.to_s, :content_type => "text/xml"
122   end
123 end