]> git.openstreetmap.org Git - rails.git/blob - app/controllers/search_controller.rb
0169a17ae494c4067b353cfb018d986e5134c84f
[rails.git] / app / controllers / search_controller.rb
1 class SearchController < ApplicationController
2   # Support searching for nodes, 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_nodes
16     do_search(false,true,false)
17   end
18   def search_relations
19     do_search(false,false,true)
20   end
21
22   def do_search(do_ways,do_nodes,do_relations)
23     type = params['type']
24     value = params['value']
25     unless type or value
26       name = params['name']
27       if name
28         type = 'name'
29         value = name
30       end
31     end
32
33     way_ids = Array.new
34     ways = Array.new
35     nodes = Array.new
36     relations = Array.new
37
38     # Matching for tags table
39     cond_tbl = Array.new
40     sql = 'id IN (SELECT id FROM current_way_tags WHERE 1=1'
41     if type
42       sql += ' AND k=?'
43       cond_tbl += [type]
44     end
45     if value
46       sql += ' AND v=?'
47       cond_tbl += [value]
48     end
49     sql += ')'
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     # First up, look for the relations we want
72     if do_relations
73       relations = Relation.find(:all, :conditions => cond_tbl, :limit => 100)
74     end
75
76     # then ways
77     if do_ways
78       ways = Way.find(:all, :conditions => cond_tbl, :limit => 100)
79     end
80
81     # Now, nodes
82     if do_nodes
83       nodes = Node.find(:all, :conditions => cond_tags, :limit => 2000)
84     end
85
86     # Fetch any node needed for our ways (only have matching nodes so far)
87     nodes += Node.find(ways.collect { |w| w.nds }.uniq)
88
89     # Print
90     user_display_name_cache = {}
91     doc = OSM::API.new.get_xml_doc
92     nodes.each do |node|
93       doc.root << node.to_xml_node(user_display_name_cache)
94     end
95
96     ways.each do |way|
97       doc.root << way.to_xml_node(user_display_name_cache)
98     end 
99
100     relations.each do |rel|
101       doc.root << rel.to_xml_node(user_display_name_cache)
102     end 
103     render :text => doc.to_s, :content_type => "text/xml"
104   end
105 end